blob: 770734c4a0169dbd44a8db014c0a81f69d96a179 [file] [log] [blame]
Janos Follath3ca07752022-08-09 11:45:47 +01001/*
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Core bignum functions
Janos Follath3ca07752022-08-09 11:45:47 +01003 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
22#if defined(MBEDTLS_BIGNUM_C)
23
24#include <string.h>
25
26#include "mbedtls/error.h"
27#include "mbedtls/platform_util.h"
Gabor Mezeie1d31c42022-09-12 16:25:24 +020028#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010029
Janos Follath3ca07752022-08-09 11:45:47 +010030#include "mbedtls/platform.h"
Janos Follath3ca07752022-08-09 11:45:47 +010031
32#include "bignum_core.h"
Tom Cosgrove958fd3d2022-08-24 11:08:51 +010033#include "bn_mul.h"
34#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010035
Janos Follath2e328c82022-08-22 11:19:10 +010036size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010037{
38 size_t j;
39 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
40
41 for( j = 0; j < biL; j++ )
42 {
Janos Follathb7a88ec2022-08-19 12:24:40 +010043 if( a & mask ) break;
Janos Follath3ca07752022-08-09 11:45:47 +010044
45 mask >>= 1;
46 }
47
Gabor Mezei89e31462022-08-12 15:36:56 +020048 return( j );
Janos Follath3ca07752022-08-09 11:45:47 +010049}
50
Janos Follathb7a88ec2022-08-19 12:24:40 +010051size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +010052{
53 size_t i, j;
54
Janos Follathb7a88ec2022-08-19 12:24:40 +010055 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010056 return( 0 );
57
Janos Follathb7a88ec2022-08-19 12:24:40 +010058 for( i = A_limbs - 1; i > 0; i-- )
59 if( A[i] != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010060 break;
61
Janos Follathb7a88ec2022-08-19 12:24:40 +010062 j = biL - mbedtls_mpi_core_clz( A[i] );
Janos Follath3ca07752022-08-09 11:45:47 +010063
64 return( ( i * biL ) + j );
65}
66
Janos Follath3ca07752022-08-09 11:45:47 +010067/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
68 * into the storage form used by mbedtls_mpi. */
Janos Follathb7a88ec2022-08-19 12:24:40 +010069static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010070{
71 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010072 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010073 mbedtls_mpi_uint tmp = 0;
74
Janos Follathb7a88ec2022-08-19 12:24:40 +010075 for( i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++ )
Janos Follath3ca07752022-08-09 11:45:47 +010076 {
77 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010078 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010079 }
80
81 return( tmp );
82}
83
Janos Follathb7a88ec2022-08-19 12:24:40 +010084static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010085{
86#if defined(__BYTE_ORDER__)
87
88/* Nothing to do on bigendian systems. */
89#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
Janos Follathb7a88ec2022-08-19 12:24:40 +010090 return( a );
Janos Follath3ca07752022-08-09 11:45:47 +010091#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
92
93#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
94
95/* For GCC and Clang, have builtins for byte swapping. */
96#if defined(__GNUC__) && defined(__GNUC_PREREQ)
97#if __GNUC_PREREQ(4,3)
98#define have_bswap
99#endif
100#endif
101
102#if defined(__clang__) && defined(__has_builtin)
103#if __has_builtin(__builtin_bswap32) && \
104 __has_builtin(__builtin_bswap64)
105#define have_bswap
106#endif
107#endif
108
109#if defined(have_bswap)
110 /* The compiler is hopefully able to statically evaluate this! */
111 switch( sizeof(mbedtls_mpi_uint) )
112 {
113 case 4:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100114 return( __builtin_bswap32(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100115 case 8:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100116 return( __builtin_bswap64(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100117 }
118#endif
119#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
120#endif /* __BYTE_ORDER__ */
121
122 /* Fall back to C-based reordering if we don't know the byte order
123 * or we couldn't use a compiler-specific builtin. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100124 return( mpi_bigendian_to_host_c( a ) );
Janos Follath3ca07752022-08-09 11:45:47 +0100125}
126
Janos Follathb7a88ec2022-08-19 12:24:40 +0100127void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +0100128 size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +0100129{
130 mbedtls_mpi_uint *cur_limb_left;
131 mbedtls_mpi_uint *cur_limb_right;
Janos Follathc4596412022-08-22 10:01:27 +0100132 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100133 return;
134
135 /*
136 * Traverse limbs and
137 * - adapt byte-order in each limb
138 * - swap the limbs themselves.
139 * For that, simultaneously traverse the limbs from left to right
140 * and from right to left, as long as the left index is not bigger
141 * than the right index (it's not a problem if limbs is odd and the
142 * indices coincide in the last iteration).
143 */
Janos Follathc4596412022-08-22 10:01:27 +0100144 for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 );
Janos Follath3ca07752022-08-09 11:45:47 +0100145 cur_limb_left <= cur_limb_right;
146 cur_limb_left++, cur_limb_right-- )
147 {
148 mbedtls_mpi_uint tmp;
149 /* Note that if cur_limb_left == cur_limb_right,
150 * this code effectively swaps the bytes only once. */
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100151 tmp = mpi_bigendian_to_host( *cur_limb_left );
Janos Follath3ca07752022-08-09 11:45:47 +0100152 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
153 *cur_limb_right = tmp;
154 }
155}
156
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200157/* Whether min <= A, in constant time.
158 * A_limbs must be at least 1. */
159unsigned mbedtls_mpi_core_uint_le_mpi( mbedtls_mpi_uint min,
160 const mbedtls_mpi_uint *A,
161 size_t A_limbs )
162{
163 /* min <= least significant limb? */
164 unsigned min_le_lsl = 1 ^ mbedtls_ct_mpi_uint_lt( A[0], min );
165
Gilles Peskine6b7ce962022-12-15 15:04:33 +0100166 /* limbs other than the least significant one are all zero? */
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200167 mbedtls_mpi_uint msll_mask = 0;
168 for( size_t i = 1; i < A_limbs; i++ )
169 msll_mask |= A[i];
170 /* The most significant limbs of A are not all zero iff msll_mask != 0. */
171 unsigned msll_nonzero = mbedtls_ct_mpi_uint_mask( msll_mask ) & 1;
172
173 /* min <= A iff the lowest limb of A is >= min or the other limbs
174 * are not all zero. */
175 return( min_le_lsl | msll_nonzero );
176}
177
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200178void mbedtls_mpi_core_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei1c628d52022-09-27 12:13:51 +0200179 const mbedtls_mpi_uint *A,
Gabor Mezei3eff4252022-09-26 17:26:42 +0200180 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200181 unsigned char assign )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200182{
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200183 if( X == A )
184 return;
185
Gabor Mezei1c628d52022-09-27 12:13:51 +0200186 mbedtls_ct_mpi_uint_cond_assign( limbs, X, A, assign );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200187}
188
Gabor Mezeie5b85852022-09-30 13:54:02 +0200189void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X,
190 mbedtls_mpi_uint *Y,
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200191 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200192 unsigned char swap )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200193{
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200194 if( X == Y )
195 return;
196
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200197 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
198 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200199
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200200 for( size_t i = 0; i < limbs; i++ )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200201 {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200202 mbedtls_mpi_uint tmp = X[i];
203 X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask );
204 Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200205 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200206}
207
Janos Follath3ca07752022-08-09 11:45:47 +0100208int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100209 size_t X_limbs,
210 const unsigned char *input,
211 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100212{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100213 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100214
Janos Follathb7a88ec2022-08-19 12:24:40 +0100215 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100216 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
217
218 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200219 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100220 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100221
Janos Follathb7a88ec2022-08-19 12:24:40 +0100222 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100223 {
224 size_t offset = ( ( i % ciL ) << 3 );
225 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
226 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200227 }
Janos Follath3ca07752022-08-09 11:45:47 +0100228
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100229 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100230}
231
Janos Follath3ca07752022-08-09 11:45:47 +0100232int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100233 size_t X_limbs,
234 const unsigned char *input,
235 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100236{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100237 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100238
Janos Follathb7a88ec2022-08-19 12:24:40 +0100239 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100240 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
241
Janos Follathb7a88ec2022-08-19 12:24:40 +0100242 /* If X_limbs is 0, input_length must also be 0 (from previous test).
243 * Nothing to do. */
244 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200245 return( 0 );
246
Janos Follathb7a88ec2022-08-19 12:24:40 +0100247 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200248
249 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100250 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200251 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100252 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200253 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100254 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100255 }
256
Janos Follathb7a88ec2022-08-19 12:24:40 +0100257 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200258
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100259 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100260}
261
Janos Follathb7a88ec2022-08-19 12:24:40 +0100262int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
263 size_t A_limbs,
264 unsigned char *output,
265 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100266{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100267 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100268 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100269
Janos Follathb7a88ec2022-08-19 12:24:40 +0100270 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100271 {
272 bytes_to_copy = stored_bytes;
273 }
274 else
275 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100276 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100277
Janos Follathaf3f39c2022-08-22 09:06:32 +0100278 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100279 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100280 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100281 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100282 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100283 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
284 }
285 }
286
Janos Follathcc939082022-08-15 12:08:49 +0100287 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100288 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100289
Janos Follathb7a88ec2022-08-19 12:24:40 +0100290 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100291 {
292 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100293 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100294 }
295
296 return( 0 );
297}
298
Janos Follath3ca07752022-08-09 11:45:47 +0100299int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100300 size_t X_limbs,
301 unsigned char *output,
302 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100303{
304 size_t stored_bytes;
305 size_t bytes_to_copy;
306 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100307
Janos Follathb7a88ec2022-08-19 12:24:40 +0100308 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100309
Janos Follathb7a88ec2022-08-19 12:24:40 +0100310 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100311 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100312 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100313 * null bytes and record the position at which to start
314 * writing the significant bytes. In this case, the execution
315 * trace of this function does not depend on the value of the
316 * number. */
317 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100318 p = output + output_length - stored_bytes;
319 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100320 }
321 else
322 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100323 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100324 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100325 bytes_to_copy = output_length;
326 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100327 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100328 {
329 if( GET_BYTE( X, i ) != 0 )
330 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
331 }
332 }
333
Janos Follathcc939082022-08-15 12:08:49 +0100334 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100335 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
336
337 return( 0 );
338}
339
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200340void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs,
341 size_t count )
342{
343 size_t i, v0, v1;
344 mbedtls_mpi_uint r0 = 0, r1;
345
346 v0 = count / biL;
347 v1 = count & (biL - 1);
348
349 if( v0 > limbs || ( v0 == limbs && v1 > 0 ) )
350 {
351 memset( X, 0, limbs * ciL );
352 return;
353 }
354
355 /*
356 * shift by count / limb_size
357 */
358 if( v0 > 0 )
359 {
360 for( i = 0; i < limbs - v0; i++ )
361 X[i] = X[i + v0];
362
363 for( ; i < limbs; i++ )
364 X[i] = 0;
365 }
366
367 /*
368 * shift by count % limb_size
369 */
370 if( v1 > 0 )
371 {
372 for( i = limbs; i > 0; i-- )
373 {
374 r1 = X[i - 1] << (biL - v1);
375 X[i - 1] >>= v1;
376 X[i - 1] |= r0;
377 r0 = r1;
378 }
379 }
380}
381
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100382mbedtls_mpi_uint mbedtls_mpi_core_add( mbedtls_mpi_uint *X,
383 const mbedtls_mpi_uint *A,
384 const mbedtls_mpi_uint *B,
385 size_t limbs )
Hanno Beckerc9887132022-08-24 12:54:36 +0100386{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100387 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200388
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100389 for( size_t i = 0; i < limbs; i++ )
Hanno Beckerc9887132022-08-24 12:54:36 +0100390 {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100391 mbedtls_mpi_uint t = c + A[i];
392 c = ( t < A[i] );
393 t += B[i];
394 c += ( t < B[i] );
395 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100396 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100397
Hanno Beckerc9887132022-08-24 12:54:36 +0100398 return( c );
399}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200400
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100401mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
402 const mbedtls_mpi_uint *A,
Tom Cosgroveb4964862022-08-30 11:57:22 +0100403 size_t limbs,
404 unsigned cond )
405{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100406 mbedtls_mpi_uint c = 0;
407
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100408 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
409 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
Tom Cosgrove93549902022-08-30 17:41:23 +0100410
Tom Cosgroveb4964862022-08-30 11:57:22 +0100411 for( size_t i = 0; i < limbs; i++ )
412 {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100413 mbedtls_mpi_uint add = mask & A[i];
414 mbedtls_mpi_uint t = c + X[i];
415 c = ( t < X[i] );
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100416 t += add;
417 c += ( t < add );
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100418 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100419 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100420
Tom Cosgroveb4964862022-08-30 11:57:22 +0100421 return( c );
422}
423
424mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
425 const mbedtls_mpi_uint *A,
426 const mbedtls_mpi_uint *B,
427 size_t limbs )
428{
429 mbedtls_mpi_uint c = 0;
430
431 for( size_t i = 0; i < limbs; i++ )
432 {
433 mbedtls_mpi_uint z = ( A[i] < c );
434 mbedtls_mpi_uint t = A[i] - c;
435 c = ( t < B[i] ) + z;
436 X[i] = t - B[i];
437 }
438
439 return( c );
440}
441
442mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
443 const mbedtls_mpi_uint *s, size_t s_len,
444 mbedtls_mpi_uint b )
445{
446 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100447 /*
448 * It is a documented precondition of this function that d_len >= s_len.
449 * If that's not the case, we swap these round: this turns what would be
450 * a buffer overflow into an incorrect result.
451 */
Tom Cosgroveb4964862022-08-30 11:57:22 +0100452 if( d_len < s_len )
453 s_len = d_len;
454 size_t excess_len = d_len - s_len;
455 size_t steps_x8 = s_len / 8;
456 size_t steps_x1 = s_len & 7;
457
458 while( steps_x8-- )
459 {
460 MULADDC_X8_INIT
461 MULADDC_X8_CORE
462 MULADDC_X8_STOP
463 }
464
465 while( steps_x1-- )
466 {
467 MULADDC_X1_INIT
468 MULADDC_X1_CORE
469 MULADDC_X1_STOP
470 }
471
472 while( excess_len-- )
473 {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100474 *d += c;
475 c = ( *d < c );
476 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100477 }
478
479 return( c );
480}
481
482/*
483 * Fast Montgomery initialization (thanks to Tom St Denis).
484 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100485mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
Tom Cosgroveb4964862022-08-30 11:57:22 +0100486{
487 mbedtls_mpi_uint x = N[0];
488
489 x += ( ( N[0] + 2 ) & 4 ) << 1;
490
491 for( unsigned int i = biL; i >= 8; i /= 2 )
492 x *= ( 2 - ( N[0] * x ) );
493
494 return( ~x + 1 );
495}
496
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100497void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
498 const mbedtls_mpi_uint *A,
499 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100500 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100501 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100502 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100503 mbedtls_mpi_uint mm,
504 mbedtls_mpi_uint *T )
505{
Tom Cosgrove72594632022-08-24 11:51:58 +0100506 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100507
Tom Cosgrove67c92472022-09-02 13:28:59 +0100508 for( size_t i = 0; i < AN_limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100509 {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100510 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100511 mbedtls_mpi_uint u0 = A[i];
512 mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100513
Tom Cosgrove72594632022-08-24 11:51:58 +0100514 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
515 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove67c92472022-09-02 13:28:59 +0100516
517 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100518 }
519
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100520 /*
521 * The result we want is (T >= N) ? T - N : T.
522 *
523 * For better constant-time properties in this function, we always do the
524 * subtraction, with the result in X.
525 *
526 * We also look to see if there was any carry in the final additions in the
527 * loop above.
528 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100529
Tom Cosgrove72594632022-08-24 11:51:58 +0100530 mbedtls_mpi_uint carry = T[AN_limbs];
531 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100532
533 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100534 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100535 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100536 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100537 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100538 * 1) T < N : (carry, borrow) = (0, 1): we want T
539 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
540 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100541 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100542 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100543 *
544 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100545 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100546 */
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100547 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100548}
549
Minos Galanakisa081c512022-10-24 12:16:28 +0100550int mbedtls_mpi_core_get_mont_r2_unsafe( mbedtls_mpi *X,
Minos Galanakis51d638b2022-10-24 09:59:44 +0100551 const mbedtls_mpi *N )
Hanno Beckerec440f22022-08-11 17:29:32 +0100552{
553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
554
Hanno Beckerec440f22022-08-11 17:29:32 +0100555 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 1 ) );
556 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, N->n * 2 * biL ) );
557 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( X, X, N ) );
558 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( X, N->n ) );
559
560cleanup:
561 return( ret );
562}
563
Janos Follath59cbd1b2022-10-28 18:13:43 +0100564MBEDTLS_STATIC_TESTABLE
Janos Follathe50f2f12022-10-26 15:14:33 +0100565void mbedtls_mpi_core_ct_uint_table_lookup( mbedtls_mpi_uint *dest,
Janos Follath8904a2d2022-10-31 15:32:28 +0000566 const mbedtls_mpi_uint *table,
567 size_t limbs,
568 size_t count,
569 size_t index )
Janos Follathe50f2f12022-10-26 15:14:33 +0100570{
Janos Follath8904a2d2022-10-31 15:32:28 +0000571 for( size_t i = 0; i < count; i++, table += limbs )
Janos Follathe50f2f12022-10-26 15:14:33 +0100572 {
573 unsigned char assign = mbedtls_ct_size_bool_eq( i, index );
Janos Follath8904a2d2022-10-31 15:32:28 +0000574 mbedtls_mpi_core_cond_assign( dest, table, limbs, assign );
Janos Follathe50f2f12022-10-26 15:14:33 +0100575 }
576}
577
Gilles Peskine009d1952022-09-09 21:00:00 +0200578/* Fill X with n_bytes random bytes.
579 * X must already have room for those bytes.
580 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200581 * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
582 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200583 */
584int mbedtls_mpi_core_fill_random(
585 mbedtls_mpi_uint *X, size_t X_limbs,
586 size_t n_bytes,
587 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
588{
589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
590 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
591 const size_t overhead = ( limbs * ciL ) - n_bytes;
592
593 if( X_limbs < limbs )
594 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
595
596 memset( X, 0, overhead );
597 memset( (unsigned char *) X + limbs * ciL, 0, ( X_limbs - limbs ) * ciL );
598 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X + overhead, n_bytes ) );
599 mbedtls_mpi_core_bigendian_to_host( X, limbs );
600
601cleanup:
602 return( ret );
603}
604
Gilles Peskine70375b22022-09-21 15:47:23 +0200605int mbedtls_mpi_core_random( mbedtls_mpi_uint *X,
606 mbedtls_mpi_uint min,
607 const mbedtls_mpi_uint *N,
608 size_t limbs,
609 int (*f_rng)(void *, unsigned char *, size_t),
610 void *p_rng )
611{
612 unsigned ge_lower = 1, lt_upper = 0;
613 size_t n_bits = mbedtls_mpi_core_bitlen( N, limbs );
614 size_t n_bytes = ( n_bits + 7 ) / 8;
615 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
616
617 /*
618 * When min == 0, each try has at worst a probability 1/2 of failing
619 * (the msb has a probability 1/2 of being 0, and then the result will
620 * be < N), so after 30 tries failure probability is a most 2**(-30).
621 *
622 * When N is just below a power of 2, as is the case when generating
623 * a random scalar on most elliptic curves, 1 try is enough with
624 * overwhelming probability. When N is just above a power of 2,
625 * as when generating a random scalar on secp224k1, each try has
626 * a probability of failing that is almost 1/2.
627 *
628 * The probabilities are almost the same if min is nonzero but negligible
629 * compared to N. This is always the case when N is crypto-sized, but
630 * it's convenient to support small N for testing purposes. When N
631 * is small, use a higher repeat count, otherwise the probability of
632 * failure is macroscopic.
633 */
634 int count = ( n_bytes > 4 ? 30 : 250 );
635
636 /*
637 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
638 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
639 * - use the same byte ordering;
640 * - keep the leftmost n_bits bits of the generated octet string;
641 * - try until result is in the desired range.
642 * This also avoids any bias, which is especially important for ECDSA.
643 */
644 do
645 {
646 MBEDTLS_MPI_CHK( mbedtls_mpi_core_fill_random( X, limbs,
647 n_bytes,
648 f_rng, p_rng ) );
649 mbedtls_mpi_core_shift_r( X, limbs, 8 * n_bytes - n_bits );
650
651 if( --count == 0 )
652 {
653 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
654 goto cleanup;
655 }
656
657 ge_lower = mbedtls_mpi_core_uint_le_mpi( min, X, limbs );
658 lt_upper = mbedtls_mpi_core_lt_ct( X, N, limbs );
659 }
660 while( ge_lower == 0 || lt_upper == 0 );
661
662cleanup:
663 return( ret );
664}
665
Janos Follath2a8bcf82022-11-02 10:47:30 +0000666/* BEGIN MERGE SLOT 1 */
667
Janos Follatha77911e2022-10-08 09:48:20 +0100668static size_t exp_mod_get_window_size( size_t Ebits )
Janos Follathb6673f02022-09-30 14:13:14 +0100669{
670 size_t wsize = ( Ebits > 671 ) ? 6 : ( Ebits > 239 ) ? 5 :
Janos Follatha77911e2022-10-08 09:48:20 +0100671 ( Ebits > 79 ) ? 4 : 1;
Janos Follathb6673f02022-09-30 14:13:14 +0100672
673#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
674 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
675 wsize = MBEDTLS_MPI_WINDOW_SIZE;
676#endif
677
678 return( wsize );
679}
680
Gilles Peskine0de0a042022-11-16 20:12:49 +0100681static void exp_mod_precompute_window( const mbedtls_mpi_uint *A,
682 const mbedtls_mpi_uint *N,
683 size_t AN_limbs,
684 mbedtls_mpi_uint mm,
685 const mbedtls_mpi_uint *RR,
686 size_t welem,
687 mbedtls_mpi_uint *Wtable,
688 mbedtls_mpi_uint *temp )
689{
Gilles Peskine0de0a042022-11-16 20:12:49 +0100690 /* W[0] = 1 (in Montgomery presentation) */
691 memset( Wtable, 0, AN_limbs * ciL );
692 Wtable[0] = 1;
693 mbedtls_mpi_core_montmul( Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp );
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100694
Gilles Peskine0de0a042022-11-16 20:12:49 +0100695 /* W[1] = A * R^2 * R^-1 mod N = A * R mod N */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100696 mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
697 mbedtls_mpi_core_montmul( W1, A, RR, AN_limbs, N, AN_limbs, mm, temp );
698
Gilles Peskine0de0a042022-11-16 20:12:49 +0100699 /* W[i+1] = W[i] * W[1], i >= 2 */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100700 mbedtls_mpi_uint *Wprev = W1;
701 for( size_t i = 2; i < welem; i++ )
702 {
703 mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
704 mbedtls_mpi_core_montmul( Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp );
705 Wprev = Wcur;
706 }
Gilles Peskine0de0a042022-11-16 20:12:49 +0100707}
708
Gilles Peskine7d89d352022-11-16 22:54:14 +0100709/* Exponentiation: X := A^E mod N.
710 *
711 * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
712 *
713 * RR must contain 2^{2*biL} mod N.
Janos Follath3321b582022-11-22 21:08:33 +0000714 *
715 * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
716 * (The difference is that the body in our loop processes a single bit instead
717 * of a full window.)
Gilles Peskine7d89d352022-11-16 22:54:14 +0100718 */
Janos Follathb6673f02022-09-30 14:13:14 +0100719int mbedtls_mpi_core_exp_mod( mbedtls_mpi_uint *X,
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000720 const mbedtls_mpi_uint *A,
Janos Follathb6673f02022-09-30 14:13:14 +0100721 const mbedtls_mpi_uint *N,
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000722 size_t AN_limbs,
Janos Follathb6673f02022-09-30 14:13:14 +0100723 const mbedtls_mpi_uint *E,
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000724 size_t E_limbs,
Janos Follathb6673f02022-09-30 14:13:14 +0100725 const mbedtls_mpi_uint *RR )
726{
Gilles Peskinecf979b02022-11-16 20:04:00 +0100727 const size_t wsize = exp_mod_get_window_size( E_limbs * biL );
728 const size_t welem = ( (size_t) 1 ) << wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100729
730 /* Allocate memory pool and set pointers to parts of it */
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000731 const size_t table_limbs = welem * AN_limbs;
732 const size_t temp_limbs = 2 * AN_limbs + 1;
Gilles Peskine07f2c692022-11-16 19:48:23 +0100733 const size_t select_limbs = AN_limbs;
734 const size_t total_limbs = table_limbs + temp_limbs + select_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100735
Gilles Peskine4380d7b2022-11-16 22:20:59 +0100736 /* heap allocated memory pool */
737 mbedtls_mpi_uint *mempool =
738 mbedtls_calloc( total_limbs, sizeof(mbedtls_mpi_uint) );
Janos Follathb6673f02022-09-30 14:13:14 +0100739 if( mempool == NULL )
740 {
Gilles Peskine4380d7b2022-11-16 22:20:59 +0100741 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Janos Follathb6673f02022-09-30 14:13:14 +0100742 }
743
Gilles Peskinecf979b02022-11-16 20:04:00 +0100744 /* pointers to temporaries within memory pool */
745 mbedtls_mpi_uint *const Wtable = mempool;
746 mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
747 mbedtls_mpi_uint *const temp = Wselect + select_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100748
749 /*
750 * Window precomputation
751 */
752
Gilles Peskinecf979b02022-11-16 20:04:00 +0100753 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init( N );
754
Gilles Peskine0de0a042022-11-16 20:12:49 +0100755 /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
756 exp_mod_precompute_window( A, N, AN_limbs,
757 mm, RR,
758 welem, Wtable, temp );
Janos Follathb6673f02022-09-30 14:13:14 +0100759
760 /*
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000761 * Fixed window exponentiation
Janos Follathb6673f02022-09-30 14:13:14 +0100762 */
763
764 /* X = 1 (in Montgomery presentation) initially */
Gilles Peskine07f2c692022-11-16 19:48:23 +0100765 memcpy( X, Wtable, AN_limbs * ciL );
Janos Follathb6673f02022-09-30 14:13:14 +0100766
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100767 /* We'll process the bits of E from most significant
768 * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
769 * (limb_index=0, E_bit_index=0). */
770 size_t E_limb_index = E_limbs;
771 size_t E_bit_index = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100772 /* At any given time, window contains window_bits bits from E.
773 * window_bits can go up to wsize. */
Janos Follathbad42c42022-11-09 14:30:44 +0000774 size_t window_bits = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100775 mbedtls_mpi_uint window = 0;
Gilles Peskinecf979b02022-11-16 20:04:00 +0100776
Gilles Peskine3b63d092022-11-16 22:06:18 +0100777 do
Janos Follathb6673f02022-09-30 14:13:14 +0100778 {
Janos Follathb6673f02022-09-30 14:13:14 +0100779 /* Square */
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000780 mbedtls_mpi_core_montmul( X, X, X, AN_limbs, N, AN_limbs, mm, temp );
Janos Follathb6673f02022-09-30 14:13:14 +0100781
Janos Follath3321b582022-11-22 21:08:33 +0000782 /* Move to the next bit of the exponent */
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100783 if( E_bit_index == 0 )
784 {
785 --E_limb_index;
786 E_bit_index = biL - 1;
787 }
788 else
789 {
790 --E_bit_index;
791 }
Janos Follath3321b582022-11-22 21:08:33 +0000792 /* Insert next exponent bit into window */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100793 ++window_bits;
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100794 window <<= 1;
795 window |= ( E[E_limb_index] >> E_bit_index ) & 1;
Gilles Peskine3b63d092022-11-16 22:06:18 +0100796
797 /* Clear window if it's full. Also clear the window at the end,
798 * when we've finished processing the exponent. */
799 if( window_bits == wsize ||
800 ( E_bit_index == 0 && E_limb_index == 0 ) )
801 {
Gilles Peskine0b270a52022-11-16 22:54:03 +0100802 /* Select Wtable[window] without leaking window through
803 * memory access patterns. */
Gilles Peskine3b63d092022-11-16 22:06:18 +0100804 mbedtls_mpi_core_ct_uint_table_lookup( Wselect, Wtable,
805 AN_limbs, welem, window );
Gilles Peskine0b270a52022-11-16 22:54:03 +0100806 /* Multiply X by the selected element. */
Janos Follath3321b582022-11-22 21:08:33 +0000807 mbedtls_mpi_core_montmul( X, X, Wselect, AN_limbs, N, AN_limbs, mm,
808 temp );
Gilles Peskine3b63d092022-11-16 22:06:18 +0100809 window = 0;
810 window_bits = 0;
811 }
Janos Follathb6673f02022-09-30 14:13:14 +0100812 }
Gilles Peskine3b63d092022-11-16 22:06:18 +0100813 while( ! ( E_bit_index == 0 && E_limb_index == 0 ) );
Janos Follathb6673f02022-09-30 14:13:14 +0100814
815 /* Convert X back to normal presentation */
Gilles Peskinecf979b02022-11-16 20:04:00 +0100816 const mbedtls_mpi_uint one = 1;
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000817 mbedtls_mpi_core_montmul( X, X, &one, 1, N, AN_limbs, mm, temp );
Janos Follathb6673f02022-09-30 14:13:14 +0100818
Gilles Peskine7d89d352022-11-16 22:54:14 +0100819 mbedtls_platform_zeroize( mempool, total_limbs * sizeof(mbedtls_mpi_uint) );
Janos Follathb6673f02022-09-30 14:13:14 +0100820 mbedtls_free( mempool );
Gilles Peskine4380d7b2022-11-16 22:20:59 +0100821 return( 0 );
Janos Follathb6673f02022-09-30 14:13:14 +0100822}
823
Janos Follath2a8bcf82022-11-02 10:47:30 +0000824/* END MERGE SLOT 1 */
825
826/* BEGIN MERGE SLOT 2 */
827
828/* END MERGE SLOT 2 */
829
830/* BEGIN MERGE SLOT 3 */
831
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100832mbedtls_mpi_uint mbedtls_mpi_core_sub_int( mbedtls_mpi_uint *X,
833 const mbedtls_mpi_uint *A,
834 mbedtls_mpi_uint c, /* doubles as carry */
835 size_t limbs )
Hanno Beckerd9b23482022-08-25 08:25:19 +0100836{
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100837 for( size_t i = 0; i < limbs; i++ )
Hanno Beckerd9b23482022-08-25 08:25:19 +0100838 {
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100839 mbedtls_mpi_uint s = A[i];
840 mbedtls_mpi_uint t = s - c;
841 c = ( t > s );
842 X[i] = t;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100843 }
844
845 return( c );
846}
847
Janos Follath2a8bcf82022-11-02 10:47:30 +0000848/* END MERGE SLOT 3 */
849
850/* BEGIN MERGE SLOT 4 */
851
852/* END MERGE SLOT 4 */
853
854/* BEGIN MERGE SLOT 5 */
855
856/* END MERGE SLOT 5 */
857
858/* BEGIN MERGE SLOT 6 */
859
860/* END MERGE SLOT 6 */
861
862/* BEGIN MERGE SLOT 7 */
863
864/* END MERGE SLOT 7 */
865
866/* BEGIN MERGE SLOT 8 */
867
868/* END MERGE SLOT 8 */
869
870/* BEGIN MERGE SLOT 9 */
871
872/* END MERGE SLOT 9 */
873
874/* BEGIN MERGE SLOT 10 */
875
876/* END MERGE SLOT 10 */
877
Janos Follath3ca07752022-08-09 11:45:47 +0100878#endif /* MBEDTLS_BIGNUM_C */