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