blob: 3b660d00569b5db2b67f3c625e130fa4f4896d08 [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
Janos Follath3ca07752022-08-09 11:45:47 +010030#include "mbedtls/platform.h"
Janos Follath3ca07752022-08-09 11:45:47 +010031
32#include "bignum_core.h"
Tom Cosgrove958fd3d2022-08-24 11:08:51 +010033#include "bn_mul.h"
34#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010035
Janos Follath2e328c82022-08-22 11:19:10 +010036size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010037{
38 size_t j;
39 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
40
41 for( j = 0; j < biL; j++ )
42 {
Janos Follathb7a88ec2022-08-19 12:24:40 +010043 if( a & mask ) break;
Janos Follath3ca07752022-08-09 11:45:47 +010044
45 mask >>= 1;
46 }
47
Gabor Mezei89e31462022-08-12 15:36:56 +020048 return( j );
Janos Follath3ca07752022-08-09 11:45:47 +010049}
50
Janos Follathb7a88ec2022-08-19 12:24:40 +010051size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +010052{
53 size_t i, j;
54
Janos Follathb7a88ec2022-08-19 12:24:40 +010055 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010056 return( 0 );
57
Janos Follathb7a88ec2022-08-19 12:24:40 +010058 for( i = A_limbs - 1; i > 0; i-- )
59 if( A[i] != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010060 break;
61
Janos Follathb7a88ec2022-08-19 12:24:40 +010062 j = biL - mbedtls_mpi_core_clz( A[i] );
Janos Follath3ca07752022-08-09 11:45:47 +010063
64 return( ( i * biL ) + j );
65}
66
Janos Follath3ca07752022-08-09 11:45:47 +010067/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
68 * into the storage form used by mbedtls_mpi. */
Janos Follathb7a88ec2022-08-19 12:24:40 +010069static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010070{
71 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010072 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010073 mbedtls_mpi_uint tmp = 0;
74
Janos Follathb7a88ec2022-08-19 12:24:40 +010075 for( i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++ )
Janos Follath3ca07752022-08-09 11:45:47 +010076 {
77 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010078 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010079 }
80
81 return( tmp );
82}
83
Janos Follathb7a88ec2022-08-19 12:24:40 +010084static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010085{
86#if defined(__BYTE_ORDER__)
87
88/* Nothing to do on bigendian systems. */
89#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
Janos Follathb7a88ec2022-08-19 12:24:40 +010090 return( a );
Janos Follath3ca07752022-08-09 11:45:47 +010091#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
92
93#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
94
95/* For GCC and Clang, have builtins for byte swapping. */
96#if defined(__GNUC__) && defined(__GNUC_PREREQ)
97#if __GNUC_PREREQ(4,3)
98#define have_bswap
99#endif
100#endif
101
102#if defined(__clang__) && defined(__has_builtin)
103#if __has_builtin(__builtin_bswap32) && \
104 __has_builtin(__builtin_bswap64)
105#define have_bswap
106#endif
107#endif
108
109#if defined(have_bswap)
110 /* The compiler is hopefully able to statically evaluate this! */
111 switch( sizeof(mbedtls_mpi_uint) )
112 {
113 case 4:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100114 return( __builtin_bswap32(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100115 case 8:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100116 return( __builtin_bswap64(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100117 }
118#endif
119#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
120#endif /* __BYTE_ORDER__ */
121
122 /* Fall back to C-based reordering if we don't know the byte order
123 * or we couldn't use a compiler-specific builtin. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100124 return( mpi_bigendian_to_host_c( a ) );
Janos Follath3ca07752022-08-09 11:45:47 +0100125}
126
Janos Follathb7a88ec2022-08-19 12:24:40 +0100127void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +0100128 size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +0100129{
130 mbedtls_mpi_uint *cur_limb_left;
131 mbedtls_mpi_uint *cur_limb_right;
Janos Follathc4596412022-08-22 10:01:27 +0100132 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100133 return;
134
135 /*
136 * Traverse limbs and
137 * - adapt byte-order in each limb
138 * - swap the limbs themselves.
139 * For that, simultaneously traverse the limbs from left to right
140 * and from right to left, as long as the left index is not bigger
141 * than the right index (it's not a problem if limbs is odd and the
142 * indices coincide in the last iteration).
143 */
Janos Follathc4596412022-08-22 10:01:27 +0100144 for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 );
Janos Follath3ca07752022-08-09 11:45:47 +0100145 cur_limb_left <= cur_limb_right;
146 cur_limb_left++, cur_limb_right-- )
147 {
148 mbedtls_mpi_uint tmp;
149 /* Note that if cur_limb_left == cur_limb_right,
150 * this code effectively swaps the bytes only once. */
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100151 tmp = mpi_bigendian_to_host( *cur_limb_left );
Janos Follath3ca07752022-08-09 11:45:47 +0100152 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
153 *cur_limb_right = tmp;
154 }
155}
156
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200157void mbedtls_mpi_core_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei1c628d52022-09-27 12:13:51 +0200158 const mbedtls_mpi_uint *A,
Gabor Mezei3eff4252022-09-26 17:26:42 +0200159 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200160 unsigned char assign )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200161{
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200162 if( X == A )
163 return;
164
Gabor Mezei1c628d52022-09-27 12:13:51 +0200165 mbedtls_ct_mpi_uint_cond_assign( limbs, X, A, assign );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200166}
167
Gabor Mezeie5b85852022-09-30 13:54:02 +0200168void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X,
169 mbedtls_mpi_uint *Y,
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200170 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200171 unsigned char swap )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200172{
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200173 if( X == Y )
174 return;
175
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200176 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
177 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200178
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200179 for( size_t i = 0; i < limbs; i++ )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200180 {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200181 mbedtls_mpi_uint tmp = X[i];
182 X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask );
183 Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200184 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200185}
186
Janos Follath3ca07752022-08-09 11:45:47 +0100187int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100188 size_t X_limbs,
189 const unsigned char *input,
190 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100191{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100192 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100193
Janos Follathb7a88ec2022-08-19 12:24:40 +0100194 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100195 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
196
197 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200198 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100199 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100200
Janos Follathb7a88ec2022-08-19 12:24:40 +0100201 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100202 {
203 size_t offset = ( ( i % ciL ) << 3 );
204 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
205 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200206 }
Janos Follath3ca07752022-08-09 11:45:47 +0100207
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100208 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100209}
210
Janos Follath3ca07752022-08-09 11:45:47 +0100211int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100212 size_t X_limbs,
213 const unsigned char *input,
214 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100215{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100216 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100217
Janos Follathb7a88ec2022-08-19 12:24:40 +0100218 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100219 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
220
Janos Follathb7a88ec2022-08-19 12:24:40 +0100221 /* If X_limbs is 0, input_length must also be 0 (from previous test).
222 * Nothing to do. */
223 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200224 return( 0 );
225
Janos Follathb7a88ec2022-08-19 12:24:40 +0100226 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200227
228 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100229 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200230 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100231 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200232 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100233 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100234 }
235
Janos Follathb7a88ec2022-08-19 12:24:40 +0100236 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200237
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100238 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100239}
240
Janos Follathb7a88ec2022-08-19 12:24:40 +0100241int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
242 size_t A_limbs,
243 unsigned char *output,
244 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100245{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100246 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100247 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100248
Janos Follathb7a88ec2022-08-19 12:24:40 +0100249 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100250 {
251 bytes_to_copy = stored_bytes;
252 }
253 else
254 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100255 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100256
Janos Follathaf3f39c2022-08-22 09:06:32 +0100257 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100258 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100259 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100260 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100261 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100262 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
263 }
264 }
265
Janos Follathcc939082022-08-15 12:08:49 +0100266 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100267 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100268
Janos Follathb7a88ec2022-08-19 12:24:40 +0100269 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100270 {
271 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100272 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100273 }
274
275 return( 0 );
276}
277
Janos Follath3ca07752022-08-09 11:45:47 +0100278int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100279 size_t X_limbs,
280 unsigned char *output,
281 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100282{
283 size_t stored_bytes;
284 size_t bytes_to_copy;
285 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100286
Janos Follathb7a88ec2022-08-19 12:24:40 +0100287 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100288
Janos Follathb7a88ec2022-08-19 12:24:40 +0100289 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100290 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100291 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100292 * null bytes and record the position at which to start
293 * writing the significant bytes. In this case, the execution
294 * trace of this function does not depend on the value of the
295 * number. */
296 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100297 p = output + output_length - stored_bytes;
298 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100299 }
300 else
301 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100302 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100303 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100304 bytes_to_copy = output_length;
305 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100306 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100307 {
308 if( GET_BYTE( X, i ) != 0 )
309 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
310 }
311 }
312
Janos Follathcc939082022-08-15 12:08:49 +0100313 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100314 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
315
316 return( 0 );
317}
318
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200319void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs,
320 size_t count )
321{
322 size_t i, v0, v1;
323 mbedtls_mpi_uint r0 = 0, r1;
324
325 v0 = count / biL;
326 v1 = count & (biL - 1);
327
328 if( v0 > limbs || ( v0 == limbs && v1 > 0 ) )
329 {
330 memset( X, 0, limbs * ciL );
331 return;
332 }
333
334 /*
335 * shift by count / limb_size
336 */
337 if( v0 > 0 )
338 {
339 for( i = 0; i < limbs - v0; i++ )
340 X[i] = X[i + v0];
341
342 for( ; i < limbs; i++ )
343 X[i] = 0;
344 }
345
346 /*
347 * shift by count % limb_size
348 */
349 if( v1 > 0 )
350 {
351 for( i = limbs; i > 0; i-- )
352 {
353 r1 = X[i - 1] << (biL - v1);
354 X[i - 1] >>= v1;
355 X[i - 1] |= r0;
356 r0 = r1;
357 }
358 }
359}
360
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100361mbedtls_mpi_uint mbedtls_mpi_core_add( mbedtls_mpi_uint *X,
362 const mbedtls_mpi_uint *A,
363 const mbedtls_mpi_uint *B,
364 size_t limbs )
Hanno Beckerc9887132022-08-24 12:54:36 +0100365{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100366 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200367
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100368 for( size_t i = 0; i < limbs; i++ )
Hanno Beckerc9887132022-08-24 12:54:36 +0100369 {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100370 mbedtls_mpi_uint t = c + A[i];
371 c = ( t < A[i] );
372 t += B[i];
373 c += ( t < B[i] );
374 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100375 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100376
Hanno Beckerc9887132022-08-24 12:54:36 +0100377 return( c );
378}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200379
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100380mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
381 const mbedtls_mpi_uint *A,
Tom Cosgroveb4964862022-08-30 11:57:22 +0100382 size_t limbs,
383 unsigned cond )
384{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100385 mbedtls_mpi_uint c = 0;
386
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100387 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
388 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
Tom Cosgrove93549902022-08-30 17:41:23 +0100389
Tom Cosgroveb4964862022-08-30 11:57:22 +0100390 for( size_t i = 0; i < limbs; i++ )
391 {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100392 mbedtls_mpi_uint add = mask & A[i];
393 mbedtls_mpi_uint t = c + X[i];
394 c = ( t < X[i] );
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100395 t += add;
396 c += ( t < add );
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100397 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100398 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100399
Tom Cosgroveb4964862022-08-30 11:57:22 +0100400 return( c );
401}
402
403mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
404 const mbedtls_mpi_uint *A,
405 const mbedtls_mpi_uint *B,
406 size_t limbs )
407{
408 mbedtls_mpi_uint c = 0;
409
410 for( size_t i = 0; i < limbs; i++ )
411 {
412 mbedtls_mpi_uint z = ( A[i] < c );
413 mbedtls_mpi_uint t = A[i] - c;
414 c = ( t < B[i] ) + z;
415 X[i] = t - B[i];
416 }
417
418 return( c );
419}
420
421mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
422 const mbedtls_mpi_uint *s, size_t s_len,
423 mbedtls_mpi_uint b )
424{
425 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100426 /*
427 * It is a documented precondition of this function that d_len >= s_len.
428 * If that's not the case, we swap these round: this turns what would be
429 * a buffer overflow into an incorrect result.
430 */
Tom Cosgroveb4964862022-08-30 11:57:22 +0100431 if( d_len < s_len )
432 s_len = d_len;
433 size_t excess_len = d_len - s_len;
434 size_t steps_x8 = s_len / 8;
435 size_t steps_x1 = s_len & 7;
436
437 while( steps_x8-- )
438 {
439 MULADDC_X8_INIT
440 MULADDC_X8_CORE
441 MULADDC_X8_STOP
442 }
443
444 while( steps_x1-- )
445 {
446 MULADDC_X1_INIT
447 MULADDC_X1_CORE
448 MULADDC_X1_STOP
449 }
450
451 while( excess_len-- )
452 {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100453 *d += c;
454 c = ( *d < c );
455 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100456 }
457
458 return( c );
459}
460
461/*
462 * Fast Montgomery initialization (thanks to Tom St Denis).
463 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100464mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
Tom Cosgroveb4964862022-08-30 11:57:22 +0100465{
466 mbedtls_mpi_uint x = N[0];
467
468 x += ( ( N[0] + 2 ) & 4 ) << 1;
469
470 for( unsigned int i = biL; i >= 8; i /= 2 )
471 x *= ( 2 - ( N[0] * x ) );
472
473 return( ~x + 1 );
474}
475
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100476void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
477 const mbedtls_mpi_uint *A,
478 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100479 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100480 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100481 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100482 mbedtls_mpi_uint mm,
483 mbedtls_mpi_uint *T )
484{
Tom Cosgrove72594632022-08-24 11:51:58 +0100485 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100486
Tom Cosgrove67c92472022-09-02 13:28:59 +0100487 for( size_t i = 0; i < AN_limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100488 {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100489 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100490 mbedtls_mpi_uint u0 = A[i];
491 mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100492
Tom Cosgrove72594632022-08-24 11:51:58 +0100493 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
494 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove67c92472022-09-02 13:28:59 +0100495
496 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100497 }
498
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100499 /*
500 * The result we want is (T >= N) ? T - N : T.
501 *
502 * For better constant-time properties in this function, we always do the
503 * subtraction, with the result in X.
504 *
505 * We also look to see if there was any carry in the final additions in the
506 * loop above.
507 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100508
Tom Cosgrove72594632022-08-24 11:51:58 +0100509 mbedtls_mpi_uint carry = T[AN_limbs];
510 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100511
512 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100513 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100514 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100515 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100516 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100517 * 1) T < N : (carry, borrow) = (0, 1): we want T
518 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
519 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100520 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100521 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100522 *
523 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100524 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100525 */
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100526 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100527}
528
Minos Galanakisa081c512022-10-24 12:16:28 +0100529int mbedtls_mpi_core_get_mont_r2_unsafe( mbedtls_mpi *X,
Minos Galanakis51d638b2022-10-24 09:59:44 +0100530 const mbedtls_mpi *N )
Hanno Beckerec440f22022-08-11 17:29:32 +0100531{
532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
533
Hanno Beckerec440f22022-08-11 17:29:32 +0100534 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 1 ) );
535 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, N->n * 2 * biL ) );
536 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( X, X, N ) );
537 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( X, N->n ) );
538
539cleanup:
540 return( ret );
541}
542
Janos Follathe50f2f12022-10-26 15:14:33 +0100543void mbedtls_mpi_core_ct_uint_table_lookup( mbedtls_mpi_uint *dest,
Janos Follath8904a2d2022-10-31 15:32:28 +0000544 const mbedtls_mpi_uint *table,
545 size_t limbs,
546 size_t count,
547 size_t index )
Janos Follathe50f2f12022-10-26 15:14:33 +0100548{
Janos Follath8904a2d2022-10-31 15:32:28 +0000549 for( size_t i = 0; i < count; i++, table += limbs )
Janos Follathe50f2f12022-10-26 15:14:33 +0100550 {
551 unsigned char assign = mbedtls_ct_size_bool_eq( i, index );
Janos Follath8904a2d2022-10-31 15:32:28 +0000552 mbedtls_mpi_core_cond_assign( dest, table, limbs, assign );
Janos Follathe50f2f12022-10-26 15:14:33 +0100553 }
554}
555
Gilles Peskine009d1952022-09-09 21:00:00 +0200556/* Fill X with n_bytes random bytes.
557 * X must already have room for those bytes.
558 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200559 * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
560 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200561 */
562int mbedtls_mpi_core_fill_random(
563 mbedtls_mpi_uint *X, size_t X_limbs,
564 size_t n_bytes,
565 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
566{
567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
568 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
569 const size_t overhead = ( limbs * ciL ) - n_bytes;
570
571 if( X_limbs < limbs )
572 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
573
574 memset( X, 0, overhead );
575 memset( (unsigned char *) X + limbs * ciL, 0, ( X_limbs - limbs ) * ciL );
576 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X + overhead, n_bytes ) );
577 mbedtls_mpi_core_bigendian_to_host( X, limbs );
578
579cleanup:
580 return( ret );
581}
582
Janos Follath2a8bcf82022-11-02 10:47:30 +0000583/* BEGIN MERGE SLOT 1 */
584
Janos Follathb6673f02022-09-30 14:13:14 +0100585static size_t mpi_exp_mod_get_window_size( size_t Ebits )
586{
587 size_t wsize = ( Ebits > 671 ) ? 6 : ( Ebits > 239 ) ? 5 :
588 ( Ebits > 79 ) ? 4 : ( Ebits > 23 ) ? 3 : 1;
589
590#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
591 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
592 wsize = MBEDTLS_MPI_WINDOW_SIZE;
593#endif
594
595 return( wsize );
596}
597
598int mbedtls_mpi_core_exp_mod( mbedtls_mpi_uint *X,
599 mbedtls_mpi_uint const *A,
600 const mbedtls_mpi_uint *N,
601 size_t n,
602 const mbedtls_mpi_uint *E,
603 size_t E_len,
604 const mbedtls_mpi_uint *RR )
605{
606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
607 /* heap allocated memory pool */
608 mbedtls_mpi_uint *mempool = NULL;
609 /* pointers to temporaries within memory pool */
610 mbedtls_mpi_uint *Wtbl, *Wselect, *temp;
611 /* pointers to table entries */
612 mbedtls_mpi_uint *Wcur, *Wlast, *W1;
613
614 size_t wsize, welem;
615 mbedtls_mpi_uint one = 1, mm;
616
617 mm = mbedtls_mpi_core_montmul_init( N ); /* Compute Montgomery constant */
618 E += E_len; /* Skip to end of exponent buffer */
619
620 wsize = mpi_exp_mod_get_window_size( E_len * biL );
Janos Follathbad42c42022-11-09 14:30:44 +0000621 welem = ( (size_t) 1 ) << wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100622
623 /* Allocate memory pool and set pointers to parts of it */
624 const size_t table_limbs = welem * n;
625 const size_t temp_limbs = 2 * n + 1;
626 const size_t wselect_limbs = n;
627 const size_t total_limbs = table_limbs + temp_limbs + wselect_limbs;
628
629 mempool = mbedtls_calloc( total_limbs, sizeof(mbedtls_mpi_uint) );
630 if( mempool == NULL )
631 {
632 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
633 goto cleanup;
634 }
635
636 Wtbl = mempool;
637 Wselect = Wtbl + table_limbs;
638 temp = Wselect + wselect_limbs;
639
640 /*
641 * Window precomputation
642 */
643
644 /* W[0] = 1 (in Montgomery presentation) */
645 memset( Wtbl, 0, n * ciL ); Wtbl[0] = 1;
646 mbedtls_mpi_core_montmul( Wtbl, Wtbl, RR, n, N, n, mm, temp );
647 Wcur = Wtbl + n;
648 /* W[1] = A * R^2 * R^-1 mod N = A * R mod N */
649 memcpy( Wcur, A, n * ciL );
650 mbedtls_mpi_core_montmul( Wcur, Wcur, RR, n, N, n, mm, temp );
651 W1 = Wcur;
652 Wcur += n;
653 /* W[i+1] = W[i] * W[1], i >= 2 */
654 Wlast = W1;
655 for( size_t i=2; i < welem; i++, Wlast += n, Wcur += n )
656 mbedtls_mpi_core_montmul( Wcur, Wlast, W1, n, N, n, mm, temp );
657
658 /*
659 * Sliding window exponentiation
660 */
661
662 /* X = 1 (in Montgomery presentation) initially */
663 memcpy( X, Wtbl, n * ciL );
664
665 size_t limb_bits_remaining = 0;
Janos Follathbad42c42022-11-09 14:30:44 +0000666 mbedtls_mpi_uint cur_limb, window = 0;
667 size_t window_bits = 0;
Janos Follathb6673f02022-09-30 14:13:14 +0100668 while( 1 )
669 {
670 size_t window_bits_missing = wsize - window_bits;
671
672 const int no_more_bits =
673 ( limb_bits_remaining == 0 ) && ( E_len == 0 );
674 const int window_full =
675 ( window_bits_missing == 0 );
676
677 /* Clear window if it's full or if we don't have further bits. */
678 if( window_full || no_more_bits )
679 {
680 if( window_bits == 0 )
681 break;
682 /* Select table entry, square and multiply */
683 mbedtls_mpi_core_ct_uint_table_lookup( Wselect, Wtbl,
684 n, welem, window );
685 mbedtls_mpi_core_montmul( X, X, Wselect, n, N, n, mm, temp );
686 window = window_bits = 0;
687 continue;
688 }
689
690 /* Load next exponent limb if necessary */
691 if( limb_bits_remaining == 0 )
692 {
693 cur_limb = *--E;
694 E_len--;
695 limb_bits_remaining = biL;
696 }
697
698 /* Square */
699 mbedtls_mpi_core_montmul( X, X, X, n, N, n, mm, temp );
700
701 /* Insert next exponent bit into window */
702 window <<= 1;
703 window |= ( cur_limb >> ( biL - 1 ) );
704 cur_limb <<= 1;
705 window_bits++;
706 limb_bits_remaining--;
707 }
708
709 /* Convert X back to normal presentation */
710 mbedtls_mpi_core_montmul( X, X, &one, 1, N, n, mm, temp );
711
712 ret = 0;
713
714cleanup:
715
716 mbedtls_free( mempool );
717 return( ret );
718}
719
Janos Follath2a8bcf82022-11-02 10:47:30 +0000720/* END MERGE SLOT 1 */
721
722/* BEGIN MERGE SLOT 2 */
723
724/* END MERGE SLOT 2 */
725
726/* BEGIN MERGE SLOT 3 */
727
728/* END MERGE SLOT 3 */
729
730/* BEGIN MERGE SLOT 4 */
731
732/* END MERGE SLOT 4 */
733
734/* BEGIN MERGE SLOT 5 */
735
736/* END MERGE SLOT 5 */
737
738/* BEGIN MERGE SLOT 6 */
739
740/* END MERGE SLOT 6 */
741
742/* BEGIN MERGE SLOT 7 */
743
744/* END MERGE SLOT 7 */
745
746/* BEGIN MERGE SLOT 8 */
747
748/* END MERGE SLOT 8 */
749
750/* BEGIN MERGE SLOT 9 */
751
752/* END MERGE SLOT 9 */
753
754/* BEGIN MERGE SLOT 10 */
755
756/* END MERGE SLOT 10 */
757
Janos Follath3ca07752022-08-09 11:45:47 +0100758#endif /* MBEDTLS_BIGNUM_C */