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