blob: 6aa1e0006686b256bdb4862b763fc6260cc68908 [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
30#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdio.h>
34#include <stdlib.h>
35#define mbedtls_printf printf
36#define mbedtls_calloc calloc
37#define mbedtls_free free
38#endif
39
40#include "bignum_core.h"
Tom Cosgrove958fd3d2022-08-24 11:08:51 +010041#include "bn_mul.h"
42#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010043
Janos Follath2e328c82022-08-22 11:19:10 +010044size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010045{
46 size_t j;
47 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
48
49 for( j = 0; j < biL; j++ )
50 {
Janos Follathb7a88ec2022-08-19 12:24:40 +010051 if( a & mask ) break;
Janos Follath3ca07752022-08-09 11:45:47 +010052
53 mask >>= 1;
54 }
55
Gabor Mezei89e31462022-08-12 15:36:56 +020056 return( j );
Janos Follath3ca07752022-08-09 11:45:47 +010057}
58
Janos Follathb7a88ec2022-08-19 12:24:40 +010059size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +010060{
61 size_t i, j;
62
Janos Follathb7a88ec2022-08-19 12:24:40 +010063 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010064 return( 0 );
65
Janos Follathb7a88ec2022-08-19 12:24:40 +010066 for( i = A_limbs - 1; i > 0; i-- )
67 if( A[i] != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010068 break;
69
Janos Follathb7a88ec2022-08-19 12:24:40 +010070 j = biL - mbedtls_mpi_core_clz( A[i] );
Janos Follath3ca07752022-08-09 11:45:47 +010071
72 return( ( i * biL ) + j );
73}
74
Janos Follath3ca07752022-08-09 11:45:47 +010075/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
76 * into the storage form used by mbedtls_mpi. */
Janos Follathb7a88ec2022-08-19 12:24:40 +010077static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010078{
79 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010080 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010081 mbedtls_mpi_uint tmp = 0;
82
Janos Follathb7a88ec2022-08-19 12:24:40 +010083 for( i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++ )
Janos Follath3ca07752022-08-09 11:45:47 +010084 {
85 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010086 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010087 }
88
89 return( tmp );
90}
91
Janos Follathb7a88ec2022-08-19 12:24:40 +010092static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010093{
94#if defined(__BYTE_ORDER__)
95
96/* Nothing to do on bigendian systems. */
97#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
Janos Follathb7a88ec2022-08-19 12:24:40 +010098 return( a );
Janos Follath3ca07752022-08-09 11:45:47 +010099#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
100
101#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
102
103/* For GCC and Clang, have builtins for byte swapping. */
104#if defined(__GNUC__) && defined(__GNUC_PREREQ)
105#if __GNUC_PREREQ(4,3)
106#define have_bswap
107#endif
108#endif
109
110#if defined(__clang__) && defined(__has_builtin)
111#if __has_builtin(__builtin_bswap32) && \
112 __has_builtin(__builtin_bswap64)
113#define have_bswap
114#endif
115#endif
116
117#if defined(have_bswap)
118 /* The compiler is hopefully able to statically evaluate this! */
119 switch( sizeof(mbedtls_mpi_uint) )
120 {
121 case 4:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100122 return( __builtin_bswap32(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100123 case 8:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100124 return( __builtin_bswap64(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100125 }
126#endif
127#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
128#endif /* __BYTE_ORDER__ */
129
130 /* Fall back to C-based reordering if we don't know the byte order
131 * or we couldn't use a compiler-specific builtin. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100132 return( mpi_bigendian_to_host_c( a ) );
Janos Follath3ca07752022-08-09 11:45:47 +0100133}
134
Janos Follathb7a88ec2022-08-19 12:24:40 +0100135void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +0100136 size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +0100137{
138 mbedtls_mpi_uint *cur_limb_left;
139 mbedtls_mpi_uint *cur_limb_right;
Janos Follathc4596412022-08-22 10:01:27 +0100140 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100141 return;
142
143 /*
144 * Traverse limbs and
145 * - adapt byte-order in each limb
146 * - swap the limbs themselves.
147 * For that, simultaneously traverse the limbs from left to right
148 * and from right to left, as long as the left index is not bigger
149 * than the right index (it's not a problem if limbs is odd and the
150 * indices coincide in the last iteration).
151 */
Janos Follathc4596412022-08-22 10:01:27 +0100152 for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 );
Janos Follath3ca07752022-08-09 11:45:47 +0100153 cur_limb_left <= cur_limb_right;
154 cur_limb_left++, cur_limb_right-- )
155 {
156 mbedtls_mpi_uint tmp;
157 /* Note that if cur_limb_left == cur_limb_right,
158 * this code effectively swaps the bytes only once. */
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100159 tmp = mpi_bigendian_to_host( *cur_limb_left );
Janos Follath3ca07752022-08-09 11:45:47 +0100160 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
161 *cur_limb_right = tmp;
162 }
163}
164
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200165int mbedtls_mpi_core_cond_assign( mbedtls_mpi_uint *X,
166 size_t X_limbs,
167 const mbedtls_mpi_uint *Y,
168 size_t Y_limbs,
169 unsigned char assign )
170{
171 if( X_limbs < Y_limbs )
172 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
173
174 if( X != NULL && Y != NULL )
175 {
176 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
177 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( assign );
178
179 mbedtls_ct_mpi_uint_cond_assign( X_limbs, X, Y, assign );
180
181 for( size_t i = Y_limbs; i < X_limbs; i++ )
182 X[i] &= ~limb_mask;
183
184 return( 0 );
185 }
186
187 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
188}
189
190int mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X,
191 size_t X_limbs,
192 mbedtls_mpi_uint *Y,
193 size_t Y_limbs,
194 unsigned char swap )
195{
196 if( X == Y )
197 return( 0 );
198
199 if( X_limbs != Y_limbs )
200 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
201
202 if( X != NULL && Y != NULL )
203 {
204 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
205 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap );
206
207 for( size_t i = 0; i < X_limbs; i++ )
208 {
209 mbedtls_mpi_uint tmp = X[i];
210 X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask );
211 Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask );
212 }
213
214 return( 0 );
215 }
216
217 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
218}
219
Janos Follath3ca07752022-08-09 11:45:47 +0100220int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100221 size_t X_limbs,
222 const unsigned char *input,
223 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100224{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100225 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100226
Janos Follathb7a88ec2022-08-19 12:24:40 +0100227 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100228 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
229
230 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200231 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100232 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100233
Janos Follathb7a88ec2022-08-19 12:24:40 +0100234 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100235 {
236 size_t offset = ( ( i % ciL ) << 3 );
237 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
238 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200239 }
Janos Follath3ca07752022-08-09 11:45:47 +0100240
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100241 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100242}
243
Janos Follath3ca07752022-08-09 11:45:47 +0100244int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100245 size_t X_limbs,
246 const unsigned char *input,
247 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100248{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100249 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100250
Janos Follathb7a88ec2022-08-19 12:24:40 +0100251 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100252 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
253
Janos Follathb7a88ec2022-08-19 12:24:40 +0100254 /* If X_limbs is 0, input_length must also be 0 (from previous test).
255 * Nothing to do. */
256 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200257 return( 0 );
258
Janos Follathb7a88ec2022-08-19 12:24:40 +0100259 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200260
261 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100262 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200263 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100264 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200265 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100266 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100267 }
268
Janos Follathb7a88ec2022-08-19 12:24:40 +0100269 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200270
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100271 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100272}
273
Janos Follathb7a88ec2022-08-19 12:24:40 +0100274int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
275 size_t A_limbs,
276 unsigned char *output,
277 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100278{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100279 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100280 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100281
Janos Follathb7a88ec2022-08-19 12:24:40 +0100282 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100283 {
284 bytes_to_copy = stored_bytes;
285 }
286 else
287 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100288 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100289
Janos Follathaf3f39c2022-08-22 09:06:32 +0100290 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100291 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100292 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100293 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100294 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100295 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
296 }
297 }
298
Janos Follathcc939082022-08-15 12:08:49 +0100299 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100300 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100301
Janos Follathb7a88ec2022-08-19 12:24:40 +0100302 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100303 {
304 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100305 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100306 }
307
308 return( 0 );
309}
310
Janos Follath3ca07752022-08-09 11:45:47 +0100311int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100312 size_t X_limbs,
313 unsigned char *output,
314 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100315{
316 size_t stored_bytes;
317 size_t bytes_to_copy;
318 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100319
Janos Follathb7a88ec2022-08-19 12:24:40 +0100320 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100321
Janos Follathb7a88ec2022-08-19 12:24:40 +0100322 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100323 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100324 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100325 * null bytes and record the position at which to start
326 * writing the significant bytes. In this case, the execution
327 * trace of this function does not depend on the value of the
328 * number. */
329 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100330 p = output + output_length - stored_bytes;
331 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100332 }
333 else
334 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100335 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100336 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100337 bytes_to_copy = output_length;
338 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100339 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100340 {
341 if( GET_BYTE( X, i ) != 0 )
342 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
343 }
344 }
345
Janos Follathcc939082022-08-15 12:08:49 +0100346 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100347 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
348
349 return( 0 );
350}
351
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100352mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
353 const mbedtls_mpi_uint *A,
Tom Cosgroveb4964862022-08-30 11:57:22 +0100354 size_t limbs,
355 unsigned cond )
356{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100357 mbedtls_mpi_uint c = 0;
358
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100359 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
360 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
Tom Cosgrove93549902022-08-30 17:41:23 +0100361
Tom Cosgroveb4964862022-08-30 11:57:22 +0100362 for( size_t i = 0; i < limbs; i++ )
363 {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100364 mbedtls_mpi_uint add = mask & A[i];
365 mbedtls_mpi_uint t = c + X[i];
366 c = ( t < X[i] );
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100367 t += add;
368 c += ( t < add );
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100369 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100370 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100371
Tom Cosgroveb4964862022-08-30 11:57:22 +0100372 return( c );
373}
374
375mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
376 const mbedtls_mpi_uint *A,
377 const mbedtls_mpi_uint *B,
378 size_t limbs )
379{
380 mbedtls_mpi_uint c = 0;
381
382 for( size_t i = 0; i < limbs; i++ )
383 {
384 mbedtls_mpi_uint z = ( A[i] < c );
385 mbedtls_mpi_uint t = A[i] - c;
386 c = ( t < B[i] ) + z;
387 X[i] = t - B[i];
388 }
389
390 return( c );
391}
392
393mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
394 const mbedtls_mpi_uint *s, size_t s_len,
395 mbedtls_mpi_uint b )
396{
397 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100398 /*
399 * It is a documented precondition of this function that d_len >= s_len.
400 * If that's not the case, we swap these round: this turns what would be
401 * a buffer overflow into an incorrect result.
402 */
Tom Cosgroveb4964862022-08-30 11:57:22 +0100403 if( d_len < s_len )
404 s_len = d_len;
405 size_t excess_len = d_len - s_len;
406 size_t steps_x8 = s_len / 8;
407 size_t steps_x1 = s_len & 7;
408
409 while( steps_x8-- )
410 {
411 MULADDC_X8_INIT
412 MULADDC_X8_CORE
413 MULADDC_X8_STOP
414 }
415
416 while( steps_x1-- )
417 {
418 MULADDC_X1_INIT
419 MULADDC_X1_CORE
420 MULADDC_X1_STOP
421 }
422
423 while( excess_len-- )
424 {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100425 *d += c;
426 c = ( *d < c );
427 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100428 }
429
430 return( c );
431}
432
433/*
434 * Fast Montgomery initialization (thanks to Tom St Denis).
435 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100436mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
Tom Cosgroveb4964862022-08-30 11:57:22 +0100437{
438 mbedtls_mpi_uint x = N[0];
439
440 x += ( ( N[0] + 2 ) & 4 ) << 1;
441
442 for( unsigned int i = biL; i >= 8; i /= 2 )
443 x *= ( 2 - ( N[0] * x ) );
444
445 return( ~x + 1 );
446}
447
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100448void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
449 const mbedtls_mpi_uint *A,
450 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100451 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100452 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100453 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100454 mbedtls_mpi_uint mm,
455 mbedtls_mpi_uint *T )
456{
Tom Cosgrove72594632022-08-24 11:51:58 +0100457 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100458
Tom Cosgrove67c92472022-09-02 13:28:59 +0100459 for( size_t i = 0; i < AN_limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100460 {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100461 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100462 mbedtls_mpi_uint u0 = A[i];
463 mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100464
Tom Cosgrove72594632022-08-24 11:51:58 +0100465 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
466 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove67c92472022-09-02 13:28:59 +0100467
468 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100469 }
470
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100471 /*
472 * The result we want is (T >= N) ? T - N : T.
473 *
474 * For better constant-time properties in this function, we always do the
475 * subtraction, with the result in X.
476 *
477 * We also look to see if there was any carry in the final additions in the
478 * loop above.
479 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100480
Tom Cosgrove72594632022-08-24 11:51:58 +0100481 mbedtls_mpi_uint carry = T[AN_limbs];
482 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100483
484 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100485 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100486 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100487 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100488 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100489 * 1) T < N : (carry, borrow) = (0, 1): we want T
490 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
491 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100492 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100493 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100494 *
495 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100496 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100497 */
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100498 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100499}
500
Janos Follath3ca07752022-08-09 11:45:47 +0100501#endif /* MBEDTLS_BIGNUM_C */