blob: 1c5cb8ca1013d4b9ca9c5673077d9476086f5e4e [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 Mezeib9030702022-07-18 23:09:45 +020043#define MPI_VALIDATE_RET( cond ) \
44 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
45#define MPI_VALIDATE( cond ) \
46 MBEDTLS_INTERNAL_VALIDATE( cond )
47
Gabor Mezeif049dbf2022-07-18 23:02:33 +020048#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
49#define biL (ciL << 3) /* bits in limb */
50#define biH (ciL << 2) /* half limb size */
51
52/*
53 * Convert between bits/chars and number of limbs
54 * Divide first in order to avoid potential overflows
55 */
56#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
57#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
58
59/*
60 * Count leading zero bits in a given integer
61 */
62static size_t mpi_clz( const mbedtls_mpi_uint x )
63{
64 size_t j;
65 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
66
67 for( j = 0; j < biL; j++ )
68 {
69 if( x & mask ) break;
70
71 mask >>= 1;
72 }
73
74 return j;
75}
76
77/*
78 * Return the number of bits
79 */
80static size_t mpi_bitlen( const mbedtls_mpi_uint *X, size_t nx )
81{
82 size_t i, j;
83
84 if( nx == 0 )
85 return( 0 );
86
87 for( i = nx - 1; i > 0; i-- )
88 if( X[i] != 0 )
89 break;
90
91 j = biL - mpi_clz( X[i] );
92
93 return( ( i * biL ) + j );
94}
95
Gabor Mezeib9030702022-07-18 23:09:45 +020096/* Get a specific byte, without range checks. */
97#define GET_BYTE( X, i ) \
98 ( ( ( X )[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
99
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200100void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
101{
102 if ( r == NULL )
103 return;
104
105 r->n = 0;
106 r->p = NULL;
107}
108
109int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
110 mbedtls_mpi_mod_modulus *m,
111 mbedtls_mpi_uint *X )
112{
113 if( X == NULL || m == NULL || r == NULL || X >= m->p)
114 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
115
116 r->n = m->n;
117 r->p = X;
118
119 return( 0 );
120}
121
122void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
123{
124 if ( m == NULL )
125 return;
126
Janos Follath281ccda2022-07-19 13:14:36 +0100127 m->p = NULL;
128 m->n = 0;
129 m->plen = 0;
130 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
131 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200132}
133
134void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
135{
136 if ( m == NULL )
137 return;
138
Janos Follathba5c1392022-07-19 13:42:07 +0100139 switch( m->int_rep )
140 {
141 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
142 mbedtls_free( m->rep.mont ); break;
143 case MBEDTLS_MPI_MOD_REP_OPT_RED:
144 mbedtls_free( m->rep.mont ); break;
145 default:
146 break;
147 }
148
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200149 m->p = NULL;
150 m->n = 0;
151 m->plen = 0;
Janos Follath281ccda2022-07-19 13:14:36 +0100152 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
153 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200154}
155
156int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
157 mbedtls_mpi_uint *X,
158 size_t nx,
159 int ext_rep,
160 int int_rep )
161{
Janos Follathba5c1392022-07-19 13:42:07 +0100162 int ret = 0;
163
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200164 if ( X == NULL || m == NULL )
165 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
166
167 m->p = X;
168 m->n = nx;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200169 m->plen = mpi_bitlen( X, nx );
170
Janos Follathba5c1392022-07-19 13:42:07 +0100171 switch( ext_rep )
172 {
173 case MBEDTLS_MPI_MOD_EXT_REP_LE:
174 case MBEDTLS_MPI_MOD_EXT_REP_BE:
175 m->ext_rep = ext_rep; break;
176 default:
177 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
178 goto exit;
179 }
180
181 switch( int_rep )
182 {
183 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
184 m->int_rep = int_rep;
185 m->rep.mont = NULL; break;
186 case MBEDTLS_MPI_MOD_REP_OPT_RED:
187 m->int_rep = int_rep;
188 m->rep.ored = NULL; break;
189 default:
190 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
191 goto exit;
192 }
193
194exit:
195
196 if( ret != 0 )
197 {
198 mbedtls_mpi_mod_modulus_free( m );
199 }
200
201 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200202}
203
Gabor Mezeib9030702022-07-18 23:09:45 +0200204/* Check X to have at least n limbs and set it to 0. */
205static int mpi_core_clear( mbedtls_mpi_uint *X,
206 size_t nx,
207 size_t limbs )
208{
209 if( X == NULL )
210 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
211
212 else if( nx < limbs )
213 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
214
215 else
216 {
217 memset( X, 0, nx * ciL );
218 return( 0 );
219 }
220}
221
222/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
223 * into the storage form used by mbedtls_mpi. */
224
225static 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
283static void mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
284 size_t limbs )
285{
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;
350 size_t const limbs = CHARS_TO_LIMBS( buflen );
351 size_t const overhead = ( limbs * ciL ) - buflen;
352 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
360 /* Avoid calling `memcpy` with NULL source or destination argument,
361 * even if buflen is 0. */
362 if( buflen != 0 )
363 {
364 Xp = (unsigned char*) X;
365 memcpy( Xp + overhead, buf, buflen );
366
367 mpi_core_bigendian_to_host( X, nx );
368 }
369
370cleanup:
371 return( ret );
372}
373
374/*
375 * Export X into unsigned binary data, little endian
376 */
377int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
378 size_t nx,
379 unsigned char *buf,
380 size_t buflen )
381{
382 size_t stored_bytes = nx * ciL;
383 size_t bytes_to_copy;
384 size_t i;
385
386 if( stored_bytes < buflen )
387 {
388 bytes_to_copy = stored_bytes;
389 }
390 else
391 {
392 bytes_to_copy = buflen;
393
394 /* The output buffer is smaller than the allocated size of X.
395 * However X may fit if its leading bytes are zero. */
396 for( i = bytes_to_copy; i < stored_bytes; i++ )
397 {
398 if( GET_BYTE( X, i ) != 0 )
399 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
400 }
401 }
402
403 for( i = 0; i < bytes_to_copy; i++ )
404 buf[i] = GET_BYTE( X, i );
405
406 if( stored_bytes < buflen )
407 {
408 /* Write trailing 0 bytes */
409 memset( buf + stored_bytes, 0, buflen - stored_bytes );
410 }
411
412 return( 0 );
413}
414
415/*
416 * Export X into unsigned binary data, big endian
417 */
418int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
419 size_t nx,
420 unsigned char *buf,
421 size_t buflen )
422{
423 size_t stored_bytes;
424 size_t bytes_to_copy;
425 unsigned char *p;
426 size_t i;
427
428 MPI_VALIDATE_RET( X != NULL );
429 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
430
431 stored_bytes = nx * ciL;
432
433 if( stored_bytes < buflen )
434 {
435 /* There is enough space in the output buffer. Write initial
436 * null bytes and record the position at which to start
437 * writing the significant bytes. In this case, the execution
438 * trace of this function does not depend on the value of the
439 * number. */
440 bytes_to_copy = stored_bytes;
441 p = buf + buflen - stored_bytes;
442 memset( buf, 0, buflen - stored_bytes );
443 }
444 else
445 {
446 /* The output buffer is smaller than the allocated size of X.
447 * However X may fit if its leading bytes are zero. */
448 bytes_to_copy = buflen;
449 p = buf;
450 for( i = bytes_to_copy; i < stored_bytes; i++ )
451 {
452 if( GET_BYTE( X, i ) != 0 )
453 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
454 }
455 }
456
457 for( i = 0; i < bytes_to_copy; i++ )
458 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
459
460 return( 0 );
461}
462
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200463int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
464 mbedtls_mpi_mod_modulus *m,
465 unsigned char *buf,
466 size_t buflen )
467{
Janos Follath5005edb2022-07-19 12:45:13 +0100468 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200469 return mbedtls_mpi_core_read_le( X, m->n, buf, buflen );
470
Janos Follath5005edb2022-07-19 12:45:13 +0100471 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200472 return mbedtls_mpi_core_read_be( X, m->n, buf, buflen );
473
474 else
475 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
476
477 return( 0 );
478}
479
480int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X,
481 mbedtls_mpi_mod_modulus *m,
482 unsigned char *buf,
483 size_t buflen )
484{
Janos Follath5005edb2022-07-19 12:45:13 +0100485 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200486 return mbedtls_mpi_core_write_le( X, m->n, buf, buflen );
487
Janos Follath5005edb2022-07-19 12:45:13 +0100488 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200489 return mbedtls_mpi_core_write_be( X, m->n, buf, buflen );
490
491 else
492 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
493
494 return( 0 );
495}
496
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200497#endif /* MBEDTLS_BIGNUM_C */