blob: 3c083a438962b07fc8c4ae829c073cef807a2d75 [file] [log] [blame]
Janos Follath3ca07752022-08-09 11:45:47 +01001/*
2 * Multi-precision integer library
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
24#include <string.h>
25
26#include "mbedtls/error.h"
27#include "mbedtls/platform_util.h"
28
29#if defined(MBEDTLS_PLATFORM_C)
30#include "mbedtls/platform.h"
31#else
32#include <stdio.h>
33#include <stdlib.h>
34#define mbedtls_printf printf
35#define mbedtls_calloc calloc
36#define mbedtls_free free
37#endif
38
39#include "bignum_core.h"
40
Janos Follath3ca07752022-08-09 11:45:47 +010041#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
42#define biL (ciL << 3) /* bits in limb */
43#define biH (ciL << 2) /* half limb size */
44
45/*
46 * Convert between bits/chars and number of limbs
47 * Divide first in order to avoid potential overflows
48 */
49#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
50#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
51/* Get a specific byte, without range checks. */
52#define GET_BYTE( X, i ) \
53 ( ( ( X )[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
54
55/*
56 * Count leading zero bits in a given integer
57 */
58size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x )
59{
60 size_t j;
61 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
62
63 for( j = 0; j < biL; j++ )
64 {
65 if( x & mask ) break;
66
67 mask >>= 1;
68 }
69
70 return j;
71}
72
73/*
74 * Return the number of bits
75 */
76size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx )
77{
78 size_t i, j;
79
80 if( nx == 0 )
81 return( 0 );
82
83 for( i = nx - 1; i > 0; i-- )
84 if( X[i] != 0 )
85 break;
86
87 j = biL - mbedtls_mpi_core_clz( X[i] );
88
89 return( ( i * biL ) + j );
90}
91
92/* Check X to have at least n limbs and set it to 0. */
93static int mpi_core_clear( mbedtls_mpi_uint *X,
94 size_t nx,
95 size_t limbs )
96{
97 if( nx < limbs )
98 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
99
100 if( X != NULL )
101 memset( X, 0, nx * ciL );
102
103 return( 0 );
104}
105
106/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
107 * into the storage form used by mbedtls_mpi. */
108static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
109{
110 uint8_t i;
111 unsigned char *x_ptr;
112 mbedtls_mpi_uint tmp = 0;
113
114 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
115 {
116 tmp <<= CHAR_BIT;
117 tmp |= (mbedtls_mpi_uint) *x_ptr;
118 }
119
120 return( tmp );
121}
122
123static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
124{
125#if defined(__BYTE_ORDER__)
126
127/* Nothing to do on bigendian systems. */
128#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
129 return( x );
130#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
131
132#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
133
134/* For GCC and Clang, have builtins for byte swapping. */
135#if defined(__GNUC__) && defined(__GNUC_PREREQ)
136#if __GNUC_PREREQ(4,3)
137#define have_bswap
138#endif
139#endif
140
141#if defined(__clang__) && defined(__has_builtin)
142#if __has_builtin(__builtin_bswap32) && \
143 __has_builtin(__builtin_bswap64)
144#define have_bswap
145#endif
146#endif
147
148#if defined(have_bswap)
149 /* The compiler is hopefully able to statically evaluate this! */
150 switch( sizeof(mbedtls_mpi_uint) )
151 {
152 case 4:
153 return( __builtin_bswap32(x) );
154 case 8:
155 return( __builtin_bswap64(x) );
156 }
157#endif
158#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
159#endif /* __BYTE_ORDER__ */
160
161 /* Fall back to C-based reordering if we don't know the byte order
162 * or we couldn't use a compiler-specific builtin. */
163 return( mpi_bigendian_to_host_c( x ) );
164}
165
166void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
167 size_t limbs )
168{
169 mbedtls_mpi_uint *cur_limb_left;
170 mbedtls_mpi_uint *cur_limb_right;
171 if( limbs == 0 )
172 return;
173
174 /*
175 * Traverse limbs and
176 * - adapt byte-order in each limb
177 * - swap the limbs themselves.
178 * For that, simultaneously traverse the limbs from left to right
179 * and from right to left, as long as the left index is not bigger
180 * than the right index (it's not a problem if limbs is odd and the
181 * indices coincide in the last iteration).
182 */
183 for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 );
184 cur_limb_left <= cur_limb_right;
185 cur_limb_left++, cur_limb_right-- )
186 {
187 mbedtls_mpi_uint tmp;
188 /* Note that if cur_limb_left == cur_limb_right,
189 * this code effectively swaps the bytes only once. */
190 tmp = mpi_bigendian_to_host( *cur_limb_left );
191 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
192 *cur_limb_right = tmp;
193 }
194}
195
196/*
197 * Import X from unsigned binary data, little endian
198 *
199 * The MPI needs to have enough limbs to store the full value (in particular,
200 * this function does not skip 0s in the input).
201 */
202int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
203 size_t nx,
204 const unsigned char *buf,
205 size_t buflen )
206{
207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
208 size_t i;
209 size_t const limbs = CHARS_TO_LIMBS( buflen );
210
211 /* Ensure that target MPI has at least the necessary number of limbs */
212 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
213
214 for( i = 0; i < buflen; i++ )
215 X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
216
217cleanup:
218 return( ret );
219}
220
221/*
222 * Import X from unsigned binary data, big endian
223 *
224 * The MPI needs to have enough limbs to store the full value (in particular,
225 * this function does not skip 0s in the input).
226 */
227int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
228 size_t nx,
229 const unsigned char *buf,
230 size_t buflen )
231{
232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
233 size_t const limbs = CHARS_TO_LIMBS( buflen );
234 size_t overhead;
235 unsigned char *Xp;
236
Janos Follath3ca07752022-08-09 11:45:47 +0100237 /* Ensure that target MPI has at least the necessary number of limbs */
238 MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) );
239
240 overhead = ( nx * ciL ) - buflen;
241
242 /* Avoid calling `memcpy` with NULL source or destination argument,
243 * even if buflen is 0. */
244 if( buflen != 0 )
245 {
246 Xp = (unsigned char*) X;
247 memcpy( Xp + overhead, buf, buflen );
248
249 mbedtls_mpi_core_bigendian_to_host( X, nx );
250 }
251
252cleanup:
253 return( ret );
254}
255
256/*
257 * Export X into unsigned binary data, little endian
258 */
259int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
260 size_t nx,
261 unsigned char *buf,
262 size_t buflen )
263{
264 size_t stored_bytes = nx * ciL;
265 size_t bytes_to_copy;
266 size_t i;
267
268 if( stored_bytes < buflen )
269 {
270 bytes_to_copy = stored_bytes;
271 }
272 else
273 {
274 bytes_to_copy = buflen;
275
276 /* The output buffer is smaller than the allocated size of X.
277 * However X may fit if its leading bytes are zero. */
278 for( i = bytes_to_copy; i < stored_bytes; i++ )
279 {
280 if( GET_BYTE( X, i ) != 0 )
281 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
282 }
283 }
284
285 for( i = 0; i < bytes_to_copy; i++ )
286 buf[i] = GET_BYTE( X, i );
287
288 if( stored_bytes < buflen )
289 {
290 /* Write trailing 0 bytes */
291 memset( buf + stored_bytes, 0, buflen - stored_bytes );
292 }
293
294 return( 0 );
295}
296
297/*
298 * Export X into unsigned binary data, big endian
299 */
300int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
301 size_t nx,
302 unsigned char *buf,
303 size_t buflen )
304{
305 size_t stored_bytes;
306 size_t bytes_to_copy;
307 unsigned char *p;
308 size_t i;
309
Janos Follath3ca07752022-08-09 11:45:47 +0100310 stored_bytes = nx * ciL;
311
312 if( stored_bytes < buflen )
313 {
314 /* There is enough space in the output buffer. Write initial
315 * null bytes and record the position at which to start
316 * writing the significant bytes. In this case, the execution
317 * trace of this function does not depend on the value of the
318 * number. */
319 bytes_to_copy = stored_bytes;
320 p = buf + buflen - stored_bytes;
321 memset( buf, 0, buflen - stored_bytes );
322 }
323 else
324 {
325 /* The output buffer is smaller than the allocated size of X.
326 * However X may fit if its leading bytes are zero. */
327 bytes_to_copy = buflen;
328 p = buf;
329 for( i = bytes_to_copy; i < stored_bytes; i++ )
330 {
331 if( GET_BYTE( X, i ) != 0 )
332 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
333 }
334 }
335
336 for( i = 0; i < bytes_to_copy; i++ )
337 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
338
339 return( 0 );
340}
341
342#endif /* MBEDTLS_BIGNUM_C */