blob: d1253f8d007d72667f38d2a673ad31750dadf3fc [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
2 * Internal bignum functions
3 *
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
Gabor Mezeib9030702022-07-18 23:09:45 +020024#include <string.h>
25
26#include "mbedtls/platform_util.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020027#include "mbedtls/error.h"
28#include "mbedtls/bignum.h"
Gabor Mezeib9030702022-07-18 23:09:45 +020029#include "bignum_core.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020030#include "bignum_mod.h"
Gabor Mezeic5328cf2022-07-18 23:13:13 +020031#include "bignum_mod_raw.h"
Janos Follath8b718b52022-07-25 11:31:02 +010032#include "constant_time_internal.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020033
Janos Follathba5c1392022-07-19 13:42:07 +010034#if defined(MBEDTLS_PLATFORM_C)
35#include "mbedtls/platform.h"
36#else
37#include <stdio.h>
38#include <stdlib.h>
Gabor Mezeie66b1d42022-08-02 11:49:59 +020039#define mbedtls_printf printf
40#define mbedtls_calloc calloc
41#define mbedtls_free free
Janos Follathba5c1392022-07-19 13:42:07 +010042#endif
43
Gabor Mezei66669142022-08-03 12:52:26 +020044#define MPI_VALIDATE_RET( cond ) \
45 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
46#define MPI_VALIDATE( cond ) \
47 MBEDTLS_INTERNAL_VALIDATE( cond )
48
49#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
50#define biL (ciL << 3) /* bits in limb */
51#define biH (ciL << 2) /* half limb size */
52
53/*
54 * Convert between bits/chars and number of limbs
55 * Divide first in order to avoid potential overflows
56 */
57#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
58#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
59
Gabor Mezeif049dbf2022-07-18 23:02:33 +020060/*
61 * Count leading zero bits in a given integer
62 */
Janos Follath4670f882022-07-21 18:25:42 +010063size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020064{
65 size_t j;
66 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
67
68 for( j = 0; j < biL; j++ )
69 {
70 if( x & mask ) break;
71
72 mask >>= 1;
73 }
74
75 return j;
76}
77
78/*
79 * Return the number of bits
80 */
Janos Follath4670f882022-07-21 18:25:42 +010081size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020082{
83 size_t i, j;
84
85 if( nx == 0 )
86 return( 0 );
87
88 for( i = nx - 1; i > 0; i-- )
89 if( X[i] != 0 )
90 break;
91
Janos Follath4670f882022-07-21 18:25:42 +010092 j = biL - mbedtls_mpi_core_clz( X[i] );
Gabor Mezeif049dbf2022-07-18 23:02:33 +020093
94 return( ( i * biL ) + j );
95}
96
Gabor Mezeib9030702022-07-18 23:09:45 +020097/* Get a specific byte, without range checks. */
98#define GET_BYTE( X, i ) \
99 ( ( ( X )[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
100
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200101int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
102 mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +0100103 mbedtls_mpi_uint *p,
104 size_t pn )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200105{
Janos Follath8b718b52022-07-25 11:31:02 +0100106 if( p == NULL || m == NULL || r == NULL )
107 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
108
109 if( pn < m->n || !mbedtls_mpi_core_lt_ct( m->p, p, pn ) )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200110 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
111
112 r->n = m->n;
Janos Follath8b718b52022-07-25 11:31:02 +0100113 r->p = p;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200114
115 return( 0 );
116}
117
Gabor Mezei37b06362022-08-02 17:22:18 +0200118void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
119{
120 if ( r == NULL )
121 return;
122
123 r->n = 0;
124 r->p = NULL;
125}
126
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200127void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
128{
129 if ( m == NULL )
130 return;
131
Janos Follath281ccda2022-07-19 13:14:36 +0100132 m->p = NULL;
133 m->n = 0;
134 m->plen = 0;
135 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
136 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200137}
138
139void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
140{
141 if ( m == NULL )
142 return;
143
Janos Follathba5c1392022-07-19 13:42:07 +0100144 switch( m->int_rep )
145 {
146 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
147 mbedtls_free( m->rep.mont ); break;
148 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Gabor Mezeid8f5bc22022-08-02 11:51:25 +0200149 mbedtls_free( m->rep.ored ); break;
Janos Follathba5c1392022-07-19 13:42:07 +0100150 default:
151 break;
152 }
153
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200154 m->p = NULL;
155 m->n = 0;
156 m->plen = 0;
Janos Follath281ccda2022-07-19 13:14:36 +0100157 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
158 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200159}
160
161int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Gabor Mezei535f36d2022-08-02 11:50:44 +0200162 mbedtls_mpi_uint *p,
163 size_t pn,
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200164 int ext_rep,
165 int int_rep )
166{
Janos Follathba5c1392022-07-19 13:42:07 +0100167 int ret = 0;
168
Gabor Mezei535f36d2022-08-02 11:50:44 +0200169 if ( p == NULL || m == NULL )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200170 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
171
Gabor Mezei535f36d2022-08-02 11:50:44 +0200172 m->p = p;
173 m->n = pn;
174 m->plen = mbedtls_mpi_core_bitlen( p, pn );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200175
Janos Follathba5c1392022-07-19 13:42:07 +0100176 switch( ext_rep )
177 {
178 case MBEDTLS_MPI_MOD_EXT_REP_LE:
179 case MBEDTLS_MPI_MOD_EXT_REP_BE:
180 m->ext_rep = ext_rep; break;
181 default:
182 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
183 goto exit;
184 }
185
186 switch( int_rep )
187 {
188 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
189 m->int_rep = int_rep;
190 m->rep.mont = NULL; break;
191 case MBEDTLS_MPI_MOD_REP_OPT_RED:
192 m->int_rep = int_rep;
193 m->rep.ored = NULL; break;
194 default:
195 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
196 goto exit;
197 }
198
199exit:
200
201 if( ret != 0 )
202 {
203 mbedtls_mpi_mod_modulus_free( m );
204 }
205
206 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200207}
208
Gabor Mezeib9030702022-07-18 23:09:45 +0200209/* Check X to have at least n limbs and set it to 0. */
210static int mpi_core_clear( mbedtls_mpi_uint *X,
211 size_t nx,
212 size_t limbs )
213{
Janos Follath91dc67d2022-07-22 14:24:58 +0100214 if( nx < limbs )
Gabor Mezeib9030702022-07-18 23:09:45 +0200215 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
216
Janos Follath91dc67d2022-07-22 14:24:58 +0100217 if( X != NULL )
Gabor Mezeib9030702022-07-18 23:09:45 +0200218 memset( X, 0, nx * ciL );
Janos Follath91dc67d2022-07-22 14:24:58 +0100219
220 return( 0 );
Gabor Mezeib9030702022-07-18 23:09:45 +0200221}
222
223/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
224 * into the storage form used by mbedtls_mpi. */
Gabor Mezeib9030702022-07-18 23:09:45 +0200225static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
226{
227 uint8_t i;
228 unsigned char *x_ptr;
229 mbedtls_mpi_uint tmp = 0;
230
231 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
232 {
233 tmp <<= CHAR_BIT;
234 tmp |= (mbedtls_mpi_uint) *x_ptr;
235 }
236
237 return( tmp );
238}
239
240static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
241{
242#if defined(__BYTE_ORDER__)
243
244/* Nothing to do on bigendian systems. */
245#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
246 return( x );
247#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
248
249#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
250
251/* For GCC and Clang, have builtins for byte swapping. */
252#if defined(__GNUC__) && defined(__GNUC_PREREQ)
253#if __GNUC_PREREQ(4,3)
254#define have_bswap
255#endif
256#endif
257
258#if defined(__clang__) && defined(__has_builtin)
259#if __has_builtin(__builtin_bswap32) && \
260 __has_builtin(__builtin_bswap64)
261#define have_bswap
262#endif
263#endif
264
265#if defined(have_bswap)
266 /* The compiler is hopefully able to statically evaluate this! */
267 switch( sizeof(mbedtls_mpi_uint) )
268 {
269 case 4:
270 return( __builtin_bswap32(x) );
271 case 8:
272 return( __builtin_bswap64(x) );
273 }
274#endif
275#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
276#endif /* __BYTE_ORDER__ */
277
278 /* Fall back to C-based reordering if we don't know the byte order
279 * or we couldn't use a compiler-specific builtin. */
280 return( mpi_bigendian_to_host_c( x ) );
281}
282
Janos Follath4670f882022-07-21 18:25:42 +0100283void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
284 size_t limbs )
Gabor Mezeib9030702022-07-18 23:09:45 +0200285{
286 mbedtls_mpi_uint *cur_limb_left;
287 mbedtls_mpi_uint *cur_limb_right;
288 if( limbs == 0 )
289 return;
290
291 /*
292 * Traverse limbs and
293 * - adapt byte-order in each limb
294 * - swap the limbs themselves.
295 * For that, simultaneously traverse the limbs from left to right
296 * and from right to left, as long as the left index is not bigger
297 * than the right index (it's not a problem if limbs is odd and the
298 * indices coincide in the last iteration).
299 */
300 for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 );
301 cur_limb_left <= cur_limb_right;
302 cur_limb_left++, cur_limb_right-- )
303 {
304 mbedtls_mpi_uint tmp;
305 /* Note that if cur_limb_left == cur_limb_right,
306 * this code effectively swaps the bytes only once. */
307 tmp = mpi_bigendian_to_host( *cur_limb_left );
308 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
309 *cur_limb_right = tmp;
310 }
311}
312
313/*
314 * Import X from unsigned binary data, little endian
315 *
316 * This function is guaranteed to return an MPI with at least the necessary
317 * number of limbs (in particular, it does not skip 0s in the input).
318 */
319int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
320 size_t nx,
321 const unsigned char *buf,
322 size_t buflen )
323{
324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
325 size_t i;
326 size_t const limbs = CHARS_TO_LIMBS( buflen );
327
328 /* Ensure that target MPI has at least the necessary number of limbs */
329 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
330
331 for( i = 0; i < buflen; i++ )
332 X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
333
334cleanup:
335 return( ret );
336}
337
338/*
339 * Import X from unsigned binary data, big endian
340 *
341 * This function is guaranteed to return an MPI with exactly the necessary
342 * number of limbs (in particular, it does not skip 0s in the input).
343 */
344int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
345 size_t nx,
346 const unsigned char *buf,
347 size_t buflen )
348{
349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf1d617d2022-07-21 09:29:32 +0100350 size_t const limbs = CHARS_TO_LIMBS( buflen );
351 size_t overhead;
Gabor Mezeib9030702022-07-18 23:09:45 +0200352 unsigned char *Xp;
353
354 MPI_VALIDATE_RET( X != NULL );
355 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
356
357 /* Ensure that target MPI has at least the necessary number of limbs */
358 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
359
Janos Follathf1d617d2022-07-21 09:29:32 +0100360 overhead = ( nx * ciL ) - buflen;
361
Gabor Mezeib9030702022-07-18 23:09:45 +0200362 /* Avoid calling `memcpy` with NULL source or destination argument,
363 * even if buflen is 0. */
364 if( buflen != 0 )
365 {
366 Xp = (unsigned char*) X;
367 memcpy( Xp + overhead, buf, buflen );
368
Janos Follath4670f882022-07-21 18:25:42 +0100369 mbedtls_mpi_core_bigendian_to_host( X, nx );
Gabor Mezeib9030702022-07-18 23:09:45 +0200370 }
371
372cleanup:
373 return( ret );
374}
375
376/*
377 * Export X into unsigned binary data, little endian
378 */
379int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
380 size_t nx,
381 unsigned char *buf,
382 size_t buflen )
383{
384 size_t stored_bytes = nx * ciL;
385 size_t bytes_to_copy;
386 size_t i;
387
388 if( stored_bytes < buflen )
389 {
390 bytes_to_copy = stored_bytes;
391 }
392 else
393 {
394 bytes_to_copy = buflen;
395
396 /* The output buffer is smaller than the allocated size of X.
397 * However X may fit if its leading bytes are zero. */
398 for( i = bytes_to_copy; i < stored_bytes; i++ )
399 {
400 if( GET_BYTE( X, i ) != 0 )
401 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
402 }
403 }
404
405 for( i = 0; i < bytes_to_copy; i++ )
406 buf[i] = GET_BYTE( X, i );
407
408 if( stored_bytes < buflen )
409 {
410 /* Write trailing 0 bytes */
411 memset( buf + stored_bytes, 0, buflen - stored_bytes );
412 }
413
414 return( 0 );
415}
416
417/*
418 * Export X into unsigned binary data, big endian
419 */
420int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
421 size_t nx,
422 unsigned char *buf,
423 size_t buflen )
424{
425 size_t stored_bytes;
426 size_t bytes_to_copy;
427 unsigned char *p;
428 size_t i;
429
430 MPI_VALIDATE_RET( X != NULL );
431 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
432
433 stored_bytes = nx * ciL;
434
435 if( stored_bytes < buflen )
436 {
437 /* There is enough space in the output buffer. Write initial
438 * null bytes and record the position at which to start
439 * writing the significant bytes. In this case, the execution
440 * trace of this function does not depend on the value of the
441 * number. */
442 bytes_to_copy = stored_bytes;
443 p = buf + buflen - stored_bytes;
444 memset( buf, 0, buflen - stored_bytes );
445 }
446 else
447 {
448 /* The output buffer is smaller than the allocated size of X.
449 * However X may fit if its leading bytes are zero. */
450 bytes_to_copy = buflen;
451 p = buf;
452 for( i = bytes_to_copy; i < stored_bytes; i++ )
453 {
454 if( GET_BYTE( X, i ) != 0 )
455 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
456 }
457 }
458
459 for( i = 0; i < bytes_to_copy; i++ )
460 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
461
462 return( 0 );
463}
464
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200465int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
466 mbedtls_mpi_mod_modulus *m,
467 unsigned char *buf,
468 size_t buflen )
469{
Janos Follath8b718b52022-07-25 11:31:02 +0100470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
471
Gabor Mezeic0b93042022-08-02 11:52:37 +0200472 if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_LE )
Janos Follath8b718b52022-07-25 11:31:02 +0100473 ret = mbedtls_mpi_core_read_le( X, m->n, buf, buflen );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200474
Gabor Mezeic0b93042022-08-02 11:52:37 +0200475 else if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_BE )
Janos Follath8b718b52022-07-25 11:31:02 +0100476 ret = mbedtls_mpi_core_read_be( X, m->n, buf, buflen );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200477 else
478 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
479
Janos Follath8b718b52022-07-25 11:31:02 +0100480 if( ret != 0 )
481 goto cleanup;
482
483 if( !mbedtls_mpi_core_lt_ct( X, m->p, m->n ) )
484 {
485 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
486 goto cleanup;
487 }
488
489cleanup:
490
491 return( ret );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200492}
493
494int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X,
495 mbedtls_mpi_mod_modulus *m,
496 unsigned char *buf,
497 size_t buflen )
498{
Gabor Mezeic0b93042022-08-02 11:52:37 +0200499 if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200500 return mbedtls_mpi_core_write_le( X, m->n, buf, buflen );
501
Gabor Mezeic0b93042022-08-02 11:52:37 +0200502 else if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200503 return mbedtls_mpi_core_write_be( X, m->n, buf, buflen );
504
505 else
506 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
507
508 return( 0 );
509}
510
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200511#endif /* MBEDTLS_BIGNUM_C */