blob: e3451aeda9bd3a2895468b3ecff145902dfa12ec [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{
Dave Rodgman6d23ff62022-11-28 14:38:53 +000086 if ( MBEDTLS_IS_BIG_ENDIAN )
Janos Follath3ca07752022-08-09 11:45:47 +010087 {
Dave Rodgman6d23ff62022-11-28 14:38:53 +000088 /* Nothing to do on bigendian systems. */
89 return( a );
Janos Follath3ca07752022-08-09 11:45:47 +010090 }
Dave Rodgman6d23ff62022-11-28 14:38:53 +000091 else
92 {
93 switch( sizeof(mbedtls_mpi_uint) )
94 {
95 case 4:
96 return (mbedtls_mpi_uint) MBEDTLS_BSWAP32( (uint32_t)a );
97 case 8:
98 return (mbedtls_mpi_uint) MBEDTLS_BSWAP64( (uint64_t)a );
99 }
Janos Follath3ca07752022-08-09 11:45:47 +0100100
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000101 /* Fall back to C-based reordering if we don't know the byte order
102 * or we couldn't use a compiler-specific builtin. */
103 return( mpi_bigendian_to_host_c( a ) );
104 }
Janos Follath3ca07752022-08-09 11:45:47 +0100105}
106
Janos Follathb7a88ec2022-08-19 12:24:40 +0100107void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +0100108 size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +0100109{
110 mbedtls_mpi_uint *cur_limb_left;
111 mbedtls_mpi_uint *cur_limb_right;
Janos Follathc4596412022-08-22 10:01:27 +0100112 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100113 return;
114
115 /*
116 * Traverse limbs and
117 * - adapt byte-order in each limb
118 * - swap the limbs themselves.
119 * For that, simultaneously traverse the limbs from left to right
120 * and from right to left, as long as the left index is not bigger
121 * than the right index (it's not a problem if limbs is odd and the
122 * indices coincide in the last iteration).
123 */
Janos Follathc4596412022-08-22 10:01:27 +0100124 for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 );
Janos Follath3ca07752022-08-09 11:45:47 +0100125 cur_limb_left <= cur_limb_right;
126 cur_limb_left++, cur_limb_right-- )
127 {
128 mbedtls_mpi_uint tmp;
129 /* Note that if cur_limb_left == cur_limb_right,
130 * this code effectively swaps the bytes only once. */
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100131 tmp = mpi_bigendian_to_host( *cur_limb_left );
Janos Follath3ca07752022-08-09 11:45:47 +0100132 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
133 *cur_limb_right = tmp;
134 }
135}
136
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200137void mbedtls_mpi_core_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei1c628d52022-09-27 12:13:51 +0200138 const mbedtls_mpi_uint *A,
Gabor Mezei3eff4252022-09-26 17:26:42 +0200139 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200140 unsigned char assign )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200141{
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200142 if( X == A )
143 return;
144
Gabor Mezei1c628d52022-09-27 12:13:51 +0200145 mbedtls_ct_mpi_uint_cond_assign( limbs, X, A, assign );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200146}
147
Gabor Mezeie5b85852022-09-30 13:54:02 +0200148void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X,
149 mbedtls_mpi_uint *Y,
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200150 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200151 unsigned char swap )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200152{
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200153 if( X == Y )
154 return;
155
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200156 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
157 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200158
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200159 for( size_t i = 0; i < limbs; i++ )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200160 {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200161 mbedtls_mpi_uint tmp = X[i];
162 X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask );
163 Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200164 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200165}
166
Janos Follath3ca07752022-08-09 11:45:47 +0100167int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100168 size_t X_limbs,
169 const unsigned char *input,
170 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100171{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100172 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100173
Janos Follathb7a88ec2022-08-19 12:24:40 +0100174 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100175 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
176
177 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200178 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100179 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100180
Janos Follathb7a88ec2022-08-19 12:24:40 +0100181 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100182 {
183 size_t offset = ( ( i % ciL ) << 3 );
184 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
185 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200186 }
Janos Follath3ca07752022-08-09 11:45:47 +0100187
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100188 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100189}
190
Janos Follath3ca07752022-08-09 11:45:47 +0100191int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100192 size_t X_limbs,
193 const unsigned char *input,
194 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100195{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100196 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100197
Janos Follathb7a88ec2022-08-19 12:24:40 +0100198 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100199 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
200
Janos Follathb7a88ec2022-08-19 12:24:40 +0100201 /* If X_limbs is 0, input_length must also be 0 (from previous test).
202 * Nothing to do. */
203 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200204 return( 0 );
205
Janos Follathb7a88ec2022-08-19 12:24:40 +0100206 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200207
208 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100209 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200210 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100211 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200212 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100213 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100214 }
215
Janos Follathb7a88ec2022-08-19 12:24:40 +0100216 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200217
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100218 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100219}
220
Janos Follathb7a88ec2022-08-19 12:24:40 +0100221int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
222 size_t A_limbs,
223 unsigned char *output,
224 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100225{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100226 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100227 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100228
Janos Follathb7a88ec2022-08-19 12:24:40 +0100229 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100230 {
231 bytes_to_copy = stored_bytes;
232 }
233 else
234 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100235 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100236
Janos Follathaf3f39c2022-08-22 09:06:32 +0100237 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100238 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100239 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100240 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100241 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100242 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
243 }
244 }
245
Janos Follathcc939082022-08-15 12:08:49 +0100246 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100247 output[i] = GET_BYTE( A, i );
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 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100252 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100253 }
254
255 return( 0 );
256}
257
Janos Follath3ca07752022-08-09 11:45:47 +0100258int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100259 size_t X_limbs,
260 unsigned char *output,
261 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100262{
263 size_t stored_bytes;
264 size_t bytes_to_copy;
265 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100266
Janos Follathb7a88ec2022-08-19 12:24:40 +0100267 stored_bytes = X_limbs * ciL;
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 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100271 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100272 * null bytes and record the position at which to start
273 * writing the significant bytes. In this case, the execution
274 * trace of this function does not depend on the value of the
275 * number. */
276 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100277 p = output + output_length - stored_bytes;
278 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100279 }
280 else
281 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100282 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100283 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100284 bytes_to_copy = output_length;
285 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100286 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100287 {
288 if( GET_BYTE( X, i ) != 0 )
289 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
290 }
291 }
292
Janos Follathcc939082022-08-15 12:08:49 +0100293 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100294 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
295
296 return( 0 );
297}
298
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200299void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs,
300 size_t count )
301{
302 size_t i, v0, v1;
303 mbedtls_mpi_uint r0 = 0, r1;
304
305 v0 = count / biL;
306 v1 = count & (biL - 1);
307
308 if( v0 > limbs || ( v0 == limbs && v1 > 0 ) )
309 {
310 memset( X, 0, limbs * ciL );
311 return;
312 }
313
314 /*
315 * shift by count / limb_size
316 */
317 if( v0 > 0 )
318 {
319 for( i = 0; i < limbs - v0; i++ )
320 X[i] = X[i + v0];
321
322 for( ; i < limbs; i++ )
323 X[i] = 0;
324 }
325
326 /*
327 * shift by count % limb_size
328 */
329 if( v1 > 0 )
330 {
331 for( i = limbs; i > 0; i-- )
332 {
333 r1 = X[i - 1] << (biL - v1);
334 X[i - 1] >>= v1;
335 X[i - 1] |= r0;
336 r0 = r1;
337 }
338 }
339}
340
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100341mbedtls_mpi_uint mbedtls_mpi_core_add( mbedtls_mpi_uint *X,
342 const mbedtls_mpi_uint *A,
343 const mbedtls_mpi_uint *B,
344 size_t limbs )
Hanno Beckerc9887132022-08-24 12:54:36 +0100345{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100346 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200347
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100348 for( size_t i = 0; i < limbs; i++ )
Hanno Beckerc9887132022-08-24 12:54:36 +0100349 {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100350 mbedtls_mpi_uint t = c + A[i];
351 c = ( t < A[i] );
352 t += B[i];
353 c += ( t < B[i] );
354 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100355 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100356
Hanno Beckerc9887132022-08-24 12:54:36 +0100357 return( c );
358}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200359
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100360mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
361 const mbedtls_mpi_uint *A,
Tom Cosgroveb4964862022-08-30 11:57:22 +0100362 size_t limbs,
363 unsigned cond )
364{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100365 mbedtls_mpi_uint c = 0;
366
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100367 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
368 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
Tom Cosgrove93549902022-08-30 17:41:23 +0100369
Tom Cosgroveb4964862022-08-30 11:57:22 +0100370 for( size_t i = 0; i < limbs; i++ )
371 {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100372 mbedtls_mpi_uint add = mask & A[i];
373 mbedtls_mpi_uint t = c + X[i];
374 c = ( t < X[i] );
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100375 t += add;
376 c += ( t < add );
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100377 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100378 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100379
Tom Cosgroveb4964862022-08-30 11:57:22 +0100380 return( c );
381}
382
383mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
384 const mbedtls_mpi_uint *A,
385 const mbedtls_mpi_uint *B,
386 size_t limbs )
387{
388 mbedtls_mpi_uint c = 0;
389
390 for( size_t i = 0; i < limbs; i++ )
391 {
392 mbedtls_mpi_uint z = ( A[i] < c );
393 mbedtls_mpi_uint t = A[i] - c;
394 c = ( t < B[i] ) + z;
395 X[i] = t - B[i];
396 }
397
398 return( c );
399}
400
401mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
402 const mbedtls_mpi_uint *s, size_t s_len,
403 mbedtls_mpi_uint b )
404{
405 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100406 /*
407 * It is a documented precondition of this function that d_len >= s_len.
408 * If that's not the case, we swap these round: this turns what would be
409 * a buffer overflow into an incorrect result.
410 */
Tom Cosgroveb4964862022-08-30 11:57:22 +0100411 if( d_len < s_len )
412 s_len = d_len;
413 size_t excess_len = d_len - s_len;
414 size_t steps_x8 = s_len / 8;
415 size_t steps_x1 = s_len & 7;
416
417 while( steps_x8-- )
418 {
419 MULADDC_X8_INIT
420 MULADDC_X8_CORE
421 MULADDC_X8_STOP
422 }
423
424 while( steps_x1-- )
425 {
426 MULADDC_X1_INIT
427 MULADDC_X1_CORE
428 MULADDC_X1_STOP
429 }
430
431 while( excess_len-- )
432 {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100433 *d += c;
434 c = ( *d < c );
435 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100436 }
437
438 return( c );
439}
440
441/*
442 * Fast Montgomery initialization (thanks to Tom St Denis).
443 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100444mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
Tom Cosgroveb4964862022-08-30 11:57:22 +0100445{
446 mbedtls_mpi_uint x = N[0];
447
448 x += ( ( N[0] + 2 ) & 4 ) << 1;
449
450 for( unsigned int i = biL; i >= 8; i /= 2 )
451 x *= ( 2 - ( N[0] * x ) );
452
453 return( ~x + 1 );
454}
455
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100456void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
457 const mbedtls_mpi_uint *A,
458 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100459 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100460 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100461 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100462 mbedtls_mpi_uint mm,
463 mbedtls_mpi_uint *T )
464{
Tom Cosgrove72594632022-08-24 11:51:58 +0100465 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100466
Tom Cosgrove67c92472022-09-02 13:28:59 +0100467 for( size_t i = 0; i < AN_limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100468 {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100469 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100470 mbedtls_mpi_uint u0 = A[i];
471 mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100472
Tom Cosgrove72594632022-08-24 11:51:58 +0100473 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
474 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove67c92472022-09-02 13:28:59 +0100475
476 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100477 }
478
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100479 /*
480 * The result we want is (T >= N) ? T - N : T.
481 *
482 * For better constant-time properties in this function, we always do the
483 * subtraction, with the result in X.
484 *
485 * We also look to see if there was any carry in the final additions in the
486 * loop above.
487 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100488
Tom Cosgrove72594632022-08-24 11:51:58 +0100489 mbedtls_mpi_uint carry = T[AN_limbs];
490 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100491
492 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100493 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100494 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100495 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100496 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100497 * 1) T < N : (carry, borrow) = (0, 1): we want T
498 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
499 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100500 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100501 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100502 *
503 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100504 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100505 */
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100506 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100507}
508
Minos Galanakisa081c512022-10-24 12:16:28 +0100509int mbedtls_mpi_core_get_mont_r2_unsafe( mbedtls_mpi *X,
Minos Galanakis51d638b2022-10-24 09:59:44 +0100510 const mbedtls_mpi *N )
Hanno Beckerec440f22022-08-11 17:29:32 +0100511{
512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
513
Hanno Beckerec440f22022-08-11 17:29:32 +0100514 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 1 ) );
515 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, N->n * 2 * biL ) );
516 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( X, X, N ) );
517 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( X, N->n ) );
518
519cleanup:
520 return( ret );
521}
522
Janos Follathe50f2f12022-10-26 15:14:33 +0100523void mbedtls_mpi_core_ct_uint_table_lookup( mbedtls_mpi_uint *dest,
Janos Follath8904a2d2022-10-31 15:32:28 +0000524 const mbedtls_mpi_uint *table,
525 size_t limbs,
526 size_t count,
527 size_t index )
Janos Follathe50f2f12022-10-26 15:14:33 +0100528{
Janos Follath8904a2d2022-10-31 15:32:28 +0000529 for( size_t i = 0; i < count; i++, table += limbs )
Janos Follathe50f2f12022-10-26 15:14:33 +0100530 {
531 unsigned char assign = mbedtls_ct_size_bool_eq( i, index );
Janos Follath8904a2d2022-10-31 15:32:28 +0000532 mbedtls_mpi_core_cond_assign( dest, table, limbs, assign );
Janos Follathe50f2f12022-10-26 15:14:33 +0100533 }
534}
535
Gilles Peskine009d1952022-09-09 21:00:00 +0200536/* Fill X with n_bytes random bytes.
537 * X must already have room for those bytes.
538 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200539 * deterministic ECDSA (see RFC 6979 ยง3.3 and the specification of
540 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200541 */
542int mbedtls_mpi_core_fill_random(
543 mbedtls_mpi_uint *X, size_t X_limbs,
544 size_t n_bytes,
545 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
546{
547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
548 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
549 const size_t overhead = ( limbs * ciL ) - n_bytes;
550
551 if( X_limbs < limbs )
552 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
553
554 memset( X, 0, overhead );
555 memset( (unsigned char *) X + limbs * ciL, 0, ( X_limbs - limbs ) * ciL );
556 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X + overhead, n_bytes ) );
557 mbedtls_mpi_core_bigendian_to_host( X, limbs );
558
559cleanup:
560 return( ret );
561}
562
Janos Follath2a8bcf82022-11-02 10:47:30 +0000563/* BEGIN MERGE SLOT 1 */
564
565/* END MERGE SLOT 1 */
566
567/* BEGIN MERGE SLOT 2 */
568
569/* END MERGE SLOT 2 */
570
571/* BEGIN MERGE SLOT 3 */
572
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100573mbedtls_mpi_uint mbedtls_mpi_core_sub_int( mbedtls_mpi_uint *X,
574 const mbedtls_mpi_uint *A,
575 mbedtls_mpi_uint c, /* doubles as carry */
576 size_t limbs )
Hanno Beckerd9b23482022-08-25 08:25:19 +0100577{
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100578 for( size_t i = 0; i < limbs; i++ )
Hanno Beckerd9b23482022-08-25 08:25:19 +0100579 {
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100580 mbedtls_mpi_uint s = A[i];
581 mbedtls_mpi_uint t = s - c;
582 c = ( t > s );
583 X[i] = t;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100584 }
585
586 return( c );
587}
588
Janos Follath2a8bcf82022-11-02 10:47:30 +0000589/* END MERGE SLOT 3 */
590
591/* BEGIN MERGE SLOT 4 */
592
593/* END MERGE SLOT 4 */
594
595/* BEGIN MERGE SLOT 5 */
596
597/* END MERGE SLOT 5 */
598
599/* BEGIN MERGE SLOT 6 */
600
601/* END MERGE SLOT 6 */
602
603/* BEGIN MERGE SLOT 7 */
604
605/* END MERGE SLOT 7 */
606
607/* BEGIN MERGE SLOT 8 */
608
609/* END MERGE SLOT 8 */
610
611/* BEGIN MERGE SLOT 9 */
612
613/* END MERGE SLOT 9 */
614
615/* BEGIN MERGE SLOT 10 */
616
617/* END MERGE SLOT 10 */
618
Janos Follath3ca07752022-08-09 11:45:47 +0100619#endif /* MBEDTLS_BIGNUM_C */