Skip to content

Commit e0b592d

Browse files
Merge pull request #12196 from holtrop-wolfssl/zd-22801
Document that TFM APIs disallow even moduli
2 parents 11e7979 + 0163372 commit e0b592d

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎wolfcrypt/src/tfm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2940,7 +2940,7 @@ static int _fp_exptmod_base_2(fp_int * X, int digits, fp_int * P,
29402940
#undef WINSIZE
29412941
#endif
29422942

2943-
/* Y = (G * X) mod P */
2943+
/* Y = (G ** X) mod P */
29442944
int fp_exptmod(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
29452945
{
29462946
#if defined(WOLFSSL_ESP32_CRYPT_RSA_PRI_EXPTMOD)

‎wolfssl/wolfcrypt/tfm.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,8 @@ int fp_invmod_mont_ct(fp_int *a, fp_int *b, fp_int *c, fp_digit mp);
640640
/* c = [a, b] */
641641
/*int fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/
642642

643-
/* setups the montgomery reduction */
643+
/* setups the montgomery reduction
644+
* The modulus must be odd: returns FP_VAL if it is even. */
644645
int fp_montgomery_setup(fp_int *a, fp_digit *rho);
645646

646647
/* computes a = B**n mod b without division or multiplication useful for
@@ -652,7 +653,10 @@ int fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
652653
int fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
653654
int fp_montgomery_reduce_ex(fp_int *a, fp_int *m, fp_digit mp, int ct);
654655

655-
/* d = a**b (mod c) */
656+
/* Y = G**X (mod P)
657+
* Only odd moduli are supported (Montgomery reduction is used). An even P
658+
* results in FP_VAL being returned, except for the trivial cases P == 1,
659+
* X == 0 and G == 0, which return the mathematically correct result. */
656660
int fp_exptmod(fp_int *G, fp_int *X, fp_int *P, fp_int *Y);
657661
int fp_exptmod_ex(fp_int *G, fp_int *X, int minDigits, fp_int *P, fp_int *Y);
658662
int fp_exptmod_nct(fp_int *G, fp_int *X, fp_int *P, fp_int *Y);
@@ -702,6 +706,7 @@ enum {
702706

703707
/* non-blocking version of timing resistant fp_exptmod function */
704708
/* supports cache resistance */
709+
/* P must be odd: returns FP_VAL if it is even. */
705710
int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y);
706711

707712
#endif /* WC_RSA_NONBLOCK */
@@ -843,6 +848,8 @@ MP_API int mp_addmod_ct (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
843848
MP_API int mp_mod(mp_int *a, mp_int *b, mp_int *c);
844849
MP_API int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
845850
MP_API int mp_invmod_mont_ct(mp_int *a, mp_int *b, mp_int *c, fp_digit mp);
851+
/* mp_exptmod*: modulus p must be odd; an even p returns MP_VAL (see
852+
* fp_exptmod). */
846853
MP_API int mp_exptmod (mp_int * g, mp_int * x, mp_int * p, mp_int * y);
847854
MP_API int mp_exptmod_ex (mp_int * g, mp_int * x, int minDigits, mp_int * p,
848855
mp_int * y);

0 commit comments

Comments
 (0)