blob: 04f6049233b8b26a533a6e151c92a61eb7be8246 [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"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020032
Janos Follathba5c1392022-07-19 13:42:07 +010033#if defined(MBEDTLS_PLATFORM_C)
34#include "mbedtls/platform.h"
35#else
36#include <stdio.h>
37#include <stdlib.h>
38#define mbedtls_printf printf
39#define mbedtls_calloc calloc
40#define mbedtls_free free
41#endif
42
Gabor Mezeif049dbf2022-07-18 23:02:33 +020043/*
44 * Count leading zero bits in a given integer
45 */
Janos Follath4670f882022-07-21 18:25:42 +010046size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020047{
48 size_t j;
49 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
50
51 for( j = 0; j < biL; j++ )
52 {
53 if( x & mask ) break;
54
55 mask >>= 1;
56 }
57
58 return j;
59}
60
61/*
62 * Return the number of bits
63 */
Janos Follath4670f882022-07-21 18:25:42 +010064size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020065{
66 size_t i, j;
67
68 if( nx == 0 )
69 return( 0 );
70
71 for( i = nx - 1; i > 0; i-- )
72 if( X[i] != 0 )
73 break;
74
Janos Follath4670f882022-07-21 18:25:42 +010075 j = biL - mbedtls_mpi_core_clz( X[i] );
Gabor Mezeif049dbf2022-07-18 23:02:33 +020076
77 return( ( i * biL ) + j );
78}
79
Gabor Mezeib9030702022-07-18 23:09:45 +020080/* Get a specific byte, without range checks. */
81#define GET_BYTE( X, i ) \
82 ( ( ( X )[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
83
Gabor Mezeif049dbf2022-07-18 23:02:33 +020084void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
85{
86 if ( r == NULL )
87 return;
88
89 r->n = 0;
90 r->p = NULL;
91}
92
93int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
94 mbedtls_mpi_mod_modulus *m,
95 mbedtls_mpi_uint *X )
96{
97 if( X == NULL || m == NULL || r == NULL || X >= m->p)
98 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
99
100 r->n = m->n;
101 r->p = X;
102
103 return( 0 );
104}
105
106void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
107{
108 if ( m == NULL )
109 return;
110
Janos Follath281ccda2022-07-19 13:14:36 +0100111 m->p = NULL;
112 m->n = 0;
113 m->plen = 0;
114 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
115 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200116}
117
118void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
119{
120 if ( m == NULL )
121 return;
122
Janos Follathba5c1392022-07-19 13:42:07 +0100123 switch( m->int_rep )
124 {
125 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
126 mbedtls_free( m->rep.mont ); break;
127 case MBEDTLS_MPI_MOD_REP_OPT_RED:
128 mbedtls_free( m->rep.mont ); break;
129 default:
130 break;
131 }
132
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200133 m->p = NULL;
134 m->n = 0;
135 m->plen = 0;
Janos Follath281ccda2022-07-19 13:14:36 +0100136 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
137 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200138}
139
140int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
141 mbedtls_mpi_uint *X,
142 size_t nx,
143 int ext_rep,
144 int int_rep )
145{
Janos Follathba5c1392022-07-19 13:42:07 +0100146 int ret = 0;
147
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200148 if ( X == NULL || m == NULL )
149 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
150
151 m->p = X;
152 m->n = nx;
Janos Follath4670f882022-07-21 18:25:42 +0100153 m->plen = mbedtls_mpi_core_bitlen( X, nx );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200154
Janos Follathba5c1392022-07-19 13:42:07 +0100155 switch( ext_rep )
156 {
157 case MBEDTLS_MPI_MOD_EXT_REP_LE:
158 case MBEDTLS_MPI_MOD_EXT_REP_BE:
159 m->ext_rep = ext_rep; break;
160 default:
161 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
162 goto exit;
163 }
164
165 switch( int_rep )
166 {
167 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
168 m->int_rep = int_rep;
169 m->rep.mont = NULL; break;
170 case MBEDTLS_MPI_MOD_REP_OPT_RED:
171 m->int_rep = int_rep;
172 m->rep.ored = NULL; break;
173 default:
174 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
175 goto exit;
176 }
177
178exit:
179
180 if( ret != 0 )
181 {
182 mbedtls_mpi_mod_modulus_free( m );
183 }
184
185 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200186}
187
Gabor Mezeib9030702022-07-18 23:09:45 +0200188/* Check X to have at least n limbs and set it to 0. */
189static int mpi_core_clear( mbedtls_mpi_uint *X,
190 size_t nx,
191 size_t limbs )
192{
Janos Follath91dc67d2022-07-22 14:24:58 +0100193 if( nx < limbs )
Gabor Mezeib9030702022-07-18 23:09:45 +0200194 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
195
Janos Follath91dc67d2022-07-22 14:24:58 +0100196 if( X != NULL )
Gabor Mezeib9030702022-07-18 23:09:45 +0200197 memset( X, 0, nx * ciL );
Janos Follath91dc67d2022-07-22 14:24:58 +0100198
199 return( 0 );
Gabor Mezeib9030702022-07-18 23:09:45 +0200200}
201
202/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
203 * into the storage form used by mbedtls_mpi. */
204
205static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
206{
207 uint8_t i;
208 unsigned char *x_ptr;
209 mbedtls_mpi_uint tmp = 0;
210
211 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
212 {
213 tmp <<= CHAR_BIT;
214 tmp |= (mbedtls_mpi_uint) *x_ptr;
215 }
216
217 return( tmp );
218}
219
220static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
221{
222#if defined(__BYTE_ORDER__)
223
224/* Nothing to do on bigendian systems. */
225#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
226 return( x );
227#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
228
229#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
230
231/* For GCC and Clang, have builtins for byte swapping. */
232#if defined(__GNUC__) && defined(__GNUC_PREREQ)
233#if __GNUC_PREREQ(4,3)
234#define have_bswap
235#endif
236#endif
237
238#if defined(__clang__) && defined(__has_builtin)
239#if __has_builtin(__builtin_bswap32) && \
240 __has_builtin(__builtin_bswap64)
241#define have_bswap
242#endif
243#endif
244
245#if defined(have_bswap)
246 /* The compiler is hopefully able to statically evaluate this! */
247 switch( sizeof(mbedtls_mpi_uint) )
248 {
249 case 4:
250 return( __builtin_bswap32(x) );
251 case 8:
252 return( __builtin_bswap64(x) );
253 }
254#endif
255#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
256#endif /* __BYTE_ORDER__ */
257
258 /* Fall back to C-based reordering if we don't know the byte order
259 * or we couldn't use a compiler-specific builtin. */
260 return( mpi_bigendian_to_host_c( x ) );
261}
262
Janos Follath4670f882022-07-21 18:25:42 +0100263void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
264 size_t limbs )
Gabor Mezeib9030702022-07-18 23:09:45 +0200265{
266 mbedtls_mpi_uint *cur_limb_left;
267 mbedtls_mpi_uint *cur_limb_right;
268 if( limbs == 0 )
269 return;
270
271 /*
272 * Traverse limbs and
273 * - adapt byte-order in each limb
274 * - swap the limbs themselves.
275 * For that, simultaneously traverse the limbs from left to right
276 * and from right to left, as long as the left index is not bigger
277 * than the right index (it's not a problem if limbs is odd and the
278 * indices coincide in the last iteration).
279 */
280 for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 );
281 cur_limb_left <= cur_limb_right;
282 cur_limb_left++, cur_limb_right-- )
283 {
284 mbedtls_mpi_uint tmp;
285 /* Note that if cur_limb_left == cur_limb_right,
286 * this code effectively swaps the bytes only once. */
287 tmp = mpi_bigendian_to_host( *cur_limb_left );
288 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
289 *cur_limb_right = tmp;
290 }
291}
292
293/*
294 * Import X from unsigned binary data, little endian
295 *
296 * This function is guaranteed to return an MPI with at least the necessary
297 * number of limbs (in particular, it does not skip 0s in the input).
298 */
299int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
300 size_t nx,
301 const unsigned char *buf,
302 size_t buflen )
303{
304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
305 size_t i;
306 size_t const limbs = CHARS_TO_LIMBS( buflen );
307
308 /* Ensure that target MPI has at least the necessary number of limbs */
309 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
310
311 for( i = 0; i < buflen; i++ )
312 X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
313
314cleanup:
315 return( ret );
316}
317
318/*
319 * Import X from unsigned binary data, big endian
320 *
321 * This function is guaranteed to return an MPI with exactly the necessary
322 * number of limbs (in particular, it does not skip 0s in the input).
323 */
324int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
325 size_t nx,
326 const unsigned char *buf,
327 size_t buflen )
328{
329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf1d617d2022-07-21 09:29:32 +0100330 size_t const limbs = CHARS_TO_LIMBS( buflen );
331 size_t overhead;
Gabor Mezeib9030702022-07-18 23:09:45 +0200332 unsigned char *Xp;
333
334 MPI_VALIDATE_RET( X != NULL );
335 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
336
337 /* Ensure that target MPI has at least the necessary number of limbs */
338 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
339
Janos Follathf1d617d2022-07-21 09:29:32 +0100340 overhead = ( nx * ciL ) - buflen;
341
Gabor Mezeib9030702022-07-18 23:09:45 +0200342 /* Avoid calling `memcpy` with NULL source or destination argument,
343 * even if buflen is 0. */
344 if( buflen != 0 )
345 {
346 Xp = (unsigned char*) X;
347 memcpy( Xp + overhead, buf, buflen );
348
Janos Follath4670f882022-07-21 18:25:42 +0100349 mbedtls_mpi_core_bigendian_to_host( X, nx );
Gabor Mezeib9030702022-07-18 23:09:45 +0200350 }
351
352cleanup:
353 return( ret );
354}
355
356/*
357 * Export X into unsigned binary data, little endian
358 */
359int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
360 size_t nx,
361 unsigned char *buf,
362 size_t buflen )
363{
364 size_t stored_bytes = nx * ciL;
365 size_t bytes_to_copy;
366 size_t i;
367
368 if( stored_bytes < buflen )
369 {
370 bytes_to_copy = stored_bytes;
371 }
372 else
373 {
374 bytes_to_copy = buflen;
375
376 /* The output buffer is smaller than the allocated size of X.
377 * However X may fit if its leading bytes are zero. */
378 for( i = bytes_to_copy; i < stored_bytes; i++ )
379 {
380 if( GET_BYTE( X, i ) != 0 )
381 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
382 }
383 }
384
385 for( i = 0; i < bytes_to_copy; i++ )
386 buf[i] = GET_BYTE( X, i );
387
388 if( stored_bytes < buflen )
389 {
390 /* Write trailing 0 bytes */
391 memset( buf + stored_bytes, 0, buflen - stored_bytes );
392 }
393
394 return( 0 );
395}
396
397/*
398 * Export X into unsigned binary data, big endian
399 */
400int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
401 size_t nx,
402 unsigned char *buf,
403 size_t buflen )
404{
405 size_t stored_bytes;
406 size_t bytes_to_copy;
407 unsigned char *p;
408 size_t i;
409
410 MPI_VALIDATE_RET( X != NULL );
411 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
412
413 stored_bytes = nx * ciL;
414
415 if( stored_bytes < buflen )
416 {
417 /* There is enough space in the output buffer. Write initial
418 * null bytes and record the position at which to start
419 * writing the significant bytes. In this case, the execution
420 * trace of this function does not depend on the value of the
421 * number. */
422 bytes_to_copy = stored_bytes;
423 p = buf + buflen - stored_bytes;
424 memset( buf, 0, buflen - stored_bytes );
425 }
426 else
427 {
428 /* The output buffer is smaller than the allocated size of X.
429 * However X may fit if its leading bytes are zero. */
430 bytes_to_copy = buflen;
431 p = buf;
432 for( i = bytes_to_copy; i < stored_bytes; i++ )
433 {
434 if( GET_BYTE( X, i ) != 0 )
435 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
436 }
437 }
438
439 for( i = 0; i < bytes_to_copy; i++ )
440 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
441
442 return( 0 );
443}
444
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200445int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
446 mbedtls_mpi_mod_modulus *m,
447 unsigned char *buf,
448 size_t buflen )
449{
Janos Follath5005edb2022-07-19 12:45:13 +0100450 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200451 return mbedtls_mpi_core_read_le( X, m->n, buf, buflen );
452
Janos Follath5005edb2022-07-19 12:45:13 +0100453 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200454 return mbedtls_mpi_core_read_be( X, m->n, buf, buflen );
455
456 else
457 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
458
459 return( 0 );
460}
461
462int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X,
463 mbedtls_mpi_mod_modulus *m,
464 unsigned char *buf,
465 size_t buflen )
466{
Janos Follath5005edb2022-07-19 12:45:13 +0100467 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200468 return mbedtls_mpi_core_write_le( X, m->n, buf, buflen );
469
Janos Follath5005edb2022-07-19 12:45:13 +0100470 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200471 return mbedtls_mpi_core_write_be( X, m->n, buf, buflen );
472
473 else
474 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
475
476 return( 0 );
477}
478
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200479#endif /* MBEDTLS_BIGNUM_C */