diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index ab6d23ccd45..328f02a8418 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -111,7 +111,8 @@ This library provides single precision (SP) integer math functions. #if defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS) && !defined(WOLFSSL_SP_ASM) /* force off unneeded vector register save/restore. */ #undef SAVE_VECTOR_REGISTERS - #define SAVE_VECTOR_REGISTERS(fail_clause) SAVE_NO_VECTOR_REGISTERS(fail_clause) + #define SAVE_VECTOR_REGISTERS(fail_clause) \ + SAVE_NO_VECTOR_REGISTERS(fail_clause) #undef RESTORE_VECTOR_REGISTERS #define RESTORE_VECTOR_REGISTERS() RESTORE_NO_VECTOR_REGISTERS() #endif @@ -651,9 +652,9 @@ while (0) * * Using divq instruction on Intel x64. * - * @param [in] hi SP integer digit. High digit of the dividend. - * @param [in] lo SP integer digit. Lower digit of the dividend. - * @param [in] d SP integer digit. Number to divide by. + * @param [in] hi SP integer digit. High digit of the dividend. + * @param [in] lo SP integer digit. Low digit of the dividend. + * @param [in] d SP integer digit. Number to divide by. * @return The division result. */ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, @@ -856,9 +857,9 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, * * Using divl instruction on Intel x64. * - * @param [in] hi SP integer digit. High digit of the dividend. - * @param [in] lo SP integer digit. Lower digit of the dividend. - * @param [in] d SP integer digit. Number to divide by. + * @param [in] hi SP integer digit. High digit of the dividend. + * @param [in] lo SP integer digit. Low digit of the dividend. + * @param [in] d SP integer digit. Number to divide by. * @return The division result. */ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, @@ -1036,9 +1037,9 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, * Using udiv instruction on Aarch64. * Constant time. * - * @param [in] hi SP integer digit. High digit of the dividend. - * @param [in] lo SP integer digit. Lower digit of the dividend. - * @param [in] d SP integer digit. Number to divide by. + * @param [in] hi SP integer digit. High digit of the dividend. + * @param [in] lo SP integer digit. Low digit of the dividend. + * @param [in] d SP integer digit. Number to divide by. * @return The division result. */ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, @@ -1252,9 +1253,9 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, * No division instruction used - does operation bit by bit. * Constant time. * - * @param [in] hi SP integer digit. High digit of the dividend. - * @param [in] lo SP integer digit. Lower digit of the dividend. - * @param [in] d SP integer digit. Number to divide by. + * @param [in] hi SP integer digit. High digit of the dividend. + * @param [in] lo SP integer digit. Low digit of the dividend. + * @param [in] d SP integer digit. Number to divide by. * @return The division result. */ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, @@ -1354,12 +1355,12 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, #else /* Divide a two digit number by a digit number and return. (hi | lo) / d * - * Using udiv instruction on arm32 + * Using udiv instruction on ARM32. * Constant time. * - * @param [in] hi SP integer digit. High digit of the dividend. - * @param [in] lo SP integer digit. Lower digit of the dividend. - * @param [in] d SP integer digit. Number to divide by. + * @param [in] hi SP integer digit. High digit of the dividend. + * @param [in] lo SP integer digit. Low digit of the dividend. + * @param [in] d SP integer digit. Number to divide by. * @return The division result. */ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, @@ -2895,9 +2896,9 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, * No division instruction used - does operation bit by bit. * Constant time. * - * @param [in] hi SP integer digit. High digit of the dividend. - * @param [in] lo SP integer digit. Lower digit of the dividend. - * @param [in] d SP integer digit. Number to divide by. + * @param [in] hi SP integer digit. High digit of the dividend. + * @param [in] lo SP integer digit. Low digit of the dividend. + * @param [in] d SP integer digit. Number to divide by. * @return The division result. */ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, @@ -5013,7 +5014,7 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, #endif /* SP_ASM_ADDC_REG */ #ifndef SP_ASM_SUBB_REG #define SP_ASM_SUBB_REG SP_ASM_SUBB - #endif /* SP_ASM_ADDC_REG */ + #endif /* SP_ASM_SUBB_REG */ #endif /* SQR_MUL_ASM */ #endif /* !WOLFSSL_NO_ASM */ @@ -5074,7 +5075,7 @@ static void _sp_mont_setup(const sp_int* m, sp_int_digit* rho); * * Assumes a is not NULL. * - * @param [out] a SP integer to set to zero. + * @param [out] a SP integer to set to zero. */ static void _sp_zero(sp_int* a) { @@ -5090,8 +5091,8 @@ static void _sp_zero(sp_int* a) /* Initialize the multi-precision number to be zero with a given max size. * - * @param [out] a SP integer. - * @param [in] size Number of words to say are available. + * @param [out] a SP integer. + * @param [in] size Number of words to make available. */ static void _sp_init_size(sp_int* a, unsigned int size) { @@ -5107,8 +5108,8 @@ static void _sp_init_size(sp_int* a, unsigned int size) /* Initialize the multi-precision number to be zero with a given max size. * - * @param [out] a SP integer. - * @param [in] size Number of words to say are available. + * @param [out] a SP integer. + * @param [in] size Number of words to make available. * * @return MP_OKAY on success. * @return MP_VAL when a is NULL. @@ -5131,7 +5132,7 @@ int sp_init_size(sp_int* a, unsigned int size) /* Initialize the multi-precision number to be zero. * - * @param [out] a SP integer. + * @param [out] a SP integer. * * @return MP_OKAY on success. * @return MP_VAL when a is NULL. @@ -5155,12 +5156,12 @@ int sp_init(sp_int* a) #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || !defined(NO_DH) || defined(HAVE_ECC) /* Initialize up to six multi-precision numbers to be zero. * - * @param [out] n1 SP integer. - * @param [out] n2 SP integer. - * @param [out] n3 SP integer. - * @param [out] n4 SP integer. - * @param [out] n5 SP integer. - * @param [out] n6 SP integer. + * @param [out] n1 SP integer. + * @param [out] n2 SP integer. + * @param [out] n3 SP integer. + * @param [out] n4 SP integer. + * @param [out] n5 SP integer. + * @param [out] n6 SP integer. * * @return MP_OKAY on success. */ @@ -5193,7 +5194,7 @@ int sp_init_multi(sp_int* n1, sp_int* n2, sp_int* n3, sp_int* n4, sp_int* n5, /* Free the memory allocated in the multi-precision number. * - * @param [in] a SP integer. + * @param [in] a SP integer. */ void sp_free(sp_int* a) { @@ -5209,11 +5210,11 @@ void sp_free(sp_int* a) /* Grow multi-precision number to be able to hold l digits. * This function does nothing as the number of digits is fixed. * - * @param [in,out] a SP integer. - * @param [in] l Number of digits to grow to. + * @param [in, out] a SP integer. + * @param [in] l Number of digits to grow to. * - * @return MP_OKAY on success - * @return MP_MEM if the number of digits requested is more than available. + * @return MP_OKAY on success. + * @return MP_MEM when the number of digits requested is more than available. */ int sp_grow(sp_int* a, int l) { @@ -5244,7 +5245,7 @@ int sp_grow(sp_int* a, int l) defined(HAVE_ECC) || defined(WOLFSSL_PUBLIC_MP) /* Set the multi-precision number to zero. * - * @param [out] a SP integer to set to zero. + * @param [out] a SP integer to set to zero. */ void sp_zero(sp_int* a) { @@ -5257,7 +5258,7 @@ void sp_zero(sp_int* a) /* Clear the data from the multi-precision number, set to zero and free. * - * @param [out] a SP integer. + * @param [out] a SP integer. */ void sp_clear(sp_int* a) { @@ -5285,11 +5286,11 @@ void sp_clear(sp_int* a) * * Use when security sensitive data needs to be wiped. * - * @param [in] a SP integer. + * @param [in] a SP integer. */ void sp_forcezero(sp_int* a) { - /* Zeroize when a vald pointer passed in. */ + /* Zeroize when a valid pointer passed in. */ if (a != NULL) { /* Ensure all data zeroized - data not zeroed when used decreases. */ ForceZero(a->dp, a->size * (word32)SP_WORD_SIZEOF); @@ -5309,8 +5310,8 @@ void sp_forcezero(sp_int* a) !defined(NO_RSA) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) /* Copy value of multi-precision number a into r. * - * @param [in] a SP integer - source. - * @param [out] r SP integer - destination. + * @param [in] a SP integer - source. + * @param [out] r SP integer - destination. */ static void _sp_copy(const sp_int* a, sp_int* r) { @@ -5331,8 +5332,8 @@ static void _sp_copy(const sp_int* a, sp_int* r) /* Copy value of multi-precision number a into r. * - * @param [in] a SP integer - source. - * @param [out] r SP integer - destination. + * @param [in] a SP integer - source. + * @param [out] r SP integer - destination. * * @return MP_OKAY on success. */ @@ -5407,8 +5408,8 @@ static void _sp_copy_2_ct(const sp_int* a1, const sp_int* a2, sp_int* r1, #if defined(WOLFSSL_SP_MATH_ALL) || (defined(HAVE_ECC) && defined(FP_ECC)) /* Initializes r and copies in value from a. * - * @param [out] r SP integer - destination. - * @param [in] a SP integer - source. + * @param [out] r SP integer - destination. + * @param [in] a SP integer - source. * * @return MP_OKAY on success. * @return MP_VAL when a or r is NULL. @@ -5433,8 +5434,8 @@ int sp_init_copy(sp_int* r, const sp_int* a) * * Avoid using this API as three copy operations are performed. * - * @param [in,out] a SP integer to swap. - * @param [in,out] b SP integer to swap. + * @param [in, out] a SP integer to swap. + * @param [in, out] b SP integer to swap. * * @return MP_OKAY on success. * @return MP_VAL when a or b is NULL. @@ -5484,11 +5485,11 @@ int sp_exch(sp_int* a, sp_int* b) !defined(WC_NO_CACHE_RESISTANT) /* Conditional swap of SP int values in constant time. * - * @param [in] a First SP int to conditionally swap. - * @param [in] b Second SP int to conditionally swap. - * @param [in] cnt Count of words to copy. - * @param [in] swap When value is 1 then swap. - * @param [in] t Temporary SP int to use in swap. + * @param [in] a First SP int to conditionally swap. + * @param [in] b Second SP int to conditionally swap. + * @param [in] cnt Count of words to copy. + * @param [in] swap When value is 1 then swap. + * @param [in] t Temporary SP int to use in swap. * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. */ @@ -5531,10 +5532,10 @@ int sp_cond_swap_ct_ex(sp_int* a, sp_int* b, int cnt, int swap, sp_int* t) /* Conditional swap of SP int values in constant time. * - * @param [in] a First SP int to conditionally swap. - * @param [in] b Second SP int to conditionally swap. - * @param [in] cnt Count of words to copy. - * @param [in] swap When value is 1 then swap. + * @param [in] a First SP int to conditionally swap. + * @param [in] b Second SP int to conditionally swap. + * @param [in] cnt Count of words to copy. + * @param [in] swap When value is 1 then swap. * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. */ @@ -5558,8 +5559,8 @@ int sp_cond_swap_ct(sp_int* a, sp_int* b, int cnt, int swap) #ifdef WOLFSSL_SP_INT_NEGATIVE /* Calculate the absolute value of the multi-precision number. * - * @param [in] a SP integer to calculate absolute value of. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to calculate absolute value of. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when a or r is NULL. @@ -5587,7 +5588,7 @@ int sp_abs(const sp_int* a, sp_int* r) * * @return MP_GT when a is greater than b. * @return MP_LT when a is less than b. - * @return MP_EQ when a is equals b. + * @return MP_EQ when a is equal to b. */ static int _sp_cmp_abs(const sp_int* a, const sp_int* b) { @@ -5626,14 +5627,14 @@ static int _sp_cmp_abs(const sp_int* a, const sp_int* b) #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) /* Compare absolute value of two multi-precision numbers. * - * Pointers are compared such that NULL is less than not NULL. + * Pointers are compared such that NULL is less than non-NULL. * * @param [in] a SP integer. * @param [in] b SP integer. * * @return MP_GT when a is greater than b. * @return MP_LT when a is less than b. - * @return MP_EQ when a equals b. + * @return MP_EQ when a is equal to b. */ int sp_cmp_mag(const sp_int* a, const sp_int* b) { @@ -5672,7 +5673,7 @@ int sp_cmp_mag(const sp_int* a, const sp_int* b) * * @return MP_GT when a is greater than b. * @return MP_LT when a is less than b. - * @return MP_EQ when a is equals b. + * @return MP_EQ when a is equal to b. */ static int _sp_cmp(const sp_int* a, const sp_int* b) { @@ -5708,14 +5709,14 @@ static int _sp_cmp(const sp_int* a, const sp_int* b) !defined(NO_DH) || defined(WOLFSSL_SP_MATH_ALL) /* Compare two multi-precision numbers. * - * Pointers are compared such that NULL is less than not NULL. + * Pointers are compared such that NULL is less than non-NULL. * * @param [in] a SP integer. * @param [in] b SP integer. * * @return MP_GT when a is greater than b. * @return MP_LT when a is less than b. - * @return MP_EQ when a is equals b. + * @return MP_EQ when a is equal to b. */ int sp_cmp(const sp_int* a, const sp_int* b) { @@ -5756,7 +5757,7 @@ int sp_cmp(const sp_int* a, const sp_int* b) * * @return MP_GT when a is greater than b. * @return MP_LT when a is less than b. - * @return MP_EQ when a is equals b. + * @return MP_EQ when a is equal to b. */ static int _sp_cmp_ct(const sp_int* a, const sp_int* b, unsigned int n) { @@ -5779,9 +5780,9 @@ static int _sp_cmp_ct(const sp_int* a, const sp_int* b, unsigned int n) /* Compare two multi-precision numbers in constant time. * - * Pointers are compared such that NULL is less than not NULL. + * Pointers are compared such that NULL is less than non-NULL. * Assumes a and b are positive. - * Assumes a and b have n digits set at sometime. + * Assumes a and b have had n digits set at some point. * * @param [in] a SP integer. * @param [in] b SP integer. @@ -5789,7 +5790,7 @@ static int _sp_cmp_ct(const sp_int* a, const sp_int* b, unsigned int n) * * @return MP_GT when a is greater than b. * @return MP_LT when a is less than b. - * @return MP_EQ when a is equals b. + * @return MP_EQ when a is equal to b. */ int sp_cmp_ct(const sp_int* a, const sp_int* b, unsigned int n) { @@ -5828,8 +5829,8 @@ int sp_cmp_ct(const sp_int* a, const sp_int* b, unsigned int n) * * When a is NULL, result is 0. * - * @param [in] a SP integer. - * @param [in] b Bit position to check. + * @param [in] a SP integer. + * @param [in] b Bit position to check. * * @return 0 when bit is not set. * @return 1 when bit is set. @@ -5858,7 +5859,7 @@ int sp_is_bit_set(const sp_int* a, unsigned int b) * * When a is NULL, result is 0. * - * @param [in] a SP integer. + * @param [in] a SP integer. * * @return Number of bits in the SP integer value. */ @@ -5946,7 +5947,7 @@ static const int sp_lnz[SP_LNZ_CNT] = { * * When a is not NULL, result is 0. * - * @param [in] a SP integer to use. + * @param [in] a SP integer to use. * * @return Number of least significant zero bits. */ @@ -5968,7 +5969,7 @@ int sp_cnt_lsb(const sp_int* a) /* Use 4-bit table to get count. */ for (j = 0; j < SP_WORD_SIZE; j += SP_LNZ_BITS) { - /* Get number of lesat significant 0 bits in nibble. */ + /* Get number of least significant 0 bits in nibble. */ int cnt = sp_lnz[(a->dp[i] >> j) & SP_LNZ_MASK]; /* Done if not all 4 bits are zero. */ if (cnt != 4) { @@ -5990,7 +5991,7 @@ int sp_cnt_lsb(const sp_int* a) * * When a is NULL, result is 0. * - * @param [in] a SP integer. + * @param [in] a SP integer. * * @return 1 when the top bit of top byte is set. * @return 0 when the top bit of top byte is not set. @@ -6024,8 +6025,8 @@ int sp_leading_bit(const sp_int* a) /* Set one bit of a: a |= 1 << i * The field 'used' is updated in a. * - * @param [in,out] a SP integer to set bit into. - * @param [in] i Index of bit to set. + * @param [in, out] a SP integer to set bit into. + * @param [in] i Index of bit to set. * * @return MP_OKAY on success. * @return MP_VAL when a is NULL, index is negative or index is too large. @@ -6036,7 +6037,7 @@ int sp_set_bit(sp_int* a, int i) /* Get index of word to set. */ sp_size_t w = (sp_size_t)(i >> SP_WORD_SHIFT); - /* Check for valid number and and space for bit. */ + /* Check for valid number and space for bit. */ if ((a == NULL) || (i < 0) || (w >= a->size)) { err = MP_VAL; } @@ -6069,11 +6070,11 @@ int sp_set_bit(sp_int* a, int i) /* Exponentiate 2 to the power of e: a = 2^e * This is done by setting the 'e'th bit. * - * @param [out] a SP integer to hold result. - * @param [in] e Exponent. + * @param [out] a SP integer to hold result. + * @param [in] e Exponent. * * @return MP_OKAY on success. - * @return MP_VAL when a is NULL, e is negative or 2^exponent is too large. + * @return MP_VAL when a is NULL, e is negative or 2^e is too large. */ int sp_2expt(sp_int* a, int e) { @@ -6102,8 +6103,8 @@ int sp_2expt(sp_int* a, int e) defined(HAVE_ECC) /* Set the multi-precision number to be the value of the digit. * - * @param [out] a SP integer to become number. - * @param [in] d Digit to be set. + * @param [out] a SP integer to become number. + * @param [in] d Digit to be set. */ static void _sp_set(sp_int* a, sp_int_digit d) { @@ -6120,8 +6121,8 @@ static void _sp_set(sp_int* a, sp_int_digit d) /* Set the multi-precision number to be the value of the digit. * - * @param [out] a SP integer to become number. - * @param [in] d Digit to be set. + * @param [out] a SP integer to become number. + * @param [in] d Digit to be set. * * @return MP_OKAY on success. * @return MP_VAL when a is NULL. @@ -6147,8 +6148,8 @@ int sp_set(sp_int* a, sp_int_digit d) * * Number may be larger than the size of a digit. * - * @param [out] a SP integer to set. - * @param [in] n Long value to set. + * @param [out] a SP integer to set. + * @param [in] n Long value to set. * * @return MP_OKAY on success. * @return MP_VAL when a is NULL. @@ -6200,12 +6201,12 @@ int sp_set_int(sp_int* a, unsigned long n) * * When a is NULL, MP_LT is returned. * - * @param [in] a SP integer to compare. - * @param [in] d Digit to compare with. + * @param [in] a SP integer to compare. + * @param [in] d Digit to compare with. * * @return MP_GT when a is greater than d. * @return MP_LT when a is less than d. - * @return MP_EQ when a is equals d. + * @return MP_EQ when a is equal to d. */ int sp_cmp_d(const sp_int* a, sp_int_digit d) { @@ -6255,9 +6256,9 @@ int sp_cmp_d(const sp_int* a, sp_int_digit d) defined(WOLFSSL_SP_SUB_D)) || defined(WOLFSSL_SP_READ_RADIX_10) /* Add a one digit number to the multi-precision number. * - * @param [in] a SP integer be added to. - * @param [in] d Digit to add. - * @param [out] r SP integer to store result in. + * @param [in] a SP integer to be added to. + * @param [in] d Digit to add. + * @param [out] r SP integer to store result in. * * @return MP_OKAY on success. * @return MP_VAL when result is too large for fixed size dp array. @@ -6321,9 +6322,9 @@ static int _sp_add_d(const sp_int* a, sp_int_digit d, sp_int* r) !defined(WC_NO_RNG)) /* Sub a one digit number from the multi-precision number. * - * @param [in] a SP integer be subtracted from. - * @param [in] d Digit to subtract. - * @param [out] r SP integer to store result in. + * @param [in] a SP integer to be subtracted from. + * @param [in] d Digit to subtract. + * @param [out] r SP integer to store result in. */ static void _sp_sub_d(const sp_int* a, sp_int_digit d, sp_int* r) { @@ -6367,9 +6368,9 @@ static void _sp_sub_d(const sp_int* a, sp_int_digit d, sp_int* r) #ifdef WOLFSSL_SP_ADD_D /* Add a one digit number to the multi-precision number. * - * @param [in] a SP integer be added to. - * @param [in] d Digit to add. - * @param [out] r SP integer to store result in. + * @param [in] a SP integer to be added to. + * @param [in] d Digit to add. + * @param [out] r SP integer to store result in. * * @return MP_OKAY on success. * @return MP_VAL when result is too large for fixed size dp array. @@ -6430,9 +6431,9 @@ int sp_add_d(const sp_int* a, sp_int_digit d, sp_int* r) #ifdef WOLFSSL_SP_SUB_D /* Sub a one digit number from the multi-precision number. * - * @param [in] a SP integer be subtracted from. - * @param [in] d Digit to subtract. - * @param [out] r SP integer to store result in. + * @param [in] a SP integer to be subtracted from. + * @param [in] d Digit to subtract. + * @param [out] r SP integer to store result in. * * @return MP_OKAY on success. * @return MP_VAL when a or r is NULL. @@ -6500,10 +6501,10 @@ int sp_sub_d(const sp_int* a, sp_int_digit d, sp_int* r) /* Multiply a by digit n and put result into r shifting up o digits. * r = (a * n) << (o * SP_WORD_SIZE) * - * @param [in] a SP integer to be multiplied. - * @param [in] d SP digit to multiply by. - * @param [out] r SP integer result. - * @param [in] o Number of digits to move result up by. + * @param [in] a SP integer to be multiplied. + * @param [in] d SP digit to multiply by. + * @param [out] r SP integer result. + * @param [in] o Number of digits to move result up by. * @return MP_OKAY on success. * @return MP_VAL when result is too large for sp_int. */ @@ -6580,14 +6581,15 @@ static int _sp_mul_d(const sp_int* a, sp_int_digit d, sp_int* r, unsigned int o) * WOLFSSL_SP_SMALL || (WOLFSSL_KEY_GEN && !NO_RSA) */ #ifdef WOLFSSL_SP_MUL_D -/* Multiply a by digit n and put result into r. r = a * n +/* Multiply a by digit d and put result into r. r = a * d * - * @param [in] a SP integer to multiply. - * @param [in] n Digit to multiply by. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to multiply. + * @param [in] d Digit to multiply by. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. - * @return MP_VAL when a or b is NULL, or a has maximum number of digits used. + * @return MP_VAL when a or r is NULL, or a has the maximum number of digits + * used. */ int sp_mul_d(const sp_int* a, sp_int_digit d, sp_int* r) { @@ -6639,9 +6641,9 @@ int sp_mul_d(const sp_int* a, sp_int_digit d, sp_int* r) #ifndef SP_ASM_DIV_WORD /* Divide a two digit number by a digit number and return. (hi | lo) / d * - * @param [in] hi SP integer digit. High digit of the dividend. - * @param [in] lo SP integer digit. Lower digit of the dividend. - * @param [in] d SP integer digit. Number to divide by. + * @param [in] hi SP integer digit. High digit of the dividend. + * @param [in] lo SP integer digit. Low digit of the dividend. + * @param [in] d SP integer digit. Number to divide by. * @return The division result. */ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, @@ -6751,9 +6753,9 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo, * * Used in checking prime: (a % 3) == 0?. * - * @param [in] a SP integer to be divided. - * @param [out] r SP integer that is the quotient. May be NULL. - * @param [out] rem SP integer that is the remainder. May be NULL. + * @param [in] a SP integer to be divided. + * @param [out] r SP integer that is the quotient. May be NULL. + * @param [out] rem SP integer that is the remainder. May be NULL. */ static void _sp_div_3(const sp_int* a, sp_int* r, sp_int_digit* rem) { @@ -6860,9 +6862,9 @@ static void _sp_div_3(const sp_int* a, sp_int* r, sp_int_digit* rem) * * Used when writing with a radix of 10 - decimal number. * - * @param [in] a SP integer to be divided. - * @param [out] r SP integer that is the quotient. May be NULL. - * @param [out] rem SP integer that is the remainder. May be NULL. + * @param [in] a SP integer to be divided. + * @param [out] r SP integer that is the quotient. May be NULL. + * @param [out] rem SP integer that is the remainder. May be NULL. */ static void _sp_div_10(const sp_int* a, sp_int* r, sp_int_digit* rem) { @@ -6947,10 +6949,10 @@ static void _sp_div_10(const sp_int* a, sp_int* r, sp_int_digit* rem) #if defined(WOLFSSL_SP_DIV_D) || defined(WOLFSSL_SP_MOD_D) /* Divide by small number: r = a / d and rem = a % d * - * @param [in] a SP integer to be divided. - * @param [in] d Digit to divide by. - * @param [out] r SP integer that is the quotient. May be NULL. - * @param [out] rem SP integer that is the remainder. May be NULL. + * @param [in] a SP integer to be divided. + * @param [in] d Digit to divide by. + * @param [out] r SP integer that is the quotient. May be NULL. + * @param [out] rem SP integer that is the remainder. May be NULL. */ static void _sp_div_small(const sp_int* a, sp_int_digit d, sp_int* r, sp_int_digit* rem) @@ -7051,10 +7053,10 @@ static void _sp_div_small(const sp_int* a, sp_int_digit d, sp_int* r, * * Use trial division algorithm. * - * @param [in] a SP integer to be divided. - * @param [in] d Digit to divide by. - * @param [out] r SP integer that is the quotient. May be NULL. - * @param [out] rem Digit that is the remainder. May be NULL. + * @param [in] a SP integer to be divided. + * @param [in] d Digit to divide by. + * @param [out] r SP integer that is the quotient. May be NULL. + * @param [out] rem Digit that is the remainder. May be NULL. */ static void _sp_div_d(const sp_int* a, sp_int_digit d, sp_int* r, sp_int_digit* rem) @@ -7111,10 +7113,10 @@ static void _sp_div_d(const sp_int* a, sp_int_digit d, sp_int* r, * remainder. * r = a / d; rem = a % d * - * @param [in] a SP integer to be divided. - * @param [in] d Digit to divide by. - * @param [out] r SP integer that is the quotient. May be NULL. - * @param [out] rem Digit that is the remainder. May be NULL. + * @param [in] a SP integer to be divided. + * @param [in] d Digit to divide by. + * @param [out] r SP integer that is the quotient. May be NULL. + * @param [out] rem Digit that is the remainder. May be NULL. * * @return MP_OKAY on success. * @return MP_VAL when a is NULL or d is 0. @@ -7170,9 +7172,9 @@ int sp_div_d(const sp_int* a, sp_int_digit d, sp_int* r, sp_int_digit* rem) #ifdef WOLFSSL_SP_MOD_D /* Calculate a modulo the digit d into r: r = a mod d * - * @param [in] a SP integer to reduce. - * @param [in] d Digit to that is the modulus. - * @param [out] r Digit that is the result. + * @param [in] a SP integer to reduce. + * @param [in] d Digit that is the modulus. + * @param [out] r Digit that is the result. */ static void _sp_mod_d(const sp_int* a, const sp_int_digit d, sp_int_digit* r) { @@ -7210,9 +7212,9 @@ static void _sp_mod_d(const sp_int* a, const sp_int_digit d, sp_int_digit* r) /* Calculate a modulo the digit d into r: r = a mod d * - * @param [in] a SP integer to reduce. - * @param [in] d Digit to that is the modulus. - * @param [out] r Digit that is the result. + * @param [in] a SP integer to reduce. + * @param [in] d Digit that is the modulus. + * @param [out] r Digit that is the result. * * @return MP_OKAY on success. * @return MP_VAL when a is NULL or d is 0. @@ -7285,8 +7287,8 @@ int sp_mod_d(const sp_int* a, sp_int_digit d, sp_int_digit* r) !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_SP_INVMOD) /* Divides a by 2 and stores in r: r = a >> 1 * - * @param [in] a SP integer to divide. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to divide. + * @param [out] r SP integer to hold result. */ static void _sp_div_2(const sp_int* a, sp_int* r) { @@ -7312,8 +7314,8 @@ static void _sp_div_2(const sp_int* a, sp_int* r) #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC) /* Divides a by 2 and stores in r: r = a >> 1 * - * @param [in] a SP integer to divide. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to divide. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when a or r is NULL. @@ -7346,9 +7348,9 @@ int sp_div_2(const sp_int* a, sp_int* r) * * r = a / 2 (mod m) - constant time (a < m and positive) * - * @param [in] a SP integer to divide. - * @param [in] m SP integer that is modulus. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to divide. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when a, m or r is NULL. @@ -7444,10 +7446,10 @@ int sp_div_2_mod_ct(const sp_int* a, const sp_int* m, sp_int* r) #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_SP_INVMOD) /* Add offset b to a into r: r = a + (b << (o * SP_WORD_SIZEOF)) * - * @param [in] a SP integer to add to. - * @param [in] b SP integer to add. - * @param [out] r SP integer to store result in. - * @param [in] o Number of digits to offset b. + * @param [in] a SP integer to add to. + * @param [in] b SP integer to add. + * @param [out] r SP integer to store result in. + * @param [in] o Number of digits to offset b. */ static void _sp_add_off(const sp_int* a, const sp_int* b, sp_int* r, int o) { @@ -7467,7 +7469,7 @@ static void _sp_add_off(const sp_int* a, const sp_int* b, sp_int* r, int o) for (; (i < o) && (i < a->used); i++) { r->dp[i] = a->dp[i]; } - /* Set result to 0 for digits beyonf those in a. */ + /* Set result to 0 for digits beyond those in a. */ for (; i < o; i++) { r->dp[i] = 0; } @@ -7591,10 +7593,10 @@ static void _sp_add_off(const sp_int* a, const sp_int* b, sp_int* r, int o) * * When using offset, r == a is faster. * - * @param [in] a SP integer to subtract from. - * @param [in] b SP integer to subtract. - * @param [out] r SP integer to store result in. - * @param [in] o Number of digits to offset b. + * @param [in] a SP integer to subtract from. + * @param [in] b SP integer to subtract. + * @param [out] r SP integer to store result in. + * @param [in] o Number of digits to offset b. */ static void _sp_sub_off(const sp_int* a, const sp_int* b, sp_int* r, sp_size_t o) @@ -7671,9 +7673,9 @@ static void _sp_sub_off(const sp_int* a, const sp_int* b, sp_int* r, #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_SP_INVMOD) /* Add b to a into r: r = a + b * - * @param [in] a SP integer to add to. - * @param [in] b SP integer to add. - * @param [out] r SP integer to store result in. + * @param [in] a SP integer to add to. + * @param [in] b SP integer to add. + * @param [out] r SP integer to store result in. * * @return MP_OKAY on success. * @return MP_VAL when a, b, or r is NULL. @@ -7686,7 +7688,7 @@ int sp_add(const sp_int* a, const sp_int* b, sp_int* r) if ((a == NULL) || (b == NULL) || (r == NULL)) { err = MP_VAL; } - /* Check that r as big as a and b plus one word. */ + /* Check that r is as big as a and b plus one word. */ if ((err == MP_OKAY) && ((a->used >= r->size) || (b->used >= r->size))) { err = MP_VAL; } @@ -7731,9 +7733,9 @@ int sp_add(const sp_int* a, const sp_int* b, sp_int* r) * * a must be greater than b unless WOLFSSL_SP_INT_NEGATIVE is defined. * - * @param [in] a SP integer to subtract from. - * @param [in] b SP integer to subtract. - * @param [out] r SP integer to store result in. + * @param [in] a SP integer to subtract from. + * @param [in] b SP integer to subtract. + * @param [out] r SP integer to store result in. * * @return MP_OKAY on success. * @return MP_VAL when a, b, or r is NULL. @@ -7746,7 +7748,7 @@ int sp_sub(const sp_int* a, const sp_int* b, sp_int* r) if ((a == NULL) || (b == NULL) || (r == NULL)) { err = MP_VAL; } - /* Check that r as big as a and b plus one word. */ + /* Check that r is as big as a and b plus one word. */ if ((err == MP_OKAY) && ((a->used >= r->size) || (b->used >= r->size))) { err = MP_VAL; } @@ -7794,12 +7796,12 @@ int sp_sub(const sp_int* a, const sp_int* b, sp_int* r) #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \ (!defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_CUSTOM_CURVES)) || \ defined(WOLFCRYPT_HAVE_ECCSI) || defined(WOLFCRYPT_HAVE_SAKKE) -/* Add two value and reduce: r = (a + b) % m +/* Add two values and reduce: r = (a + b) % m * - * @param [in] a SP integer to add. - * @param [in] b SP integer to add with. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to add. + * @param [in] b SP integer to add with. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -7809,7 +7811,8 @@ static int _sp_addmod(const sp_int* a, const sp_int* b, const sp_int* m, { int err = MP_OKAY; /* Calculate used based on digits used in a and b. */ - sp_size_t used = (sp_size_t)(((a->used >= b->used) ? a->used + 1U : b->used + 1U)); + sp_size_t used = (sp_size_t)(((a->used >= b->used) ? a->used + 1U : + b->used + 1U)); DECL_SP_INT(t, used); /* Allocate a temporary SP int to hold sum. */ @@ -7828,12 +7831,12 @@ static int _sp_addmod(const sp_int* a, const sp_int* b, const sp_int* m, return err; } -/* Add two value and reduce: r = (a + b) % m +/* Add two values and reduce: r = (a + b) % m * - * @param [in] a SP integer to add. - * @param [in] b SP integer to add with. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to add. + * @param [in] b SP integer to add with. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when a, b, m or r is NULL. @@ -7883,10 +7886,10 @@ int sp_addmod(const sp_int* a, const sp_int* b, const sp_int* m, sp_int* r) /* Sub b from a and reduce: r = (a - b) % m * Result is always positive. * - * @param [in] a SP integer to subtract from - * @param [in] b SP integer to subtract. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to subtract from. + * @param [in] b SP integer to subtract. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -7942,7 +7945,7 @@ static int _sp_submod(const sp_int* a, const sp_int* b, const sp_int* m, err = sp_sub(a, b, t); } if (err == MP_OKAY) { - /* Reduce result mod m into result. */ + /* Reduce result mod m into r. */ err = sp_mod(t, m, r); } FREE_SP_INT(t, NULL); @@ -7954,10 +7957,10 @@ static int _sp_submod(const sp_int* a, const sp_int* b, const sp_int* m, /* Sub b from a and reduce: r = (a - b) % m * Result is always positive. * - * @param [in] a SP integer to subtract from - * @param [in] b SP integer to subtract. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to subtract from. + * @param [in] b SP integer to subtract. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when a, b, m or r is NULL. @@ -8032,17 +8035,17 @@ static void sp_clamp_ct(sp_int* a) } #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC) -/* Add two value and reduce: r = (a + b) % m +/* Add two values and reduce: r = (a + b) % m * * r = a + b (mod m) - constant time (a < m and b < m, a, b and m are positive) * * Assumes a, b, m and r are not NULL. * m and r must not be the same pointer. * - * @param [in] a SP integer to add. - * @param [in] b SP integer to add with. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to add. + * @param [in] b SP integer to add with. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. */ @@ -8206,10 +8209,10 @@ int sp_addmod_ct(const sp_int* a, const sp_int* b, const sp_int* m, sp_int* r) * Assumes a, b, m and r are not NULL. * m and r must not be the same pointer. * - * @param [in] a SP integer to subtract from - * @param [in] b SP integer to subtract. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to subtract from. + * @param [in] b SP integer to subtract. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. */ @@ -8317,10 +8320,10 @@ static void _sp_submod_ct(const sp_int* a, const sp_int* b, const sp_int* m, * Assumes a, b, m and r are not NULL. * m and r must not be the same pointer. * - * @param [in] a SP integer to subtract from - * @param [in] b SP integer to subtract. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to subtract from. + * @param [in] b SP integer to subtract. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. */ @@ -8377,15 +8380,15 @@ void sp_xor_ct(const sp_int* a, const sp_int* b, int len, sp_int* r) #endif /******************** - * Shifting functoins + * Shifting functions ********************/ #if !defined(NO_DH) || defined(HAVE_ECC) || (!defined(NO_RSA) && \ defined(WC_RSA_BLINDING) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) /* Left shift the multi-precision number by a number of digits. * - * @param [in,out] a SP integer to shift. - * @param [in] s Number of digits to shift. + * @param [in, out] a SP integer to shift. + * @param [in] s Number of digits to shift. * * @return MP_OKAY on success. * @return MP_VAL when a is NULL, s is negative or the result is too big. @@ -8425,8 +8428,8 @@ int sp_lshd(sp_int* a, int s) * * Used by sp_mul_2d() and other internal functions. * - * @param [in,out] a SP integer to shift. - * @param [in] n Number of bits to shift left. + * @param [in, out] a SP integer to shift. + * @param [in] n Number of bits to shift left. * * @return MP_OKAY on success. * @return MP_VAL when the result is too big. @@ -8487,8 +8490,8 @@ static int sp_lshb(sp_int* a, int n) #ifdef WOLFSSL_SP_MATH_ALL /* Shift a right by c digits: a = a >> (n * SP_WORD_SIZE) * - * @param [in, out] a SP integer to shift. - * @param [in] c Number of digits to shift. + * @param [in, out] a SP integer to shift. + * @param [in] c Number of digits to shift. */ void sp_rshd(sp_int* a, int c) { @@ -8517,9 +8520,9 @@ void sp_rshd(sp_int* a, int c) defined(WOLFSSL_HAVE_SP_DH) /* Shift a right by n bits into r: r = a >> n * - * @param [in] a SP integer to shift. - * @param [in] n Number of bits to shift. - * @param [out] r SP integer to store result in. + * @param [in] a SP integer to shift. + * @param [in] n Number of bits to shift. + * @param [out] r SP integer to store result in. */ int sp_rshb(const sp_int* a, int n, sp_int* r) { @@ -8614,10 +8617,10 @@ static void _sp_div_same_size(sp_int* a, const sp_int* d, sp_int* r) * * Note: a is constantly having multiplies of d subtracted. * - * @param [in, out] a SP integer to be divided and remainder on out. - * @param [in] d SP integer to divide by. - * @param [out] r SP integer that is the quotient. - * @param [out] trial SP integer that is product in trial division. + * @param [in, out] a SP integer to be divided and remainder on out. + * @param [in] d SP integer to divide by. + * @param [out] r SP integer that is the quotient. + * @param [out] trial SP integer that is product in trial division. * * @return MP_OKAY on success. * @return MP_VAL when operation fails - only when compiling small code. @@ -8705,7 +8708,7 @@ static int _sp_div_impl(sp_int* a, const sp_int* d, sp_int* r, sp_int* trial) break; } - /* Subtract the trial and add qoutient to result. */ + /* Subtract the trial and add quotient to result. */ _sp_sub_off(a, trial, a, 0); r->dp[i - d->used] += t; /* Handle overflow of digit. */ @@ -8795,11 +8798,11 @@ static int _sp_div_impl(sp_int* a, const sp_int* d, sp_int* r, sp_int* trial) /* Divide a by d and return the quotient in r and the remainder in rem. * r = a / d; rem = a % d * - * @param [in] a SP integer to be divided. - * @param [in] d SP integer to divide by. - * @param [out] r SP integer that is the quotient. - * @param [out] rem SP integer that is the remainder. - * @param [in] used Number of digits in temporaries to use. + * @param [in] a SP integer to be divided. + * @param [in] d SP integer to divide by. + * @param [out] r SP integer that is the quotient. + * @param [out] rem SP integer that is the remainder. + * @param [in] used Number of digits in temporaries to use. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -8971,10 +8974,10 @@ static int _sp_div(const sp_int* a, const sp_int* d, sp_int* r, sp_int* rem, /* Divide a by d and return the quotient in r and the remainder in rem. * r = a / d; rem = a % d * - * @param [in] a SP integer to be divided. - * @param [in] d SP integer to divide by. - * @param [out] r SP integer that is the quotient. - * @param [out] rem SP integer that is the remainder. + * @param [in] a SP integer to be divided. + * @param [in] d SP integer to divide by. + * @param [out] r SP integer that is the quotient. + * @param [out] rem SP integer that is the remainder. * * @return MP_OKAY on success. * @return MP_VAL when a or d is NULL, r and rem are NULL, or d is 0. @@ -9010,8 +9013,8 @@ int sp_div(const sp_int* a, const sp_int* d, sp_int* r, sp_int* rem) if (a->used == SP_INT_DIGITS) { /* May need to shift number being divided left into a new word. */ int bits = SP_WORD_SIZE - (sp_count_bits(d) % SP_WORD_SIZE); - if ((bits != SP_WORD_SIZE) && - (sp_count_bits(a) + bits > (int)(SP_INT_DIGITS * SP_WORD_SIZE))) { + if ((bits != SP_WORD_SIZE) && (sp_count_bits(a) + bits > + (int)(SP_INT_DIGITS * SP_WORD_SIZE))) { err = MP_VAL; } else { @@ -9052,11 +9055,13 @@ int sp_div(const sp_int* a, const sp_int* d, sp_int* r, sp_int* rem) !defined(WOLFSSL_RSA_PUBLIC_ONLY)) #ifndef FREESCALE_LTC_TFM #ifdef WOLFSSL_SP_INT_NEGATIVE -/* Calculate the remainder of dividing a by m: r = a mod m. r is m. +/* Calculate the remainder of dividing a by m: r = a mod m. + * + * Parameter r can be the same pointer as parameter m. * - * @param [in] a SP integer to reduce. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to store result in. + * @param [in] a SP integer to reduce. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to store result in. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -9091,9 +9096,9 @@ static int _sp_mod(const sp_int* a, const sp_int* m, sp_int* r) /* Calculate the remainder of dividing a by m: r = a mod m. * - * @param [in] a SP integer to reduce. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to store result in. + * @param [in] a SP integer to reduce. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to store result in. * * @return MP_OKAY on success. * @return MP_VAL when a, m or r is NULL or m is 0. @@ -9147,13 +9152,13 @@ int sp_mod(const sp_int* a, const sp_int* m, sp_int* r) */ #ifdef SQR_MUL_ASM -/* Multiply a by b into r where a and b have same no. digits. r = a * b +/* Multiply a by b into r where a and b have same number of digits. r = a * b * - * Optimised code for when number of digits in a and b are the same. + * Optimized code for when number of digits in a and b are the same. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply by. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply by. + * @param [out] r SP integer to hold result. * * @return MP_OKAY otherwise. * @return MP_MEM when dynamic memory allocation fails. @@ -9227,9 +9232,9 @@ static int _sp_mul_nxn(const sp_int* a, const sp_int* b, sp_int* r) /* Multiply a by b into r. r = a * b * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply by. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply by. + * @param [out] r SP integer to hold result. * * @return MP_OKAY otherwise. * @return MP_MEM when dynamic memory allocation fails. @@ -9250,8 +9255,7 @@ static int _sp_mul(const sp_int* a, const sp_int* b, sp_int* r) #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t = (sp_int_digit*)XMALLOC(sizeof(sp_int_digit) * - (size_t)(a->used + b->used), NULL, - DYNAMIC_TYPE_BIGINT); + (size_t)(a->used + b->used), NULL, DYNAMIC_TYPE_BIGINT); if (t == NULL) { err = MP_MEM; } @@ -9303,9 +9307,9 @@ static int _sp_mul(const sp_int* a, const sp_int* b, sp_int* r) #else /* Multiply a by b into r. r = a * b * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply by. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply by. + * @param [out] r SP integer to hold result. * * @return MP_OKAY otherwise. * @return MP_MEM when dynamic memory allocation fails. @@ -9326,8 +9330,7 @@ static int _sp_mul(const sp_int* a, const sp_int* b, sp_int* r) #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t = (sp_int_digit*)XMALLOC(sizeof(sp_int_digit) * - (size_t)(a->used + b->used), NULL, - DYNAMIC_TYPE_BIGINT); + (size_t)(a->used + b->used), NULL, DYNAMIC_TYPE_BIGINT); if (t == NULL) { err = MP_MEM; } @@ -9392,9 +9395,9 @@ static int _sp_mul(const sp_int* a, const sp_int* b, sp_int* r) * * Long-hand implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -9511,9 +9514,9 @@ static int _sp_mul_4(const sp_int* a, const sp_int* b, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -9578,9 +9581,9 @@ static int _sp_mul_4(const sp_int* a, const sp_int* b, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -9681,9 +9684,9 @@ static int _sp_mul_6(const sp_int* a, const sp_int* b, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -9828,9 +9831,9 @@ static int _sp_mul_8(const sp_int* a, const sp_int* b, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -10091,9 +10094,9 @@ static int _sp_mul_12(const sp_int* a, const sp_int* b, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -10513,9 +10516,9 @@ static int _sp_mul_16(const sp_int* a, const sp_int* b, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -11316,9 +11319,9 @@ static int _sp_mul_24(const sp_int* a, const sp_int* b, sp_int* r) * * Karatsuba implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -11423,7 +11426,7 @@ static int _sp_mul_32(const sp_int* a, const sp_int* b, sp_int* r) } } z1->dp[32] += l; - /* z1 = z1 - z0 - z1 */ + /* z1 = z1 - z0 - z2 */ l = 0; h = 0; for (i = 0; i < 32; i++) { @@ -11482,9 +11485,9 @@ static int _sp_mul_32(const sp_int* a, const sp_int* b, sp_int* r) * * Karatsuba implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -11589,7 +11592,7 @@ static int _sp_mul_48(const sp_int* a, const sp_int* b, sp_int* r) } } z1->dp[48] += l; - /* z1 = z1 - z0 - z1 */ + /* z1 = z1 - z0 - z2 */ l = 0; h = 0; for (i = 0; i < 48; i++) { @@ -11601,7 +11604,7 @@ static int _sp_mul_48(const sp_int* a, const sp_int* b, sp_int* r) h = 0; } z1->dp[i] += l; - /* r += z1 << 16 */ + /* r += z1 << 24 */ l = 0; h = 0; for (i = 0; i < 24; i++) { @@ -11648,9 +11651,9 @@ static int _sp_mul_48(const sp_int* a, const sp_int* b, sp_int* r) * * Karatsuba implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -11755,7 +11758,7 @@ static int _sp_mul_64(const sp_int* a, const sp_int* b, sp_int* r) } } z1->dp[64] += l; - /* z1 = z1 - z0 - z1 */ + /* z1 = z1 - z0 - z2 */ l = 0; h = 0; for (i = 0; i < 64; i++) { @@ -11767,7 +11770,7 @@ static int _sp_mul_64(const sp_int* a, const sp_int* b, sp_int* r) h = 0; } z1->dp[i] += l; - /* r += z1 << 16 */ + /* r += z1 << 32 */ l = 0; h = 0; for (i = 0; i < 32; i++) { @@ -11814,9 +11817,9 @@ static int _sp_mul_64(const sp_int* a, const sp_int* b, sp_int* r) * * Karatsuba implementation. * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -11921,7 +11924,7 @@ static int _sp_mul_96(const sp_int* a, const sp_int* b, sp_int* r) } } z1->dp[96] += l; - /* z1 = z1 - z0 - z1 */ + /* z1 = z1 - z0 - z2 */ l = 0; h = 0; for (i = 0; i < 96; i++) { @@ -11933,7 +11936,7 @@ static int _sp_mul_96(const sp_int* a, const sp_int* b, sp_int* r) h = 0; } z1->dp[i] += l; - /* r += z1 << 16 */ + /* r += z1 << 48 */ l = 0; h = 0; for (i = 0; i < 48; i++) { @@ -11980,13 +11983,13 @@ static int _sp_mul_96(const sp_int* a, const sp_int* b, sp_int* r) /* Multiply a by b and store in r: r = a * b * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [out] r SP integer result. * * @return MP_OKAY on success. - * @return MP_VAL when a, b or is NULL; or the result will be too big for fixed - * data length. + * @return MP_VAL when a, b or r is NULL; or the result will be too big for + * fixed data length. * @return MP_MEM when dynamic memory allocation fails. */ int sp_mul(const sp_int* a, const sp_int* b, sp_int* r) @@ -12138,10 +12141,10 @@ int sp_mul(const sp_int* a, const sp_int* b, sp_int* r) (!defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)) || defined(OPENSSL_ALL) /* Multiply a by b mod m and store in r: r = (a * b) mod m * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -12174,10 +12177,10 @@ static int _sp_mulmod_tmp(const sp_int* a, const sp_int* b, const sp_int* m, /* Multiply a by b mod m and store in r: r = (a * b) mod m * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -12207,10 +12210,10 @@ static int _sp_mulmod(const sp_int* a, const sp_int* b, const sp_int* m, /* Multiply a by b mod m and store in r: r = (a * b) mod m * - * @param [in] a SP integer to multiply. - * @param [in] b SP integer to multiply. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer result. + * @param [in] a SP integer to multiply. + * @param [in] b SP integer to multiply. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_VAL when a, b, m or r is NULL; m is 0; or a * b is too big for @@ -12273,12 +12276,12 @@ int sp_mulmod(const sp_int* a, const sp_int* b, const sp_int* m, sp_int* r) * 2.4.2. c = (b - c) mod m * 3. NO_INVERSE if u == 0 * - * @param [in] a SP integer to find inverse of. - * @param [in] m SP integer this is the modulus. - * @param [in] u SP integer to use in calculation. - * @param [in] v SP integer to use in calculation. - * @param [in] b SP integer to use in calculation - * @param [out] c SP integer that is the inverse. + * @param [in] a SP integer to find inverse of. + * @param [in] m SP integer that is the modulus. + * @param [in] u SP integer to use in calculation. + * @param [in] v SP integer to use in calculation. + * @param [in] b SP integer to use in calculation. + * @param [out] c SP integer that is the inverse. * * @return MP_OKAY on success. * @return MP_VAL when no inverse. @@ -12312,7 +12315,7 @@ static int _sp_invmod_bin(const sp_int* a, const sp_int* m, sp_int* u, else if ((v->dp[0] & 1) == 0) { /* 2.2.1. v /= 2 */ _sp_div_2(v, v); - /* 2.1.2. c = (c / 2) mod m */ + /* 2.2.2. c = (c / 2) mod m */ if (sp_isodd(c)) { _sp_add_off(c, m, c, 0); } @@ -12365,13 +12368,13 @@ static int _sp_invmod_bin(const sp_int* a, const sp_int* m, sp_int* u, * 4. If c < 0 then c += m * 5. inv = c * - * @param [in] a SP integer to find inverse of. - * @param [in] m SP integer this is the modulus. - * @param [in] u SP integer to use in calculation. - * @param [in] v SP integer to use in calculation. - * @param [in] b SP integer to use in calculation - * @param [in] c SP integer to use in calculation - * @param [out] inv SP integer that is the inverse. + * @param [in] a SP integer to find inverse of. + * @param [in] m SP integer that is the modulus. + * @param [in] u SP integer to use in calculation. + * @param [in] v SP integer to use in calculation. + * @param [in] b SP integer to use in calculation. + * @param [in] c SP integer to use in calculation. + * @param [out] inv SP integer that is the inverse. * * @return MP_OKAY on success. * @return MP_VAL when no inverse. @@ -12514,9 +12517,9 @@ static int _sp_invmod_div(const sp_int* a, const sp_int* m, sp_int* x, * * r*a = x*m + 1 * - * @param [in] a SP integer to find inverse of. - * @param [in] m SP integer this is the modulus. - * @param [out] r SP integer to hold result. r cannot be m. + * @param [in] a SP integer to find inverse of. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. r cannot be m. * * @return MP_OKAY on success. * @return MP_VAL when m is even and a divides m evenly. @@ -12620,9 +12623,9 @@ static int _sp_invmod(const sp_int* a, const sp_int* m, sp_int* r) * * r*a = x*m + 1 * - * @param [in] a SP integer to find inverse of. - * @param [in] m SP integer this is the modulus. - * @param [out] r SP integer to hold result. r cannot be m. + * @param [in] a SP integer to find inverse of. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. r cannot be m. * * @return MP_OKAY on success. * @return MP_VAL when a, m or r is NULL; a or m is zero; a and m are even or @@ -12726,10 +12729,10 @@ int sp_invmod(const sp_int* a, const sp_int* m, sp_int* r) * 8. If j > 0 then r = (t * pre[j-1]) mod m * 9. Else r = t * - * @param [in] a SP integer, Montgomery form, to find inverse of. - * @param [in] m SP integer this is the modulus. - * @param [out] r SP integer to hold result. - * @param [in] mp SP integer digit that is the bottom digit of inv(-m). + * @param [in] a SP integer, Montgomery form, to find inverse of. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. + * @param [in] mp SP integer digit that is the bottom digit of inv(-m). * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -12753,7 +12756,8 @@ static int _sp_invmod_mont_ct(const sp_int* a, const sp_int* m, sp_int* r, ALLOC_DYN_SP_INT_ARRAY(pre, m->used * 2U + 1U, CT_INV_MOD_PRE_CNT + 2, err, NULL); #else - ALLOC_SP_INT_ARRAY(pre, m->used * 2U + 1U, CT_INV_MOD_PRE_CNT + 2, err, NULL); + ALLOC_SP_INT_ARRAY(pre, m->used * 2U + 1U, CT_INV_MOD_PRE_CNT + 2, err, + NULL); #endif if (err == MP_OKAY) { t = pre[CT_INV_MOD_PRE_CNT + 0]; @@ -12897,10 +12901,10 @@ static int _sp_invmod_mont_ct(const sp_int* a, const sp_int* m, sp_int* r, * Modulus (m) must be a prime and greater than 2. * For prime m, inv = a ^ (m-2) mod m as 1 = a ^ (m-1) mod m. * - * @param [in] a SP integer, Montgomery form, to find inverse of. - * @param [in] m SP integer this is the modulus. - * @param [out] r SP integer to hold result. - * @param [in] mp SP integer digit that is the bottom digit of inv(-m). + * @param [in] a SP integer, Montgomery form, to find inverse of. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. + * @param [in] mp SP integer digit that is the bottom digit of inv(-m). * * @return MP_OKAY on success. * @return MP_VAL when a, m or r is NULL; a is 0 or m is less than 3. @@ -12952,8 +12956,9 @@ int sp_invmod_mont_ct(const sp_int* a, const sp_int* m, sp_int* r, #ifndef WC_PROTECT_ENCRYPTED_MEM /* Internal. Exponentiates b to the power of e modulo m into r: r = b ^ e mod m - * Process the exponent one bit at a time. - * Is constant time and can be cache attack resistant. + * + * Processes the exponent one bit at a time. + * Implementation is constant time and can be cache attack resistant. * * Algorithm: * b: base, e: exponent, m: modulus, r: result, bits: #bits to use @@ -12968,12 +12973,12 @@ int sp_invmod_mont_ct(const sp_int* a, const sp_int* m, sp_int* r, * 4.5. t[j] = t[j] * b * 5. r = t[1] * - * @param [in] b SP integer that is the base. - * @param [in] e SP integer that is the exponent. - * @param [in] bits Number of bits in exponent to use. May be greater than - * count of bits in e. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] b SP integer that is the base. + * @param [in] e SP integer that is the exponent. + * @param [in] bits Number of bits in exponent to use. May be greater than + * count of bits in e. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -13057,7 +13062,8 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits, if (err == MP_OKAY) { /* 4.2. y = e[i] */ - int y = (int)((e->dp[i >> SP_WORD_SHIFT] >> (i & (int)SP_WORD_MASK)) & 1); + int y = (int)((e->dp[i >> SP_WORD_SHIFT] >> + (i & (int)SP_WORD_MASK)) & 1); /* 4.3. j = y & s */ int j = y & s; /* 4.4 s = s | y */ @@ -13103,12 +13109,12 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits, * 3.4. t[y] = t[3], t[y^1] = t[2] * 4. r = t[0] * - * @param [in] b SP integer that is the base. - * @param [in] e SP integer that is the exponent. - * @param [in] bits Number of bits in exponent to use. May be greater than - * count of bits in e. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] b SP integer that is the base. + * @param [in] e SP integer that is the exponent. + * @param [in] bits Number of bits in exponent to use. May be greater than + * count of bits in e. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -13211,12 +13217,12 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits, * 7. t[1] = FromMont(t[1]) * 8. r = t[1] * - * @param [in] b SP integer that is the base. - * @param [in] e SP integer that is the exponent. - * @param [in] bits Number of bits in exponent to use. May be greater than - * count of bits in e. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] b SP integer that is the base. + * @param [in] e SP integer that is the exponent. + * @param [in] bits Number of bits in exponent to use. May be greater than + * count of bits in e. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -13296,7 +13302,8 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits, if (err == MP_OKAY) { /* 6.2. y = e[i] */ - int y = (int)((e->dp[i >> SP_WORD_SHIFT] >> (i & (int)SP_WORD_MASK)) & 1); + int y = (int)((e->dp[i >> SP_WORD_SHIFT] >> + (i & (int)SP_WORD_MASK)) & 1); /* 6.3 j = y & s */ int j = y & s; /* 6.4 s = s | y */ @@ -13352,12 +13359,12 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits, * 5. t[0] = FromMont(t[0]) * 6. r = t[0] * - * @param [in] b SP integer that is the base. - * @param [in] e SP integer that is the exponent. - * @param [in] bits Number of bits in exponent to use. May be greater than - * count of bits in e. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] b SP integer that is the base. + * @param [in] e SP integer that is the exponent. + * @param [in] bits Number of bits in exponent to use. May be greater than + * count of bits in e. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -13483,12 +13490,12 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits, * 7. tr = FromMont(tr) * 8. r = tr * - * @param [in] b SP integer that is the base. - * @param [in] e SP integer that is the exponent. - * @param [in] bits Number of bits in exponent to use. May be greater than - * count of bits in e. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] b SP integer that is the base. + * @param [in] e SP integer that is the exponent. + * @param [in] bits Number of bits in exponent to use. May be greater than + * count of bits in e. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -13706,8 +13713,8 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits, /* Internal. Exponentiates 2 to the power of e modulo m into r: r = 2 ^ e mod m * Is constant time and cache attack resistant. * - * Calculates value to make mod operations constant time expect when - * WC_NO_HARDERN defined or modulus fits in one word. + * Calculates value to make mod operations constant time except when + * WC_NO_HARDEN defined or modulus fits in one word. * * Algorithm: * b: base, e: exponent, m: modulus, r: result, bits: #bits to use @@ -13726,11 +13733,11 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits, * 7. if Words(m) > 1 then tr = FromMont(tr) * 8. r = tr * - * @param [in] e SP integer that is the exponent. - * @param [in] digits Number of digits in base to use. May be greater than - * count of bits in b. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] e SP integer that is the exponent. + * @param [in] digits Number of digits in exponent to use. May be greater than + * count of digits in e. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -13930,12 +13937,12 @@ static int _sp_exptmod_base_2(const sp_int* e, int digits, const sp_int* m, * * Error returned when parameters r == e or r == m and base >= modulus. * - * @param [in] b SP integer that is the base. - * @param [in] e SP integer that is the exponent. - * @param [in] digits Number of digits in exponent to use. May be greater - * than count of digits in e. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] b SP integer that is the base. + * @param [in] e SP integer that is the exponent. + * @param [in] digits Number of digits in exponent to use. May be greater + * than count of digits in e. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when b, e, m or r is NULL, digits is negative, or m <= 0 or @@ -14113,10 +14120,10 @@ int sp_exptmod_ex(const sp_int* b, const sp_int* e, int digits, const sp_int* m, defined(OPENSSL_ALL) /* Exponentiates b to the power of e modulo m into r: r = b ^ e mod m * - * @param [in] b SP integer that is the base. - * @param [in] e SP integer that is the exponent. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] b SP integer that is the base. + * @param [in] e SP integer that is the exponent. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when b, e, m or r is NULL; or m <= 0 or e is negative. @@ -14152,27 +14159,27 @@ int sp_exptmod(const sp_int* b, const sp_int* e, const sp_int* m, sp_int* r) * 1. Ensure base is less than modulus. * 2. Convert base to Montgomery form * 3. Set result to table entry for top window bits, or - * if less than windows bits in exponent, 1 in Montgomery form. + * if less than window bits in exponent, 1 in Montgomery form. * 4. While at least window bits left: - * 4.1. Count number of and skip leading 0 bits unless less then window bits - * left. + * 4.1. Count number of bits and skip leading 0 bits unless less than window + * bits left. * 4.2. Montgomery square result for each leading 0 and window bits if bits * left. * 4.3. Break if less than window bits left. - * 4.4. Get top window bits from expononent and drop. + * 4.4. Get top window bits from exponent and drop. * 4.5. Montgomery multiply result by table entry. * 5. While bits left: - * 5.1. Montogmery square result + * 5.1. Montgomery square result * 5.2. If exponent bit set * 5.2.1. Montgomery multiply result by Montgomery form of base. * 6. Convert result back from Montgomery form. * - * @param [in] b SP integer that is the base. - * @param [in] e SP integer that is the exponent. - * @param [in] bits Number of bits in exponent to use. May be greater than - * count of bits in e. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] b SP integer that is the base. + * @param [in] e SP integer that is the exponent. + * @param [in] bits Number of bits in exponent to use. May be greater than + * count of bits in e. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -14252,7 +14259,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m, } } else { - /* Copy base into Montogmery base variable. */ + /* Copy base into Montgomery base variable. */ _sp_copy(b, bm); } } @@ -14295,7 +14302,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m, } /* 3. Set result to table entry for top window bits, or - * if less than windows bits in exponent, 1 in Montgomery form. + * if less than window bits in exponent, 1 in Montgomery form. */ if (err == MP_OKAY) { sp_int_digit n; @@ -14341,7 +14348,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m, /* Number of squares to before due to top bits being 0. */ int sqrs = 0; - /* 4.1. Count number of and skip leading 0 bits unless less + /* 4.1. Count number of bits and skip leading 0 bits unless less * than window bits. */ do { @@ -14426,7 +14433,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m, n = e->dp[0]; /* 5. While bits left: */ for (--c; (err == MP_OKAY) && (c >= 0); c--) { - /* 5.1. Montogmery square result */ + /* 5.1. Montgomery square result */ err = sp_sqr(tr, tr); if (err == MP_OKAY) { err = _sp_mont_red(tr, m, mp, 0); @@ -14472,15 +14479,15 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m, * 1. Convert base to Montgomery form * 2. Set result to base (assumes exponent is not zero) * 3. For each bit in exponent starting at second highest - * 3.1. Montogmery square result + * 3.1. Montgomery square result * 3.2. If exponent bit set * 3.2.1. Montgomery multiply result by Montgomery form of base. * 4. Convert result back from Montgomery form. * - * @param [in] b SP integer that is the base. - * @param [in] e SP integer that is the exponent. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] b SP integer that is the base. + * @param [in] e SP integer that is the exponent. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when b, e, m or r is NULL; or m <= 0 or e is negative. @@ -14577,10 +14584,10 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m, /* Exponentiates b to the power of e modulo m into r: r = b ^ e mod m * Non-constant time implementation. * - * @param [in] b SP integer that is the base. - * @param [in] e SP integer that is the exponent. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. + * @param [in] b SP integer that is the base. + * @param [in] e SP integer that is the exponent. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when b, e, m or r is NULL; or m <= 0 or e is negative. @@ -14656,11 +14663,11 @@ int sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m, sp_int* r) #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY) /* Divide by 2^e: r = a >> e and rem = bits shifted out * - * @param [in] a SP integer to divide. - * @param [in] e Exponent bits (dividing by 2^e). - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer to hold result. - * @param [out] rem SP integer to hold remainder. + * @param [in] a SP integer to divide. + * @param [in] e Exponent bits (dividing by 2^e). + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer to hold result. + * @param [out] rem SP integer to hold remainder. * * @return MP_OKAY on success. * @return MP_VAL when a is NULL or e is negative. @@ -14686,7 +14693,7 @@ int sp_div_2d(const sp_int* a, int e, sp_int* r, sp_int* rem) } else { if (rem != NULL) { - /* Copy a in to remainder. */ + /* Copy a into remainder. */ err = sp_copy(a, rem); } if (err == MP_OKAY) { @@ -14719,9 +14726,9 @@ int sp_div_2d(const sp_int* a, int e, sp_int* r, sp_int* rem) defined(HAVE_ECC) /* The bottom e bits: r = a & ((1 << e) - 1) * - * @param [in] a SP integer to reduce. - * @param [in] e Modulus bits (modulus equals 2^e). - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to reduce. + * @param [in] e Modulus bits (modulus equals 2^e). + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when a or r is NULL, e is negative or e is too large for @@ -14792,9 +14799,9 @@ int sp_mod_2d(const sp_int* a, int e, sp_int* r) !defined(NO_DH))) || defined(OPENSSL_ALL) /* Multiply by 2^e: r = a << e * - * @param [in] a SP integer to multiply. - * @param [in] e Multiplier bits (multiplier equals 2^e). - * @param [out] r SP integer to hold result. + * @param [in] a SP integer to multiply. + * @param [in] e Multiplier bits (multiplier equals 2^e). + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when a or r is NULL, e is negative, or result is too big for @@ -14809,7 +14816,7 @@ int sp_mul_2d(const sp_int* a, int e, sp_int* r) err = MP_VAL; } - /* Ensure result has enough allocated digits for result. */ + /* Ensure r has enough allocated digits for result. */ if ((err == MP_OKAY) && ((unsigned int)(sp_count_bits(a) + e) > (unsigned int)r->size * SP_WORD_SIZE)) { @@ -14853,8 +14860,8 @@ int sp_mul_2d(const sp_int* a, int e, sp_int* r) #ifdef SQR_MUL_ASM /* Square a and store in r. r = a * a * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -14967,8 +14974,8 @@ static int _sp_sqr(const sp_int* a, sp_int* r) #else /* !SQR_MUL_ASM */ /* Square a and store in r. r = a * a * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -14988,9 +14995,8 @@ static int _sp_sqr(const sp_int* a, sp_int* r) #endif #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - t = (sp_int_digit*)XMALLOC(sizeof(sp_int_digit) * - (size_t)(a->used * 2), NULL, - DYNAMIC_TYPE_BIGINT); + t = (sp_int_digit*)XMALLOC(sizeof(sp_int_digit) * (size_t)(a->used * 2), + NULL, DYNAMIC_TYPE_BIGINT); if (t == NULL) { err = MP_MEM; } @@ -15080,8 +15086,8 @@ static int _sp_sqr(const sp_int* a, sp_int* r) * * Long-hand implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -15186,8 +15192,8 @@ static int _sp_sqr_4(const sp_int* a, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -15246,8 +15252,8 @@ static int _sp_sqr_4(const sp_int* a, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -15341,8 +15347,8 @@ static int _sp_sqr_6(const sp_int* a, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -15471,8 +15477,8 @@ static int _sp_sqr_8(const sp_int* a, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -15687,8 +15693,8 @@ static int _sp_sqr_12(const sp_int* a, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -16016,8 +16022,8 @@ static int _sp_sqr_16(const sp_int* a, sp_int* r) * * Comba implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -16586,8 +16592,8 @@ static int _sp_sqr_24(const sp_int* a, sp_int* r) * * Karatsuba implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -16661,7 +16667,7 @@ static int _sp_sqr_32(const sp_int* a, sp_int* r) } } z1->dp[32] += l; - /* z1 = z1 - z0 - z1 */ + /* z1 = z1 - z0 - z2 */ l = z1->dp[0]; h = 0; SP_ASM_SUBB(l, h, z0->dp[0]); @@ -16725,8 +16731,8 @@ static int _sp_sqr_32(const sp_int* a, sp_int* r) * * Karatsuba implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -16800,7 +16806,7 @@ static int _sp_sqr_48(const sp_int* a, sp_int* r) } } z1->dp[48] += l; - /* z1 = z1 - z0 - z1 */ + /* z1 = z1 - z0 - z2 */ l = z1->dp[0]; h = 0; SP_ASM_SUBB(l, h, z0->dp[0]); @@ -16817,7 +16823,7 @@ static int _sp_sqr_48(const sp_int* a, sp_int* r) h = 0; } z1->dp[i] += l; - /* r += z1 << 16 */ + /* r += z1 << 24 */ l = 0; h = 0; for (i = 0; i < 24; i++) { @@ -16864,8 +16870,8 @@ static int _sp_sqr_48(const sp_int* a, sp_int* r) * * Karatsuba implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -16939,7 +16945,7 @@ static int _sp_sqr_64(const sp_int* a, sp_int* r) } } z1->dp[64] += l; - /* z1 = z1 - z0 - z1 */ + /* z1 = z1 - z0 - z2 */ l = z1->dp[0]; h = 0; SP_ASM_SUBB(l, h, z0->dp[0]); @@ -16956,7 +16962,7 @@ static int _sp_sqr_64(const sp_int* a, sp_int* r) h = 0; } z1->dp[i] += l; - /* r += z1 << 16 */ + /* r += z1 << 32 */ l = 0; h = 0; for (i = 0; i < 32; i++) { @@ -17003,8 +17009,8 @@ static int _sp_sqr_64(const sp_int* a, sp_int* r) * * Karatsuba implementation. * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -17078,7 +17084,7 @@ static int _sp_sqr_96(const sp_int* a, sp_int* r) } } z1->dp[96] += l; - /* z1 = z1 - z0 - z1 */ + /* z1 = z1 - z0 - z2 */ l = z1->dp[0]; h = 0; SP_ASM_SUBB(l, h, z0->dp[0]); @@ -17095,7 +17101,7 @@ static int _sp_sqr_96(const sp_int* a, sp_int* r) h = 0; } z1->dp[i] += l; - /* r += z1 << 16 */ + /* r += z1 << 48 */ l = 0; h = 0; for (i = 0; i < 48; i++) { @@ -17142,8 +17148,8 @@ static int _sp_sqr_96(const sp_int* a, sp_int* r) /* Square a and store in r. r = a * a * - * @param [in] a SP integer to square. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_VAL when a or r is NULL, or the result will be too big for fixed @@ -17283,9 +17289,9 @@ int sp_sqr(const sp_int* a, sp_int* r) !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || !defined(NO_DH) || defined(HAVE_ECC) /* Square a mod m and store in r: r = (a * a) mod m * - * @param [in] a SP integer to square. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -17316,9 +17322,9 @@ static int _sp_sqrmod(const sp_int* a, const sp_int* m, sp_int* r) /* Square a mod m and store in r: r = (a * a) mod m * - * @param [in] a SP integer to square. - * @param [in] m SP integer that is the modulus. - * @param [out] r SP integer result. + * @param [in] a SP integer to square. + * @param [in] m SP integer that is the modulus. + * @param [out] r SP integer result. * * @return MP_OKAY on success. * @return MP_VAL when a, m or r is NULL; or m is 0; or a squared is too big @@ -17386,10 +17392,10 @@ int sp_sqrmod(const sp_int* a, const sp_int* m, sp_int* r) * 3. a >>= NumBits(m) * 4. a = a % m * - * @param [in,out] a SP integer to Montgomery reduce. - * @param [in] m SP integer that is the modulus. - * @param [in] mp SP integer digit that is the bottom digit of inv(-m). - * @param [in] ct Indicates operation must be constant time. + * @param [in, out] a SP integer to Montgomery reduce. + * @param [in] m SP integer that is the modulus. + * @param [in] mp SP integer digit that is the bottom digit of inv(-m). + * @param [in] ct Indicates operation must be constant time. * * @return MP_OKAY on success. */ @@ -17853,10 +17859,10 @@ static int _sp_mont_red(sp_int* a, const sp_int* m, sp_int_digit mp, int ct) (defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)) /* Reduce a number in Montgomery form. * - * @param [in,out] a SP integer to Montgomery reduce. - * @param [in] m SP integer that is the modulus. - * @param [in] mp SP integer digit that is the bottom digit of inv(-m). - * @param [in] ct Indicates operation must be constant time. + * @param [in, out] a SP integer to Montgomery reduce. + * @param [in] m SP integer that is the modulus. + * @param [in] mp SP integer digit that is the bottom digit of inv(-m). + * @param [in] ct Indicates operation must be constant time. * * @return MP_OKAY on success. * @return MP_VAL when a or m is NULL or m is zero. @@ -17879,7 +17885,7 @@ int sp_mont_red_ex(sp_int* a, const sp_int* m, sp_int_digit mp, int ct) err = MP_VAL; } else { - /* Perform Montogomery Reduction. */ + /* Perform Montgomery Reduction. */ err = _sp_mont_red(a, m, mp, ct); } @@ -17895,8 +17901,8 @@ int sp_mont_red_ex(sp_int* a, const sp_int* m, sp_int_digit mp, int ct) * Jeffrey Hurchalla's method. * https://arxiv.org/pdf/2204.04342.pdf * - * @param [in] m SP integer that is the modulus. - * @param [out] mp SP integer digit that is the bottom digit of inv(-m). + * @param [in] m SP integer that is the modulus. + * @param [out] mp SP integer digit that is the bottom digit of inv(-m). */ static void _sp_mont_setup(const sp_int* m, sp_int_digit* rho) { @@ -17924,8 +17930,8 @@ static void _sp_mont_setup(const sp_int* m, sp_int_digit* rho) * * Used when performing Montgomery Reduction. * - * @param [in] m SP integer that is the modulus. - * @param [out] mp SP integer digit that is the bottom digit of inv(-m). + * @param [in] m SP integer that is the modulus. + * @param [out] mp SP integer digit that is the bottom digit of inv(-m). * * @return MP_OKAY on success. * @return MP_VAL when m or rho is NULL. @@ -17954,12 +17960,11 @@ int sp_mont_setup(const sp_int* m, sp_int_digit* rho) /* Calculate the normalization value of m. * norm = 2^k - m, where k is the number of bits in m * - * @param [out] norm SP integer that normalises numbers into Montgomery - * form. - * @param [in] m SP integer that is the modulus. + * @param [out] norm SP integer that normalizes numbers into Montgomery form. + * @param [in] m SP integer that is the modulus. * * @return MP_OKAY on success. - * @return MP_VAL when norm or m is NULL, or number of bits in m is maximual. + * @return MP_VAL when norm or m is NULL, or number of bits in m is maximal. */ int sp_mont_norm(sp_int* norm, const sp_int* m) { @@ -18020,7 +18025,7 @@ int sp_mont_norm(sp_int* norm, const sp_int* m) * * When a is NULL, return s 0. * - * @param [in] a SP integer. + * @param [in] a SP integer. * * @return The count of 8-bit values. * @return 0 when a is NULL. @@ -18039,12 +18044,12 @@ int sp_unsigned_bin_size(const sp_int* a) /* Convert a number as an array of bytes in big-endian format to a * multi-precision number. * - * @param [out] a SP integer. - * @param [in] in Array of bytes. - * @param [in] inSz Number of data bytes in array. + * @param [out] a SP integer. + * @param [in] in Array of bytes. + * @param [in] inSz Number of data bytes in array. * * @return MP_OKAY on success. - * @return MP_VAL when the number is too big to fit in an SP. + * @return MP_VAL when the number is too big to fit in an SP integer. */ int sp_read_unsigned_bin(sp_int* a, const byte* in, word32 inSz) { @@ -18141,8 +18146,8 @@ int sp_read_unsigned_bin(sp_int* a, const byte* in, word32 inSz) * The array must be large enough for encoded number - use mp_unsigned_bin_size * to calculate the number of bytes required. * - * @param [in] a SP integer. - * @param [out] out Array to put encoding into. + * @param [in] a SP integer. + * @param [out] out Array to put encoding into. * * @return MP_OKAY on success. * @return MP_VAL when a or out is NULL. @@ -18159,9 +18164,9 @@ int sp_to_unsigned_bin(const sp_int* a, byte* out) * to calculate the number of bytes required. * Front-pads the output array with zeros to make number the size of the array. * - * @param [in] a SP integer. - * @param [out] out Array to put encoding into. - * @param [in] outSz Size of the array in bytes. + * @param [in] a SP integer. + * @param [out] out Array to put encoding into. + * @param [in] outSz Size of the array in bytes. * * @return MP_OKAY on success. * @return MP_VAL when a or out is NULL. @@ -18233,9 +18238,9 @@ int sp_to_unsigned_bin_len(const sp_int* a, byte* out, int outSz) * to calculate the number of bytes required. * Front-pads the output array with zeros to make number the size of the array. * - * @param [in] a SP integer. - * @param [out] out Array to put encoding into. - * @param [in] outSz Size of the array in bytes. + * @param [in] a SP integer. + * @param [out] out Array to put encoding into. + * @param [in] outSz Size of the array in bytes. * * @return MP_OKAY on success. * @return MP_VAL when a or out is NULL. @@ -18300,9 +18305,9 @@ int sp_to_unsigned_bin_len_ct(const sp_int* a, byte* out, int outSz) * The array must be large enough for encoded number - use mp_unsigned_bin_size * to calculate the number of bytes required. * - * @param [in] o Offset into array o start encoding. - * @param [in] a SP integer. - * @param [out] out Array to put encoding into. + * @param [in] o Offset into array to start encoding. + * @param [in] a SP integer. + * @param [out] out Array to put encoding into. * * @return Index of next byte after data. * @return MP_VAL when a or out is NULL. @@ -18329,12 +18334,12 @@ int sp_to_unsigned_bin_at_pos(int o, const sp_int* a, unsigned char* out) * * Assumes negative sign and leading zeros have been stripped. * - * @param [out] a SP integer. - * @param [in] in NUL terminated string. + * @param [out] a SP integer. + * @param [in] in NUL terminated string. * * @return MP_OKAY on success. - * @return MP_VAL when radix not supported, value is negative, or a character - * is not valid. + * @return MP_VAL when radix is not supported, value is negative, or a + * character is not valid. */ static int _sp_read_radix_16(sp_int* a, const char* in) { @@ -18405,12 +18410,12 @@ static int _sp_read_radix_16(sp_int* a, const char* in) * * Assumes negative sign and leading zeros have been stripped. * - * @param [out] a SP integer. - * @param [in] in NUL terminated string. + * @param [out] a SP integer. + * @param [in] in NUL terminated string. * * @return MP_OKAY on success. - * @return MP_VAL when radix not supported, value is negative, or a character - * is not valid. + * @return MP_VAL when radix is not supported, value is negative, or a + * character is not valid. */ static int _sp_read_radix_10(sp_int* a, const char* in) { @@ -18459,13 +18464,13 @@ static int _sp_read_radix_10(sp_int* a, const char* in) * * Negative values supported when WOLFSSL_SP_INT_NEGATIVE is defined. * - * @param [out] a SP integer. - * @param [in] in NUL terminated string. - * @param [in] radix Number of values in a digit. + * @param [out] a SP integer. + * @param [in] in NUL terminated string. + * @param [in] radix Number of values in a digit. * * @return MP_OKAY on success. - * @return MP_VAL when a or in is NULL, radix not supported, value is negative, - * or a character is not valid. + * @return MP_VAL when a or in is NULL, radix is not supported, value is + * negative, or a character is not valid. */ int sp_read_radix(sp_int* a, const char* in, int radix) { @@ -18535,8 +18540,8 @@ int sp_read_radix(sp_int* a, const char* in, int radix) * Assumes str is large enough for result. * Use sp_radix_size() to calculate required length. * - * @param [in] a SP integer to convert. - * @param [out] str String to hold hex string result. + * @param [in] a SP integer to convert. + * @param [out] str String to hold hex string result. * * @return MP_OKAY on success. * @return MP_VAL when a or str is NULL. @@ -18638,8 +18643,8 @@ int sp_tohex(const sp_int* a, char* str) * Assumes str is large enough for result. * Use sp_radix_size() to calculate required length. * - * @param [in] a SP integer to convert. - * @param [out] str String to hold hex string result. + * @param [in] a SP integer to convert. + * @param [out] str String to hold hex string result. * * @return MP_OKAY on success. * @return MP_VAL when a or str is NULL. @@ -18715,13 +18720,13 @@ int sp_todecimal(const sp_int* a, char* str) defined(WC_MP_TO_RADIX) /* Put the string version, big-endian, of a in str using the given radix. * - * @param [in] a SP integer to convert. - * @param [out] str String to hold hex string result. - * @param [in] radix Base of character. - * Valid values: MP_RADIX_HEX, MP_RADIX_DEC. + * @param [in] a SP integer to convert. + * @param [out] str String to hold hex string result. + * @param [in] radix Base of character. + * Valid values: MP_RADIX_HEX, MP_RADIX_DEC. * * @return MP_OKAY on success. - * @return MP_VAL when a or str is NULL, or radix not supported. + * @return MP_VAL when a or str is NULL, or radix is not supported. */ int sp_toradix(const sp_int* a, char* str, int radix) { @@ -18756,13 +18761,13 @@ int sp_toradix(const sp_int* a, char* str, int radix) /* Calculate the length of the string version, big-endian, of a using the given * radix. * - * @param [in] a SP integer to convert. - * @param [in] radix Base of character. - * Valid values: MP_RADIX_HEX, MP_RADIX_DEC. - * @param [out] size The number of characters in encoding. + * @param [in] a SP integer to convert. + * @param [in] radix Base of character. + * Valid values: MP_RADIX_HEX, MP_RADIX_DEC. + * @param [out] size The number of characters in encoding. * * @return MP_OKAY on success. - * @return MP_VAL when a or size is NULL, or radix not supported. + * @return MP_VAL when a or size is NULL, or radix is not supported. */ int sp_radix_size(const sp_int* a, int radix, int* size) { @@ -18867,13 +18872,13 @@ int sp_radix_size(const sp_int* a, int radix, int* size) /* Generate a random prime for RSA only. * - * @param [out] r SP integer to hold result. - * @param [in] len Number of bytes in prime. Use -ve to indicate the two - * lowest bits must be set. - * @param [in] rng Random number generator. - * @param [in] heap Heap hint. Unused. + * @param [out] r SP integer to hold result. + * @param [in] len Number of bytes in prime. Use -ve to indicate the two + * lowest bits must be set. + * @param [in] rng Random number generator. + * @param [in] heap Heap hint. Unused. * - * @return MP_OKAY on success + * @return MP_OKAY on success. * @return MP_VAL when r or rng is NULL, length is not supported or random * number generator fails. */ @@ -19015,17 +19020,16 @@ int sp_rand_prime(sp_int* r, int len, WC_RNG* rng, void* heap) * HAC pp. 139 Algorithm 4.24 * * Sets result to 0 if definitely composite or 1 if probably prime. - * Randomly the chance of error is no more than 1/4 and often - * very much lower. + * The chance of a random error is no more than 1/4 and often very much lower. * * a is assumed to be odd. * - * @param [in] a SP integer to check. - * @param [in] b SP integer that is a small prime. - * @param [out] result MP_YES when number is likely prime. - * MP_NO otherwise. - * @param [in] n1 SP integer temporary. - * @param [in] r SP integer temporary. + * @param [in] a SP integer to check. + * @param [in] b SP integer that is a small prime. + * @param [out] result MP_YES when number is likely prime. + * MP_NO otherwise. + * @param [in] n1 SP integer temporary. + * @param [in] r SP integer temporary. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -19259,10 +19263,10 @@ static WC_INLINE int sp_div_primes(const sp_int* a, int* haveRes, int* result) /* Check whether a is prime by checking t iterations of Miller-Rabin. * - * @param [in] a SP integer to check. - * @param [in] trials Number of trials of Miller-Rabin test to perform. - * @param [out] result MP_YES when number is prime. - * MP_NO otherwise. + * @param [in] a SP integer to check. + * @param [in] trials Number of trials of Miller-Rabin test to perform. + * @param [out] result MP_YES when number is prime. + * MP_NO otherwise. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -19308,13 +19312,13 @@ static int _sp_prime_trials(const sp_int* a, int trials, int* result) } /* Check whether a is prime. - * Checks against a number of small primes and does t iterations of + * Checks against a number of small primes and does trials iterations of * Miller-Rabin. * - * @param [in] a SP integer to check. - * @param [in] trials Number of trials of Miller-Rabin test to perform. - * @param [out] result MP_YES when number is prime. - * MP_NO otherwise. + * @param [in] a SP integer to check. + * @param [in] trials Number of trials of Miller-Rabin test to perform. + * @param [out] result MP_YES when number is prime. + * MP_NO otherwise. * * @return MP_OKAY on success. * @return MP_VAL when a or result is NULL, or trials is out of range. @@ -19378,11 +19382,11 @@ int sp_prime_is_prime(const sp_int* a, int trials, int* result) * * t random numbers should give a (1/4)^t chance of a false prime. * - * @param [in] a SP integer to check. - * @param [in] trials Number of iterations of Miller-Rabin test to perform. - * @param [out] result MP_YES when number is prime. - * MP_NO otherwise. - * @param [in] rng Random number generator for Miller-Rabin testing. + * @param [in] a SP integer to check. + * @param [in] trials Number of iterations of Miller-Rabin test to perform. + * @param [out] result MP_YES when number is prime. + * MP_NO otherwise. + * @param [in] rng Random number generator for Miller-Rabin testing. * * @return MP_OKAY on success. * @return MP_VAL when a, result or rng is NULL. @@ -19465,14 +19469,14 @@ static int _sp_prime_random_trials(const sp_int* a, int trials, int* result, #endif /*!WC_NO_RNG */ /* Check whether a is prime. - * Checks against a number of small primes and does t iterations of + * Checks against a number of small primes and does trials iterations of * Miller-Rabin. * - * @param [in] a SP integer to check. - * @param [in] trials Number of iterations of Miller-Rabin test to perform. - * @param [out] result MP_YES when number is prime. - * MP_NO otherwise. - * @param [in] rng Random number generator for Miller-Rabin testing. + * @param [in] a SP integer to check. + * @param [in] trials Number of iterations of Miller-Rabin test to perform. + * @param [out] result MP_YES when number is prime. + * MP_NO otherwise. + * @param [in] rng Random number generator for Miller-Rabin testing. * * @return MP_OKAY on success. * @return MP_VAL when a, result or rng is NULL. @@ -19504,6 +19508,7 @@ int sp_prime_is_prime_ex(const sp_int* a, int trials, int* result, WC_RNG* rng) err = MP_VAL; } + /* A value of 1 is not prime. */ if ((err == MP_OKAY) && sp_isone(a)) { ret = MP_NO; haveRes = 1; @@ -19543,7 +19548,7 @@ int sp_prime_is_prime_ex(const sp_int* a, int trials, int* result, WC_RNG* rng) #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) -/* Calculates the Greatest Common Denominator (GCD) of a and b into r. +/* Calculates the Greatest Common Divisor (GCD) of a and b into r. * * Find the largest number that divides both a and b without remainder. * r <= a, r <= b, a % r == 0, b % r == 0 @@ -19559,9 +19564,9 @@ int sp_prime_is_prime_ex(const sp_int* a, int trials, int* result, WC_RNG* rng) * 4.2. u <= v, v <= t, t <= u * 5. r = u * - * @param [in] a SP integer of first operand. - * @param [in] b SP integer of second operand. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer of first operand. + * @param [in] b SP integer of second operand. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -19649,16 +19654,16 @@ static WC_INLINE int _sp_gcd(const sp_int* a, const sp_int* b, sp_int* r) return err; } -/* Calculates the Greatest Common Denominator (GCD) of a and b into r. +/* Calculates the Greatest Common Divisor (GCD) of a and b into r. * * Find the largest number that divides both a and b without remainder. * r <= a, r <= b, a % r == 0, b % r == 0 * * a and b are positive integers. * - * @param [in] a SP integer of first operand. - * @param [in] b SP integer of second operand. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer of first operand. + * @param [in] b SP integer of second operand. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when a, b or r is NULL or too large. @@ -19720,7 +19725,7 @@ int sp_gcd(const sp_int* a, const sp_int* b, sp_int* r) * a and b are positive integers. * * lcm(a, b) = (a / gcd(a, b)) * b - * Divide the common divisor from a and multiply by b. + * Divide the larger value by the common divisor and multiply by the other. * * Algorithm: * 1. t0 = gcd(a, b) @@ -19731,9 +19736,9 @@ int sp_gcd(const sp_int* a, const sp_int* b, sp_int* r) * 3.1. t1 = b / t0 * 3.2. r = a * t1 * - * @param [in] a SP integer of first operand. - * @param [in] b SP integer of second operand. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer of first operand. + * @param [in] b SP integer of second operand. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_MEM when dynamic memory allocation fails. @@ -19793,9 +19798,9 @@ static int _sp_lcm(const sp_int* a, const sp_int* b, sp_int* r) * * a and b are positive integers. * - * @param [in] a SP integer of first operand. - * @param [in] b SP integer of second operand. - * @param [out] r SP integer to hold result. + * @param [in] a SP integer of first operand. + * @param [in] b SP integer of second operand. + * @param [out] r SP integer to hold result. * * @return MP_OKAY on success. * @return MP_VAL when a, b or r is NULL; or a or b is zero. @@ -19815,7 +19820,7 @@ int sp_lcm(const sp_int* a, const sp_int* b, sp_int* r) err = MP_VAL; } #endif - /* Ensure r has space for maximumal result. */ + /* Ensure r has space for maximal result. */ else if (r->size < a->used + b->used) { err = MP_VAL; } @@ -19857,7 +19862,7 @@ word32 CheckRunTimeFastMath(void) } #ifdef WOLFSSL_CHECK_MEM_ZERO -/* Add an MP to check. +/* Add an SP integer to the memory check list. * * @param [in] name Name of address to check. * @param [in] sp sp_int that needs to be checked. @@ -19869,7 +19874,7 @@ void sp_memzero_add(const char* name, sp_int* sp) /* Check the memory in the data pointer for memory that must be zero. * - * @param [in] sp sp_int that needs to be checked. + * @param [in] sp sp_int that needs to be checked. */ void sp_memzero_check(sp_int* sp) {