Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 1 | /* |
| 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 Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 41 | size_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 Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 53 | return( j ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 56 | size_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 Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 72 | /* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint |
| 73 | * into the storage form used by mbedtls_mpi. */ |
| 74 | static 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 Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 80 | for( i = 0, x_ptr = (unsigned char *) &x; i < ciL; i++, x_ptr++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 81 | { |
| 82 | tmp <<= CHAR_BIT; |
| 83 | tmp |= (mbedtls_mpi_uint) *x_ptr; |
| 84 | } |
| 85 | |
| 86 | return( tmp ); |
| 87 | } |
| 88 | |
| 89 | static 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 | |
| 132 | void 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 Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 162 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
| 163 | size_t nx, |
| 164 | const unsigned char *buf, |
| 165 | size_t buflen ) |
| 166 | { |
Gabor Mezei | 5a5c0c5 | 2022-08-12 15:40:09 +0200 | [diff] [blame^] | 167 | const size_t limbs = CHARS_TO_LIMBS( buflen ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 168 | |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 169 | if( nx < limbs ) |
| 170 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 171 | |
| 172 | if( X != NULL ) |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 173 | { |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 174 | memset( X, 0, nx * ciL ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 175 | |
Gabor Mezei | 5a5c0c5 | 2022-08-12 15:40:09 +0200 | [diff] [blame^] | 176 | for( size_t i = 0; i < buflen; i++ ) |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 177 | X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3); |
| 178 | } |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 179 | |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 180 | return( 0 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 183 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
| 184 | size_t nx, |
| 185 | const unsigned char *buf, |
| 186 | size_t buflen ) |
| 187 | { |
Gabor Mezei | 5a5c0c5 | 2022-08-12 15:40:09 +0200 | [diff] [blame^] | 188 | const size_t limbs = CHARS_TO_LIMBS( buflen ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 189 | |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 190 | if( nx < limbs ) |
| 191 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 192 | |
| 193 | if( X != NULL ) |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 194 | { |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 195 | memset( X, 0, nx * ciL ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 196 | |
Gabor Mezei | 5a5c0c5 | 2022-08-12 15:40:09 +0200 | [diff] [blame^] | 197 | const size_t overhead = ( nx * ciL ) - buflen; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 198 | |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 199 | /* Avoid calling `memcpy` with NULL source or destination argument, |
| 200 | * even if buflen is 0. */ |
Gabor Mezei | 5f56df4 | 2022-08-12 14:41:54 +0200 | [diff] [blame] | 201 | if( buf != NULL ) |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 202 | { |
Gabor Mezei | 5a5c0c5 | 2022-08-12 15:40:09 +0200 | [diff] [blame^] | 203 | unsigned char *Xp = (unsigned char *) X; |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 204 | memcpy( Xp + overhead, buf, buflen ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 205 | |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 206 | mbedtls_mpi_core_bigendian_to_host( X, nx ); |
| 207 | } |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 210 | return( 0 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 213 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X, |
| 214 | size_t nx, |
| 215 | unsigned char *buf, |
| 216 | size_t buflen ) |
| 217 | { |
| 218 | size_t stored_bytes = nx * ciL; |
| 219 | size_t bytes_to_copy; |
| 220 | size_t i; |
| 221 | |
| 222 | if( stored_bytes < buflen ) |
| 223 | { |
| 224 | bytes_to_copy = stored_bytes; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | bytes_to_copy = buflen; |
| 229 | |
| 230 | /* The output buffer is smaller than the allocated size of X. |
| 231 | * However X may fit if its leading bytes are zero. */ |
| 232 | for( i = bytes_to_copy; i < stored_bytes; i++ ) |
| 233 | { |
| 234 | if( GET_BYTE( X, i ) != 0 ) |
| 235 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | for( i = 0; i < bytes_to_copy; i++ ) |
| 240 | buf[i] = GET_BYTE( X, i ); |
| 241 | |
| 242 | if( stored_bytes < buflen ) |
| 243 | { |
| 244 | /* Write trailing 0 bytes */ |
| 245 | memset( buf + stored_bytes, 0, buflen - stored_bytes ); |
| 246 | } |
| 247 | |
| 248 | return( 0 ); |
| 249 | } |
| 250 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 251 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, |
| 252 | size_t nx, |
| 253 | unsigned char *buf, |
| 254 | size_t buflen ) |
| 255 | { |
| 256 | size_t stored_bytes; |
| 257 | size_t bytes_to_copy; |
| 258 | unsigned char *p; |
| 259 | size_t i; |
| 260 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 261 | stored_bytes = nx * ciL; |
| 262 | |
| 263 | if( stored_bytes < buflen ) |
| 264 | { |
| 265 | /* There is enough space in the output buffer. Write initial |
| 266 | * null bytes and record the position at which to start |
| 267 | * writing the significant bytes. In this case, the execution |
| 268 | * trace of this function does not depend on the value of the |
| 269 | * number. */ |
| 270 | bytes_to_copy = stored_bytes; |
| 271 | p = buf + buflen - stored_bytes; |
| 272 | memset( buf, 0, buflen - stored_bytes ); |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | /* The output buffer is smaller than the allocated size of X. |
| 277 | * However X may fit if its leading bytes are zero. */ |
| 278 | bytes_to_copy = buflen; |
| 279 | p = buf; |
| 280 | for( i = bytes_to_copy; i < stored_bytes; i++ ) |
| 281 | { |
| 282 | if( GET_BYTE( X, i ) != 0 ) |
| 283 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | for( i = 0; i < bytes_to_copy; i++ ) |
| 288 | p[bytes_to_copy - i - 1] = GET_BYTE( X, i ); |
| 289 | |
| 290 | return( 0 ); |
| 291 | } |
| 292 | |
| 293 | #endif /* MBEDTLS_BIGNUM_C */ |