blob: 1929c173f0b102a2227724546fc79505a4b3014a [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/*
42 * Count leading zero bits in a given integer
43 */
44size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x )
45{
46 size_t j;
47 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
48
49 for( j = 0; j < biL; j++ )
50 {
51 if( x & mask ) break;
52
53 mask >>= 1;
54 }
55
56 return j;
57}
58
59/*
60 * Return the number of bits
61 */
62size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx )
63{
64 size_t i, j;
65
66 if( nx == 0 )
67 return( 0 );
68
69 for( i = nx - 1; i > 0; i-- )
70 if( X[i] != 0 )
71 break;
72
73 j = biL - mbedtls_mpi_core_clz( X[i] );
74
75 return( ( i * biL ) + j );
76}
77
Janos Follath3ca07752022-08-09 11:45:47 +010078/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
79 * into the storage form used by mbedtls_mpi. */
80static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
81{
82 uint8_t i;
83 unsigned char *x_ptr;
84 mbedtls_mpi_uint tmp = 0;
85
86 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
87 {
88 tmp <<= CHAR_BIT;
89 tmp |= (mbedtls_mpi_uint) *x_ptr;
90 }
91
92 return( tmp );
93}
94
95static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
96{
97#if defined(__BYTE_ORDER__)
98
99/* Nothing to do on bigendian systems. */
100#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
101 return( x );
102#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
103
104#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
105
106/* For GCC and Clang, have builtins for byte swapping. */
107#if defined(__GNUC__) && defined(__GNUC_PREREQ)
108#if __GNUC_PREREQ(4,3)
109#define have_bswap
110#endif
111#endif
112
113#if defined(__clang__) && defined(__has_builtin)
114#if __has_builtin(__builtin_bswap32) && \
115 __has_builtin(__builtin_bswap64)
116#define have_bswap
117#endif
118#endif
119
120#if defined(have_bswap)
121 /* The compiler is hopefully able to statically evaluate this! */
122 switch( sizeof(mbedtls_mpi_uint) )
123 {
124 case 4:
125 return( __builtin_bswap32(x) );
126 case 8:
127 return( __builtin_bswap64(x) );
128 }
129#endif
130#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
131#endif /* __BYTE_ORDER__ */
132
133 /* Fall back to C-based reordering if we don't know the byte order
134 * or we couldn't use a compiler-specific builtin. */
135 return( mpi_bigendian_to_host_c( x ) );
136}
137
138void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
139 size_t limbs )
140{
141 mbedtls_mpi_uint *cur_limb_left;
142 mbedtls_mpi_uint *cur_limb_right;
143 if( limbs == 0 )
144 return;
145
146 /*
147 * Traverse limbs and
148 * - adapt byte-order in each limb
149 * - swap the limbs themselves.
150 * For that, simultaneously traverse the limbs from left to right
151 * and from right to left, as long as the left index is not bigger
152 * than the right index (it's not a problem if limbs is odd and the
153 * indices coincide in the last iteration).
154 */
155 for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 );
156 cur_limb_left <= cur_limb_right;
157 cur_limb_left++, cur_limb_right-- )
158 {
159 mbedtls_mpi_uint tmp;
160 /* Note that if cur_limb_left == cur_limb_right,
161 * this code effectively swaps the bytes only once. */
162 tmp = mpi_bigendian_to_host( *cur_limb_left );
163 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
164 *cur_limb_right = tmp;
165 }
166}
167
168/*
169 * Import X from unsigned binary data, little endian
170 *
171 * The MPI needs to have enough limbs to store the full value (in particular,
172 * this function does not skip 0s in the input).
173 */
174int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
175 size_t nx,
176 const unsigned char *buf,
177 size_t buflen )
178{
Janos Follath3ca07752022-08-09 11:45:47 +0100179 size_t i;
180 size_t const limbs = CHARS_TO_LIMBS( buflen );
181
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100182 if( nx < limbs )
183 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
184
185 if( X != NULL )
186 memset( X, 0, nx * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100187
188 for( i = 0; i < buflen; i++ )
189 X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
190
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100191 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100192}
193
194/*
195 * Import X from unsigned binary data, big endian
196 *
197 * The MPI needs to have enough limbs to store the full value (in particular,
198 * this function does not skip 0s in the input).
199 */
200int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
201 size_t nx,
202 const unsigned char *buf,
203 size_t buflen )
204{
Janos Follath3ca07752022-08-09 11:45:47 +0100205 size_t const limbs = CHARS_TO_LIMBS( buflen );
206 size_t overhead;
207 unsigned char *Xp;
208
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100209 if( nx < limbs )
210 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
211
212 if( X != NULL )
213 memset( X, 0, nx * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100214
215 overhead = ( nx * ciL ) - buflen;
216
217 /* Avoid calling `memcpy` with NULL source or destination argument,
218 * even if buflen is 0. */
Janos Follath56a10f92022-08-11 15:19:00 +0100219 if( buf != NULL )
Janos Follath3ca07752022-08-09 11:45:47 +0100220 {
221 Xp = (unsigned char*) X;
222 memcpy( Xp + overhead, buf, buflen );
223
224 mbedtls_mpi_core_bigendian_to_host( X, nx );
225 }
226
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100227 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100228}
229
230/*
231 * Export X into unsigned binary data, little endian
232 */
233int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
234 size_t nx,
235 unsigned char *buf,
236 size_t buflen )
237{
238 size_t stored_bytes = nx * ciL;
239 size_t bytes_to_copy;
240 size_t i;
241
242 if( stored_bytes < buflen )
243 {
244 bytes_to_copy = stored_bytes;
245 }
246 else
247 {
248 bytes_to_copy = buflen;
249
250 /* The output buffer is smaller than the allocated size of X.
251 * However X may fit if its leading bytes are zero. */
252 for( i = bytes_to_copy; i < stored_bytes; i++ )
253 {
254 if( GET_BYTE( X, i ) != 0 )
255 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
256 }
257 }
258
259 for( i = 0; i < bytes_to_copy; i++ )
260 buf[i] = GET_BYTE( X, i );
261
262 if( stored_bytes < buflen )
263 {
264 /* Write trailing 0 bytes */
265 memset( buf + stored_bytes, 0, buflen - stored_bytes );
266 }
267
268 return( 0 );
269}
270
271/*
272 * Export X into unsigned binary data, big endian
273 */
274int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
275 size_t nx,
276 unsigned char *buf,
277 size_t buflen )
278{
279 size_t stored_bytes;
280 size_t bytes_to_copy;
281 unsigned char *p;
282 size_t i;
283
Janos Follath3ca07752022-08-09 11:45:47 +0100284 stored_bytes = nx * ciL;
285
286 if( stored_bytes < buflen )
287 {
288 /* There is enough space in the output buffer. Write initial
289 * null bytes and record the position at which to start
290 * writing the significant bytes. In this case, the execution
291 * trace of this function does not depend on the value of the
292 * number. */
293 bytes_to_copy = stored_bytes;
294 p = buf + buflen - stored_bytes;
295 memset( buf, 0, buflen - stored_bytes );
296 }
297 else
298 {
299 /* The output buffer is smaller than the allocated size of X.
300 * However X may fit if its leading bytes are zero. */
301 bytes_to_copy = buflen;
302 p = buf;
303 for( i = bytes_to_copy; i < stored_bytes; i++ )
304 {
305 if( GET_BYTE( X, i ) != 0 )
306 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
307 }
308 }
309
310 for( i = 0; i < bytes_to_copy; i++ )
311 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
312
313 return( 0 );
314}
315
316#endif /* MBEDTLS_BIGNUM_C */