blob: 233b22ded68ddeb989a021e5a5116bc4bd0bc2e8 [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,
Gabor Mezei1c628d52022-09-27 12:13:51 +0200166 const mbedtls_mpi_uint *A,
Gabor Mezei3eff4252022-09-26 17:26:42 +0200167 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200168 unsigned char assign )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200169{
Gabor Mezei1c628d52022-09-27 12:13:51 +0200170 mbedtls_ct_mpi_uint_cond_assign( limbs, X, A, assign );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200171}
172
Gabor Mezeie5b85852022-09-30 13:54:02 +0200173void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X,
174 mbedtls_mpi_uint *Y,
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200175 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200176 unsigned char swap )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200177{
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200178 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
179 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200180
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200181 for( size_t i = 0; i < limbs; i++ )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200182 {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200183 mbedtls_mpi_uint tmp = X[i];
184 X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask );
185 Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200186 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200187}
188
Janos Follath3ca07752022-08-09 11:45:47 +0100189int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100190 size_t X_limbs,
191 const unsigned char *input,
192 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100193{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100194 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100195
Janos Follathb7a88ec2022-08-19 12:24:40 +0100196 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100197 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
198
199 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200200 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100201 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100202
Janos Follathb7a88ec2022-08-19 12:24:40 +0100203 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100204 {
205 size_t offset = ( ( i % ciL ) << 3 );
206 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
207 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200208 }
Janos Follath3ca07752022-08-09 11:45:47 +0100209
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100210 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100211}
212
Janos Follath3ca07752022-08-09 11:45:47 +0100213int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100214 size_t X_limbs,
215 const unsigned char *input,
216 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100217{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100218 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100219
Janos Follathb7a88ec2022-08-19 12:24:40 +0100220 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100221 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
222
Janos Follathb7a88ec2022-08-19 12:24:40 +0100223 /* If X_limbs is 0, input_length must also be 0 (from previous test).
224 * Nothing to do. */
225 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200226 return( 0 );
227
Janos Follathb7a88ec2022-08-19 12:24:40 +0100228 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200229
230 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100231 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200232 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100233 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200234 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100235 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100236 }
237
Janos Follathb7a88ec2022-08-19 12:24:40 +0100238 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200239
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100240 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100241}
242
Janos Follathb7a88ec2022-08-19 12:24:40 +0100243int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
244 size_t A_limbs,
245 unsigned char *output,
246 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100247{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100248 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100249 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100250
Janos Follathb7a88ec2022-08-19 12:24:40 +0100251 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100252 {
253 bytes_to_copy = stored_bytes;
254 }
255 else
256 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100257 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100258
Janos Follathaf3f39c2022-08-22 09:06:32 +0100259 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100260 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100261 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100262 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100263 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100264 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
265 }
266 }
267
Janos Follathcc939082022-08-15 12:08:49 +0100268 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100269 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100270
Janos Follathb7a88ec2022-08-19 12:24:40 +0100271 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100272 {
273 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100274 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100275 }
276
277 return( 0 );
278}
279
Janos Follath3ca07752022-08-09 11:45:47 +0100280int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100281 size_t X_limbs,
282 unsigned char *output,
283 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100284{
285 size_t stored_bytes;
286 size_t bytes_to_copy;
287 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100288
Janos Follathb7a88ec2022-08-19 12:24:40 +0100289 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100290
Janos Follathb7a88ec2022-08-19 12:24:40 +0100291 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100292 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100293 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100294 * null bytes and record the position at which to start
295 * writing the significant bytes. In this case, the execution
296 * trace of this function does not depend on the value of the
297 * number. */
298 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100299 p = output + output_length - stored_bytes;
300 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100301 }
302 else
303 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100304 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100305 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100306 bytes_to_copy = output_length;
307 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100308 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100309 {
310 if( GET_BYTE( X, i ) != 0 )
311 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
312 }
313 }
314
Janos Follathcc939082022-08-15 12:08:49 +0100315 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100316 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
317
318 return( 0 );
319}
320
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100321mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
322 const mbedtls_mpi_uint *A,
Tom Cosgroveb4964862022-08-30 11:57:22 +0100323 size_t limbs,
324 unsigned cond )
325{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100326 mbedtls_mpi_uint c = 0;
327
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100328 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
329 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
Tom Cosgrove93549902022-08-30 17:41:23 +0100330
Tom Cosgroveb4964862022-08-30 11:57:22 +0100331 for( size_t i = 0; i < limbs; i++ )
332 {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100333 mbedtls_mpi_uint add = mask & A[i];
334 mbedtls_mpi_uint t = c + X[i];
335 c = ( t < X[i] );
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100336 t += add;
337 c += ( t < add );
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100338 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100339 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100340
Tom Cosgroveb4964862022-08-30 11:57:22 +0100341 return( c );
342}
343
344mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
345 const mbedtls_mpi_uint *A,
346 const mbedtls_mpi_uint *B,
347 size_t limbs )
348{
349 mbedtls_mpi_uint c = 0;
350
351 for( size_t i = 0; i < limbs; i++ )
352 {
353 mbedtls_mpi_uint z = ( A[i] < c );
354 mbedtls_mpi_uint t = A[i] - c;
355 c = ( t < B[i] ) + z;
356 X[i] = t - B[i];
357 }
358
359 return( c );
360}
361
362mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
363 const mbedtls_mpi_uint *s, size_t s_len,
364 mbedtls_mpi_uint b )
365{
366 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100367 /*
368 * It is a documented precondition of this function that d_len >= s_len.
369 * If that's not the case, we swap these round: this turns what would be
370 * a buffer overflow into an incorrect result.
371 */
Tom Cosgroveb4964862022-08-30 11:57:22 +0100372 if( d_len < s_len )
373 s_len = d_len;
374 size_t excess_len = d_len - s_len;
375 size_t steps_x8 = s_len / 8;
376 size_t steps_x1 = s_len & 7;
377
378 while( steps_x8-- )
379 {
380 MULADDC_X8_INIT
381 MULADDC_X8_CORE
382 MULADDC_X8_STOP
383 }
384
385 while( steps_x1-- )
386 {
387 MULADDC_X1_INIT
388 MULADDC_X1_CORE
389 MULADDC_X1_STOP
390 }
391
392 while( excess_len-- )
393 {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100394 *d += c;
395 c = ( *d < c );
396 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100397 }
398
399 return( c );
400}
401
402/*
403 * Fast Montgomery initialization (thanks to Tom St Denis).
404 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100405mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
Tom Cosgroveb4964862022-08-30 11:57:22 +0100406{
407 mbedtls_mpi_uint x = N[0];
408
409 x += ( ( N[0] + 2 ) & 4 ) << 1;
410
411 for( unsigned int i = biL; i >= 8; i /= 2 )
412 x *= ( 2 - ( N[0] * x ) );
413
414 return( ~x + 1 );
415}
416
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100417void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
418 const mbedtls_mpi_uint *A,
419 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100420 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100421 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100422 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100423 mbedtls_mpi_uint mm,
424 mbedtls_mpi_uint *T )
425{
Tom Cosgrove72594632022-08-24 11:51:58 +0100426 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100427
Tom Cosgrove67c92472022-09-02 13:28:59 +0100428 for( size_t i = 0; i < AN_limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100429 {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100430 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100431 mbedtls_mpi_uint u0 = A[i];
432 mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100433
Tom Cosgrove72594632022-08-24 11:51:58 +0100434 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
435 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove67c92472022-09-02 13:28:59 +0100436
437 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100438 }
439
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100440 /*
441 * The result we want is (T >= N) ? T - N : T.
442 *
443 * For better constant-time properties in this function, we always do the
444 * subtraction, with the result in X.
445 *
446 * We also look to see if there was any carry in the final additions in the
447 * loop above.
448 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100449
Tom Cosgrove72594632022-08-24 11:51:58 +0100450 mbedtls_mpi_uint carry = T[AN_limbs];
451 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100452
453 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100454 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100455 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100456 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100457 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100458 * 1) T < N : (carry, borrow) = (0, 1): we want T
459 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
460 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100461 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100462 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100463 *
464 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100465 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100466 */
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100467 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100468}
469
Janos Follath3ca07752022-08-09 11:45:47 +0100470#endif /* MBEDTLS_BIGNUM_C */