blob: 431921ade29623091865a39ba1fa07dc70a4f375 [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
Gabor Mezei89e31462022-08-12 15:36:56 +020053 return( j );
Janos Follath3ca07752022-08-09 11:45:47 +010054}
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
Gabor Mezei89e31462022-08-12 15:36:56 +020080 for( i = 0, x_ptr = (unsigned char *) &x; i < ciL; i++, x_ptr++ )
Janos Follath3ca07752022-08-09 11:45:47 +010081 {
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{
Gabor Mezei5a5c0c52022-08-12 15:40:09 +0200167 const size_t limbs = CHARS_TO_LIMBS( buflen );
Janos Follath3ca07752022-08-09 11:45:47 +0100168
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100169 if( nx < limbs )
170 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
171
172 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200173 {
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100174 memset( X, 0, nx * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100175
Gabor Mezei5a5c0c52022-08-12 15:40:09 +0200176 for( size_t i = 0; i < buflen; i++ )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200177 X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
178 }
Janos Follath3ca07752022-08-09 11:45:47 +0100179
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100180 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100181}
182
Janos Follath3ca07752022-08-09 11:45:47 +0100183int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
184 size_t nx,
185 const unsigned char *buf,
186 size_t buflen )
187{
Janos Follath620c58c2022-08-15 11:58:42 +0100188 const size_t limbs = CHARS_TO_LIMBS( buflen );
Janos Follath3ca07752022-08-09 11:45:47 +0100189
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100190 if( nx < limbs )
191 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
192
Gabor Mezeic414ba32022-08-12 17:47:39 +0200193 /* If nx is 0, buflen must also be 0 (from previous test). Nothing to do. */
194 if( nx == 0 )
195 return( 0 );
196
197 memset( X, 0, nx * ciL );
198
199 /* memcpy() with (NULL, 0) is undefined behaviour */
200 if( buflen != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200201 {
Gabor Mezeic414ba32022-08-12 17:47:39 +0200202 size_t overhead = ( nx * ciL ) - buflen;
203 unsigned char *Xp = (unsigned char *) X;
204 memcpy( Xp + overhead, buf, buflen );
Janos Follath3ca07752022-08-09 11:45:47 +0100205 }
206
Gabor Mezeic414ba32022-08-12 17:47:39 +0200207 mbedtls_mpi_core_bigendian_to_host( X, nx );
208
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100209 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100210}
211
Janos Follath3ca07752022-08-09 11:45:47 +0100212int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
213 size_t nx,
214 unsigned char *buf,
215 size_t buflen )
216{
217 size_t stored_bytes = nx * ciL;
218 size_t bytes_to_copy;
219 size_t i;
220
221 if( stored_bytes < buflen )
222 {
223 bytes_to_copy = stored_bytes;
224 }
225 else
226 {
227 bytes_to_copy = buflen;
228
229 /* The output buffer is smaller than the allocated size of X.
230 * However X may fit if its leading bytes are zero. */
231 for( i = bytes_to_copy; i < stored_bytes; i++ )
232 {
233 if( GET_BYTE( X, i ) != 0 )
234 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
235 }
236 }
237
238 for( i = 0; i < bytes_to_copy; i++ )
239 buf[i] = GET_BYTE( X, i );
240
241 if( stored_bytes < buflen )
242 {
243 /* Write trailing 0 bytes */
244 memset( buf + stored_bytes, 0, buflen - stored_bytes );
245 }
246
247 return( 0 );
248}
249
Janos Follath3ca07752022-08-09 11:45:47 +0100250int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
251 size_t nx,
252 unsigned char *buf,
253 size_t buflen )
254{
255 size_t stored_bytes;
256 size_t bytes_to_copy;
257 unsigned char *p;
258 size_t i;
259
Janos Follath3ca07752022-08-09 11:45:47 +0100260 stored_bytes = nx * ciL;
261
262 if( stored_bytes < buflen )
263 {
264 /* There is enough space in the output buffer. Write initial
265 * null bytes and record the position at which to start
266 * writing the significant bytes. In this case, the execution
267 * trace of this function does not depend on the value of the
268 * number. */
269 bytes_to_copy = stored_bytes;
270 p = buf + buflen - stored_bytes;
271 memset( buf, 0, buflen - stored_bytes );
272 }
273 else
274 {
275 /* The output buffer is smaller than the allocated size of X.
276 * However X may fit if its leading bytes are zero. */
277 bytes_to_copy = buflen;
278 p = buf;
279 for( i = bytes_to_copy; i < stored_bytes; i++ )
280 {
281 if( GET_BYTE( X, i ) != 0 )
282 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
283 }
284 }
285
286 for( i = 0; i < bytes_to_copy; i++ )
287 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
288
289 return( 0 );
290}
291
292#endif /* MBEDTLS_BIGNUM_C */