blob: 4e5012b79b56c948dc2219f423c6e3fb7c3f30d7 [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"
28
29#if defined(MBEDTLS_PLATFORM_C)
30#include "mbedtls/platform.h"
31#else
32#include <stdio.h>
33#include <stdlib.h>
34#define mbedtls_printf printf
35#define mbedtls_calloc calloc
36#define mbedtls_free free
37#endif
38
39#include "bignum_core.h"
Tom Cosgrove958fd3d2022-08-24 11:08:51 +010040#include "bn_mul.h"
41#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010042
Janos Follath2e328c82022-08-22 11:19:10 +010043size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010044{
45 size_t j;
46 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
47
48 for( j = 0; j < biL; j++ )
49 {
Janos Follathb7a88ec2022-08-19 12:24:40 +010050 if( a & mask ) break;
Janos Follath3ca07752022-08-09 11:45:47 +010051
52 mask >>= 1;
53 }
54
Gabor Mezei89e31462022-08-12 15:36:56 +020055 return( j );
Janos Follath3ca07752022-08-09 11:45:47 +010056}
57
Janos Follathb7a88ec2022-08-19 12:24:40 +010058size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +010059{
60 size_t i, j;
61
Janos Follathb7a88ec2022-08-19 12:24:40 +010062 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010063 return( 0 );
64
Janos Follathb7a88ec2022-08-19 12:24:40 +010065 for( i = A_limbs - 1; i > 0; i-- )
66 if( A[i] != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010067 break;
68
Janos Follathb7a88ec2022-08-19 12:24:40 +010069 j = biL - mbedtls_mpi_core_clz( A[i] );
Janos Follath3ca07752022-08-09 11:45:47 +010070
71 return( ( i * biL ) + j );
72}
73
Janos Follath3ca07752022-08-09 11:45:47 +010074/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
75 * into the storage form used by mbedtls_mpi. */
Janos Follathb7a88ec2022-08-19 12:24:40 +010076static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010077{
78 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010079 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010080 mbedtls_mpi_uint tmp = 0;
81
Janos Follathb7a88ec2022-08-19 12:24:40 +010082 for( i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++ )
Janos Follath3ca07752022-08-09 11:45:47 +010083 {
84 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010085 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010086 }
87
88 return( tmp );
89}
90
Janos Follathb7a88ec2022-08-19 12:24:40 +010091static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010092{
93#if defined(__BYTE_ORDER__)
94
95/* Nothing to do on bigendian systems. */
96#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
Janos Follathb7a88ec2022-08-19 12:24:40 +010097 return( a );
Janos Follath3ca07752022-08-09 11:45:47 +010098#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
99
100#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
101
102/* For GCC and Clang, have builtins for byte swapping. */
103#if defined(__GNUC__) && defined(__GNUC_PREREQ)
104#if __GNUC_PREREQ(4,3)
105#define have_bswap
106#endif
107#endif
108
109#if defined(__clang__) && defined(__has_builtin)
110#if __has_builtin(__builtin_bswap32) && \
111 __has_builtin(__builtin_bswap64)
112#define have_bswap
113#endif
114#endif
115
116#if defined(have_bswap)
117 /* The compiler is hopefully able to statically evaluate this! */
118 switch( sizeof(mbedtls_mpi_uint) )
119 {
120 case 4:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100121 return( __builtin_bswap32(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100122 case 8:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100123 return( __builtin_bswap64(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100124 }
125#endif
126#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
127#endif /* __BYTE_ORDER__ */
128
129 /* Fall back to C-based reordering if we don't know the byte order
130 * or we couldn't use a compiler-specific builtin. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100131 return( mpi_bigendian_to_host_c( a ) );
Janos Follath3ca07752022-08-09 11:45:47 +0100132}
133
Janos Follathb7a88ec2022-08-19 12:24:40 +0100134void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +0100135 size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +0100136{
137 mbedtls_mpi_uint *cur_limb_left;
138 mbedtls_mpi_uint *cur_limb_right;
Janos Follathc4596412022-08-22 10:01:27 +0100139 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100140 return;
141
142 /*
143 * Traverse limbs and
144 * - adapt byte-order in each limb
145 * - swap the limbs themselves.
146 * For that, simultaneously traverse the limbs from left to right
147 * and from right to left, as long as the left index is not bigger
148 * than the right index (it's not a problem if limbs is odd and the
149 * indices coincide in the last iteration).
150 */
Janos Follathc4596412022-08-22 10:01:27 +0100151 for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 );
Janos Follath3ca07752022-08-09 11:45:47 +0100152 cur_limb_left <= cur_limb_right;
153 cur_limb_left++, cur_limb_right-- )
154 {
155 mbedtls_mpi_uint tmp;
156 /* Note that if cur_limb_left == cur_limb_right,
157 * this code effectively swaps the bytes only once. */
158 tmp = mpi_bigendian_to_host( *cur_limb_left );
159 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
160 *cur_limb_right = tmp;
161 }
162}
163
Janos Follath3ca07752022-08-09 11:45:47 +0100164int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100165 size_t X_limbs,
166 const unsigned char *input,
167 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100168{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100169 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100170
Janos Follathb7a88ec2022-08-19 12:24:40 +0100171 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100172 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
173
174 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200175 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100176 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100177
Janos Follathb7a88ec2022-08-19 12:24:40 +0100178 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100179 {
180 size_t offset = ( ( i % ciL ) << 3 );
181 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
182 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200183 }
Janos Follath3ca07752022-08-09 11:45:47 +0100184
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100185 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100186}
187
Janos Follath3ca07752022-08-09 11:45:47 +0100188int mbedtls_mpi_core_read_be( 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
Janos Follathb7a88ec2022-08-19 12:24:40 +0100198 /* If X_limbs is 0, input_length must also be 0 (from previous test).
199 * Nothing to do. */
200 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200201 return( 0 );
202
Janos Follathb7a88ec2022-08-19 12:24:40 +0100203 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200204
205 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100206 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200207 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100208 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200209 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100210 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100211 }
212
Janos Follathb7a88ec2022-08-19 12:24:40 +0100213 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200214
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100215 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100216}
217
Janos Follathb7a88ec2022-08-19 12:24:40 +0100218int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
219 size_t A_limbs,
220 unsigned char *output,
221 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100222{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100223 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100224 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100225
Janos Follathb7a88ec2022-08-19 12:24:40 +0100226 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100227 {
228 bytes_to_copy = stored_bytes;
229 }
230 else
231 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100232 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100233
Janos Follathaf3f39c2022-08-22 09:06:32 +0100234 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100235 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100236 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100237 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100238 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100239 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
240 }
241 }
242
Janos Follathcc939082022-08-15 12:08:49 +0100243 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100244 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100245
Janos Follathb7a88ec2022-08-19 12:24:40 +0100246 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100247 {
248 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100249 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100250 }
251
252 return( 0 );
253}
254
Janos Follath3ca07752022-08-09 11:45:47 +0100255int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100256 size_t X_limbs,
257 unsigned char *output,
258 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100259{
260 size_t stored_bytes;
261 size_t bytes_to_copy;
262 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100263
Janos Follathb7a88ec2022-08-19 12:24:40 +0100264 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100265
Janos Follathb7a88ec2022-08-19 12:24:40 +0100266 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100267 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100268 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100269 * null bytes and record the position at which to start
270 * writing the significant bytes. In this case, the execution
271 * trace of this function does not depend on the value of the
272 * number. */
273 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100274 p = output + output_length - stored_bytes;
275 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100276 }
277 else
278 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100279 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100280 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100281 bytes_to_copy = output_length;
282 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100283 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100284 {
285 if( GET_BYTE( X, i ) != 0 )
286 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
287 }
288 }
289
Janos Follathcc939082022-08-15 12:08:49 +0100290 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100291 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
292
293 return( 0 );
294}
295
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100296void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
297 const mbedtls_mpi_uint *A,
298 const mbedtls_mpi_uint *B,
Tom Cosgrove72594632022-08-24 11:51:58 +0100299 size_t B_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100300 const mbedtls_mpi_uint *N,
Tom Cosgrove72594632022-08-24 11:51:58 +0100301 size_t AN_limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100302 mbedtls_mpi_uint mm,
303 mbedtls_mpi_uint *T )
304{
Tom Cosgrove72594632022-08-24 11:51:58 +0100305 memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100306
Tom Cosgrove72594632022-08-24 11:51:58 +0100307 for( size_t i = 0; i < AN_limbs; i++, T++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100308 {
309 mbedtls_mpi_uint u0, u1;
310 /* T = (T + u0*B + u1*N) / 2^biL */
311 u0 = A[i];
312 u1 = ( T[0] + u0 * B[0] ) * mm;
313
Tom Cosgrove72594632022-08-24 11:51:58 +0100314 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
315 (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100316 }
317
318 /* It's possible that the result in T is > N, and so we might need to subtract N */
319
Tom Cosgrove72594632022-08-24 11:51:58 +0100320 mbedtls_mpi_uint carry = T[AN_limbs];
321 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100322
323 /*
324 * Both carry and borrow can only be 0 or 1.
325 *
326 * If carry = 1, the result in T must be > N by definition, and the subtraction
Tom Cosgrove72594632022-08-24 11:51:58 +0100327 * using only AN_limbs limbs will create borrow, but that will have the correct
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100328 * final result.
329 *
330 * i.e. (carry, borrow) of (1, 1) => return X
331 *
332 * If carry = 0, then we want to use the result of the subtraction iff
333 * borrow = 0.
334 *
335 * i.e. (carry, borrow) of (0, 0) => return X
336 * (0, 1) => return T
337 *
338 * We've confirmed that the unit tests exercise this function with all 3 of
339 * the valid (carry, borrow) combinations (listed above), and that we don't
340 * see (carry, borrow) = (1, 0).
341 *
342 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100343 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100344 */
Tom Cosgrove72594632022-08-24 11:51:58 +0100345 mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100346}
347
348/*
349 * Fast Montgomery initialization (thanks to Tom St Denis).
350 */
Tom Cosgrovef0ffb152022-08-24 11:17:15 +0100351mbedtls_mpi_uint mbedtls_mpi_montg_init( const mbedtls_mpi_uint *N )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100352{
Tom Cosgrovef0ffb152022-08-24 11:17:15 +0100353 mbedtls_mpi_uint x = N[0];
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100354
Tom Cosgrovef0ffb152022-08-24 11:17:15 +0100355 x += ( ( N[0] + 2 ) & 4 ) << 1;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100356
357 for( unsigned int i = biL; i >= 8; i /= 2 )
Tom Cosgrovef0ffb152022-08-24 11:17:15 +0100358 x *= ( 2 - ( N[0] * x ) );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100359
360 return( ~x + 1 );
361}
362
363mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
364 const mbedtls_mpi_uint *s, size_t s_len,
365 mbedtls_mpi_uint b )
366{
367 mbedtls_mpi_uint c = 0; /* carry */
368 if( d_len < s_len )
369 s_len = d_len;
370 size_t excess_len = d_len - s_len;
371 size_t steps_x8 = s_len / 8;
372 size_t steps_x1 = s_len & 7;
373
374 while( steps_x8-- )
375 {
376 MULADDC_X8_INIT
377 MULADDC_X8_CORE
378 MULADDC_X8_STOP
379 }
380
381 while( steps_x1-- )
382 {
383 MULADDC_X1_INIT
384 MULADDC_X1_CORE
385 MULADDC_X1_STOP
386 }
387
388 while( excess_len-- )
389 {
390 *d += c; c = ( *d < c ); d++;
391 }
392
393 return( c );
394}
395
Tom Cosgrove72594632022-08-24 11:51:58 +0100396mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
397 const mbedtls_mpi_uint *A,
398 const mbedtls_mpi_uint *B,
399 size_t limbs )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100400{
401 mbedtls_mpi_uint c = 0;
402
Tom Cosgrove72594632022-08-24 11:51:58 +0100403 for( size_t i = 0; i < limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100404 {
Tom Cosgrove72594632022-08-24 11:51:58 +0100405 mbedtls_mpi_uint z = ( A[i] < c );
406 mbedtls_mpi_uint t = A[i] - c;
407 c = ( t < B[i] ) + z;
408 X[i] = t - B[i];
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100409 }
410
411 return( c );
412}
413
Tom Cosgrove72594632022-08-24 11:51:58 +0100414mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *A,
415 const mbedtls_mpi_uint *B,
416 size_t limbs,
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100417 unsigned cond )
418{
419 mbedtls_mpi_uint c = 0, t;
Tom Cosgrove72594632022-08-24 11:51:58 +0100420 for( size_t i = 0; i < limbs; i++ )
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100421 {
Tom Cosgrove72594632022-08-24 11:51:58 +0100422 mbedtls_mpi_uint add = cond * B[i];
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100423 t = c;
Tom Cosgrove72594632022-08-24 11:51:58 +0100424 t += A[i]; c = ( t < A[i] );
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100425 t += add; c += ( t < add );
Tom Cosgrove72594632022-08-24 11:51:58 +0100426 A[i] = t;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100427 }
428 return( c );
429}
430
Janos Follath3ca07752022-08-09 11:45:47 +0100431#endif /* MBEDTLS_BIGNUM_C */