@@ -17774,6 +17774,73 @@ int sp_to_unsigned_bin_len(const sp_int* a, byte* out, int outSz)
1777417774 return err;
1777517775}
1777617776
17777+ /* Convert the multi-precision number to an array of bytes in big-endian format.
17778+ *
17779+ * Constant-time implementation.
17780+ *
17781+ * The array must be large enough for encoded number - use mp_unsigned_bin_size
17782+ * to calculate the number of bytes required.
17783+ * Front-pads the output array with zeros to make number the size of the array.
17784+ *
17785+ * @param [in] a SP integer.
17786+ * @param [out] out Array to put encoding into.
17787+ * @param [in] outSz Size of the array in bytes.
17788+ *
17789+ * @return MP_OKAY on success.
17790+ * @return MP_VAL when a or out is NULL.
17791+ */
17792+ int sp_to_unsigned_bin_len_ct(const sp_int* a, byte* out, int outSz)
17793+ {
17794+ int err = MP_OKAY;
17795+
17796+ /* Validate parameters. */
17797+ if ((a == NULL) || (out == NULL) || (outSz < 0)) {
17798+ err = MP_VAL;
17799+ }
17800+
17801+ #if SP_WORD_SIZE > 8
17802+ if (err == MP_OKAY) {
17803+ /* Start at the end of the buffer - least significant byte. */
17804+ int j;
17805+ unsigned int i;
17806+ sp_digit mask = (sp_digit)-1;
17807+ sp_int_digit d;
17808+
17809+ /* Put each digit in. */
17810+ i = 0;
17811+ for (j = outSz - 1; j >= 0; ) {
17812+ int b;
17813+ d = a->dp[i];
17814+ /* Place each byte of a digit into the buffer. */
17815+ for (b = 0; (j >= 0) && (b < SP_WORD_SIZEOF); b++) {
17816+ out[j--] = (byte)(d & mask);
17817+ d >>= 8;
17818+ }
17819+ mask &= (sp_digit)0 - (i < a->used - 1);
17820+ i += (unsigned int)(1 & mask);
17821+ }
17822+ }
17823+ #else
17824+ if ((err == MP_OKAY) && ((unsigned int)outSz < a->used)) {
17825+ err = MP_VAL;
17826+ }
17827+ if (err == MP_OKAY) {
17828+ unsigned int i;
17829+ int j;
17830+ sp_digit mask = (sp_digit)-1;
17831+
17832+ i = 0;
17833+ for (j = outSz - 1; j >= 0; j--) {
17834+ out[j] = a->dp[i] & mask;
17835+ mask &= (sp_digit)0 - (i < a->used - 1);
17836+ i += (unsigned int)(1 & mask);
17837+ }
17838+ }
17839+ #endif
17840+
17841+ return err;
17842+ }
17843+
1777717844#if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
1777817845 !defined(WOLFSSL_RSA_VERIFY_ONLY)
1777917846/* Store the number in big-endian format in array at an offset.
0 commit comments