blob: b6a806ecbce1cdc5e35cd789e15a2fd257cfdc42 [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
Gabor Mezeib9030702022-07-18 23:09:45 +020033#define MPI_VALIDATE_RET( cond ) \
34 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
35#define MPI_VALIDATE( cond ) \
36 MBEDTLS_INTERNAL_VALIDATE( cond )
37
Gabor Mezeif049dbf2022-07-18 23:02:33 +020038#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
39#define biL (ciL << 3) /* bits in limb */
40#define biH (ciL << 2) /* half limb size */
41
42/*
43 * Convert between bits/chars and number of limbs
44 * Divide first in order to avoid potential overflows
45 */
46#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
47#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
48
49/*
50 * Count leading zero bits in a given integer
51 */
52static size_t mpi_clz( const mbedtls_mpi_uint x )
53{
54 size_t j;
55 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
56
57 for( j = 0; j < biL; j++ )
58 {
59 if( x & mask ) break;
60
61 mask >>= 1;
62 }
63
64 return j;
65}
66
67/*
68 * Return the number of bits
69 */
70static size_t mpi_bitlen( const mbedtls_mpi_uint *X, size_t nx )
71{
72 size_t i, j;
73
74 if( nx == 0 )
75 return( 0 );
76
77 for( i = nx - 1; i > 0; i-- )
78 if( X[i] != 0 )
79 break;
80
81 j = biL - mpi_clz( X[i] );
82
83 return( ( i * biL ) + j );
84}
85
Gabor Mezeib9030702022-07-18 23:09:45 +020086/* Get a specific byte, without range checks. */
87#define GET_BYTE( X, i ) \
88 ( ( ( X )[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
89
Gabor Mezeif049dbf2022-07-18 23:02:33 +020090void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
91{
92 if ( r == NULL )
93 return;
94
95 r->n = 0;
96 r->p = NULL;
97}
98
99int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
100 mbedtls_mpi_mod_modulus *m,
101 mbedtls_mpi_uint *X )
102{
103 if( X == NULL || m == NULL || r == NULL || X >= m->p)
104 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
105
106 r->n = m->n;
107 r->p = X;
108
109 return( 0 );
110}
111
112void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
113{
114 if ( m == NULL )
115 return;
116
117 m->rep.mont = 0;
118}
119
120void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
121{
122 if ( m == NULL )
123 return;
124
125 m->p = NULL;
126 m->n = 0;
127 m->plen = 0;
128 m->ext_rep = 0;
129 m->int_rep = 0;
130 m->rep.mont = NULL;
131 m->rep.ored = NULL;
132}
133
134int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
135 mbedtls_mpi_uint *X,
136 size_t nx,
137 int ext_rep,
138 int int_rep )
139{
140 if ( X == NULL || m == NULL )
141 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
142
143 m->p = X;
144 m->n = nx;
145 m->ext_rep = ext_rep;
146 m->int_rep = int_rep;
147 m->plen = mpi_bitlen( X, nx );
148
149 return( 0 );
150}
151
Gabor Mezeib9030702022-07-18 23:09:45 +0200152/* Check X to have at least n limbs and set it to 0. */
153static int mpi_core_clear( mbedtls_mpi_uint *X,
154 size_t nx,
155 size_t limbs )
156{
157 if( X == NULL )
158 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
159
160 else if( nx < limbs )
161 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
162
163 else
164 {
165 memset( X, 0, nx * ciL );
166 return( 0 );
167 }
168}
169
170/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
171 * into the storage form used by mbedtls_mpi. */
172
173static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
174{
175 uint8_t i;
176 unsigned char *x_ptr;
177 mbedtls_mpi_uint tmp = 0;
178
179 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
180 {
181 tmp <<= CHAR_BIT;
182 tmp |= (mbedtls_mpi_uint) *x_ptr;
183 }
184
185 return( tmp );
186}
187
188static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
189{
190#if defined(__BYTE_ORDER__)
191
192/* Nothing to do on bigendian systems. */
193#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
194 return( x );
195#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
196
197#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
198
199/* For GCC and Clang, have builtins for byte swapping. */
200#if defined(__GNUC__) && defined(__GNUC_PREREQ)
201#if __GNUC_PREREQ(4,3)
202#define have_bswap
203#endif
204#endif
205
206#if defined(__clang__) && defined(__has_builtin)
207#if __has_builtin(__builtin_bswap32) && \
208 __has_builtin(__builtin_bswap64)
209#define have_bswap
210#endif
211#endif
212
213#if defined(have_bswap)
214 /* The compiler is hopefully able to statically evaluate this! */
215 switch( sizeof(mbedtls_mpi_uint) )
216 {
217 case 4:
218 return( __builtin_bswap32(x) );
219 case 8:
220 return( __builtin_bswap64(x) );
221 }
222#endif
223#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
224#endif /* __BYTE_ORDER__ */
225
226 /* Fall back to C-based reordering if we don't know the byte order
227 * or we couldn't use a compiler-specific builtin. */
228 return( mpi_bigendian_to_host_c( x ) );
229}
230
231static void mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
232 size_t limbs )
233{
234 mbedtls_mpi_uint *cur_limb_left;
235 mbedtls_mpi_uint *cur_limb_right;
236 if( limbs == 0 )
237 return;
238
239 /*
240 * Traverse limbs and
241 * - adapt byte-order in each limb
242 * - swap the limbs themselves.
243 * For that, simultaneously traverse the limbs from left to right
244 * and from right to left, as long as the left index is not bigger
245 * than the right index (it's not a problem if limbs is odd and the
246 * indices coincide in the last iteration).
247 */
248 for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 );
249 cur_limb_left <= cur_limb_right;
250 cur_limb_left++, cur_limb_right-- )
251 {
252 mbedtls_mpi_uint tmp;
253 /* Note that if cur_limb_left == cur_limb_right,
254 * this code effectively swaps the bytes only once. */
255 tmp = mpi_bigendian_to_host( *cur_limb_left );
256 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
257 *cur_limb_right = tmp;
258 }
259}
260
261/*
262 * Import X from unsigned binary data, little endian
263 *
264 * This function is guaranteed to return an MPI with at least the necessary
265 * number of limbs (in particular, it does not skip 0s in the input).
266 */
267int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
268 size_t nx,
269 const unsigned char *buf,
270 size_t buflen )
271{
272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
273 size_t i;
274 size_t const limbs = CHARS_TO_LIMBS( buflen );
275
276 /* Ensure that target MPI has at least the necessary number of limbs */
277 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
278
279 for( i = 0; i < buflen; i++ )
280 X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
281
282cleanup:
283 return( ret );
284}
285
286/*
287 * Import X from unsigned binary data, big endian
288 *
289 * This function is guaranteed to return an MPI with exactly the necessary
290 * number of limbs (in particular, it does not skip 0s in the input).
291 */
292int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
293 size_t nx,
294 const unsigned char *buf,
295 size_t buflen )
296{
297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
298 size_t const limbs = CHARS_TO_LIMBS( buflen );
299 size_t const overhead = ( limbs * ciL ) - buflen;
300 unsigned char *Xp;
301
302 MPI_VALIDATE_RET( X != NULL );
303 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
304
305 /* Ensure that target MPI has at least the necessary number of limbs */
306 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
307
308 /* Avoid calling `memcpy` with NULL source or destination argument,
309 * even if buflen is 0. */
310 if( buflen != 0 )
311 {
312 Xp = (unsigned char*) X;
313 memcpy( Xp + overhead, buf, buflen );
314
315 mpi_core_bigendian_to_host( X, nx );
316 }
317
318cleanup:
319 return( ret );
320}
321
322/*
323 * Export X into unsigned binary data, little endian
324 */
325int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
326 size_t nx,
327 unsigned char *buf,
328 size_t buflen )
329{
330 size_t stored_bytes = nx * ciL;
331 size_t bytes_to_copy;
332 size_t i;
333
334 if( stored_bytes < buflen )
335 {
336 bytes_to_copy = stored_bytes;
337 }
338 else
339 {
340 bytes_to_copy = buflen;
341
342 /* The output buffer is smaller than the allocated size of X.
343 * However X may fit if its leading bytes are zero. */
344 for( i = bytes_to_copy; i < stored_bytes; i++ )
345 {
346 if( GET_BYTE( X, i ) != 0 )
347 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
348 }
349 }
350
351 for( i = 0; i < bytes_to_copy; i++ )
352 buf[i] = GET_BYTE( X, i );
353
354 if( stored_bytes < buflen )
355 {
356 /* Write trailing 0 bytes */
357 memset( buf + stored_bytes, 0, buflen - stored_bytes );
358 }
359
360 return( 0 );
361}
362
363/*
364 * Export X into unsigned binary data, big endian
365 */
366int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
367 size_t nx,
368 unsigned char *buf,
369 size_t buflen )
370{
371 size_t stored_bytes;
372 size_t bytes_to_copy;
373 unsigned char *p;
374 size_t i;
375
376 MPI_VALIDATE_RET( X != NULL );
377 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
378
379 stored_bytes = nx * ciL;
380
381 if( stored_bytes < buflen )
382 {
383 /* There is enough space in the output buffer. Write initial
384 * null bytes and record the position at which to start
385 * writing the significant bytes. In this case, the execution
386 * trace of this function does not depend on the value of the
387 * number. */
388 bytes_to_copy = stored_bytes;
389 p = buf + buflen - stored_bytes;
390 memset( buf, 0, buflen - stored_bytes );
391 }
392 else
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 bytes_to_copy = buflen;
397 p = buf;
398 for( i = bytes_to_copy; i < stored_bytes; i++ )
399 {
400 if( GET_BYTE( X, i ) != 0 )
401 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
402 }
403 }
404
405 for( i = 0; i < bytes_to_copy; i++ )
406 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
407
408 return( 0 );
409}
410
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200411int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
412 mbedtls_mpi_mod_modulus *m,
413 unsigned char *buf,
414 size_t buflen )
415{
Janos Follath5005edb2022-07-19 12:45:13 +0100416 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200417 return mbedtls_mpi_core_read_le( X, m->n, buf, buflen );
418
Janos Follath5005edb2022-07-19 12:45:13 +0100419 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200420 return mbedtls_mpi_core_read_be( X, m->n, buf, buflen );
421
422 else
423 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
424
425 return( 0 );
426}
427
428int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X,
429 mbedtls_mpi_mod_modulus *m,
430 unsigned char *buf,
431 size_t buflen )
432{
Janos Follath5005edb2022-07-19 12:45:13 +0100433 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200434 return mbedtls_mpi_core_write_le( X, m->n, buf, buflen );
435
Janos Follath5005edb2022-07-19 12:45:13 +0100436 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200437 return mbedtls_mpi_core_write_be( X, m->n, buf, buflen );
438
439 else
440 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
441
442 return( 0 );
443}
444
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200445#endif /* MBEDTLS_BIGNUM_C */