Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Internal bignum functions |
| 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 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 24 | #include <string.h> |
| 25 | |
| 26 | #include "mbedtls/platform_util.h" |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 27 | #include "mbedtls/error.h" |
| 28 | #include "mbedtls/bignum.h" |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 29 | #include "bignum_core.h" |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 30 | #include "bignum_mod.h" |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 31 | #include "bignum_mod_raw.h" |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 32 | #include "constant_time_internal.h" |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 33 | |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 34 | #if defined(MBEDTLS_PLATFORM_C) |
| 35 | #include "mbedtls/platform.h" |
| 36 | #else |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
Gabor Mezei | e66b1d4 | 2022-08-02 11:49:59 +0200 | [diff] [blame] | 39 | #define mbedtls_printf printf |
| 40 | #define mbedtls_calloc calloc |
| 41 | #define mbedtls_free free |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 42 | #endif |
| 43 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 44 | /* |
| 45 | * Count leading zero bits in a given integer |
| 46 | */ |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 47 | size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x ) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 48 | { |
| 49 | size_t j; |
| 50 | mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); |
| 51 | |
| 52 | for( j = 0; j < biL; j++ ) |
| 53 | { |
| 54 | if( x & mask ) break; |
| 55 | |
| 56 | mask >>= 1; |
| 57 | } |
| 58 | |
| 59 | return j; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Return the number of bits |
| 64 | */ |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 65 | size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx ) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 66 | { |
| 67 | size_t i, j; |
| 68 | |
| 69 | if( nx == 0 ) |
| 70 | return( 0 ); |
| 71 | |
| 72 | for( i = nx - 1; i > 0; i-- ) |
| 73 | if( X[i] != 0 ) |
| 74 | break; |
| 75 | |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 76 | j = biL - mbedtls_mpi_core_clz( X[i] ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 77 | |
| 78 | return( ( i * biL ) + j ); |
| 79 | } |
| 80 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 81 | /* Get a specific byte, without range checks. */ |
| 82 | #define GET_BYTE( X, i ) \ |
| 83 | ( ( ( X )[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff ) |
| 84 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 85 | int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r, |
| 86 | mbedtls_mpi_mod_modulus *m, |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 87 | mbedtls_mpi_uint *p, |
| 88 | size_t pn ) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 89 | { |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 90 | if( p == NULL || m == NULL || r == NULL ) |
| 91 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 92 | |
| 93 | if( pn < m->n || !mbedtls_mpi_core_lt_ct( m->p, p, pn ) ) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 94 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 95 | |
| 96 | r->n = m->n; |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 97 | r->p = p; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 98 | |
| 99 | return( 0 ); |
| 100 | } |
| 101 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame^] | 102 | void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r ) |
| 103 | { |
| 104 | if ( r == NULL ) |
| 105 | return; |
| 106 | |
| 107 | r->n = 0; |
| 108 | r->p = NULL; |
| 109 | } |
| 110 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 111 | void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ) |
| 112 | { |
| 113 | if ( m == NULL ) |
| 114 | return; |
| 115 | |
Janos Follath | 281ccda | 2022-07-19 13:14:36 +0100 | [diff] [blame] | 116 | m->p = NULL; |
| 117 | m->n = 0; |
| 118 | m->plen = 0; |
| 119 | m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID; |
| 120 | m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ) |
| 124 | { |
| 125 | if ( m == NULL ) |
| 126 | return; |
| 127 | |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 128 | switch( m->int_rep ) |
| 129 | { |
| 130 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
| 131 | mbedtls_free( m->rep.mont ); break; |
| 132 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
Gabor Mezei | d8f5bc2 | 2022-08-02 11:51:25 +0200 | [diff] [blame] | 133 | mbedtls_free( m->rep.ored ); break; |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 134 | default: |
| 135 | break; |
| 136 | } |
| 137 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 138 | m->p = NULL; |
| 139 | m->n = 0; |
| 140 | m->plen = 0; |
Janos Follath | 281ccda | 2022-07-19 13:14:36 +0100 | [diff] [blame] | 141 | m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID; |
| 142 | m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, |
Gabor Mezei | 535f36d | 2022-08-02 11:50:44 +0200 | [diff] [blame] | 146 | mbedtls_mpi_uint *p, |
| 147 | size_t pn, |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 148 | int ext_rep, |
| 149 | int int_rep ) |
| 150 | { |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 151 | int ret = 0; |
| 152 | |
Gabor Mezei | 535f36d | 2022-08-02 11:50:44 +0200 | [diff] [blame] | 153 | if ( p == NULL || m == NULL ) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 154 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 155 | |
Gabor Mezei | 535f36d | 2022-08-02 11:50:44 +0200 | [diff] [blame] | 156 | m->p = p; |
| 157 | m->n = pn; |
| 158 | m->plen = mbedtls_mpi_core_bitlen( p, pn ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 159 | |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 160 | switch( ext_rep ) |
| 161 | { |
| 162 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
| 163 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
| 164 | m->ext_rep = ext_rep; break; |
| 165 | default: |
| 166 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 167 | goto exit; |
| 168 | } |
| 169 | |
| 170 | switch( int_rep ) |
| 171 | { |
| 172 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
| 173 | m->int_rep = int_rep; |
| 174 | m->rep.mont = NULL; break; |
| 175 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
| 176 | m->int_rep = int_rep; |
| 177 | m->rep.ored = NULL; break; |
| 178 | default: |
| 179 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 180 | goto exit; |
| 181 | } |
| 182 | |
| 183 | exit: |
| 184 | |
| 185 | if( ret != 0 ) |
| 186 | { |
| 187 | mbedtls_mpi_mod_modulus_free( m ); |
| 188 | } |
| 189 | |
| 190 | return( ret ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 193 | /* Check X to have at least n limbs and set it to 0. */ |
| 194 | static int mpi_core_clear( mbedtls_mpi_uint *X, |
| 195 | size_t nx, |
| 196 | size_t limbs ) |
| 197 | { |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 198 | if( nx < limbs ) |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 199 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 200 | |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 201 | if( X != NULL ) |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 202 | memset( X, 0, nx * ciL ); |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 203 | |
| 204 | return( 0 ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint |
| 208 | * into the storage form used by mbedtls_mpi. */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 209 | static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x ) |
| 210 | { |
| 211 | uint8_t i; |
| 212 | unsigned char *x_ptr; |
| 213 | mbedtls_mpi_uint tmp = 0; |
| 214 | |
| 215 | for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ ) |
| 216 | { |
| 217 | tmp <<= CHAR_BIT; |
| 218 | tmp |= (mbedtls_mpi_uint) *x_ptr; |
| 219 | } |
| 220 | |
| 221 | return( tmp ); |
| 222 | } |
| 223 | |
| 224 | static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x ) |
| 225 | { |
| 226 | #if defined(__BYTE_ORDER__) |
| 227 | |
| 228 | /* Nothing to do on bigendian systems. */ |
| 229 | #if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ) |
| 230 | return( x ); |
| 231 | #endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */ |
| 232 | |
| 233 | #if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ) |
| 234 | |
| 235 | /* For GCC and Clang, have builtins for byte swapping. */ |
| 236 | #if defined(__GNUC__) && defined(__GNUC_PREREQ) |
| 237 | #if __GNUC_PREREQ(4,3) |
| 238 | #define have_bswap |
| 239 | #endif |
| 240 | #endif |
| 241 | |
| 242 | #if defined(__clang__) && defined(__has_builtin) |
| 243 | #if __has_builtin(__builtin_bswap32) && \ |
| 244 | __has_builtin(__builtin_bswap64) |
| 245 | #define have_bswap |
| 246 | #endif |
| 247 | #endif |
| 248 | |
| 249 | #if defined(have_bswap) |
| 250 | /* The compiler is hopefully able to statically evaluate this! */ |
| 251 | switch( sizeof(mbedtls_mpi_uint) ) |
| 252 | { |
| 253 | case 4: |
| 254 | return( __builtin_bswap32(x) ); |
| 255 | case 8: |
| 256 | return( __builtin_bswap64(x) ); |
| 257 | } |
| 258 | #endif |
| 259 | #endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */ |
| 260 | #endif /* __BYTE_ORDER__ */ |
| 261 | |
| 262 | /* Fall back to C-based reordering if we don't know the byte order |
| 263 | * or we couldn't use a compiler-specific builtin. */ |
| 264 | return( mpi_bigendian_to_host_c( x ) ); |
| 265 | } |
| 266 | |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 267 | void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X, |
| 268 | size_t limbs ) |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 269 | { |
| 270 | mbedtls_mpi_uint *cur_limb_left; |
| 271 | mbedtls_mpi_uint *cur_limb_right; |
| 272 | if( limbs == 0 ) |
| 273 | return; |
| 274 | |
| 275 | /* |
| 276 | * Traverse limbs and |
| 277 | * - adapt byte-order in each limb |
| 278 | * - swap the limbs themselves. |
| 279 | * For that, simultaneously traverse the limbs from left to right |
| 280 | * and from right to left, as long as the left index is not bigger |
| 281 | * than the right index (it's not a problem if limbs is odd and the |
| 282 | * indices coincide in the last iteration). |
| 283 | */ |
| 284 | for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 ); |
| 285 | cur_limb_left <= cur_limb_right; |
| 286 | cur_limb_left++, cur_limb_right-- ) |
| 287 | { |
| 288 | mbedtls_mpi_uint tmp; |
| 289 | /* Note that if cur_limb_left == cur_limb_right, |
| 290 | * this code effectively swaps the bytes only once. */ |
| 291 | tmp = mpi_bigendian_to_host( *cur_limb_left ); |
| 292 | *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right ); |
| 293 | *cur_limb_right = tmp; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Import X from unsigned binary data, little endian |
| 299 | * |
| 300 | * This function is guaranteed to return an MPI with at least the necessary |
| 301 | * number of limbs (in particular, it does not skip 0s in the input). |
| 302 | */ |
| 303 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
| 304 | size_t nx, |
| 305 | const unsigned char *buf, |
| 306 | size_t buflen ) |
| 307 | { |
| 308 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 309 | size_t i; |
| 310 | size_t const limbs = CHARS_TO_LIMBS( buflen ); |
| 311 | |
| 312 | /* Ensure that target MPI has at least the necessary number of limbs */ |
| 313 | MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) ); |
| 314 | |
| 315 | for( i = 0; i < buflen; i++ ) |
| 316 | X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3); |
| 317 | |
| 318 | cleanup: |
| 319 | return( ret ); |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Import X from unsigned binary data, big endian |
| 324 | * |
| 325 | * This function is guaranteed to return an MPI with exactly the necessary |
| 326 | * number of limbs (in particular, it does not skip 0s in the input). |
| 327 | */ |
| 328 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
| 329 | size_t nx, |
| 330 | const unsigned char *buf, |
| 331 | size_t buflen ) |
| 332 | { |
| 333 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 334 | size_t const limbs = CHARS_TO_LIMBS( buflen ); |
| 335 | size_t overhead; |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 336 | unsigned char *Xp; |
| 337 | |
| 338 | MPI_VALIDATE_RET( X != NULL ); |
| 339 | MPI_VALIDATE_RET( buflen == 0 || buf != NULL ); |
| 340 | |
| 341 | /* Ensure that target MPI has at least the necessary number of limbs */ |
| 342 | MBEDTLS_MPI_CHK( mpi_core_clear( X, nx, limbs ) ); |
| 343 | |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 344 | overhead = ( nx * ciL ) - buflen; |
| 345 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 346 | /* Avoid calling `memcpy` with NULL source or destination argument, |
| 347 | * even if buflen is 0. */ |
| 348 | if( buflen != 0 ) |
| 349 | { |
| 350 | Xp = (unsigned char*) X; |
| 351 | memcpy( Xp + overhead, buf, buflen ); |
| 352 | |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 353 | mbedtls_mpi_core_bigendian_to_host( X, nx ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | cleanup: |
| 357 | return( ret ); |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Export X into unsigned binary data, little endian |
| 362 | */ |
| 363 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X, |
| 364 | size_t nx, |
| 365 | unsigned char *buf, |
| 366 | size_t buflen ) |
| 367 | { |
| 368 | size_t stored_bytes = nx * ciL; |
| 369 | size_t bytes_to_copy; |
| 370 | size_t i; |
| 371 | |
| 372 | if( stored_bytes < buflen ) |
| 373 | { |
| 374 | bytes_to_copy = stored_bytes; |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | bytes_to_copy = buflen; |
| 379 | |
| 380 | /* The output buffer is smaller than the allocated size of X. |
| 381 | * However X may fit if its leading bytes are zero. */ |
| 382 | for( i = bytes_to_copy; i < stored_bytes; i++ ) |
| 383 | { |
| 384 | if( GET_BYTE( X, i ) != 0 ) |
| 385 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | for( i = 0; i < bytes_to_copy; i++ ) |
| 390 | buf[i] = GET_BYTE( X, i ); |
| 391 | |
| 392 | if( stored_bytes < buflen ) |
| 393 | { |
| 394 | /* Write trailing 0 bytes */ |
| 395 | memset( buf + stored_bytes, 0, buflen - stored_bytes ); |
| 396 | } |
| 397 | |
| 398 | return( 0 ); |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Export X into unsigned binary data, big endian |
| 403 | */ |
| 404 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, |
| 405 | size_t nx, |
| 406 | unsigned char *buf, |
| 407 | size_t buflen ) |
| 408 | { |
| 409 | size_t stored_bytes; |
| 410 | size_t bytes_to_copy; |
| 411 | unsigned char *p; |
| 412 | size_t i; |
| 413 | |
| 414 | MPI_VALIDATE_RET( X != NULL ); |
| 415 | MPI_VALIDATE_RET( buflen == 0 || buf != NULL ); |
| 416 | |
| 417 | stored_bytes = nx * ciL; |
| 418 | |
| 419 | if( stored_bytes < buflen ) |
| 420 | { |
| 421 | /* There is enough space in the output buffer. Write initial |
| 422 | * null bytes and record the position at which to start |
| 423 | * writing the significant bytes. In this case, the execution |
| 424 | * trace of this function does not depend on the value of the |
| 425 | * number. */ |
| 426 | bytes_to_copy = stored_bytes; |
| 427 | p = buf + buflen - stored_bytes; |
| 428 | memset( buf, 0, buflen - stored_bytes ); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | /* The output buffer is smaller than the allocated size of X. |
| 433 | * However X may fit if its leading bytes are zero. */ |
| 434 | bytes_to_copy = buflen; |
| 435 | p = buf; |
| 436 | for( i = bytes_to_copy; i < stored_bytes; i++ ) |
| 437 | { |
| 438 | if( GET_BYTE( X, i ) != 0 ) |
| 439 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | for( i = 0; i < bytes_to_copy; i++ ) |
| 444 | p[bytes_to_copy - i - 1] = GET_BYTE( X, i ); |
| 445 | |
| 446 | return( 0 ); |
| 447 | } |
| 448 | |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 449 | int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X, |
| 450 | mbedtls_mpi_mod_modulus *m, |
| 451 | unsigned char *buf, |
| 452 | size_t buflen ) |
| 453 | { |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 454 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 455 | |
Gabor Mezei | c0b9304 | 2022-08-02 11:52:37 +0200 | [diff] [blame] | 456 | if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_LE ) |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 457 | ret = mbedtls_mpi_core_read_le( X, m->n, buf, buflen ); |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 458 | |
Gabor Mezei | c0b9304 | 2022-08-02 11:52:37 +0200 | [diff] [blame] | 459 | else if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_BE ) |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 460 | ret = mbedtls_mpi_core_read_be( X, m->n, buf, buflen ); |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 461 | else |
| 462 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 463 | |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 464 | if( ret != 0 ) |
| 465 | goto cleanup; |
| 466 | |
| 467 | if( !mbedtls_mpi_core_lt_ct( X, m->p, m->n ) ) |
| 468 | { |
| 469 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 470 | goto cleanup; |
| 471 | } |
| 472 | |
| 473 | cleanup: |
| 474 | |
| 475 | return( ret ); |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X, |
| 479 | mbedtls_mpi_mod_modulus *m, |
| 480 | unsigned char *buf, |
| 481 | size_t buflen ) |
| 482 | { |
Gabor Mezei | c0b9304 | 2022-08-02 11:52:37 +0200 | [diff] [blame] | 483 | if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_LE ) |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 484 | return mbedtls_mpi_core_write_le( X, m->n, buf, buflen ); |
| 485 | |
Gabor Mezei | c0b9304 | 2022-08-02 11:52:37 +0200 | [diff] [blame] | 486 | else if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_BE ) |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 487 | return mbedtls_mpi_core_write_be( X, m->n, buf, buflen ); |
| 488 | |
| 489 | else |
| 490 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 491 | |
| 492 | return( 0 ); |
| 493 | } |
| 494 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 495 | #endif /* MBEDTLS_BIGNUM_C */ |