blob: 2245650aedcab845fc5a84083ddb61f0c7d352ba [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 +020085int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
86 mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +010087 mbedtls_mpi_uint *p,
88 size_t pn )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020089{
Janos Follath8b718b52022-07-25 11:31:02 +010090 if( p == NULL || m == NULL || r == NULL )
91 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
92
93 if( pn < m->n || !mbedtls_mpi_core_lt_ct( m->p, p, pn ) )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020094 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
95
96 r->n = m->n;
Janos Follath8b718b52022-07-25 11:31:02 +010097 r->p = p;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020098
99 return( 0 );
100}
101
Gabor Mezei37b06362022-08-02 17:22:18 +0200102void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
103{
104 if ( r == NULL )
105 return;
106
107 r->n = 0;
108 r->p = NULL;
109}
110
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200111void 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:
Gabor Mezeid8f5bc22022-08-02 11:51:25 +0200133 mbedtls_free( m->rep.ored ); break;
Janos Follathba5c1392022-07-19 13:42:07 +0100134 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,
Gabor Mezei535f36d2022-08-02 11:50:44 +0200146 mbedtls_mpi_uint *p,
147 size_t pn,
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200148 int ext_rep,
149 int int_rep )
150{
Janos Follathba5c1392022-07-19 13:42:07 +0100151 int ret = 0;
152
Gabor Mezei535f36d2022-08-02 11:50:44 +0200153 if ( p == NULL || m == NULL )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200154 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
155
Gabor Mezei535f36d2022-08-02 11:50:44 +0200156 m->p = p;
157 m->n = pn;
158 m->plen = mbedtls_mpi_core_bitlen( p, pn );
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. */
Gabor Mezeib9030702022-07-18 23:09:45 +0200209static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
210{
211 uint8_t i;
212 unsigned char *x_ptr;
213 mbedtls_mpi_uint tmp = 0;
214
215 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
216 {
217 tmp <<= CHAR_BIT;
218 tmp |= (mbedtls_mpi_uint) *x_ptr;
219 }
220
221 return( tmp );
222}
223
224static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
225{
226#if defined(__BYTE_ORDER__)
227
228/* Nothing to do on bigendian systems. */
229#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
230 return( x );
231#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
232
233#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
234
235/* For GCC and Clang, have builtins for byte swapping. */
236#if defined(__GNUC__) && defined(__GNUC_PREREQ)
237#if __GNUC_PREREQ(4,3)
238#define have_bswap
239#endif
240#endif
241
242#if defined(__clang__) && defined(__has_builtin)
243#if __has_builtin(__builtin_bswap32) && \
244 __has_builtin(__builtin_bswap64)
245#define have_bswap
246#endif
247#endif
248
249#if defined(have_bswap)
250 /* The compiler is hopefully able to statically evaluate this! */
251 switch( sizeof(mbedtls_mpi_uint) )
252 {
253 case 4:
254 return( __builtin_bswap32(x) );
255 case 8:
256 return( __builtin_bswap64(x) );
257 }
258#endif
259#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
260#endif /* __BYTE_ORDER__ */
261
262 /* Fall back to C-based reordering if we don't know the byte order
263 * or we couldn't use a compiler-specific builtin. */
264 return( mpi_bigendian_to_host_c( x ) );
265}
266
Janos Follath4670f882022-07-21 18:25:42 +0100267void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
268 size_t limbs )
Gabor Mezeib9030702022-07-18 23:09:45 +0200269{
270 mbedtls_mpi_uint *cur_limb_left;
271 mbedtls_mpi_uint *cur_limb_right;
272 if( limbs == 0 )
273 return;
274
275 /*
276 * Traverse limbs and
277 * - adapt byte-order in each limb
278 * - swap the limbs themselves.
279 * For that, simultaneously traverse the limbs from left to right
280 * and from right to left, as long as the left index is not bigger
281 * than the right index (it's not a problem if limbs is odd and the
282 * indices coincide in the last iteration).
283 */
284 for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 );
285 cur_limb_left <= cur_limb_right;
286 cur_limb_left++, cur_limb_right-- )
287 {
288 mbedtls_mpi_uint tmp;
289 /* Note that if cur_limb_left == cur_limb_right,
290 * this code effectively swaps the bytes only once. */
291 tmp = mpi_bigendian_to_host( *cur_limb_left );
292 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
293 *cur_limb_right = tmp;
294 }
295}
296
297/*
298 * Import X from unsigned binary data, little endian
299 *
300 * This function is guaranteed to return an MPI with at least the necessary
301 * number of limbs (in particular, it does not skip 0s in the input).
302 */
303int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
304 size_t nx,
305 const unsigned char *buf,
306 size_t buflen )
307{
308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
309 size_t i;
310 size_t const limbs = CHARS_TO_LIMBS( buflen );
311
312 /* Ensure that target MPI has at least the necessary number of limbs */
313 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
314
315 for( i = 0; i < buflen; i++ )
316 X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
317
318cleanup:
319 return( ret );
320}
321
322/*
323 * Import X from unsigned binary data, big endian
324 *
325 * This function is guaranteed to return an MPI with exactly the necessary
326 * number of limbs (in particular, it does not skip 0s in the input).
327 */
328int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
329 size_t nx,
330 const unsigned char *buf,
331 size_t buflen )
332{
333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf1d617d2022-07-21 09:29:32 +0100334 size_t const limbs = CHARS_TO_LIMBS( buflen );
335 size_t overhead;
Gabor Mezeib9030702022-07-18 23:09:45 +0200336 unsigned char *Xp;
337
338 MPI_VALIDATE_RET( X != NULL );
339 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
340
341 /* Ensure that target MPI has at least the necessary number of limbs */
342 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
343
Janos Follathf1d617d2022-07-21 09:29:32 +0100344 overhead = ( nx * ciL ) - buflen;
345
Gabor Mezeib9030702022-07-18 23:09:45 +0200346 /* Avoid calling `memcpy` with NULL source or destination argument,
347 * even if buflen is 0. */
348 if( buflen != 0 )
349 {
350 Xp = (unsigned char*) X;
351 memcpy( Xp + overhead, buf, buflen );
352
Janos Follath4670f882022-07-21 18:25:42 +0100353 mbedtls_mpi_core_bigendian_to_host( X, nx );
Gabor Mezeib9030702022-07-18 23:09:45 +0200354 }
355
356cleanup:
357 return( ret );
358}
359
360/*
361 * Export X into unsigned binary data, little endian
362 */
363int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
364 size_t nx,
365 unsigned char *buf,
366 size_t buflen )
367{
368 size_t stored_bytes = nx * ciL;
369 size_t bytes_to_copy;
370 size_t i;
371
372 if( stored_bytes < buflen )
373 {
374 bytes_to_copy = stored_bytes;
375 }
376 else
377 {
378 bytes_to_copy = buflen;
379
380 /* The output buffer is smaller than the allocated size of X.
381 * However X may fit if its leading bytes are zero. */
382 for( i = bytes_to_copy; i < stored_bytes; i++ )
383 {
384 if( GET_BYTE( X, i ) != 0 )
385 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
386 }
387 }
388
389 for( i = 0; i < bytes_to_copy; i++ )
390 buf[i] = GET_BYTE( X, i );
391
392 if( stored_bytes < buflen )
393 {
394 /* Write trailing 0 bytes */
395 memset( buf + stored_bytes, 0, buflen - stored_bytes );
396 }
397
398 return( 0 );
399}
400
401/*
402 * Export X into unsigned binary data, big endian
403 */
404int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
405 size_t nx,
406 unsigned char *buf,
407 size_t buflen )
408{
409 size_t stored_bytes;
410 size_t bytes_to_copy;
411 unsigned char *p;
412 size_t i;
413
414 MPI_VALIDATE_RET( X != NULL );
415 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
416
417 stored_bytes = nx * ciL;
418
419 if( stored_bytes < buflen )
420 {
421 /* There is enough space in the output buffer. Write initial
422 * null bytes and record the position at which to start
423 * writing the significant bytes. In this case, the execution
424 * trace of this function does not depend on the value of the
425 * number. */
426 bytes_to_copy = stored_bytes;
427 p = buf + buflen - stored_bytes;
428 memset( buf, 0, buflen - stored_bytes );
429 }
430 else
431 {
432 /* The output buffer is smaller than the allocated size of X.
433 * However X may fit if its leading bytes are zero. */
434 bytes_to_copy = buflen;
435 p = buf;
436 for( i = bytes_to_copy; i < stored_bytes; i++ )
437 {
438 if( GET_BYTE( X, i ) != 0 )
439 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
440 }
441 }
442
443 for( i = 0; i < bytes_to_copy; i++ )
444 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
445
446 return( 0 );
447}
448
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200449int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
450 mbedtls_mpi_mod_modulus *m,
451 unsigned char *buf,
452 size_t buflen )
453{
Janos Follath8b718b52022-07-25 11:31:02 +0100454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
455
Gabor Mezeic0b93042022-08-02 11:52:37 +0200456 if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_LE )
Janos Follath8b718b52022-07-25 11:31:02 +0100457 ret = mbedtls_mpi_core_read_le( X, m->n, buf, buflen );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200458
Gabor Mezeic0b93042022-08-02 11:52:37 +0200459 else if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_BE )
Janos Follath8b718b52022-07-25 11:31:02 +0100460 ret = mbedtls_mpi_core_read_be( X, m->n, buf, buflen );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200461 else
462 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
463
Janos Follath8b718b52022-07-25 11:31:02 +0100464 if( ret != 0 )
465 goto cleanup;
466
467 if( !mbedtls_mpi_core_lt_ct( X, m->p, m->n ) )
468 {
469 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
470 goto cleanup;
471 }
472
473cleanup:
474
475 return( ret );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200476}
477
478int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X,
479 mbedtls_mpi_mod_modulus *m,
480 unsigned char *buf,
481 size_t buflen )
482{
Gabor Mezeic0b93042022-08-02 11:52:37 +0200483 if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200484 return mbedtls_mpi_core_write_le( X, m->n, buf, buflen );
485
Gabor Mezeic0b93042022-08-02 11:52:37 +0200486 else if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200487 return mbedtls_mpi_core_write_be( X, m->n, buf, buflen );
488
489 else
490 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
491
492 return( 0 );
493}
494
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200495#endif /* MBEDTLS_BIGNUM_C */