blob: 9d1e29c230f93e38b240bc98431e5b7275fee3d0 [file] [log] [blame]
Janos Follath3ca07752022-08-09 11:45:47 +01001/*
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Core bignum functions
Janos Follath3ca07752022-08-09 11:45:47 +01003 *
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 Follathb7a88ec2022-08-19 12:24:40 +010041size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010042{
43 size_t j;
44 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
45
46 for( j = 0; j < biL; j++ )
47 {
Janos Follathb7a88ec2022-08-19 12:24:40 +010048 if( a & mask ) break;
Janos Follath3ca07752022-08-09 11:45:47 +010049
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 Follathb7a88ec2022-08-19 12:24:40 +010056size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +010057{
58 size_t i, j;
59
Janos Follathb7a88ec2022-08-19 12:24:40 +010060 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010061 return( 0 );
62
Janos Follathb7a88ec2022-08-19 12:24:40 +010063 for( i = A_limbs - 1; i > 0; i-- )
64 if( A[i] != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010065 break;
66
Janos Follathb7a88ec2022-08-19 12:24:40 +010067 j = biL - mbedtls_mpi_core_clz( A[i] );
Janos Follath3ca07752022-08-09 11:45:47 +010068
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. */
Janos Follathb7a88ec2022-08-19 12:24:40 +010074static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010075{
76 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010077 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010078 mbedtls_mpi_uint tmp = 0;
79
Janos Follathb7a88ec2022-08-19 12:24:40 +010080 for( i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++ )
Janos Follath3ca07752022-08-09 11:45:47 +010081 {
82 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010083 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010084 }
85
86 return( tmp );
87}
88
Janos Follathb7a88ec2022-08-19 12:24:40 +010089static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010090{
91#if defined(__BYTE_ORDER__)
92
93/* Nothing to do on bigendian systems. */
94#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
Janos Follathb7a88ec2022-08-19 12:24:40 +010095 return( a );
Janos Follath3ca07752022-08-09 11:45:47 +010096#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:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100119 return( __builtin_bswap32(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100120 case 8:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100121 return( __builtin_bswap64(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100122 }
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. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100129 return( mpi_bigendian_to_host_c( a ) );
Janos Follath3ca07752022-08-09 11:45:47 +0100130}
131
Janos Follathb7a88ec2022-08-19 12:24:40 +0100132void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follath3ca07752022-08-09 11:45:47 +0100133 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 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100149 for( cur_limb_left = A, cur_limb_right = A + ( limbs - 1 );
Janos Follath3ca07752022-08-09 11:45:47 +0100150 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,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100163 size_t X_limbs,
164 const unsigned char *input,
165 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100166{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100167 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100168
Janos Follathb7a88ec2022-08-19 12:24:40 +0100169 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100170 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
171
172 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200173 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100174 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100175
Janos Follathb7a88ec2022-08-19 12:24:40 +0100176 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100177 {
178 size_t offset = ( ( i % ciL ) << 3 );
179 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
180 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200181 }
Janos Follath3ca07752022-08-09 11:45:47 +0100182
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100183 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100184}
185
Janos Follath3ca07752022-08-09 11:45:47 +0100186int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100187 size_t X_limbs,
188 const unsigned char *input,
189 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100190{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100191 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100192
Janos Follathb7a88ec2022-08-19 12:24:40 +0100193 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100194 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
195
Janos Follathb7a88ec2022-08-19 12:24:40 +0100196 /* If X_limbs is 0, input_length must also be 0 (from previous test).
197 * Nothing to do. */
198 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200199 return( 0 );
200
Janos Follathb7a88ec2022-08-19 12:24:40 +0100201 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200202
203 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100204 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200205 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100206 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200207 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100208 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100209 }
210
Janos Follathb7a88ec2022-08-19 12:24:40 +0100211 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200212
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100213 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100214}
215
Janos Follathb7a88ec2022-08-19 12:24:40 +0100216int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
217 size_t A_limbs,
218 unsigned char *output,
219 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100220{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100221 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100222 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100223
Janos Follathb7a88ec2022-08-19 12:24:40 +0100224 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100225 {
226 bytes_to_copy = stored_bytes;
227 }
228 else
229 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100230 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100231
Janos Follathb7a88ec2022-08-19 12:24:40 +0100232 /* The output outputfer is smaller than the allocated size of A.
233 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100234 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100235 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100236 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100237 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
238 }
239 }
240
Janos Follathcc939082022-08-15 12:08:49 +0100241 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100242 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100243
Janos Follathb7a88ec2022-08-19 12:24:40 +0100244 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100245 {
246 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100247 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100248 }
249
250 return( 0 );
251}
252
Janos Follath3ca07752022-08-09 11:45:47 +0100253int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100254 size_t X_limbs,
255 unsigned char *output,
256 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100257{
258 size_t stored_bytes;
259 size_t bytes_to_copy;
260 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100261
Janos Follathb7a88ec2022-08-19 12:24:40 +0100262 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100263
Janos Follathb7a88ec2022-08-19 12:24:40 +0100264 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100265 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100266 /* There is enough space in the output outputfer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100267 * null bytes and record the position at which to start
268 * writing the significant bytes. In this case, the execution
269 * trace of this function does not depend on the value of the
270 * number. */
271 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100272 p = output + output_length - stored_bytes;
273 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100274 }
275 else
276 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100277 /* The output outputfer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100278 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100279 bytes_to_copy = output_length;
280 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100281 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100282 {
283 if( GET_BYTE( X, i ) != 0 )
284 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
285 }
286 }
287
Janos Follathcc939082022-08-15 12:08:49 +0100288 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100289 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
290
291 return( 0 );
292}
293
294#endif /* MBEDTLS_BIGNUM_C */