blob: 142d15dc24e6f2db3bf3cc93249e177bbd7ebcef [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 Mezeif049dbf2022-07-18 23:02:33 +020044/*
45 * Count leading zero bits in a given integer
46 */
Janos Follath4670f882022-07-21 18:25:42 +010047size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020048{
49 size_t j;
50 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
51
52 for( j = 0; j < biL; j++ )
53 {
54 if( x & mask ) break;
55
56 mask >>= 1;
57 }
58
59 return j;
60}
61
62/*
63 * Return the number of bits
64 */
Janos Follath4670f882022-07-21 18:25:42 +010065size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020066{
67 size_t i, j;
68
69 if( nx == 0 )
70 return( 0 );
71
72 for( i = nx - 1; i > 0; i-- )
73 if( X[i] != 0 )
74 break;
75
Janos Follath4670f882022-07-21 18:25:42 +010076 j = biL - mbedtls_mpi_core_clz( X[i] );
Gabor Mezeif049dbf2022-07-18 23:02:33 +020077
78 return( ( i * biL ) + j );
79}
80
Gabor Mezeib9030702022-07-18 23:09:45 +020081/* Get a specific byte, without range checks. */
82#define GET_BYTE( X, i ) \
83 ( ( ( X )[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
84
Gabor Mezeif049dbf2022-07-18 23:02:33 +020085void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
86{
87 if ( r == NULL )
88 return;
89
90 r->n = 0;
91 r->p = NULL;
92}
93
94int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
95 mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +010096 mbedtls_mpi_uint *p,
97 size_t pn )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020098{
Janos Follath8b718b52022-07-25 11:31:02 +010099 if( p == NULL || m == NULL || r == NULL )
100 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
101
102 if( pn < m->n || !mbedtls_mpi_core_lt_ct( m->p, p, pn ) )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200103 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
104
105 r->n = m->n;
Janos Follath8b718b52022-07-25 11:31:02 +0100106 r->p = p;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200107
108 return( 0 );
109}
110
111void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
112{
113 if ( m == NULL )
114 return;
115
Janos Follath281ccda2022-07-19 13:14:36 +0100116 m->p = NULL;
117 m->n = 0;
118 m->plen = 0;
119 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
120 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200121}
122
123void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
124{
125 if ( m == NULL )
126 return;
127
Janos Follathba5c1392022-07-19 13:42:07 +0100128 switch( m->int_rep )
129 {
130 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
131 mbedtls_free( m->rep.mont ); break;
132 case MBEDTLS_MPI_MOD_REP_OPT_RED:
133 mbedtls_free( m->rep.mont ); break;
134 default:
135 break;
136 }
137
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200138 m->p = NULL;
139 m->n = 0;
140 m->plen = 0;
Janos Follath281ccda2022-07-19 13:14:36 +0100141 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
142 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200143}
144
145int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
146 mbedtls_mpi_uint *X,
147 size_t nx,
148 int ext_rep,
149 int int_rep )
150{
Janos Follathba5c1392022-07-19 13:42:07 +0100151 int ret = 0;
152
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200153 if ( X == NULL || m == NULL )
154 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
155
156 m->p = X;
157 m->n = nx;
Janos Follath4670f882022-07-21 18:25:42 +0100158 m->plen = mbedtls_mpi_core_bitlen( X, nx );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200159
Janos Follathba5c1392022-07-19 13:42:07 +0100160 switch( ext_rep )
161 {
162 case MBEDTLS_MPI_MOD_EXT_REP_LE:
163 case MBEDTLS_MPI_MOD_EXT_REP_BE:
164 m->ext_rep = ext_rep; break;
165 default:
166 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
167 goto exit;
168 }
169
170 switch( int_rep )
171 {
172 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
173 m->int_rep = int_rep;
174 m->rep.mont = NULL; break;
175 case MBEDTLS_MPI_MOD_REP_OPT_RED:
176 m->int_rep = int_rep;
177 m->rep.ored = NULL; break;
178 default:
179 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
180 goto exit;
181 }
182
183exit:
184
185 if( ret != 0 )
186 {
187 mbedtls_mpi_mod_modulus_free( m );
188 }
189
190 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200191}
192
Gabor Mezeib9030702022-07-18 23:09:45 +0200193/* Check X to have at least n limbs and set it to 0. */
194static int mpi_core_clear( mbedtls_mpi_uint *X,
195 size_t nx,
196 size_t limbs )
197{
Janos Follath91dc67d2022-07-22 14:24:58 +0100198 if( nx < limbs )
Gabor Mezeib9030702022-07-18 23:09:45 +0200199 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
200
Janos Follath91dc67d2022-07-22 14:24:58 +0100201 if( X != NULL )
Gabor Mezeib9030702022-07-18 23:09:45 +0200202 memset( X, 0, nx * ciL );
Janos Follath91dc67d2022-07-22 14:24:58 +0100203
204 return( 0 );
Gabor Mezeib9030702022-07-18 23:09:45 +0200205}
206
207/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
208 * into the storage form used by mbedtls_mpi. */
209
210static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
211{
212 uint8_t i;
213 unsigned char *x_ptr;
214 mbedtls_mpi_uint tmp = 0;
215
216 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
217 {
218 tmp <<= CHAR_BIT;
219 tmp |= (mbedtls_mpi_uint) *x_ptr;
220 }
221
222 return( tmp );
223}
224
225static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
226{
227#if defined(__BYTE_ORDER__)
228
229/* Nothing to do on bigendian systems. */
230#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
231 return( x );
232#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
233
234#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
235
236/* For GCC and Clang, have builtins for byte swapping. */
237#if defined(__GNUC__) && defined(__GNUC_PREREQ)
238#if __GNUC_PREREQ(4,3)
239#define have_bswap
240#endif
241#endif
242
243#if defined(__clang__) && defined(__has_builtin)
244#if __has_builtin(__builtin_bswap32) && \
245 __has_builtin(__builtin_bswap64)
246#define have_bswap
247#endif
248#endif
249
250#if defined(have_bswap)
251 /* The compiler is hopefully able to statically evaluate this! */
252 switch( sizeof(mbedtls_mpi_uint) )
253 {
254 case 4:
255 return( __builtin_bswap32(x) );
256 case 8:
257 return( __builtin_bswap64(x) );
258 }
259#endif
260#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
261#endif /* __BYTE_ORDER__ */
262
263 /* Fall back to C-based reordering if we don't know the byte order
264 * or we couldn't use a compiler-specific builtin. */
265 return( mpi_bigendian_to_host_c( x ) );
266}
267
Janos Follath4670f882022-07-21 18:25:42 +0100268void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
269 size_t limbs )
Gabor Mezeib9030702022-07-18 23:09:45 +0200270{
271 mbedtls_mpi_uint *cur_limb_left;
272 mbedtls_mpi_uint *cur_limb_right;
273 if( limbs == 0 )
274 return;
275
276 /*
277 * Traverse limbs and
278 * - adapt byte-order in each limb
279 * - swap the limbs themselves.
280 * For that, simultaneously traverse the limbs from left to right
281 * and from right to left, as long as the left index is not bigger
282 * than the right index (it's not a problem if limbs is odd and the
283 * indices coincide in the last iteration).
284 */
285 for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 );
286 cur_limb_left <= cur_limb_right;
287 cur_limb_left++, cur_limb_right-- )
288 {
289 mbedtls_mpi_uint tmp;
290 /* Note that if cur_limb_left == cur_limb_right,
291 * this code effectively swaps the bytes only once. */
292 tmp = mpi_bigendian_to_host( *cur_limb_left );
293 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
294 *cur_limb_right = tmp;
295 }
296}
297
298/*
299 * Import X from unsigned binary data, little endian
300 *
301 * This function is guaranteed to return an MPI with at least the necessary
302 * number of limbs (in particular, it does not skip 0s in the input).
303 */
304int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
305 size_t nx,
306 const unsigned char *buf,
307 size_t buflen )
308{
309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
310 size_t i;
311 size_t const limbs = CHARS_TO_LIMBS( buflen );
312
313 /* Ensure that target MPI has at least the necessary number of limbs */
314 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
315
316 for( i = 0; i < buflen; i++ )
317 X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
318
319cleanup:
320 return( ret );
321}
322
323/*
324 * Import X from unsigned binary data, big endian
325 *
326 * This function is guaranteed to return an MPI with exactly the necessary
327 * number of limbs (in particular, it does not skip 0s in the input).
328 */
329int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
330 size_t nx,
331 const unsigned char *buf,
332 size_t buflen )
333{
334 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf1d617d2022-07-21 09:29:32 +0100335 size_t const limbs = CHARS_TO_LIMBS( buflen );
336 size_t overhead;
Gabor Mezeib9030702022-07-18 23:09:45 +0200337 unsigned char *Xp;
338
339 MPI_VALIDATE_RET( X != NULL );
340 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
341
342 /* Ensure that target MPI has at least the necessary number of limbs */
343 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
344
Janos Follathf1d617d2022-07-21 09:29:32 +0100345 overhead = ( nx * ciL ) - buflen;
346
Gabor Mezeib9030702022-07-18 23:09:45 +0200347 /* Avoid calling `memcpy` with NULL source or destination argument,
348 * even if buflen is 0. */
349 if( buflen != 0 )
350 {
351 Xp = (unsigned char*) X;
352 memcpy( Xp + overhead, buf, buflen );
353
Janos Follath4670f882022-07-21 18:25:42 +0100354 mbedtls_mpi_core_bigendian_to_host( X, nx );
Gabor Mezeib9030702022-07-18 23:09:45 +0200355 }
356
357cleanup:
358 return( ret );
359}
360
361/*
362 * Export X into unsigned binary data, little endian
363 */
364int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
365 size_t nx,
366 unsigned char *buf,
367 size_t buflen )
368{
369 size_t stored_bytes = nx * ciL;
370 size_t bytes_to_copy;
371 size_t i;
372
373 if( stored_bytes < buflen )
374 {
375 bytes_to_copy = stored_bytes;
376 }
377 else
378 {
379 bytes_to_copy = buflen;
380
381 /* The output buffer is smaller than the allocated size of X.
382 * However X may fit if its leading bytes are zero. */
383 for( i = bytes_to_copy; i < stored_bytes; i++ )
384 {
385 if( GET_BYTE( X, i ) != 0 )
386 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
387 }
388 }
389
390 for( i = 0; i < bytes_to_copy; i++ )
391 buf[i] = GET_BYTE( X, i );
392
393 if( stored_bytes < buflen )
394 {
395 /* Write trailing 0 bytes */
396 memset( buf + stored_bytes, 0, buflen - stored_bytes );
397 }
398
399 return( 0 );
400}
401
402/*
403 * Export X into unsigned binary data, big endian
404 */
405int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
406 size_t nx,
407 unsigned char *buf,
408 size_t buflen )
409{
410 size_t stored_bytes;
411 size_t bytes_to_copy;
412 unsigned char *p;
413 size_t i;
414
415 MPI_VALIDATE_RET( X != NULL );
416 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
417
418 stored_bytes = nx * ciL;
419
420 if( stored_bytes < buflen )
421 {
422 /* There is enough space in the output buffer. Write initial
423 * null bytes and record the position at which to start
424 * writing the significant bytes. In this case, the execution
425 * trace of this function does not depend on the value of the
426 * number. */
427 bytes_to_copy = stored_bytes;
428 p = buf + buflen - stored_bytes;
429 memset( buf, 0, buflen - stored_bytes );
430 }
431 else
432 {
433 /* The output buffer is smaller than the allocated size of X.
434 * However X may fit if its leading bytes are zero. */
435 bytes_to_copy = buflen;
436 p = buf;
437 for( i = bytes_to_copy; i < stored_bytes; i++ )
438 {
439 if( GET_BYTE( X, i ) != 0 )
440 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
441 }
442 }
443
444 for( i = 0; i < bytes_to_copy; i++ )
445 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
446
447 return( 0 );
448}
449
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200450int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
451 mbedtls_mpi_mod_modulus *m,
452 unsigned char *buf,
453 size_t buflen )
454{
Janos Follath8b718b52022-07-25 11:31:02 +0100455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
456
Janos Follath5005edb2022-07-19 12:45:13 +0100457 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Janos Follath8b718b52022-07-25 11:31:02 +0100458 ret = mbedtls_mpi_core_read_le( X, m->n, buf, buflen );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200459
Janos Follath5005edb2022-07-19 12:45:13 +0100460 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Janos Follath8b718b52022-07-25 11:31:02 +0100461 ret = mbedtls_mpi_core_read_be( X, m->n, buf, buflen );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200462 else
463 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
464
Janos Follath8b718b52022-07-25 11:31:02 +0100465 if( ret != 0 )
466 goto cleanup;
467
468 if( !mbedtls_mpi_core_lt_ct( X, m->p, m->n ) )
469 {
470 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
471 goto cleanup;
472 }
473
474cleanup:
475
476 return( ret );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200477}
478
479int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X,
480 mbedtls_mpi_mod_modulus *m,
481 unsigned char *buf,
482 size_t buflen )
483{
Janos Follath5005edb2022-07-19 12:45:13 +0100484 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200485 return mbedtls_mpi_core_write_le( X, m->n, buf, buflen );
486
Janos Follath5005edb2022-07-19 12:45:13 +0100487 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200488 return mbedtls_mpi_core_write_be( X, m->n, buf, buflen );
489
490 else
491 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
492
493 return( 0 );
494}
495
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200496#endif /* MBEDTLS_BIGNUM_C */