blob: d5db4b0b0df989e67331998d7633a3332b9b34de [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 +010041size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x )
42{
43 size_t j;
44 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
45
46 for( j = 0; j < biL; j++ )
47 {
48 if( x & mask ) break;
49
50 mask >>= 1;
51 }
52
53 return j;
54}
55
Janos Follath3ca07752022-08-09 11:45:47 +010056size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx )
57{
58 size_t i, j;
59
60 if( nx == 0 )
61 return( 0 );
62
63 for( i = nx - 1; i > 0; i-- )
64 if( X[i] != 0 )
65 break;
66
67 j = biL - mbedtls_mpi_core_clz( X[i] );
68
69 return( ( i * biL ) + j );
70}
71
Janos Follath3ca07752022-08-09 11:45:47 +010072/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
73 * into the storage form used by mbedtls_mpi. */
74static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
75{
76 uint8_t i;
77 unsigned char *x_ptr;
78 mbedtls_mpi_uint tmp = 0;
79
80 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
81 {
82 tmp <<= CHAR_BIT;
83 tmp |= (mbedtls_mpi_uint) *x_ptr;
84 }
85
86 return( tmp );
87}
88
89static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
90{
91#if defined(__BYTE_ORDER__)
92
93/* Nothing to do on bigendian systems. */
94#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
95 return( x );
96#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
97
98#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
99
100/* For GCC and Clang, have builtins for byte swapping. */
101#if defined(__GNUC__) && defined(__GNUC_PREREQ)
102#if __GNUC_PREREQ(4,3)
103#define have_bswap
104#endif
105#endif
106
107#if defined(__clang__) && defined(__has_builtin)
108#if __has_builtin(__builtin_bswap32) && \
109 __has_builtin(__builtin_bswap64)
110#define have_bswap
111#endif
112#endif
113
114#if defined(have_bswap)
115 /* The compiler is hopefully able to statically evaluate this! */
116 switch( sizeof(mbedtls_mpi_uint) )
117 {
118 case 4:
119 return( __builtin_bswap32(x) );
120 case 8:
121 return( __builtin_bswap64(x) );
122 }
123#endif
124#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
125#endif /* __BYTE_ORDER__ */
126
127 /* Fall back to C-based reordering if we don't know the byte order
128 * or we couldn't use a compiler-specific builtin. */
129 return( mpi_bigendian_to_host_c( x ) );
130}
131
132void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
133 size_t limbs )
134{
135 mbedtls_mpi_uint *cur_limb_left;
136 mbedtls_mpi_uint *cur_limb_right;
137 if( limbs == 0 )
138 return;
139
140 /*
141 * Traverse limbs and
142 * - adapt byte-order in each limb
143 * - swap the limbs themselves.
144 * For that, simultaneously traverse the limbs from left to right
145 * and from right to left, as long as the left index is not bigger
146 * than the right index (it's not a problem if limbs is odd and the
147 * indices coincide in the last iteration).
148 */
149 for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 );
150 cur_limb_left <= cur_limb_right;
151 cur_limb_left++, cur_limb_right-- )
152 {
153 mbedtls_mpi_uint tmp;
154 /* Note that if cur_limb_left == cur_limb_right,
155 * this code effectively swaps the bytes only once. */
156 tmp = mpi_bigendian_to_host( *cur_limb_left );
157 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
158 *cur_limb_right = tmp;
159 }
160}
161
Janos Follath3ca07752022-08-09 11:45:47 +0100162int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
163 size_t nx,
164 const unsigned char *buf,
165 size_t buflen )
166{
Janos Follath3ca07752022-08-09 11:45:47 +0100167 size_t i;
168 size_t const limbs = CHARS_TO_LIMBS( buflen );
169
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100170 if( nx < limbs )
171 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
172
173 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200174 {
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100175 memset( X, 0, nx * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100176
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200177 for( i = 0; i < buflen; i++ )
178 X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
179 }
Janos Follath3ca07752022-08-09 11:45:47 +0100180
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100181 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100182}
183
Janos Follath3ca07752022-08-09 11:45:47 +0100184int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
185 size_t nx,
186 const unsigned char *buf,
187 size_t buflen )
188{
Janos Follath3ca07752022-08-09 11:45:47 +0100189 size_t const limbs = CHARS_TO_LIMBS( buflen );
190 size_t overhead;
191 unsigned char *Xp;
192
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100193 if( nx < limbs )
194 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
195
196 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200197 {
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100198 memset( X, 0, nx * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100199
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200200 overhead = ( nx * ciL ) - buflen;
Janos Follath3ca07752022-08-09 11:45:47 +0100201
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200202 /* Avoid calling `memcpy` with NULL source or destination argument,
203 * even if buflen is 0. */
204 if( buf != NULL && X != NULL )
205 {
206 Xp = (unsigned char*) X;
207 memcpy( Xp + overhead, buf, buflen );
Janos Follath3ca07752022-08-09 11:45:47 +0100208
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200209 mbedtls_mpi_core_bigendian_to_host( X, nx );
210 }
Janos Follath3ca07752022-08-09 11:45:47 +0100211 }
212
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100213 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100214}
215
Janos Follath3ca07752022-08-09 11:45:47 +0100216int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
217 size_t nx,
218 unsigned char *buf,
219 size_t buflen )
220{
221 size_t stored_bytes = nx * ciL;
222 size_t bytes_to_copy;
223 size_t i;
224
225 if( stored_bytes < buflen )
226 {
227 bytes_to_copy = stored_bytes;
228 }
229 else
230 {
231 bytes_to_copy = buflen;
232
233 /* The output buffer is smaller than the allocated size of X.
234 * However X may fit if its leading bytes are zero. */
235 for( i = bytes_to_copy; i < stored_bytes; i++ )
236 {
237 if( GET_BYTE( X, i ) != 0 )
238 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
239 }
240 }
241
242 for( i = 0; i < bytes_to_copy; i++ )
243 buf[i] = GET_BYTE( X, i );
244
245 if( stored_bytes < buflen )
246 {
247 /* Write trailing 0 bytes */
248 memset( buf + stored_bytes, 0, buflen - stored_bytes );
249 }
250
251 return( 0 );
252}
253
Janos Follath3ca07752022-08-09 11:45:47 +0100254int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
255 size_t nx,
256 unsigned char *buf,
257 size_t buflen )
258{
259 size_t stored_bytes;
260 size_t bytes_to_copy;
261 unsigned char *p;
262 size_t i;
263
Janos Follath3ca07752022-08-09 11:45:47 +0100264 stored_bytes = nx * ciL;
265
266 if( stored_bytes < buflen )
267 {
268 /* There is enough space in the output buffer. Write initial
269 * null bytes and record the position at which to start
270 * writing the significant bytes. In this case, the execution
271 * trace of this function does not depend on the value of the
272 * number. */
273 bytes_to_copy = stored_bytes;
274 p = buf + buflen - stored_bytes;
275 memset( buf, 0, buflen - stored_bytes );
276 }
277 else
278 {
279 /* The output buffer is smaller than the allocated size of X.
280 * However X may fit if its leading bytes are zero. */
281 bytes_to_copy = buflen;
282 p = buf;
283 for( i = bytes_to_copy; i < stored_bytes; i++ )
284 {
285 if( GET_BYTE( X, i ) != 0 )
286 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
287 }
288 }
289
290 for( i = 0; i < bytes_to_copy; i++ )
291 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
292
293 return( 0 );
294}
295
296#endif /* MBEDTLS_BIGNUM_C */