blob: bda62f22f05be02ee4bd54fef0df8954f03f6e67 [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
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200137/* Whether min <= A, in constant time.
138 * A_limbs must be at least 1. */
139unsigned mbedtls_mpi_core_uint_le_mpi( mbedtls_mpi_uint min,
140 const mbedtls_mpi_uint *A,
141 size_t A_limbs )
142{
143 /* min <= least significant limb? */
144 unsigned min_le_lsl = 1 ^ mbedtls_ct_mpi_uint_lt( A[0], min );
145
Gilles Peskine6b7ce962022-12-15 15:04:33 +0100146 /* limbs other than the least significant one are all zero? */
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200147 mbedtls_mpi_uint msll_mask = 0;
148 for( size_t i = 1; i < A_limbs; i++ )
149 msll_mask |= A[i];
150 /* The most significant limbs of A are not all zero iff msll_mask != 0. */
151 unsigned msll_nonzero = mbedtls_ct_mpi_uint_mask( msll_mask ) & 1;
152
153 /* min <= A iff the lowest limb of A is >= min or the other limbs
154 * are not all zero. */
155 return( min_le_lsl | msll_nonzero );
156}
157
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200158void mbedtls_mpi_core_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei1c628d52022-09-27 12:13:51 +0200159 const mbedtls_mpi_uint *A,
Gabor Mezei3eff4252022-09-26 17:26:42 +0200160 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200161 unsigned char assign )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200162{
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200163 if( X == A )
164 return;
165
Gabor Mezei1c628d52022-09-27 12:13:51 +0200166 mbedtls_ct_mpi_uint_cond_assign( limbs, X, A, assign );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200167}
168
Gabor Mezeie5b85852022-09-30 13:54:02 +0200169void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X,
170 mbedtls_mpi_uint *Y,
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200171 size_t limbs,
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200172 unsigned char swap )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200173{
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200174 if( X == Y )
175 return;
176
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200177 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
178 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200179
Gabor Mezeicfc0eb82022-09-15 20:15:34 +0200180 for( size_t i = 0; i < limbs; i++ )
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200181 {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200182 mbedtls_mpi_uint tmp = X[i];
183 X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask );
184 Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200185 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200186}
187
Janos Follath3ca07752022-08-09 11:45:47 +0100188int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100189 size_t X_limbs,
190 const unsigned char *input,
191 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100192{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100193 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100194
Janos Follathb7a88ec2022-08-19 12:24:40 +0100195 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100196 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
197
198 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200199 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100200 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100201
Janos Follathb7a88ec2022-08-19 12:24:40 +0100202 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100203 {
204 size_t offset = ( ( i % ciL ) << 3 );
205 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
206 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200207 }
Janos Follath3ca07752022-08-09 11:45:47 +0100208
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100209 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100210}
211
Janos Follath3ca07752022-08-09 11:45:47 +0100212int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100213 size_t X_limbs,
214 const unsigned char *input,
215 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100216{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100217 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100218
Janos Follathb7a88ec2022-08-19 12:24:40 +0100219 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100220 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
221
Janos Follathb7a88ec2022-08-19 12:24:40 +0100222 /* If X_limbs is 0, input_length must also be 0 (from previous test).
223 * Nothing to do. */
224 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200225 return( 0 );
226
Janos Follathb7a88ec2022-08-19 12:24:40 +0100227 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200228
229 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100230 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200231 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100232 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200233 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100234 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100235 }
236
Janos Follathb7a88ec2022-08-19 12:24:40 +0100237 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200238
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100239 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100240}
241
Janos Follathb7a88ec2022-08-19 12:24:40 +0100242int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
243 size_t A_limbs,
244 unsigned char *output,
245 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100246{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100247 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100248 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100249
Janos Follathb7a88ec2022-08-19 12:24:40 +0100250 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100251 {
252 bytes_to_copy = stored_bytes;
253 }
254 else
255 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100256 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100257
Janos Follathaf3f39c2022-08-22 09:06:32 +0100258 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100259 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100260 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100261 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100262 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100263 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
264 }
265 }
266
Janos Follathcc939082022-08-15 12:08:49 +0100267 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100268 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100269
Janos Follathb7a88ec2022-08-19 12:24:40 +0100270 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100271 {
272 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100273 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100274 }
275
276 return( 0 );
277}
278
Janos Follath3ca07752022-08-09 11:45:47 +0100279int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100280 size_t X_limbs,
281 unsigned char *output,
282 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100283{
284 size_t stored_bytes;
285 size_t bytes_to_copy;
286 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100287
Janos Follathb7a88ec2022-08-19 12:24:40 +0100288 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100289
Janos Follathb7a88ec2022-08-19 12:24:40 +0100290 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100291 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100292 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100293 * null bytes and record the position at which to start
294 * writing the significant bytes. In this case, the execution
295 * trace of this function does not depend on the value of the
296 * number. */
297 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100298 p = output + output_length - stored_bytes;
299 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100300 }
301 else
302 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100303 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100304 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100305 bytes_to_copy = output_length;
306 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100307 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100308 {
309 if( GET_BYTE( X, i ) != 0 )
310 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
311 }
312 }
313
Janos Follathcc939082022-08-15 12:08:49 +0100314 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100315 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
316
317 return( 0 );
318}
319
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200320void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs,
321 size_t count )
322{
323 size_t i, v0, v1;
324 mbedtls_mpi_uint r0 = 0, r1;
325
326 v0 = count / biL;
327 v1 = count & (biL - 1);
328
329 if( v0 > limbs || ( v0 == limbs && v1 > 0 ) )
330 {
331 memset( X, 0, limbs * ciL );
332 return;
333 }
334
335 /*
336 * shift by count / limb_size
337 */
338 if( v0 > 0 )
339 {
340 for( i = 0; i < limbs - v0; i++ )
341 X[i] = X[i + v0];
342
343 for( ; i < limbs; i++ )
344 X[i] = 0;
345 }
346
347 /*
348 * shift by count % limb_size
349 */
350 if( v1 > 0 )
351 {
352 for( i = limbs; i > 0; i-- )
353 {
354 r1 = X[i - 1] << (biL - v1);
355 X[i - 1] >>= v1;
356 X[i - 1] |= r0;
357 r0 = r1;
358 }
359 }
360}
361
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100362mbedtls_mpi_uint mbedtls_mpi_core_add( mbedtls_mpi_uint *X,
363 const mbedtls_mpi_uint *A,
364 const mbedtls_mpi_uint *B,
365 size_t limbs )
Hanno Beckerc9887132022-08-24 12:54:36 +0100366{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100367 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200368
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100369 for( size_t i = 0; i < limbs; i++ )
Hanno Beckerc9887132022-08-24 12:54:36 +0100370 {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100371 mbedtls_mpi_uint t = c + A[i];
372 c = ( t < A[i] );
373 t += B[i];
374 c += ( t < B[i] );
375 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100376 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100377
Hanno Beckerc9887132022-08-24 12:54:36 +0100378 return( c );
379}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200380
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100381mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
382 const mbedtls_mpi_uint *A,
Tom Cosgroveb4964862022-08-30 11:57:22 +0100383 size_t limbs,
384 unsigned cond )
385{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100386 mbedtls_mpi_uint c = 0;
387
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100388 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
389 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
Tom Cosgrove93549902022-08-30 17:41:23 +0100390
Tom Cosgroveb4964862022-08-30 11:57:22 +0100391 for( size_t i = 0; i < limbs; i++ )
392 {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100393 mbedtls_mpi_uint add = mask & A[i];
394 mbedtls_mpi_uint t = c + X[i];
395 c = ( t < X[i] );
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100396 t += add;
397 c += ( t < add );
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100398 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100399 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100400
Tom Cosgroveb4964862022-08-30 11:57:22 +0100401 return( c );
402}
403
404mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
405 const mbedtls_mpi_uint *A,
406 const mbedtls_mpi_uint *B,
407 size_t limbs )
408{
409 mbedtls_mpi_uint c = 0;
410
411 for( size_t i = 0; i < limbs; i++ )
412 {
413 mbedtls_mpi_uint z = ( A[i] < c );
414 mbedtls_mpi_uint t = A[i] - c;
415 c = ( t < B[i] ) + z;
416 X[i] = t - B[i];
417 }
418
419 return( c );
420}
421
422mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
423 const mbedtls_mpi_uint *s, size_t s_len,
424 mbedtls_mpi_uint b )
425{
426 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100427 /*
428 * It is a documented precondition of this function that d_len >= s_len.
429 * If that's not the case, we swap these round: this turns what would be
430 * a buffer overflow into an incorrect result.
431 */
Tom Cosgroveb4964862022-08-30 11:57:22 +0100432 if( d_len < s_len )
433 s_len = d_len;
434 size_t excess_len = d_len - s_len;
435 size_t steps_x8 = s_len / 8;
436 size_t steps_x1 = s_len & 7;
437
438 while( steps_x8-- )
439 {
440 MULADDC_X8_INIT
441 MULADDC_X8_CORE
442 MULADDC_X8_STOP
443 }
444
445 while( steps_x1-- )
446 {
447 MULADDC_X1_INIT
448 MULADDC_X1_CORE
449 MULADDC_X1_STOP
450 }
451
452 while( excess_len-- )
453 {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100454 *d += c;
455 c = ( *d < c );
456 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100457 }
458
459 return( c );
460}
461
462/*
463 * Fast Montgomery initialization (thanks to Tom St Denis).
464 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100465mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
Tom Cosgroveb4964862022-08-30 11:57:22 +0100466{
467 mbedtls_mpi_uint x = N[0];
468
469 x += ( ( N[0] + 2 ) & 4 ) << 1;
470
471 for( unsigned int i = biL; i >= 8; i /= 2 )
472 x *= ( 2 - ( N[0] * x ) );
473
474 return( ~x + 1 );
475}
476
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100477void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
478 const mbedtls_mpi_uint *A,
479 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100480 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100481 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100482 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100483 mbedtls_mpi_uint mm,
484 mbedtls_mpi_uint *T )
485{
Tom Cosgrove72594632022-08-24 11:51:58 +0100486 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100487
Tom Cosgrove67c92472022-09-02 13:28:59 +0100488 for( size_t i = 0; i < AN_limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100489 {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100490 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100491 mbedtls_mpi_uint u0 = A[i];
492 mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100493
Tom Cosgrove72594632022-08-24 11:51:58 +0100494 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
495 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove67c92472022-09-02 13:28:59 +0100496
497 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100498 }
499
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100500 /*
501 * The result we want is (T >= N) ? T - N : T.
502 *
503 * For better constant-time properties in this function, we always do the
504 * subtraction, with the result in X.
505 *
506 * We also look to see if there was any carry in the final additions in the
507 * loop above.
508 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100509
Tom Cosgrove72594632022-08-24 11:51:58 +0100510 mbedtls_mpi_uint carry = T[AN_limbs];
511 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100512
513 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100514 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100515 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100516 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100517 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100518 * 1) T < N : (carry, borrow) = (0, 1): we want T
519 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
520 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100521 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100522 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100523 *
524 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100525 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100526 */
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100527 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100528}
529
Minos Galanakisa081c512022-10-24 12:16:28 +0100530int mbedtls_mpi_core_get_mont_r2_unsafe( mbedtls_mpi *X,
Minos Galanakis51d638b2022-10-24 09:59:44 +0100531 const mbedtls_mpi *N )
Hanno Beckerec440f22022-08-11 17:29:32 +0100532{
533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
534
Hanno Beckerec440f22022-08-11 17:29:32 +0100535 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 1 ) );
536 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, N->n * 2 * biL ) );
537 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( X, X, N ) );
538 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( X, N->n ) );
539
540cleanup:
541 return( ret );
542}
543
Janos Follath59cbd1b2022-10-28 18:13:43 +0100544MBEDTLS_STATIC_TESTABLE
Janos Follathe50f2f12022-10-26 15:14:33 +0100545void mbedtls_mpi_core_ct_uint_table_lookup( mbedtls_mpi_uint *dest,
Janos Follath8904a2d2022-10-31 15:32:28 +0000546 const mbedtls_mpi_uint *table,
547 size_t limbs,
548 size_t count,
549 size_t index )
Janos Follathe50f2f12022-10-26 15:14:33 +0100550{
Janos Follath8904a2d2022-10-31 15:32:28 +0000551 for( size_t i = 0; i < count; i++, table += limbs )
Janos Follathe50f2f12022-10-26 15:14:33 +0100552 {
553 unsigned char assign = mbedtls_ct_size_bool_eq( i, index );
Janos Follath8904a2d2022-10-31 15:32:28 +0000554 mbedtls_mpi_core_cond_assign( dest, table, limbs, assign );
Janos Follathe50f2f12022-10-26 15:14:33 +0100555 }
556}
557
Gilles Peskine009d1952022-09-09 21:00:00 +0200558/* Fill X with n_bytes random bytes.
559 * X must already have room for those bytes.
560 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200561 * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
562 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200563 */
564int mbedtls_mpi_core_fill_random(
565 mbedtls_mpi_uint *X, size_t X_limbs,
566 size_t n_bytes,
567 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
568{
569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
570 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
571 const size_t overhead = ( limbs * ciL ) - n_bytes;
572
573 if( X_limbs < limbs )
574 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
575
576 memset( X, 0, overhead );
577 memset( (unsigned char *) X + limbs * ciL, 0, ( X_limbs - limbs ) * ciL );
578 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X + overhead, n_bytes ) );
579 mbedtls_mpi_core_bigendian_to_host( X, limbs );
580
581cleanup:
582 return( ret );
583}
584
Gilles Peskine70375b22022-09-21 15:47:23 +0200585int mbedtls_mpi_core_random( mbedtls_mpi_uint *X,
586 mbedtls_mpi_uint min,
587 const mbedtls_mpi_uint *N,
588 size_t limbs,
589 int (*f_rng)(void *, unsigned char *, size_t),
590 void *p_rng )
591{
592 unsigned ge_lower = 1, lt_upper = 0;
593 size_t n_bits = mbedtls_mpi_core_bitlen( N, limbs );
594 size_t n_bytes = ( n_bits + 7 ) / 8;
595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
596
597 /*
598 * When min == 0, each try has at worst a probability 1/2 of failing
599 * (the msb has a probability 1/2 of being 0, and then the result will
600 * be < N), so after 30 tries failure probability is a most 2**(-30).
601 *
602 * When N is just below a power of 2, as is the case when generating
603 * a random scalar on most elliptic curves, 1 try is enough with
604 * overwhelming probability. When N is just above a power of 2,
605 * as when generating a random scalar on secp224k1, each try has
606 * a probability of failing that is almost 1/2.
607 *
608 * The probabilities are almost the same if min is nonzero but negligible
609 * compared to N. This is always the case when N is crypto-sized, but
610 * it's convenient to support small N for testing purposes. When N
611 * is small, use a higher repeat count, otherwise the probability of
612 * failure is macroscopic.
613 */
614 int count = ( n_bytes > 4 ? 30 : 250 );
615
616 /*
617 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
618 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
619 * - use the same byte ordering;
620 * - keep the leftmost n_bits bits of the generated octet string;
621 * - try until result is in the desired range.
622 * This also avoids any bias, which is especially important for ECDSA.
623 */
624 do
625 {
626 MBEDTLS_MPI_CHK( mbedtls_mpi_core_fill_random( X, limbs,
627 n_bytes,
628 f_rng, p_rng ) );
629 mbedtls_mpi_core_shift_r( X, limbs, 8 * n_bytes - n_bits );
630
631 if( --count == 0 )
632 {
633 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
634 goto cleanup;
635 }
636
637 ge_lower = mbedtls_mpi_core_uint_le_mpi( min, X, limbs );
638 lt_upper = mbedtls_mpi_core_lt_ct( X, N, limbs );
639 }
640 while( ge_lower == 0 || lt_upper == 0 );
641
642cleanup:
643 return( ret );
644}
645
Janos Follath2a8bcf82022-11-02 10:47:30 +0000646/* BEGIN MERGE SLOT 1 */
647
Janos Follatha77911e2022-10-08 09:48:20 +0100648static size_t exp_mod_get_window_size( size_t Ebits )
Janos Follathb6673f02022-09-30 14:13:14 +0100649{
650 size_t wsize = ( Ebits > 671 ) ? 6 : ( Ebits > 239 ) ? 5 :
Janos Follatha77911e2022-10-08 09:48:20 +0100651 ( Ebits > 79 ) ? 4 : 1;
Janos Follathb6673f02022-09-30 14:13:14 +0100652
653#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
654 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
655 wsize = MBEDTLS_MPI_WINDOW_SIZE;
656#endif
657
658 return( wsize );
659}
660
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000661size_t mbedtls_mpi_core_exp_mod_working_limbs( size_t AN_limbs, size_t E_limbs )
662{
663 const size_t wsize = exp_mod_get_window_size( E_limbs * biL );
664 const size_t welem = ( (size_t) 1 ) << wsize;
665
666 /* How big does each part of the working memory pool need to be? */
667 const size_t table_limbs = welem * AN_limbs;
668 const size_t select_limbs = AN_limbs;
669 const size_t temp_limbs = 2 * AN_limbs + 1;
670
671 return( table_limbs + select_limbs + temp_limbs );
672}
673
Gilles Peskine0de0a042022-11-16 20:12:49 +0100674static void exp_mod_precompute_window( const mbedtls_mpi_uint *A,
675 const mbedtls_mpi_uint *N,
676 size_t AN_limbs,
677 mbedtls_mpi_uint mm,
678 const mbedtls_mpi_uint *RR,
679 size_t welem,
680 mbedtls_mpi_uint *Wtable,
681 mbedtls_mpi_uint *temp )
682{
Gilles Peskine0de0a042022-11-16 20:12:49 +0100683 /* W[0] = 1 (in Montgomery presentation) */
684 memset( Wtable, 0, AN_limbs * ciL );
685 Wtable[0] = 1;
686 mbedtls_mpi_core_montmul( Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp );
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100687
Tom Cosgroveecda1862022-12-06 10:46:30 +0000688 /* W[1] = A (already in Montgomery presentation) */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100689 mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
Tom Cosgroveecda1862022-12-06 10:46:30 +0000690 memcpy( W1, A, AN_limbs * ciL );
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100691
Gilles Peskine0de0a042022-11-16 20:12:49 +0100692 /* W[i+1] = W[i] * W[1], i >= 2 */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100693 mbedtls_mpi_uint *Wprev = W1;
694 for( size_t i = 2; i < welem; i++ )
695 {
696 mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
697 mbedtls_mpi_core_montmul( Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp );
698 Wprev = Wcur;
699 }
Gilles Peskine0de0a042022-11-16 20:12:49 +0100700}
701
Gilles Peskine7d89d352022-11-16 22:54:14 +0100702/* Exponentiation: X := A^E mod N.
703 *
Tom Cosgroveecda1862022-12-06 10:46:30 +0000704 * A must already be in Montgomery form.
705 *
Gilles Peskine7d89d352022-11-16 22:54:14 +0100706 * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
707 *
708 * RR must contain 2^{2*biL} mod N.
Janos Follath3321b582022-11-22 21:08:33 +0000709 *
710 * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
711 * (The difference is that the body in our loop processes a single bit instead
712 * of a full window.)
Gilles Peskine7d89d352022-11-16 22:54:14 +0100713 */
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000714void mbedtls_mpi_core_exp_mod( mbedtls_mpi_uint *X,
715 const mbedtls_mpi_uint *A,
716 const mbedtls_mpi_uint *N,
717 size_t AN_limbs,
718 const mbedtls_mpi_uint *E,
719 size_t E_limbs,
720 const mbedtls_mpi_uint *RR,
721 mbedtls_mpi_uint *T )
Janos Follathb6673f02022-09-30 14:13:14 +0100722{
Gilles Peskinecf979b02022-11-16 20:04:00 +0100723 const size_t wsize = exp_mod_get_window_size( E_limbs * biL );
724 const size_t welem = ( (size_t) 1 ) << wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100725
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000726 /* This is how we will use the temporary storage T, which must have space
727 * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
728 const size_t table_limbs = welem * AN_limbs;
729 const size_t select_limbs = AN_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100730
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000731 /* Pointers to specific parts of the temporary working memory pool */
732 mbedtls_mpi_uint *const Wtable = T;
733 mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
734 mbedtls_mpi_uint *const temp = Wselect + select_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100735
736 /*
737 * Window precomputation
738 */
739
Gilles Peskinecf979b02022-11-16 20:04:00 +0100740 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init( N );
741
Gilles Peskine0de0a042022-11-16 20:12:49 +0100742 /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
743 exp_mod_precompute_window( A, N, AN_limbs,
744 mm, RR,
745 welem, Wtable, temp );
Janos Follathb6673f02022-09-30 14:13:14 +0100746
747 /*
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000748 * Fixed window exponentiation
Janos Follathb6673f02022-09-30 14:13:14 +0100749 */
750
751 /* X = 1 (in Montgomery presentation) initially */
Gilles Peskine07f2c692022-11-16 19:48:23 +0100752 memcpy( X, Wtable, AN_limbs * ciL );
Janos Follathb6673f02022-09-30 14:13:14 +0100753
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100754 /* We'll process the bits of E from most significant
755 * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
756 * (limb_index=0, E_bit_index=0). */
757 size_t E_limb_index = E_limbs;
758 size_t E_bit_index = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100759 /* At any given time, window contains window_bits bits from E.
760 * window_bits can go up to wsize. */
Janos Follathbad42c42022-11-09 14:30:44 +0000761 size_t window_bits = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100762 mbedtls_mpi_uint window = 0;
Gilles Peskinecf979b02022-11-16 20:04:00 +0100763
Gilles Peskine3b63d092022-11-16 22:06:18 +0100764 do
Janos Follathb6673f02022-09-30 14:13:14 +0100765 {
Janos Follathb6673f02022-09-30 14:13:14 +0100766 /* Square */
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000767 mbedtls_mpi_core_montmul( X, X, X, AN_limbs, N, AN_limbs, mm, temp );
Janos Follathb6673f02022-09-30 14:13:14 +0100768
Janos Follath3321b582022-11-22 21:08:33 +0000769 /* Move to the next bit of the exponent */
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100770 if( E_bit_index == 0 )
771 {
772 --E_limb_index;
773 E_bit_index = biL - 1;
774 }
775 else
776 {
777 --E_bit_index;
778 }
Janos Follath3321b582022-11-22 21:08:33 +0000779 /* Insert next exponent bit into window */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100780 ++window_bits;
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100781 window <<= 1;
782 window |= ( E[E_limb_index] >> E_bit_index ) & 1;
Gilles Peskine3b63d092022-11-16 22:06:18 +0100783
784 /* Clear window if it's full. Also clear the window at the end,
785 * when we've finished processing the exponent. */
786 if( window_bits == wsize ||
787 ( E_bit_index == 0 && E_limb_index == 0 ) )
788 {
Gilles Peskine0b270a52022-11-16 22:54:03 +0100789 /* Select Wtable[window] without leaking window through
790 * memory access patterns. */
Gilles Peskine3b63d092022-11-16 22:06:18 +0100791 mbedtls_mpi_core_ct_uint_table_lookup( Wselect, Wtable,
792 AN_limbs, welem, window );
Gilles Peskine0b270a52022-11-16 22:54:03 +0100793 /* Multiply X by the selected element. */
Janos Follath3321b582022-11-22 21:08:33 +0000794 mbedtls_mpi_core_montmul( X, X, Wselect, AN_limbs, N, AN_limbs, mm,
795 temp );
Gilles Peskine3b63d092022-11-16 22:06:18 +0100796 window = 0;
797 window_bits = 0;
798 }
Janos Follathb6673f02022-09-30 14:13:14 +0100799 }
Gilles Peskine3b63d092022-11-16 22:06:18 +0100800 while( ! ( E_bit_index == 0 && E_limb_index == 0 ) );
Janos Follathb6673f02022-09-30 14:13:14 +0100801}
802
Janos Follath2a8bcf82022-11-02 10:47:30 +0000803/* END MERGE SLOT 1 */
804
805/* BEGIN MERGE SLOT 2 */
806
807/* END MERGE SLOT 2 */
808
809/* BEGIN MERGE SLOT 3 */
810
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100811mbedtls_mpi_uint mbedtls_mpi_core_sub_int( mbedtls_mpi_uint *X,
812 const mbedtls_mpi_uint *A,
813 mbedtls_mpi_uint c, /* doubles as carry */
814 size_t limbs )
Hanno Beckerd9b23482022-08-25 08:25:19 +0100815{
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100816 for( size_t i = 0; i < limbs; i++ )
Hanno Beckerd9b23482022-08-25 08:25:19 +0100817 {
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100818 mbedtls_mpi_uint s = A[i];
819 mbedtls_mpi_uint t = s - c;
820 c = ( t > s );
821 X[i] = t;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100822 }
823
824 return( c );
825}
826
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000827mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct( const mbedtls_mpi_uint *A,
828 size_t limbs )
829{
830 mbedtls_mpi_uint bits = 0;
831
832 for( size_t i = 0; i < limbs; i++ )
833 bits |= A[i];
834
835 return( bits );
836}
837
Tom Cosgrove786848b2022-12-13 10:45:19 +0000838void mbedtls_mpi_core_to_mont_rep( mbedtls_mpi_uint *X,
839 const mbedtls_mpi_uint *A,
840 const mbedtls_mpi_uint *N,
841 size_t AN_limbs,
842 mbedtls_mpi_uint mm,
843 const mbedtls_mpi_uint *rr,
844 mbedtls_mpi_uint *T )
845{
846 mbedtls_mpi_core_montmul( X, A, rr, AN_limbs, N, AN_limbs, mm, T );
847}
848
849void mbedtls_mpi_core_from_mont_rep( mbedtls_mpi_uint *X,
850 const mbedtls_mpi_uint *A,
851 const mbedtls_mpi_uint *N,
852 size_t AN_limbs,
853 mbedtls_mpi_uint mm,
854 mbedtls_mpi_uint *T )
855{
856 const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
857
858 mbedtls_mpi_core_montmul( X, A, &Rinv, 1, N, AN_limbs, mm, T );
859}
860
Janos Follath2a8bcf82022-11-02 10:47:30 +0000861/* END MERGE SLOT 3 */
862
863/* BEGIN MERGE SLOT 4 */
864
865/* END MERGE SLOT 4 */
866
867/* BEGIN MERGE SLOT 5 */
868
869/* END MERGE SLOT 5 */
870
871/* BEGIN MERGE SLOT 6 */
872
873/* END MERGE SLOT 6 */
874
875/* BEGIN MERGE SLOT 7 */
876
877/* END MERGE SLOT 7 */
878
879/* BEGIN MERGE SLOT 8 */
880
881/* END MERGE SLOT 8 */
882
883/* BEGIN MERGE SLOT 9 */
884
885/* END MERGE SLOT 9 */
886
887/* BEGIN MERGE SLOT 10 */
888
889/* END MERGE SLOT 10 */
890
Janos Follath3ca07752022-08-09 11:45:47 +0100891#endif /* MBEDTLS_BIGNUM_C */