Skip to content

Commit e162b47

Browse files
Merge pull request #6777 from tom-cosgrove-arm/issue-6292-mod_inv
Bignum: Implement high level fixed width modular inversion
2 parents cf86d70 + f723754 commit e162b47

15 files changed

+593
-46
lines changed

library/bignum_core.c

+34
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,40 @@ mbedtls_mpi_uint mbedtls_mpi_core_sub_int( mbedtls_mpi_uint *X,
824824
return( c );
825825
}
826826

827+
mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct( const mbedtls_mpi_uint *A,
828+
size_t limbs )
829+
{
830+
mbedtls_mpi_uint bits = 0;
831+
832+
for( size_t i = 0; i < limbs; i++ )
833+
bits |= A[i];
834+
835+
return( bits );
836+
}
837+
838+
void mbedtls_mpi_core_to_mont_rep( mbedtls_mpi_uint *X,
839+
const mbedtls_mpi_uint *A,
840+
const mbedtls_mpi_uint *N,
841+
size_t AN_limbs,
842+
mbedtls_mpi_uint mm,
843+
const mbedtls_mpi_uint *rr,
844+
mbedtls_mpi_uint *T )
845+
{
846+
mbedtls_mpi_core_montmul( X, A, rr, AN_limbs, N, AN_limbs, mm, T );
847+
}
848+
849+
void mbedtls_mpi_core_from_mont_rep( mbedtls_mpi_uint *X,
850+
const mbedtls_mpi_uint *A,
851+
const mbedtls_mpi_uint *N,
852+
size_t AN_limbs,
853+
mbedtls_mpi_uint mm,
854+
mbedtls_mpi_uint *T )
855+
{
856+
const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
857+
858+
mbedtls_mpi_core_montmul( X, A, &Rinv, 1, N, AN_limbs, mm, T );
859+
}
860+
827861
/* END MERGE SLOT 3 */
828862

829863
/* BEGIN MERGE SLOT 4 */

library/bignum_core.h

