blob: 847ed6eb0fc51c81593568c358a48c5104310ee8 [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 Mezeie9c013c2022-10-10 14:26:57 +0200170 if( X == A )
171 return;
172
Gabor Mezei1c628d52022-09-27 12:13:51 +0200173 mbedtls_ct_mpi_uint_cond_assign( limbs, X, A, assign );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200174}
175
Gabor Mezeie5b85852022-09-30 13:54:02 +0200176void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X,
177 mbedtls_mpi_uint *Y,
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200178 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200179 unsigned char swap )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200180{
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200181 if( X == Y )
182 return;
183
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200184 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
185 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200186
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200187 for( size_t i = 0; i < limbs; i++ )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200188 {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200189 mbedtls_mpi_uint tmp = X[i];
190 X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask );
191 Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200192 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200193}
194
Janos Follath3ca07752022-08-09 11:45:47 +0100195int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100196 size_t X_limbs,
197 const unsigned char *input,
198 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100199{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100200 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100201
Janos Follathb7a88ec2022-08-19 12:24:40 +0100202 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100203 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
204
205 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200206 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100207 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100208
Janos Follathb7a88ec2022-08-19 12:24:40 +0100209 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100210 {
211 size_t offset = ( ( i % ciL ) << 3 );
212 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
213 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200214 }
Janos Follath3ca07752022-08-09 11:45:47 +0100215
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100216 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100217}
218
Janos Follath3ca07752022-08-09 11:45:47 +0100219int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100220 size_t X_limbs,
221 const unsigned char *input,
222 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100223{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100224 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100225
Janos Follathb7a88ec2022-08-19 12:24:40 +0100226 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100227 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
228
Janos Follathb7a88ec2022-08-19 12:24:40 +0100229 /* If X_limbs is 0, input_length must also be 0 (from previous test).
230 * Nothing to do. */
231 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200232 return( 0 );
233
Janos Follathb7a88ec2022-08-19 12:24:40 +0100234 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200235
236 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100237 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200238 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100239 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200240 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100241 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100242 }
243
Janos Follathb7a88ec2022-08-19 12:24:40 +0100244 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200245
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100246 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100247}
248
Janos Follathb7a88ec2022-08-19 12:24:40 +0100249int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
250 size_t A_limbs,
251 unsigned char *output,
252 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100253{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100254 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100255 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100256
Janos Follathb7a88ec2022-08-19 12:24:40 +0100257 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100258 {
259 bytes_to_copy = stored_bytes;
260 }
261 else
262 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100263 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100264
Janos Follathaf3f39c2022-08-22 09:06:32 +0100265 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100266 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100267 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100268 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100269 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100270 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
271 }
272 }
273
Janos Follathcc939082022-08-15 12:08:49 +0100274 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100275 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100276
Janos Follathb7a88ec2022-08-19 12:24:40 +0100277 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100278 {
279 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100280 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100281 }
282
283 return( 0 );
284}
285
Janos Follath3ca07752022-08-09 11:45:47 +0100286int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100287 size_t X_limbs,
288 unsigned char *output,
289 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100290{
291 size_t stored_bytes;
292 size_t bytes_to_copy;
293 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100294
Janos Follathb7a88ec2022-08-19 12:24:40 +0100295 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100296
Janos Follathb7a88ec2022-08-19 12:24:40 +0100297 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100298 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100299 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100300 * null bytes and record the position at which to start
301 * writing the significant bytes. In this case, the execution
302 * trace of this function does not depend on the value of the
303 * number. */
304 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100305 p = output + output_length - stored_bytes;
306 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100307 }
308 else
309 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100310 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100311 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100312 bytes_to_copy = output_length;
313 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100314 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100315 {
316 if( GET_BYTE( X, i ) != 0 )
317 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
318 }
319 }
320
Janos Follathcc939082022-08-15 12:08:49 +0100321 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100322 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
323
324 return( 0 );
325}
326
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100327mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
328 const mbedtls_mpi_uint *A,
Tom Cosgroveb4964862022-08-30 11:57:22 +0100329 size_t limbs,
330 unsigned cond )
331{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100332 mbedtls_mpi_uint c = 0;
333
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100334 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
335 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
Tom Cosgrove93549902022-08-30 17:41:23 +0100336
Tom Cosgroveb4964862022-08-30 11:57:22 +0100337 for( size_t i = 0; i < limbs; i++ )
338 {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100339 mbedtls_mpi_uint add = mask & A[i];
340 mbedtls_mpi_uint t = c + X[i];
341 c = ( t < X[i] );
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100342 t += add;
343 c += ( t < add );
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100344 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100345 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100346
Tom Cosgroveb4964862022-08-30 11:57:22 +0100347 return( c );
348}
349
350mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
351 const mbedtls_mpi_uint *A,
352 const mbedtls_mpi_uint *B,
353 size_t limbs )
354{
355 mbedtls_mpi_uint c = 0;
356
357 for( size_t i = 0; i < limbs; i++ )
358 {
359 mbedtls_mpi_uint z = ( A[i] < c );
360 mbedtls_mpi_uint t = A[i] - c;
361 c = ( t < B[i] ) + z;
362 X[i] = t - B[i];
363 }
364
365 return( c );
366}
367
368mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
369 const mbedtls_mpi_uint *s, size_t s_len,
370 mbedtls_mpi_uint b )
371{
372 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100373 /*
374 * It is a documented precondition of this function that d_len >= s_len.
375 * If that's not the case, we swap these round: this turns what would be
376 * a buffer overflow into an incorrect result.
377 */
Tom Cosgroveb4964862022-08-30 11:57:22 +0100378 if( d_len < s_len )
379 s_len = d_len;
380 size_t excess_len = d_len - s_len;
381 size_t steps_x8 = s_len / 8;
382 size_t steps_x1 = s_len & 7;
383
384 while( steps_x8-- )
385 {
386 MULADDC_X8_INIT
387 MULADDC_X8_CORE
388 MULADDC_X8_STOP
389 }
390
391 while( steps_x1-- )
392 {
393 MULADDC_X1_INIT
394 MULADDC_X1_CORE
395 MULADDC_X1_STOP
396 }
397
398 while( excess_len-- )
399 {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100400 *d += c;
401 c = ( *d < c );
402 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100403 }
404
405 return( c );
406}
407
408/*
409 * Fast Montgomery initialization (thanks to Tom St Denis).
410 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100411mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
Tom Cosgroveb4964862022-08-30 11:57:22 +0100412{
413 mbedtls_mpi_uint x = N[0];
414
415 x += ( ( N[0] + 2 ) & 4 ) << 1;
416
417 for( unsigned int i = biL; i >= 8; i /= 2 )
418 x *= ( 2 - ( N[0] * x ) );
419
420 return( ~x + 1 );
421}
422
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100423void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
424 const mbedtls_mpi_uint *A,
425 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100426 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100427 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100428 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100429 mbedtls_mpi_uint mm,
430 mbedtls_mpi_uint *T )
431{
Tom Cosgrove72594632022-08-24 11:51:58 +0100432 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100433
Tom Cosgrove67c92472022-09-02 13:28:59 +0100434 for( size_t i = 0; i < AN_limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100435 {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100436 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100437 mbedtls_mpi_uint u0 = A[i];
438 mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100439
Tom Cosgrove72594632022-08-24 11:51:58 +0100440 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
441 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove67c92472022-09-02 13:28:59 +0100442
443 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100444 }
445
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100446 /*
447 * The result we want is (T >= N) ? T - N : T.
448 *
449 * For better constant-time properties in this function, we always do the
450 * subtraction, with the result in X.
451 *
452 * We also look to see if there was any carry in the final additions in the
453 * loop above.
454 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100455
Tom Cosgrove72594632022-08-24 11:51:58 +0100456 mbedtls_mpi_uint carry = T[AN_limbs];
457 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100458
459 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100460 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100461 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100462 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100463 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100464 * 1) T < N : (carry, borrow) = (0, 1): we want T
465 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
466 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100467 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100468 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100469 *
470 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100471 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100472 */
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100473 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100474}
475
Janos Follath3ca07752022-08-09 11:45:47 +0100476#endif /* MBEDTLS_BIGNUM_C */