blob: d0728e7abb7da3f14b3fb10da5edfbdbb9acbf9a [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"
28
Janos Follath3ca07752022-08-09 11:45:47 +010029#include "mbedtls/platform.h"
Janos Follath3ca07752022-08-09 11:45:47 +010030
31#include "bignum_core.h"
Tom Cosgrove958fd3d2022-08-24 11:08:51 +010032#include "bn_mul.h"
33#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010034
Janos Follath2e328c82022-08-22 11:19:10 +010035size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010036{
37 size_t j;
38 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
39
40 for( j = 0; j < biL; j++ )
41 {
Janos Follathb7a88ec2022-08-19 12:24:40 +010042 if( a & mask ) break;
Janos Follath3ca07752022-08-09 11:45:47 +010043
44 mask >>= 1;
45 }
46
Gabor Mezei89e31462022-08-12 15:36:56 +020047 return( j );
Janos Follath3ca07752022-08-09 11:45:47 +010048}
49
Janos Follathb7a88ec2022-08-19 12:24:40 +010050size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +010051{
52 size_t i, j;
53
Janos Follathb7a88ec2022-08-19 12:24:40 +010054 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010055 return( 0 );
56
Janos Follathb7a88ec2022-08-19 12:24:40 +010057 for( i = A_limbs - 1; i > 0; i-- )
58 if( A[i] != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010059 break;
60
Janos Follathb7a88ec2022-08-19 12:24:40 +010061 j = biL - mbedtls_mpi_core_clz( A[i] );
Janos Follath3ca07752022-08-09 11:45:47 +010062
63 return( ( i * biL ) + j );
64}
65
Janos Follath3ca07752022-08-09 11:45:47 +010066/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
67 * into the storage form used by mbedtls_mpi. */
Janos Follathb7a88ec2022-08-19 12:24:40 +010068static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010069{
70 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010071 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010072 mbedtls_mpi_uint tmp = 0;
73
Janos Follathb7a88ec2022-08-19 12:24:40 +010074 for( i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++ )
Janos Follath3ca07752022-08-09 11:45:47 +010075 {
76 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010077 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010078 }
79
80 return( tmp );
81}
82
Janos Follathb7a88ec2022-08-19 12:24:40 +010083static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010084{
85#if defined(__BYTE_ORDER__)
86
87/* Nothing to do on bigendian systems. */
88#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
Janos Follathb7a88ec2022-08-19 12:24:40 +010089 return( a );
Janos Follath3ca07752022-08-09 11:45:47 +010090#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
91
92#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
93
94/* For GCC and Clang, have builtins for byte swapping. */
95#if defined(__GNUC__) && defined(__GNUC_PREREQ)
96#if __GNUC_PREREQ(4,3)
97#define have_bswap
98#endif
99#endif
100
101#if defined(__clang__) && defined(__has_builtin)
102#if __has_builtin(__builtin_bswap32) && \
103 __has_builtin(__builtin_bswap64)
104#define have_bswap
105#endif
106#endif
107
108#if defined(have_bswap)
109 /* The compiler is hopefully able to statically evaluate this! */
110 switch( sizeof(mbedtls_mpi_uint) )
111 {
112 case 4:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100113 return( __builtin_bswap32(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100114 case 8:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100115 return( __builtin_bswap64(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100116 }
117#endif
118#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
119#endif /* __BYTE_ORDER__ */
120
121 /* Fall back to C-based reordering if we don't know the byte order
122 * or we couldn't use a compiler-specific builtin. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100123 return( mpi_bigendian_to_host_c( a ) );
Janos Follath3ca07752022-08-09 11:45:47 +0100124}
125
Janos Follathb7a88ec2022-08-19 12:24:40 +0100126void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +0100127 size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +0100128{
129 mbedtls_mpi_uint *cur_limb_left;
130 mbedtls_mpi_uint *cur_limb_right;
Janos Follathc4596412022-08-22 10:01:27 +0100131 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100132 return;
133
134 /*
135 * Traverse limbs and
136 * - adapt byte-order in each limb
137 * - swap the limbs themselves.
138 * For that, simultaneously traverse the limbs from left to right
139 * and from right to left, as long as the left index is not bigger
140 * than the right index (it's not a problem if limbs is odd and the
141 * indices coincide in the last iteration).
142 */
Janos Follathc4596412022-08-22 10:01:27 +0100143 for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 );
Janos Follath3ca07752022-08-09 11:45:47 +0100144 cur_limb_left <= cur_limb_right;
145 cur_limb_left++, cur_limb_right-- )
146 {
147 mbedtls_mpi_uint tmp;
148 /* Note that if cur_limb_left == cur_limb_right,
149 * this code effectively swaps the bytes only once. */
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100150 tmp = mpi_bigendian_to_host( *cur_limb_left );
Janos Follath3ca07752022-08-09 11:45:47 +0100151 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
152 *cur_limb_right = tmp;
153 }
154}
155
Janos Follath3ca07752022-08-09 11:45:47 +0100156int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100157 size_t X_limbs,
158 const unsigned char *input,
159 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100160{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100161 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100162
Janos Follathb7a88ec2022-08-19 12:24:40 +0100163 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100164 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
165
166 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200167 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100168 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100169
Janos Follathb7a88ec2022-08-19 12:24:40 +0100170 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100171 {
172 size_t offset = ( ( i % ciL ) << 3 );
173 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
174 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200175 }
Janos Follath3ca07752022-08-09 11:45:47 +0100176
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100177 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100178}
179
Janos Follath3ca07752022-08-09 11:45:47 +0100180int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100181 size_t X_limbs,
182 const unsigned char *input,
183 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100184{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100185 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100186
Janos Follathb7a88ec2022-08-19 12:24:40 +0100187 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100188 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
189
Janos Follathb7a88ec2022-08-19 12:24:40 +0100190 /* If X_limbs is 0, input_length must also be 0 (from previous test).
191 * Nothing to do. */
192 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200193 return( 0 );
194
Janos Follathb7a88ec2022-08-19 12:24:40 +0100195 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200196
197 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100198 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200199 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100200 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200201 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100202 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100203 }
204
Janos Follathb7a88ec2022-08-19 12:24:40 +0100205 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200206
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100207 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100208}
209
Janos Follathb7a88ec2022-08-19 12:24:40 +0100210int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
211 size_t A_limbs,
212 unsigned char *output,
213 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100214{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100215 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100216 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100217
Janos Follathb7a88ec2022-08-19 12:24:40 +0100218 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100219 {
220 bytes_to_copy = stored_bytes;
221 }
222 else
223 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100224 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100225
Janos Follathaf3f39c2022-08-22 09:06:32 +0100226 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100227 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100228 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100229 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100230 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100231 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
232 }
233 }
234
Janos Follathcc939082022-08-15 12:08:49 +0100235 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100236 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100237
Janos Follathb7a88ec2022-08-19 12:24:40 +0100238 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100239 {
240 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100241 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100242 }
243
244 return( 0 );
245}
246
Janos Follath3ca07752022-08-09 11:45:47 +0100247int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100248 size_t X_limbs,
249 unsigned char *output,
250 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100251{
252 size_t stored_bytes;
253 size_t bytes_to_copy;
254 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100255
Janos Follathb7a88ec2022-08-19 12:24:40 +0100256 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100257
Janos Follathb7a88ec2022-08-19 12:24:40 +0100258 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100259 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100260 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100261 * null bytes and record the position at which to start
262 * writing the significant bytes. In this case, the execution
263 * trace of this function does not depend on the value of the
264 * number. */
265 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100266 p = output + output_length - stored_bytes;
267 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100268 }
269 else
270 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100271 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100272 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100273 bytes_to_copy = output_length;
274 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100275 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100276 {
277 if( GET_BYTE( X, i ) != 0 )
278 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
279 }
280 }
281
Janos Follathcc939082022-08-15 12:08:49 +0100282 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100283 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
284
285 return( 0 );
286}
287
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100288mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
289 const mbedtls_mpi_uint *A,
Tom Cosgroveb4964862022-08-30 11:57:22 +0100290 size_t limbs,
291 unsigned cond )
292{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100293 mbedtls_mpi_uint c = 0;
294
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100295 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
296 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
Tom Cosgrove93549902022-08-30 17:41:23 +0100297
Tom Cosgroveb4964862022-08-30 11:57:22 +0100298 for( size_t i = 0; i < limbs; i++ )
299 {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100300 mbedtls_mpi_uint add = mask & A[i];
301 mbedtls_mpi_uint t = c + X[i];
302 c = ( t < X[i] );
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100303 t += add;
304 c += ( t < add );
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100305 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100306 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100307
Tom Cosgroveb4964862022-08-30 11:57:22 +0100308 return( c );
309}
310
311mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
312 const mbedtls_mpi_uint *A,
313 const mbedtls_mpi_uint *B,
314 size_t limbs )
315{
316 mbedtls_mpi_uint c = 0;
317
318 for( size_t i = 0; i < limbs; i++ )
319 {
320 mbedtls_mpi_uint z = ( A[i] < c );
321 mbedtls_mpi_uint t = A[i] - c;
322 c = ( t < B[i] ) + z;
323 X[i] = t - B[i];
324 }
325
326 return( c );
327}
328
329mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
330 const mbedtls_mpi_uint *s, size_t s_len,
331 mbedtls_mpi_uint b )
332{
333 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100334 /*
335 * It is a documented precondition of this function that d_len >= s_len.
336 * If that's not the case, we swap these round: this turns what would be
337 * a buffer overflow into an incorrect result.
338 */
Tom Cosgroveb4964862022-08-30 11:57:22 +0100339 if( d_len < s_len )
340 s_len = d_len;
341 size_t excess_len = d_len - s_len;
342 size_t steps_x8 = s_len / 8;
343 size_t steps_x1 = s_len & 7;
344
345 while( steps_x8-- )
346 {
347 MULADDC_X8_INIT
348 MULADDC_X8_CORE
349 MULADDC_X8_STOP
350 }
351
352 while( steps_x1-- )
353 {
354 MULADDC_X1_INIT
355 MULADDC_X1_CORE
356 MULADDC_X1_STOP
357 }
358
359 while( excess_len-- )
360 {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100361 *d += c;
362 c = ( *d < c );
363 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100364 }
365
366 return( c );
367}
368
369/*
370 * Fast Montgomery initialization (thanks to Tom St Denis).
371 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100372mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
Tom Cosgroveb4964862022-08-30 11:57:22 +0100373{
374 mbedtls_mpi_uint x = N[0];
375
376 x += ( ( N[0] + 2 ) & 4 ) << 1;
377
378 for( unsigned int i = biL; i >= 8; i /= 2 )
379 x *= ( 2 - ( N[0] * x ) );
380
381 return( ~x + 1 );
382}
383
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100384void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
385 const mbedtls_mpi_uint *A,
386 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100387 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100388 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100389 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100390 mbedtls_mpi_uint mm,
391 mbedtls_mpi_uint *T )
392{
Tom Cosgrove72594632022-08-24 11:51:58 +0100393 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100394
Tom Cosgrove67c92472022-09-02 13:28:59 +0100395 for( size_t i = 0; i < AN_limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100396 {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100397 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100398 mbedtls_mpi_uint u0 = A[i];
399 mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100400
Tom Cosgrove72594632022-08-24 11:51:58 +0100401 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
402 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove67c92472022-09-02 13:28:59 +0100403
404 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100405 }
406
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100407 /*
408 * The result we want is (T >= N) ? T - N : T.
409 *
410 * For better constant-time properties in this function, we always do the
411 * subtraction, with the result in X.
412 *
413 * We also look to see if there was any carry in the final additions in the
414 * loop above.
415 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100416
Tom Cosgrove72594632022-08-24 11:51:58 +0100417 mbedtls_mpi_uint carry = T[AN_limbs];
418 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100419
420 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100421 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100422 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100423 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100424 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100425 * 1) T < N : (carry, borrow) = (0, 1): we want T
426 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
427 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100428 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100429 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100430 *
431 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100432 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100433 */
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100434 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100435}
436
Janos Follath3ca07752022-08-09 11:45:47 +0100437#endif /* MBEDTLS_BIGNUM_C */