blob: 5f10eadcd14d3410902346df461d9bfd46c10766 [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
Janos Follath281ccda2022-07-19 13:14:36 +0100117 m->p = NULL;
118 m->n = 0;
119 m->plen = 0;
120 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
121 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200122}
123
124void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
125{
126 if ( m == NULL )
127 return;
128
129 m->p = NULL;
130 m->n = 0;
131 m->plen = 0;
Janos Follath281ccda2022-07-19 13:14:36 +0100132 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
133 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200134}
135
136int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
137 mbedtls_mpi_uint *X,
138 size_t nx,
139 int ext_rep,
140 int int_rep )
141{
142 if ( X == NULL || m == NULL )
143 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
144
145 m->p = X;
146 m->n = nx;
147 m->ext_rep = ext_rep;
148 m->int_rep = int_rep;
149 m->plen = mpi_bitlen( X, nx );
150
151 return( 0 );
152}
153
Gabor Mezeib9030702022-07-18 23:09:45 +0200154/* Check X to have at least n limbs and set it to 0. */
155static int mpi_core_clear( mbedtls_mpi_uint *X,
156 size_t nx,
157 size_t limbs )
158{
159 if( X == NULL )
160 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
161
162 else if( nx < limbs )
163 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
164
165 else
166 {
167 memset( X, 0, nx * ciL );
168 return( 0 );
169 }
170}
171
172/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
173 * into the storage form used by mbedtls_mpi. */
174
175static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
176{
177 uint8_t i;
178 unsigned char *x_ptr;
179 mbedtls_mpi_uint tmp = 0;
180
181 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
182 {
183 tmp <<= CHAR_BIT;
184 tmp |= (mbedtls_mpi_uint) *x_ptr;
185 }
186
187 return( tmp );
188}
189
190static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
191{
192#if defined(__BYTE_ORDER__)
193
194/* Nothing to do on bigendian systems. */
195#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
196 return( x );
197#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
198
199#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
200
201/* For GCC and Clang, have builtins for byte swapping. */
202#if defined(__GNUC__) && defined(__GNUC_PREREQ)
203#if __GNUC_PREREQ(4,3)
204#define have_bswap
205#endif
206#endif
207
208#if defined(__clang__) && defined(__has_builtin)
209#if __has_builtin(__builtin_bswap32) && \
210 __has_builtin(__builtin_bswap64)
211#define have_bswap
212#endif
213#endif
214
215#if defined(have_bswap)
216 /* The compiler is hopefully able to statically evaluate this! */
217 switch( sizeof(mbedtls_mpi_uint) )
218 {
219 case 4:
220 return( __builtin_bswap32(x) );
221 case 8:
222 return( __builtin_bswap64(x) );
223 }
224#endif
225#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
226#endif /* __BYTE_ORDER__ */
227
228 /* Fall back to C-based reordering if we don't know the byte order
229 * or we couldn't use a compiler-specific builtin. */
230 return( mpi_bigendian_to_host_c( x ) );
231}
232
233static void mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
234 size_t limbs )
235{
236 mbedtls_mpi_uint *cur_limb_left;
237 mbedtls_mpi_uint *cur_limb_right;
238 if( limbs == 0 )
239 return;
240
241 /*
242 * Traverse limbs and
243 * - adapt byte-order in each limb
244 * - swap the limbs themselves.
245 * For that, simultaneously traverse the limbs from left to right
246 * and from right to left, as long as the left index is not bigger
247 * than the right index (it's not a problem if limbs is odd and the
248 * indices coincide in the last iteration).
249 */
250 for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 );
251 cur_limb_left <= cur_limb_right;
252 cur_limb_left++, cur_limb_right-- )
253 {
254 mbedtls_mpi_uint tmp;
255 /* Note that if cur_limb_left == cur_limb_right,
256 * this code effectively swaps the bytes only once. */
257 tmp = mpi_bigendian_to_host( *cur_limb_left );
258 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
259 *cur_limb_right = tmp;
260 }
261}
262
263/*
264 * Import X from unsigned binary data, little endian
265 *
266 * This function is guaranteed to return an MPI with at least the necessary
267 * number of limbs (in particular, it does not skip 0s in the input).
268 */
269int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
270 size_t nx,
271 const unsigned char *buf,
272 size_t buflen )
273{
274 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
275 size_t i;
276 size_t const limbs = CHARS_TO_LIMBS( buflen );
277
278 /* Ensure that target MPI has at least the necessary number of limbs */
279 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
280
281 for( i = 0; i < buflen; i++ )
282 X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
283
284cleanup:
285 return( ret );
286}
287
288/*
289 * Import X from unsigned binary data, big endian
290 *
291 * This function is guaranteed to return an MPI with exactly the necessary
292 * number of limbs (in particular, it does not skip 0s in the input).
293 */
294int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
295 size_t nx,
296 const unsigned char *buf,
297 size_t buflen )
298{
299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
300 size_t const limbs = CHARS_TO_LIMBS( buflen );
301 size_t const overhead = ( limbs * ciL ) - buflen;
302 unsigned char *Xp;
303
304 MPI_VALIDATE_RET( X != NULL );
305 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
306
307 /* Ensure that target MPI has at least the necessary number of limbs */
308 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
309
310 /* Avoid calling `memcpy` with NULL source or destination argument,
311 * even if buflen is 0. */
312 if( buflen != 0 )
313 {
314 Xp = (unsigned char*) X;
315 memcpy( Xp + overhead, buf, buflen );
316
317 mpi_core_bigendian_to_host( X, nx );
318 }
319
320cleanup:
321 return( ret );
322}
323
324/*
325 * Export X into unsigned binary data, little endian
326 */
327int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
328 size_t nx,
329 unsigned char *buf,
330 size_t buflen )
331{
332 size_t stored_bytes = nx * ciL;
333 size_t bytes_to_copy;
334 size_t i;
335
336 if( stored_bytes < buflen )
337 {
338 bytes_to_copy = stored_bytes;
339 }
340 else
341 {
342 bytes_to_copy = buflen;
343
344 /* The output buffer is smaller than the allocated size of X.
345 * However X may fit if its leading bytes are zero. */
346 for( i = bytes_to_copy; i < stored_bytes; i++ )
347 {
348 if( GET_BYTE( X, i ) != 0 )
349 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
350 }
351 }
352
353 for( i = 0; i < bytes_to_copy; i++ )
354 buf[i] = GET_BYTE( X, i );
355
356 if( stored_bytes < buflen )
357 {
358 /* Write trailing 0 bytes */
359 memset( buf + stored_bytes, 0, buflen - stored_bytes );
360 }
361
362 return( 0 );
363}
364
365/*
366 * Export X into unsigned binary data, big endian
367 */
368int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
369 size_t nx,
370 unsigned char *buf,
371 size_t buflen )
372{
373 size_t stored_bytes;
374 size_t bytes_to_copy;
375 unsigned char *p;
376 size_t i;
377
378 MPI_VALIDATE_RET( X != NULL );
379 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
380
381 stored_bytes = nx * ciL;
382
383 if( stored_bytes < buflen )
384 {
385 /* There is enough space in the output buffer. Write initial
386 * null bytes and record the position at which to start
387 * writing the significant bytes. In this case, the execution
388 * trace of this function does not depend on the value of the
389 * number. */
390 bytes_to_copy = stored_bytes;
391 p = buf + buflen - stored_bytes;
392 memset( buf, 0, buflen - stored_bytes );
393 }
394 else
395 {
396 /* The output buffer is smaller than the allocated size of X.
397 * However X may fit if its leading bytes are zero. */
398 bytes_to_copy = buflen;
399 p = buf;
400 for( i = bytes_to_copy; i < stored_bytes; i++ )
401 {
402 if( GET_BYTE( X, i ) != 0 )
403 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
404 }
405 }
406
407 for( i = 0; i < bytes_to_copy; i++ )
408 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
409
410 return( 0 );
411}
412
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200413int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
414 mbedtls_mpi_mod_modulus *m,
415 unsigned char *buf,
416 size_t buflen )
417{
Janos Follath5005edb2022-07-19 12:45:13 +0100418 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200419 return mbedtls_mpi_core_read_le( X, m->n, buf, buflen );
420
Janos Follath5005edb2022-07-19 12:45:13 +0100421 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200422 return mbedtls_mpi_core_read_be( X, m->n, buf, buflen );
423
424 else
425 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
426
427 return( 0 );
428}
429
430int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X,
431 mbedtls_mpi_mod_modulus *m,
432 unsigned char *buf,
433 size_t buflen )
434{
Janos Follath5005edb2022-07-19 12:45:13 +0100435 if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_LE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200436 return mbedtls_mpi_core_write_le( X, m->n, buf, buflen );
437
Janos Follath5005edb2022-07-19 12:45:13 +0100438 else if( m->ext_rep & MBEDTLS_MPI_MOD_EXT_REP_BE )
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200439 return mbedtls_mpi_core_write_be( X, m->n, buf, buflen );
440
441 else
442 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
443
444 return( 0 );
445}
446
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200447#endif /* MBEDTLS_BIGNUM_C */