blob: 4471a4db7e58e5affe1f62781ac12525585c65fc [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
Janos Follath3ca07752022-08-09 11:45:47 +010029#include "mbedtls/platform.h"
Janos Follath3ca07752022-08-09 11:45:47 +010030
31#include "bignum_core.h"
32
Janos Follath2e328c82022-08-22 11:19:10 +010033size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010034{
35 size_t j;
36 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
37
38 for( j = 0; j < biL; j++ )
39 {
Janos Follathb7a88ec2022-08-19 12:24:40 +010040 if( a & mask ) break;
Janos Follath3ca07752022-08-09 11:45:47 +010041
42 mask >>= 1;
43 }
44
Gabor Mezei89e31462022-08-12 15:36:56 +020045 return( j );
Janos Follath3ca07752022-08-09 11:45:47 +010046}
47
Janos Follathb7a88ec2022-08-19 12:24:40 +010048size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +010049{
50 size_t i, j;
51
Janos Follathb7a88ec2022-08-19 12:24:40 +010052 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010053 return( 0 );
54
Janos Follathb7a88ec2022-08-19 12:24:40 +010055 for( i = A_limbs - 1; i > 0; i-- )
56 if( A[i] != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +010057 break;
58
Janos Follathb7a88ec2022-08-19 12:24:40 +010059 j = biL - mbedtls_mpi_core_clz( A[i] );
Janos Follath3ca07752022-08-09 11:45:47 +010060
61 return( ( i * biL ) + j );
62}
63
Janos Follath3ca07752022-08-09 11:45:47 +010064/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
65 * into the storage form used by mbedtls_mpi. */
Janos Follathb7a88ec2022-08-19 12:24:40 +010066static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010067{
68 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010069 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010070 mbedtls_mpi_uint tmp = 0;
71
Janos Follathb7a88ec2022-08-19 12:24:40 +010072 for( i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++ )
Janos Follath3ca07752022-08-09 11:45:47 +010073 {
74 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010075 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010076 }
77
78 return( tmp );
79}
80
Janos Follathb7a88ec2022-08-19 12:24:40 +010081static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a )
Janos Follath3ca07752022-08-09 11:45:47 +010082{
83#if defined(__BYTE_ORDER__)
84
85/* Nothing to do on bigendian systems. */
86#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
Janos Follathb7a88ec2022-08-19 12:24:40 +010087 return( a );
Janos Follath3ca07752022-08-09 11:45:47 +010088#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
89
90#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
91
92/* For GCC and Clang, have builtins for byte swapping. */
93#if defined(__GNUC__) && defined(__GNUC_PREREQ)
94#if __GNUC_PREREQ(4,3)
95#define have_bswap
96#endif
97#endif
98
99#if defined(__clang__) && defined(__has_builtin)
100#if __has_builtin(__builtin_bswap32) && \
101 __has_builtin(__builtin_bswap64)
102#define have_bswap
103#endif
104#endif
105
106#if defined(have_bswap)
107 /* The compiler is hopefully able to statically evaluate this! */
108 switch( sizeof(mbedtls_mpi_uint) )
109 {
110 case 4:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100111 return( __builtin_bswap32(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100112 case 8:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100113 return( __builtin_bswap64(a) );
Janos Follath3ca07752022-08-09 11:45:47 +0100114 }
115#endif
116#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
117#endif /* __BYTE_ORDER__ */
118
119 /* Fall back to C-based reordering if we don't know the byte order
120 * or we couldn't use a compiler-specific builtin. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100121 return( mpi_bigendian_to_host_c( a ) );
Janos Follath3ca07752022-08-09 11:45:47 +0100122}
123
Janos Follathb7a88ec2022-08-19 12:24:40 +0100124void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +0100125 size_t A_limbs )
Janos Follath3ca07752022-08-09 11:45:47 +0100126{
127 mbedtls_mpi_uint *cur_limb_left;
128 mbedtls_mpi_uint *cur_limb_right;
Janos Follathc4596412022-08-22 10:01:27 +0100129 if( A_limbs == 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100130 return;
131
132 /*
133 * Traverse limbs and
134 * - adapt byte-order in each limb
135 * - swap the limbs themselves.
136 * For that, simultaneously traverse the limbs from left to right
137 * and from right to left, as long as the left index is not bigger
138 * than the right index (it's not a problem if limbs is odd and the
139 * indices coincide in the last iteration).
140 */
Janos Follathc4596412022-08-22 10:01:27 +0100141 for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 );
Janos Follath3ca07752022-08-09 11:45:47 +0100142 cur_limb_left <= cur_limb_right;
143 cur_limb_left++, cur_limb_right-- )
144 {
145 mbedtls_mpi_uint tmp;
146 /* Note that if cur_limb_left == cur_limb_right,
147 * this code effectively swaps the bytes only once. */
148 tmp = mpi_bigendian_to_host( *cur_limb_left );
149 *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
150 *cur_limb_right = tmp;
151 }
152}
153
Janos Follath3ca07752022-08-09 11:45:47 +0100154int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100155 size_t X_limbs,
156 const unsigned char *input,
157 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100158{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100159 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100160
Janos Follathb7a88ec2022-08-19 12:24:40 +0100161 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100162 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
163
164 if( X != NULL )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200165 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100166 memset( X, 0, X_limbs * ciL );
Janos Follath3ca07752022-08-09 11:45:47 +0100167
Janos Follathb7a88ec2022-08-19 12:24:40 +0100168 for( size_t i = 0; i < input_length; i++ )
Janos Follathca5688e2022-08-19 12:05:28 +0100169 {
170 size_t offset = ( ( i % ciL ) << 3 );
171 X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
172 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200173 }
Janos Follath3ca07752022-08-09 11:45:47 +0100174
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100175 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100176}
177
Janos Follath3ca07752022-08-09 11:45:47 +0100178int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100179 size_t X_limbs,
180 const unsigned char *input,
181 size_t input_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100182{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100183 const size_t limbs = CHARS_TO_LIMBS( input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100184
Janos Follathb7a88ec2022-08-19 12:24:40 +0100185 if( X_limbs < limbs )
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100186 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
187
Janos Follathb7a88ec2022-08-19 12:24:40 +0100188 /* If X_limbs is 0, input_length must also be 0 (from previous test).
189 * Nothing to do. */
190 if( X_limbs == 0 )
Gabor Mezeic414ba32022-08-12 17:47:39 +0200191 return( 0 );
192
Janos Follathb7a88ec2022-08-19 12:24:40 +0100193 memset( X, 0, X_limbs * ciL );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200194
195 /* memcpy() with (NULL, 0) is undefined behaviour */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100196 if( input_length != 0 )
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200197 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100198 size_t overhead = ( X_limbs * ciL ) - input_length;
Gabor Mezeic414ba32022-08-12 17:47:39 +0200199 unsigned char *Xp = (unsigned char *) X;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100200 memcpy( Xp + overhead, input, input_length );
Janos Follath3ca07752022-08-09 11:45:47 +0100201 }
202
Janos Follathb7a88ec2022-08-19 12:24:40 +0100203 mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
Gabor Mezeic414ba32022-08-12 17:47:39 +0200204
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100205 return( 0 );
Janos Follath3ca07752022-08-09 11:45:47 +0100206}
207
Janos Follathb7a88ec2022-08-19 12:24:40 +0100208int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
209 size_t A_limbs,
210 unsigned char *output,
211 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100212{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100213 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100214 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100215
Janos Follathb7a88ec2022-08-19 12:24:40 +0100216 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100217 {
218 bytes_to_copy = stored_bytes;
219 }
220 else
221 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100222 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100223
Janos Follathaf3f39c2022-08-22 09:06:32 +0100224 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100225 * However A may fit if its leading bytes are zero. */
Janos Follathcc939082022-08-15 12:08:49 +0100226 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100227 {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100228 if( GET_BYTE( A, i ) != 0 )
Janos Follath3ca07752022-08-09 11:45:47 +0100229 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
230 }
231 }
232
Janos Follathcc939082022-08-15 12:08:49 +0100233 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follathb7a88ec2022-08-19 12:24:40 +0100234 output[i] = GET_BYTE( A, i );
Janos Follath3ca07752022-08-09 11:45:47 +0100235
Janos Follathb7a88ec2022-08-19 12:24:40 +0100236 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100237 {
238 /* Write trailing 0 bytes */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100239 memset( output + stored_bytes, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100240 }
241
242 return( 0 );
243}
244
Janos Follath3ca07752022-08-09 11:45:47 +0100245int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100246 size_t X_limbs,
247 unsigned char *output,
248 size_t output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100249{
250 size_t stored_bytes;
251 size_t bytes_to_copy;
252 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100253
Janos Follathb7a88ec2022-08-19 12:24:40 +0100254 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100255
Janos Follathb7a88ec2022-08-19 12:24:40 +0100256 if( stored_bytes < output_length )
Janos Follath3ca07752022-08-09 11:45:47 +0100257 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100258 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100259 * null bytes and record the position at which to start
260 * writing the significant bytes. In this case, the execution
261 * trace of this function does not depend on the value of the
262 * number. */
263 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100264 p = output + output_length - stored_bytes;
265 memset( output, 0, output_length - stored_bytes );
Janos Follath3ca07752022-08-09 11:45:47 +0100266 }
267 else
268 {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100269 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100270 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100271 bytes_to_copy = output_length;
272 p = output;
Janos Follathcc939082022-08-15 12:08:49 +0100273 for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100274 {
275 if( GET_BYTE( X, i ) != 0 )
276 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
277 }
278 }
279
Janos Follathcc939082022-08-15 12:08:49 +0100280 for( size_t i = 0; i < bytes_to_copy; i++ )
Janos Follath3ca07752022-08-09 11:45:47 +0100281 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
282
283 return( 0 );
284}
285
286#endif /* MBEDTLS_BIGNUM_C */