+109
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,10 @@ int mbedtls_mpi_core_random( mbedtls_mpi_uint *X,
555555
* \brief Returns the number of limbs of working memory required for
556556
* a call to `mbedtls_mpi_core_exp_mod()`.
557557
*
558+
* \note This will always be at least
559+
* `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
560+
* i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
561+
*
558562
* \param AN_limbs The number of limbs in the input `A` and the modulus `N`
559563
* (they must be the same size) that will be given to
560564
* `mbedtls_mpi_core_exp_mod()`.
@@ -625,6 +629,111 @@ mbedtls_mpi_uint mbedtls_mpi_core_sub_int( mbedtls_mpi_uint *X,
625629
mbedtls_mpi_uint b,
626630
size_t limbs );
627631

632+
/**
633+
* \brief Determine if a given MPI has the value \c 0 in constant time with
634+
* respect to the value (but not with respect to the number of limbs).
635+
*
636+
* \param[in] A The MPI to test.
637+
* \param limbs Number of limbs in \p A.
638+
*
639+
* \return 0 if `A == 0`
640+
* non-0 (may be any value) if `A != 0`.
641+
*/
642+
mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct( const mbedtls_mpi_uint *A,
643+
size_t limbs );
644+
645+
/**
646+
* \brief Returns the number of limbs of working memory required for
647+
* a call to `mbedtls_mpi_core_montmul()`.
648+
*
649+
* \param AN_limbs The number of limbs in the input `A` and the modulus `N`
650+
* (they must be the same size) that will be given to
651+
* `mbedtls_mpi_core_montmul()` or one of the other functions
652+
* that specifies this as the amount of working memory needed.
653+
*
654+
* \return The number of limbs of working memory required by
655+
* `mbedtls_mpi_core_montmul()` (or other similar function).
656+
*/
657+
static inline size_t mbedtls_mpi_core_montmul_working_limbs( size_t AN_limbs )
658+
{
659+
return( 2 * AN_limbs + 1 );
660+
}
661+
662+
/** Convert an MPI into Montgomery form.
663+
*
664+
* \p X may be aliased to \p A, but may not otherwise overlap it.
665+
*
666+
* \p X may not alias \p N (it is in canonical form, so must be stricly less
667+
* than \p N). Nor may it alias or overlap \p rr (this is unlikely to be
668+
* required in practice.)
669+
*
670+
* This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
671+
* an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we
672+
* don't want to allocate memory.
673+
*
674+
* \param[out] X The result of the conversion.
675+
* Must have the same number of limbs as \p A.
676+
* \param[in] A The MPI to convert into Montgomery form.
677+
* Must have the same number of limbs as the modulus.
678+
* \param[in] N The address of the modulus, which gives the size of
679+
* the base `R` = 2^(biL*N->limbs).
680+
* \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr.
681+
* \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
682+
* This can be determined by calling
683+
* `mbedtls_mpi_core_montmul_init()`.
684+
* \param[in] rr The residue for `2^{2*n*biL} mod N`.
685+
* \param[in,out] T Temporary storage of size at least
686+
* `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
687+
* limbs.
688+
* Its initial content is unused and
689+
* its final content is indeterminate.
690+
* It must not alias or otherwise overlap any of the
691+
* other parameters.
692+
*/
693+
void mbedtls_mpi_core_to_mont_rep( mbedtls_mpi_uint *X,
694+
const mbedtls_mpi_uint *A,
695+
const mbedtls_mpi_uint *N,
696+
size_t AN_limbs,
697+
mbedtls_mpi_uint mm,
698+
const mbedtls_mpi_uint *rr,
699+
mbedtls_mpi_uint *T );
700+
701+
/** Convert an MPI from Montgomery form.
702+
*
703+
* \p X may be aliased to \p A, but may not otherwise overlap it.
704+
*
705+
* \p X may not alias \p N (it is in canonical form, so must be stricly less
706+
* than \p N).
707+
*
708+
* This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
709+
* an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we
710+
* don't want to allocate memory.
711+
*
712+
* \param[out] X The result of the conversion.
713+
* Must have the same number of limbs as \p A.
714+
* \param[in] A The MPI to convert from Montgomery form.
715+
* Must have the same number of limbs as the modulus.
716+
* \param[in] N The address of the modulus, which gives the size of
717+
* the base `R` = 2^(biL*N->limbs).
718+
* \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
719+
* \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
720+
* This can be determined by calling
721+
* `mbedtls_mpi_core_montmul_init()`.
722+
* \param[in,out] T Temporary storage of size at least
723+
* `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
724+
* limbs.
725+
* Its initial content is unused and
726+
* its final content is indeterminate.
727+
* It must not alias or otherwise overlap any of the
728+
* other parameters.
729+
*/
730+
void mbedtls_mpi_core_from_mont_rep( mbedtls_mpi_uint *X,
731+
const mbedtls_mpi_uint *A,
732+
const mbedtls_mpi_uint *N,
733+
size_t AN_limbs,
734+
mbedtls_mpi_uint mm,
735+
mbedtls_mpi_uint *T );
736+
628737
/* END MERGE SLOT 3 */
629738

630739
/* BEGIN MERGE SLOT 4 */

library/bignum_mod.c

+90-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
7979
if (m->rep.mont.rr != NULL)
8080
{
8181
mbedtls_platform_zeroize( (mbedtls_mpi_uint *) m->rep.mont.rr,
82-
m->limbs );
82+
m->limbs * sizeof(mbedtls_mpi_uint) );
8383
mbedtls_free( (mbedtls_mpi_uint *)m->rep.mont.rr );
8484
m->rep.mont.rr = NULL;
8585
}
@@ -191,6 +191,95 @@ int mbedtls_mpi_mod_sub( mbedtls_mpi_mod_residue *X,
191191

192192
return( 0 );
193193
}
194+
195+
static int mbedtls_mpi_mod_inv_mont( mbedtls_mpi_mod_residue *X,
196+
const mbedtls_mpi_mod_residue *A,
197+
const mbedtls_mpi_mod_modulus *N,
198+
mbedtls_mpi_uint *working_memory )
199+
{
200+
/* Input already in Montgomery form, so there's little to do */
201+
mbedtls_mpi_mod_raw_inv_prime( X->p, A->p,
202+
N->p, N->limbs,
203+
N->rep.mont.rr,
204+
working_memory );
205+
return( 0 );
206+
}
207+
208+
static int mbedtls_mpi_mod_inv_non_mont( mbedtls_mpi_mod_residue *X,
209+
const mbedtls_mpi_mod_residue *A,
210+
const mbedtls_mpi_mod_modulus *N,
211+
mbedtls_mpi_uint *working_memory )
212+
{
213+
/* Need to convert input into Montgomery form */
214+
215+
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
216+
217+
mbedtls_mpi_mod_modulus Nmont;
218+
mbedtls_mpi_mod_modulus_init( &Nmont );
219+
220+
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_modulus_setup( &Nmont, N->p, N->limbs,
221+
MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
222+
223+
/* We'll use X->p to hold the Montgomery form of the input A->p */
224+
mbedtls_mpi_core_to_mont_rep( X->p, A->p, Nmont.p, Nmont.limbs,
225+
Nmont.rep.mont.mm, Nmont.rep.mont.rr,
226+
working_memory );
227+
228+
mbedtls_mpi_mod_raw_inv_prime( X->p, X->p,
229+
Nmont.p, Nmont.limbs,
230+
Nmont.rep.mont.rr,
231+
working_memory );
232+
233+
/* And convert back from Montgomery form */
234+
235+
mbedtls_mpi_core_from_mont_rep( X->p, X->p, Nmont.p, Nmont.limbs,
236+
Nmont.rep.mont.mm, working_memory );
237+
238+
cleanup:
239+
mbedtls_mpi_mod_modulus_free( &Nmont );
240+
return( ret );
241+
}
242+
243+
int mbedtls_mpi_mod_inv( mbedtls_mpi_mod_residue *X,
244+
const mbedtls_mpi_mod_residue *A,
245+
const mbedtls_mpi_mod_modulus *N )
246+
{
247+
if( X->limbs != N->limbs || A->limbs != N->limbs )
248+
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
249+
250+
/* Zero has the same value regardless of Montgomery form or not */
251+
if( mbedtls_mpi_core_check_zero_ct( A->p, A->limbs ) == 0 )
252+
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
253+
254+
size_t working_limbs =
255+
mbedtls_mpi_mod_raw_inv_prime_working_limbs( N->limbs );
256+
257+
mbedtls_mpi_uint *working_memory = mbedtls_calloc( working_limbs,
258+
sizeof(mbedtls_mpi_uint) );
259+
if( working_memory == NULL )
260+
return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
261+
262+
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
263+
264+
switch( N->int_rep )
265+
{
266+
case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
267+
ret = mbedtls_mpi_mod_inv_mont( X, A, N, working_memory );
268+
break;
269+
case MBEDTLS_MPI_MOD_REP_OPT_RED:
270+
ret = mbedtls_mpi_mod_inv_non_mont( X, A, N, working_memory );
271+
break;
272+
default:
273+
ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
274+
break;
275+
}
276+
277+
mbedtls_platform_zeroize( working_memory,
278+
working_limbs * sizeof(mbedtls_mpi_uint) );
279+
mbedtls_free( working_memory );
280+
281+
return ret;
282+
}
194283
/* END MERGE SLOT 3 */
195284

196285
/* BEGIN MERGE SLOT 4 */

library/bignum_mod.h

+29
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,35 @@ int mbedtls_mpi_mod_sub( mbedtls_mpi_mod_residue *X,
249249
const mbedtls_mpi_mod_residue *A,
250250
const mbedtls_mpi_mod_residue *B,
251251
const mbedtls_mpi_mod_modulus *N );
252+
253+
/**
254+
* \brief Perform modular inversion of an MPI with respect to a modulus \p N.
255+
*
256+
* \p A and \p X must be associated with the modulus \p N and will therefore
257+
* have the same number of limbs as \p N.
258+
*
259+
* \p X may be aliased to \p A.
260+
*
261+
* \warning Currently only supports prime moduli, but does not check for them.
262+
*
263+
* \param[out] X The modular inverse of \p A with respect to \p N.
264+
* \param[in] A The number to calculate the modular inverse of.
265+
* Must not be 0.
266+
* \param[in] N The modulus to use.
267+
*
268+
* \return \c 0 if successful.
269+
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A and \p N do not
270+
* have the same number of limbs.
271+
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A is zero.
272+
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
273+
* memory (needed for conversion to and from Mongtomery form
274+
* when not in Montgomery form already, and for temporary use
275+
* by the inversion calculation itself).
276+
*/
277+
278+
int mbedtls_mpi_mod_inv( mbedtls_mpi_mod_residue *X,
279+
const mbedtls_mpi_mod_residue *A,
280+
const mbedtls_mpi_mod_modulus *N );
252281
/* END MERGE SLOT 3 */
253282

254283
/* BEGIN MERGE SLOT 4 */

library/bignum_mod_raw.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
193193
const mbedtls_mpi_mod_modulus *m )
194194
{
195195
mbedtls_mpi_uint *T;
196-
const size_t t_limbs = m->limbs * 2 + 1;
196+
const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs( m->limbs );
197197

198198
if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL )
199199
return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
200200

