blob: 6cbc8678cf9b35185a1e7382fd8112c7e5651142 [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{
193 if( X == NULL )
194 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
195
196 else if( nx < limbs )
197 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
198
199 else
200 {
201 memset( X, 0, nx * ciL );
202 return( 0 );
203 }
204}
205
206/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
207 * into the storage form used by mbedtls_mpi. */
208
209static 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 Follath5005edb2022-07-19 12:45:13 +0100454 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200455 return mbedtls_mpi_core_read_le( X, m->n, buf, buflen );
456
Janos Follath5005edb2022-07-19 12:45:13 +0100457 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200458 return mbedtls_mpi_core_read_be( X, m->n, buf, buflen );
459
460 else
461 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
462
463 return( 0 );
464}
465
466int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X,
467 mbedtls_mpi_mod_modulus *m,
468 unsigned char *buf,
469 size_t buflen )
470{
Janos Follath5005edb2022-07-19 12:45:13 +0100471 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200472 return mbedtls_mpi_core_write_le( X, m->n, buf, buflen );
473
Janos Follath5005edb2022-07-19 12:45:13 +0100474 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200475 return mbedtls_mpi_core_write_be( X, m->n, buf, buflen );
476
477 else
478 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
479
480 return( 0 );
481}
482
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200483#endif /* MBEDTLS_BIGNUM_C */