blob: 7074a0962bbaeda3a80be32cc17c74b818776604 [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,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200181 mbedtls_mpi_uint *Y,
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200182 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200183 unsigned char swap )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200184{
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200185 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
186 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200187
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200188 for( size_t i = 0; i < limbs; i++ )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200189 {
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200190 mbedtls_mpi_uint tmp = X[i];
191 X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask );
192 Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200193 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200194}
195
Janos Follath3ca07752022-08-09 11:45:47 +0100196int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100197 size_t X_limbs,
198 const unsigned char *input,
199 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100200{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100201 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100202
Janos Follathb7a88ec2022-08-19 12:24:40 +0100203 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100204 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
205
206 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200207 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100208 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100209
Janos Follathb7a88ec2022-08-19 12:24:40 +0100210 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100211 {
212 size_t offset = ( ( i % ciL ) << 3 );
213 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
214 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200215 }
Janos Follath3ca07752022-08-09 11:45:47 +0100216
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100217 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100218}
219
Janos Follath3ca07752022-08-09 11:45:47 +0100220int mbedtls_mpi_core_read_be( 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
Janos Follathb7a88ec2022-08-19 12:24:40 +0100230 /* If X_limbs is 0, input_length must also be 0 (from previous test).
231 * Nothing to do. */
232 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200233 return( 0 );
234
Janos Follathb7a88ec2022-08-19 12:24:40 +0100235 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200236
237 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100238 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200239 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100240 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200241 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100242 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100243 }
244
Janos Follathb7a88ec2022-08-19 12:24:40 +0100245 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200246
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100247 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100248}
249
Janos Follathb7a88ec2022-08-19 12:24:40 +0100250int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
251 size_t A_limbs,
252 unsigned char *output,
253 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100254{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100255 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100256 size_t bytes_to_copy;
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 {
260 bytes_to_copy = stored_bytes;
261 }
262 else
263 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100264 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100265
Janos Follathaf3f39c2022-08-22 09:06:32 +0100266 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100267 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100268 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100269 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100270 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100271 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
272 }
273 }
274
Janos Follathcc939082022-08-15 12:08:49 +0100275 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100276 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100277
Janos Follathb7a88ec2022-08-19 12:24:40 +0100278 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100279 {
280 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100281 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100282 }
283
284 return( 0 );
285}
286
Janos Follath3ca07752022-08-09 11:45:47 +0100287int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100288 size_t X_limbs,
289 unsigned char *output,
290 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100291{
292 size_t stored_bytes;
293 size_t bytes_to_copy;
294 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100295
Janos Follathb7a88ec2022-08-19 12:24:40 +0100296 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100297
Janos Follathb7a88ec2022-08-19 12:24:40 +0100298 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100299 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100300 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100301 * null bytes and record the position at which to start
302 * writing the significant bytes. In this case, the execution
303 * trace of this function does not depend on the value of the
304 * number. */
305 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100306 p = output + output_length - stored_bytes;
307 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100308 }
309 else
310 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100311 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100312 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100313 bytes_to_copy = output_length;
314 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100315 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100316 {
317 if( GET_BYTE( X, i ) != 0 )
318 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
319 }
320 }
321
Janos Follathcc939082022-08-15 12:08:49 +0100322 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100323 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
324
325 return( 0 );
326}
327
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100328mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
329 const mbedtls_mpi_uint *A,
Tom Cosgroveb4964862022-08-30 11:57:22 +0100330 size_t limbs,
331 unsigned cond )
332{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100333 mbedtls_mpi_uint c = 0;
334
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100335 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
336 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
Tom Cosgrove93549902022-08-30 17:41:23 +0100337
Tom Cosgroveb4964862022-08-30 11:57:22 +0100338 for( size_t i = 0; i < limbs; i++ )
339 {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100340 mbedtls_mpi_uint add = mask & A[i];
341 mbedtls_mpi_uint t = c + X[i];
342 c = ( t < X[i] );
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100343 t += add;
344 c += ( t < add );
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100345 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100346 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100347
Tom Cosgroveb4964862022-08-30 11:57:22 +0100348 return( c );
349}
350
351mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
352 const mbedtls_mpi_uint *A,
353 const mbedtls_mpi_uint *B,
354 size_t limbs )
355{
356 mbedtls_mpi_uint c = 0;
357
358 for( size_t i = 0; i < limbs; i++ )
359 {
360 mbedtls_mpi_uint z = ( A[i] < c );
361 mbedtls_mpi_uint t = A[i] - c;
362 c = ( t < B[i] ) + z;
363 X[i] = t - B[i];
364 }
365
366 return( c );
367}
368
369mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
370 const mbedtls_mpi_uint *s, size_t s_len,
371 mbedtls_mpi_uint b )
372{
373 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100374 /*
375 * It is a documented precondition of this function that d_len >= s_len.
376 * If that's not the case, we swap these round: this turns what would be
377 * a buffer overflow into an incorrect result.
378 */
Tom Cosgroveb4964862022-08-30 11:57:22 +0100379 if( d_len < s_len )
380 s_len = d_len;
381 size_t excess_len = d_len - s_len;
382 size_t steps_x8 = s_len / 8;
383 size_t steps_x1 = s_len & 7;
384
385 while( steps_x8-- )
386 {
387 MULADDC_X8_INIT
388 MULADDC_X8_CORE
389 MULADDC_X8_STOP
390 }
391
392 while( steps_x1-- )
393 {
394 MULADDC_X1_INIT
395 MULADDC_X1_CORE
396 MULADDC_X1_STOP
397 }
398
399 while( excess_len-- )
400 {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100401 *d += c;
402 c = ( *d < c );
403 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100404 }
405
406 return( c );
407}
408
409/*
410 * Fast Montgomery initialization (thanks to Tom St Denis).
411 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100412mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
Tom Cosgroveb4964862022-08-30 11:57:22 +0100413{
414 mbedtls_mpi_uint x = N[0];
415
416 x += ( ( N[0] + 2 ) & 4 ) << 1;
417
418 for( unsigned int i = biL; i >= 8; i /= 2 )
419 x *= ( 2 - ( N[0] * x ) );
420
421 return( ~x + 1 );
422}
423
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100424void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
425 const mbedtls_mpi_uint *A,
426 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100427 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100428 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100429 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100430 mbedtls_mpi_uint mm,
431 mbedtls_mpi_uint *T )
432{
Tom Cosgrove72594632022-08-24 11:51:58 +0100433 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100434
Tom Cosgrove67c92472022-09-02 13:28:59 +0100435 for( size_t i = 0; i < AN_limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100436 {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100437 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100438 mbedtls_mpi_uint u0 = A[i];
439 mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100440
Tom Cosgrove72594632022-08-24 11:51:58 +0100441 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
442 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove67c92472022-09-02 13:28:59 +0100443
444 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100445 }
446
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100447 /*
448 * The result we want is (T >= N) ? T - N : T.
449 *
450 * For better constant-time properties in this function, we always do the
451 * subtraction, with the result in X.
452 *
453 * We also look to see if there was any carry in the final additions in the
454 * loop above.
455 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100456
Tom Cosgrove72594632022-08-24 11:51:58 +0100457 mbedtls_mpi_uint carry = T[AN_limbs];
458 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100459
460 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100461 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100462 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100463 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100464 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100465 * 1) T < N : (carry, borrow) = (0, 1): we want T
466 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
467 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100468 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100469 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100470 *
471 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100472 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100473 */
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100474 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100475}
476
Janos Follath3ca07752022-08-09 11:45:47 +0100477#endif /* MBEDTLS_BIGNUM_C */