201-
mbedtls_mpi_core_montmul( X, X, m->rep.mont.rr, m->limbs, m->p, m->limbs,
202-
m->rep.mont.mm, T );
201+
mbedtls_mpi_core_to_mont_rep( X, X, m->p, m->limbs,
202+
m->rep.mont.mm, m->rep.mont.rr, T );
203203

204204
mbedtls_platform_zeroize( T, t_limbs * ciL );
205205
mbedtls_free( T );
@@ -209,15 +209,13 @@ int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
209209
int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
210210
const mbedtls_mpi_mod_modulus *m )
211211
{
212-
const mbedtls_mpi_uint one = 1;
213-
const size_t t_limbs = m->limbs * 2 + 1;
212+
const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs( m->limbs );
214213
mbedtls_mpi_uint *T;
215214

216215
if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL )
217216
return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
218217

219-
mbedtls_mpi_core_montmul( X, X, &one, 1, m->p, m->limbs,
220-
m->rep.mont.mm, T );
218+
mbedtls_mpi_core_from_mont_rep( X, X, m->p, m->limbs, m->rep.mont.mm, T );
221219

222220
mbedtls_platform_zeroize( T, t_limbs * ciL );
223221
mbedtls_free( T );

library/bignum_mod_raw.h

+4
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ void mbedtls_mpi_mod_raw_mul( mbedtls_mpi_uint *X,
258258
* \brief Returns the number of limbs of working memory required for
259259
* a call to `mbedtls_mpi_mod_raw_inv_prime()`.
260260
*
261+
* \note This will always be at least
262+
* `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
263+
* i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
264+
*
261265
* \param AN_limbs The number of limbs in the input `A` and the modulus `N`
262266
* (they must be the same size) that will be given to
263267
* `mbedtls_mpi_mod_raw_inv_prime()`.

scripts/mbedtls_dev/bignum_common.py

+17
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def invmod(a: int, n: int) -> int:
3939
return b
4040
raise ValueError("Not invertible")
4141

42+
def invmod_positive(a: int, n: int) -> int:
43+
"""Return a non-negative inverse of a to modulo n."""
44+
inv = invmod(a, n)
45+
return inv if inv >= 0 else inv + n
46+
4247
def hex_to_int(val: str) -> int:
4348
"""Implement the syntax accepted by mbedtls_test_read_mpi().
4449
@@ -244,6 +249,8 @@ class ModOperationCommon(OperationCommon):
244249
#pylint: disable=abstract-method
245250
"""Target for bignum mod_raw test case generation."""
246251
moduli = MODULI_DEFAULT # type: List[str]
252+
montgomery_form_a = False
253+
disallow_zero_a = False
247254

248255
def __init__(self, val_n: str, val_a: str, val_b: str = "0",
249256
bits_in_limb: int = 64) -> None:
@@ -263,6 +270,14 @@ def from_montgomery(self, val: int) -> int:
263270
def boundary(self) -> int:
264271
return self.int_n
265272

273+
@property
274+
def arg_a(self) -> str:
275+
if self.montgomery_form_a:
276+
value_a = self.to_montgomery(self.int_a)
277+
else:
278+
value_a = self.int_a
279+
return self.format_arg('{:x}'.format(value_a))
280+
266281
@property
267282
def arg_n(self) -> str:
268283
return self.format_arg(self.val_n)
@@ -287,6 +302,8 @@ def r2(self) -> int: # pylint: disable=invalid-name
287302
def is_valid(self) -> bool:
288303
if self.int_a >= self.int_n:
289304
return False
305+
if self.disallow_zero_a and self.int_a == 0:
306+
return False
290307
if self.arity == 2 and self.int_b >= self.int_n:
291308
return False
292309
return True

0 commit comments

Comments
 (0)