blob: 9adc4effb0b6615d20a151b9d72ad81c1ae1377d [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 Mezei9f6615f2022-09-15 19:12:06 +0200165void 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 )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200170{
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200171 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
172 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( assign );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200173
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200174 mbedtls_ct_mpi_uint_cond_assign( Y_limbs, X, Y, assign );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200175
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200176 for( size_t i = Y_limbs; i < X_limbs; i++ )
177 X[i] &= ~limb_mask;
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200178}
179
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200180void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X,
181 size_t X_limbs,
182 mbedtls_mpi_uint *Y,
183 size_t Y_limbs,
184 unsigned char swap )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200185{
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200186 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
187 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200188
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200189 for( size_t i = 0; i < X_limbs; i++ )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200190 {
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200191 mbedtls_mpi_uint tmp = X[i];
192 X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask );
193 Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200194 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200195}
196
Janos Follath3ca07752022-08-09 11:45:47 +0100197int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100198 size_t X_limbs,
199 const unsigned char *input,
200 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100201{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100202 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100203
Janos Follathb7a88ec2022-08-19 12:24:40 +0100204 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100205 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
206
207 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200208 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100209 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100210
Janos Follathb7a88ec2022-08-19 12:24:40 +0100211 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100212 {
213 size_t offset = ( ( i % ciL ) << 3 );
214 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
215 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200216 }
Janos Follath3ca07752022-08-09 11:45:47 +0100217
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100218 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100219}
220
Janos Follath3ca07752022-08-09 11:45:47 +0100221int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100222 size_t X_limbs,
223 const unsigned char *input,
224 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100225{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100226 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100227
Janos Follathb7a88ec2022-08-19 12:24:40 +0100228 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100229 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
230
Janos Follathb7a88ec2022-08-19 12:24:40 +0100231 /* If X_limbs is 0, input_length must also be 0 (from previous test).
232 * Nothing to do. */
233 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200234 return( 0 );
235
Janos Follathb7a88ec2022-08-19 12:24:40 +0100236 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200237
238 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100239 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200240 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100241 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200242 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100243 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100244 }
245
Janos Follathb7a88ec2022-08-19 12:24:40 +0100246 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200247
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100248 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100249}
250
Janos Follathb7a88ec2022-08-19 12:24:40 +0100251int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
252 size_t A_limbs,
253 unsigned char *output,
254 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100255{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100256 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100257 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100258
Janos Follathb7a88ec2022-08-19 12:24:40 +0100259 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100260 {
261 bytes_to_copy = stored_bytes;
262 }
263 else
264 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100265 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100266
Janos Follathaf3f39c2022-08-22 09:06:32 +0100267 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100268 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100269 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100270 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100271 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100272 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
273 }
274 }
275
Janos Follathcc939082022-08-15 12:08:49 +0100276 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100277 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100278
Janos Follathb7a88ec2022-08-19 12:24:40 +0100279 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100280 {
281 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100282 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100283 }
284
285 return( 0 );
286}
287
Janos Follath3ca07752022-08-09 11:45:47 +0100288int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100289 size_t X_limbs,
290 unsigned char *output,
291 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100292{
293 size_t stored_bytes;
294 size_t bytes_to_copy;
295 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100296
Janos Follathb7a88ec2022-08-19 12:24:40 +0100297 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100298
Janos Follathb7a88ec2022-08-19 12:24:40 +0100299 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100300 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100301 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100302 * null bytes and record the position at which to start
303 * writing the significant bytes. In this case, the execution
304 * trace of this function does not depend on the value of the
305 * number. */
306 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100307 p = output + output_length - stored_bytes;
308 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100309 }
310 else
311 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100312 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100313 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100314 bytes_to_copy = output_length;
315 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100316 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100317 {
318 if( GET_BYTE( X, i ) != 0 )
319 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
320 }
321 }
322
Janos Follathcc939082022-08-15 12:08:49 +0100323 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100324 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
325
326 return( 0 );
327}
328
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100329mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
330 const mbedtls_mpi_uint *A,
Tom Cosgroveb4964862022-08-30 11:57:22 +0100331 size_t limbs,
332 unsigned cond )
333{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100334 mbedtls_mpi_uint c = 0;
335
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100336 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
337 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
Tom Cosgrove93549902022-08-30 17:41:23 +0100338
Tom Cosgroveb4964862022-08-30 11:57:22 +0100339 for( size_t i = 0; i < limbs; i++ )
340 {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100341 mbedtls_mpi_uint add = mask & A[i];
342 mbedtls_mpi_uint t = c + X[i];
343 c = ( t < X[i] );
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100344 t += add;
345 c += ( t < add );
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100346 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100347 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100348
Tom Cosgroveb4964862022-08-30 11:57:22 +0100349 return( c );
350}
351
352mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
353 const mbedtls_mpi_uint *A,
354 const mbedtls_mpi_uint *B,
355 size_t limbs )
356{
357 mbedtls_mpi_uint c = 0;
358
359 for( size_t i = 0; i < limbs; i++ )
360 {
361 mbedtls_mpi_uint z = ( A[i] < c );
362 mbedtls_mpi_uint t = A[i] - c;
363 c = ( t < B[i] ) + z;
364 X[i] = t - B[i];
365 }
366
367 return( c );
368}
369
370mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
371 const mbedtls_mpi_uint *s, size_t s_len,
372 mbedtls_mpi_uint b )
373{
374 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100375 /*
376 * It is a documented precondition of this function that d_len >= s_len.
377 * If that's not the case, we swap these round: this turns what would be
378 * a buffer overflow into an incorrect result.
379 */
Tom Cosgroveb4964862022-08-30 11:57:22 +0100380 if( d_len < s_len )
381 s_len = d_len;
382 size_t excess_len = d_len - s_len;
383 size_t steps_x8 = s_len / 8;
384 size_t steps_x1 = s_len & 7;
385
386 while( steps_x8-- )
387 {
388 MULADDC_X8_INIT
389 MULADDC_X8_CORE
390 MULADDC_X8_STOP
391 }
392
393 while( steps_x1-- )
394 {
395 MULADDC_X1_INIT
396 MULADDC_X1_CORE
397 MULADDC_X1_STOP
398 }
399
400 while( excess_len-- )
401 {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100402 *d += c;
403 c = ( *d < c );
404 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100405 }
406
407 return( c );
408}
409
410/*
411 * Fast Montgomery initialization (thanks to Tom St Denis).
412 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100413mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
Tom Cosgroveb4964862022-08-30 11:57:22 +0100414{
415 mbedtls_mpi_uint x = N[0];
416
417 x += ( ( N[0] + 2 ) & 4 ) << 1;
418
419 for( unsigned int i = biL; i >= 8; i /= 2 )
420 x *= ( 2 - ( N[0] * x ) );
421
422 return( ~x + 1 );
423}
424
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100425void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
426 const mbedtls_mpi_uint *A,
427 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100428 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100429 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100430 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100431 mbedtls_mpi_uint mm,
432 mbedtls_mpi_uint *T )
433{
Tom Cosgrove72594632022-08-24 11:51:58 +0100434 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100435
Tom Cosgrove67c92472022-09-02 13:28:59 +0100436 for( size_t i = 0; i < AN_limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100437 {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100438 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100439 mbedtls_mpi_uint u0 = A[i];
440 mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100441
Tom Cosgrove72594632022-08-24 11:51:58 +0100442 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
443 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove67c92472022-09-02 13:28:59 +0100444
445 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100446 }
447
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100448 /*
449 * The result we want is (T >= N) ? T - N : T.
450 *
451 * For better constant-time properties in this function, we always do the
452 * subtraction, with the result in X.
453 *
454 * We also look to see if there was any carry in the final additions in the
455 * loop above.
456 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100457
Tom Cosgrove72594632022-08-24 11:51:58 +0100458 mbedtls_mpi_uint carry = T[AN_limbs];
459 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100460
461 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100462 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100463 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100464 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100465 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100466 * 1) T < N : (carry, borrow) = (0, 1): we want T
467 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
468 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100469 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100470 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100471 *
472 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100473 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100474 */
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100475 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100476}
477
Janos Follath3ca07752022-08-09 11:45:47 +0100478#endif /* MBEDTLS_BIGNUM_C */