Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 1 | /* |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 2 | * Core bignum functions |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 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" |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 28 | #include "constant_time_internal.h" |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 29 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 31 | |
| 32 | #include "bignum_core.h" |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 33 | #include "bn_mul.h" |
| 34 | #include "constant_time_internal.h" |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 35 | |
Janos Follath | 2e328c8 | 2022-08-22 11:19:10 +0100 | [diff] [blame] | 36 | size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 37 | { |
| 38 | size_t j; |
| 39 | mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); |
| 40 | |
| 41 | for( j = 0; j < biL; j++ ) |
| 42 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 43 | if( a & mask ) break; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 44 | |
| 45 | mask >>= 1; |
| 46 | } |
| 47 | |
Gabor Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 48 | return( j ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 51 | size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 52 | { |
| 53 | size_t i, j; |
| 54 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 55 | if( A_limbs == 0 ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 56 | return( 0 ); |
| 57 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 58 | for( i = A_limbs - 1; i > 0; i-- ) |
| 59 | if( A[i] != 0 ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 60 | break; |
| 61 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 62 | j = biL - mbedtls_mpi_core_clz( A[i] ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 63 | |
| 64 | return( ( i * biL ) + j ); |
| 65 | } |
| 66 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 67 | /* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint |
| 68 | * into the storage form used by mbedtls_mpi. */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 69 | static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint a ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 70 | { |
| 71 | uint8_t i; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 72 | unsigned char *a_ptr; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 73 | mbedtls_mpi_uint tmp = 0; |
| 74 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 75 | for( i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 76 | { |
| 77 | tmp <<= CHAR_BIT; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 78 | tmp |= (mbedtls_mpi_uint) *a_ptr; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return( tmp ); |
| 82 | } |
| 83 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 84 | static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 85 | { |
| 86 | #if defined(__BYTE_ORDER__) |
| 87 | |
| 88 | /* Nothing to do on bigendian systems. */ |
| 89 | #if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ) |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 90 | return( a ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 91 | #endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */ |
| 92 | |
| 93 | #if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ) |
| 94 | |
| 95 | /* For GCC and Clang, have builtins for byte swapping. */ |
| 96 | #if defined(__GNUC__) && defined(__GNUC_PREREQ) |
| 97 | #if __GNUC_PREREQ(4,3) |
| 98 | #define have_bswap |
| 99 | #endif |
| 100 | #endif |
| 101 | |
| 102 | #if defined(__clang__) && defined(__has_builtin) |
| 103 | #if __has_builtin(__builtin_bswap32) && \ |
| 104 | __has_builtin(__builtin_bswap64) |
| 105 | #define have_bswap |
| 106 | #endif |
| 107 | #endif |
| 108 | |
| 109 | #if defined(have_bswap) |
| 110 | /* The compiler is hopefully able to statically evaluate this! */ |
| 111 | switch( sizeof(mbedtls_mpi_uint) ) |
| 112 | { |
| 113 | case 4: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 114 | return( __builtin_bswap32(a) ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 115 | case 8: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 116 | return( __builtin_bswap64(a) ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 117 | } |
| 118 | #endif |
| 119 | #endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */ |
| 120 | #endif /* __BYTE_ORDER__ */ |
| 121 | |
| 122 | /* Fall back to C-based reordering if we don't know the byte order |
| 123 | * or we couldn't use a compiler-specific builtin. */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 124 | return( mpi_bigendian_to_host_c( a ) ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 127 | void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A, |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 128 | size_t A_limbs ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 129 | { |
| 130 | mbedtls_mpi_uint *cur_limb_left; |
| 131 | mbedtls_mpi_uint *cur_limb_right; |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 132 | if( A_limbs == 0 ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 133 | return; |
| 134 | |
| 135 | /* |
| 136 | * Traverse limbs and |
| 137 | * - adapt byte-order in each limb |
| 138 | * - swap the limbs themselves. |
| 139 | * For that, simultaneously traverse the limbs from left to right |
| 140 | * and from right to left, as long as the left index is not bigger |
| 141 | * than the right index (it's not a problem if limbs is odd and the |
| 142 | * indices coincide in the last iteration). |
| 143 | */ |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 144 | for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 145 | cur_limb_left <= cur_limb_right; |
| 146 | cur_limb_left++, cur_limb_right-- ) |
| 147 | { |
| 148 | mbedtls_mpi_uint tmp; |
| 149 | /* Note that if cur_limb_left == cur_limb_right, |
| 150 | * this code effectively swaps the bytes only once. */ |
Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 151 | tmp = mpi_bigendian_to_host( *cur_limb_left ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 152 | *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right ); |
| 153 | *cur_limb_right = tmp; |
| 154 | } |
| 155 | } |
| 156 | |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 157 | void mbedtls_mpi_core_cond_assign( mbedtls_mpi_uint *X, |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 158 | const mbedtls_mpi_uint *A, |
Gabor Mezei | 3eff425 | 2022-09-26 17:26:42 +0200 | [diff] [blame] | 159 | size_t limbs, |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 160 | unsigned char assign ) |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 161 | { |
Gabor Mezei | e9c013c | 2022-10-10 14:26:57 +0200 | [diff] [blame] | 162 | if( X == A ) |
| 163 | return; |
| 164 | |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 165 | mbedtls_ct_mpi_uint_cond_assign( limbs, X, A, assign ); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 168 | void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X, |
| 169 | mbedtls_mpi_uint *Y, |
Gabor Mezei | cfc0eb8 | 2022-09-15 20:15:34 +0200 | [diff] [blame] | 170 | size_t limbs, |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 171 | unsigned char swap ) |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 172 | { |
Gabor Mezei | e9c013c | 2022-10-10 14:26:57 +0200 | [diff] [blame] | 173 | if( X == Y ) |
| 174 | return; |
| 175 | |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 176 | /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */ |
| 177 | mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap ); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 178 | |
Gabor Mezei | cfc0eb8 | 2022-09-15 20:15:34 +0200 | [diff] [blame] | 179 | for( size_t i = 0; i < limbs; i++ ) |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 180 | { |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 181 | mbedtls_mpi_uint tmp = X[i]; |
| 182 | X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask ); |
| 183 | Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask ); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 184 | } |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 187 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 188 | size_t X_limbs, |
| 189 | const unsigned char *input, |
| 190 | size_t input_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 191 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 192 | const size_t limbs = CHARS_TO_LIMBS( input_length ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 193 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 194 | if( X_limbs < limbs ) |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 195 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 196 | |
| 197 | if( X != NULL ) |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 198 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 199 | memset( X, 0, X_limbs * ciL ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 200 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 201 | for( size_t i = 0; i < input_length; i++ ) |
Janos Follath | ca5688e | 2022-08-19 12:05:28 +0100 | [diff] [blame] | 202 | { |
| 203 | size_t offset = ( ( i % ciL ) << 3 ); |
| 204 | X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset; |
| 205 | } |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 206 | } |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 207 | |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 208 | return( 0 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 211 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 212 | size_t X_limbs, |
| 213 | const unsigned char *input, |
| 214 | size_t input_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 215 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 216 | const size_t limbs = CHARS_TO_LIMBS( input_length ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 217 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 218 | if( X_limbs < limbs ) |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 219 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 220 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 221 | /* If X_limbs is 0, input_length must also be 0 (from previous test). |
| 222 | * Nothing to do. */ |
| 223 | if( X_limbs == 0 ) |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 224 | return( 0 ); |
| 225 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 226 | memset( X, 0, X_limbs * ciL ); |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 227 | |
| 228 | /* memcpy() with (NULL, 0) is undefined behaviour */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 229 | if( input_length != 0 ) |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 230 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 231 | size_t overhead = ( X_limbs * ciL ) - input_length; |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 232 | unsigned char *Xp = (unsigned char *) X; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 233 | memcpy( Xp + overhead, input, input_length ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 236 | mbedtls_mpi_core_bigendian_to_host( X, X_limbs ); |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 237 | |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 238 | return( 0 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 241 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A, |
| 242 | size_t A_limbs, |
| 243 | unsigned char *output, |
| 244 | size_t output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 245 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 246 | size_t stored_bytes = A_limbs * ciL; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 247 | size_t bytes_to_copy; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 248 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 249 | if( stored_bytes < output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 250 | { |
| 251 | bytes_to_copy = stored_bytes; |
| 252 | } |
| 253 | else |
| 254 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 255 | bytes_to_copy = output_length; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 256 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 257 | /* The output buffer is smaller than the allocated size of A. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 258 | * However A may fit if its leading bytes are zero. */ |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 259 | for( size_t i = bytes_to_copy; i < stored_bytes; i++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 260 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 261 | if( GET_BYTE( A, i ) != 0 ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 262 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 263 | } |
| 264 | } |
| 265 | |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 266 | for( size_t i = 0; i < bytes_to_copy; i++ ) |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 267 | output[i] = GET_BYTE( A, i ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 268 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 269 | if( stored_bytes < output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 270 | { |
| 271 | /* Write trailing 0 bytes */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 272 | memset( output + stored_bytes, 0, output_length - stored_bytes ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | return( 0 ); |
| 276 | } |
| 277 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 278 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 279 | size_t X_limbs, |
| 280 | unsigned char *output, |
| 281 | size_t output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 282 | { |
| 283 | size_t stored_bytes; |
| 284 | size_t bytes_to_copy; |
| 285 | unsigned char *p; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 286 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 287 | stored_bytes = X_limbs * ciL; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 288 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 289 | if( stored_bytes < output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 290 | { |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 291 | /* There is enough space in the output buffer. Write initial |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 292 | * null bytes and record the position at which to start |
| 293 | * writing the significant bytes. In this case, the execution |
| 294 | * trace of this function does not depend on the value of the |
| 295 | * number. */ |
| 296 | bytes_to_copy = stored_bytes; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 297 | p = output + output_length - stored_bytes; |
| 298 | memset( output, 0, output_length - stored_bytes ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 299 | } |
| 300 | else |
| 301 | { |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 302 | /* The output buffer is smaller than the allocated size of X. |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 303 | * However X may fit if its leading bytes are zero. */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 304 | bytes_to_copy = output_length; |
| 305 | p = output; |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 306 | for( size_t i = bytes_to_copy; i < stored_bytes; i++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 307 | { |
| 308 | if( GET_BYTE( X, i ) != 0 ) |
| 309 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 310 | } |
| 311 | } |
| 312 | |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 313 | for( size_t i = 0; i < bytes_to_copy; i++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 314 | p[bytes_to_copy - i - 1] = GET_BYTE( X, i ); |
| 315 | |
| 316 | return( 0 ); |
| 317 | } |
| 318 | |
Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 319 | void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs, |
| 320 | size_t count ) |
| 321 | { |
| 322 | size_t i, v0, v1; |
| 323 | mbedtls_mpi_uint r0 = 0, r1; |
| 324 | |
| 325 | v0 = count / biL; |
| 326 | v1 = count & (biL - 1); |
| 327 | |
| 328 | if( v0 > limbs || ( v0 == limbs && v1 > 0 ) ) |
| 329 | { |
| 330 | memset( X, 0, limbs * ciL ); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * shift by count / limb_size |
| 336 | */ |
| 337 | if( v0 > 0 ) |
| 338 | { |
| 339 | for( i = 0; i < limbs - v0; i++ ) |
| 340 | X[i] = X[i + v0]; |
| 341 | |
| 342 | for( ; i < limbs; i++ ) |
| 343 | X[i] = 0; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * shift by count % limb_size |
| 348 | */ |
| 349 | if( v1 > 0 ) |
| 350 | { |
| 351 | for( i = limbs; i > 0; i-- ) |
| 352 | { |
| 353 | r1 = X[i - 1] << (biL - v1); |
| 354 | X[i - 1] >>= v1; |
| 355 | X[i - 1] |= r0; |
| 356 | r0 = r1; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 361 | mbedtls_mpi_uint mbedtls_mpi_core_add( mbedtls_mpi_uint *X, |
| 362 | const mbedtls_mpi_uint *A, |
| 363 | const mbedtls_mpi_uint *B, |
| 364 | size_t limbs ) |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 365 | { |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 366 | mbedtls_mpi_uint c = 0; |
Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 367 | |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 368 | for( size_t i = 0; i < limbs; i++ ) |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 369 | { |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 370 | mbedtls_mpi_uint t = c + A[i]; |
| 371 | c = ( t < A[i] ); |
| 372 | t += B[i]; |
| 373 | c += ( t < B[i] ); |
| 374 | X[i] = t; |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 375 | } |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 376 | |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 377 | return( c ); |
| 378 | } |
Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 379 | |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 380 | mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X, |
| 381 | const mbedtls_mpi_uint *A, |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 382 | size_t limbs, |
| 383 | unsigned cond ) |
| 384 | { |
Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 385 | mbedtls_mpi_uint c = 0; |
| 386 | |
Tom Cosgrove | 2701dea | 2022-09-15 15:00:07 +0100 | [diff] [blame] | 387 | /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */ |
| 388 | const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond ); |
Tom Cosgrove | 9354990 | 2022-08-30 17:41:23 +0100 | [diff] [blame] | 389 | |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 390 | for( size_t i = 0; i < limbs; i++ ) |
| 391 | { |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 392 | mbedtls_mpi_uint add = mask & A[i]; |
| 393 | mbedtls_mpi_uint t = c + X[i]; |
| 394 | c = ( t < X[i] ); |
Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 395 | t += add; |
| 396 | c += ( t < add ); |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 397 | X[i] = t; |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 398 | } |
Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 399 | |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 400 | return( c ); |
| 401 | } |
| 402 | |
| 403 | mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X, |
| 404 | const mbedtls_mpi_uint *A, |
| 405 | const mbedtls_mpi_uint *B, |
| 406 | size_t limbs ) |
| 407 | { |
| 408 | mbedtls_mpi_uint c = 0; |
| 409 | |
| 410 | for( size_t i = 0; i < limbs; i++ ) |
| 411 | { |
| 412 | mbedtls_mpi_uint z = ( A[i] < c ); |
| 413 | mbedtls_mpi_uint t = A[i] - c; |
| 414 | c = ( t < B[i] ) + z; |
| 415 | X[i] = t - B[i]; |
| 416 | } |
| 417 | |
| 418 | return( c ); |
| 419 | } |
| 420 | |
| 421 | mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, |
| 422 | const mbedtls_mpi_uint *s, size_t s_len, |
| 423 | mbedtls_mpi_uint b ) |
| 424 | { |
| 425 | mbedtls_mpi_uint c = 0; /* carry */ |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 426 | /* |
| 427 | * It is a documented precondition of this function that d_len >= s_len. |
| 428 | * If that's not the case, we swap these round: this turns what would be |
| 429 | * a buffer overflow into an incorrect result. |
| 430 | */ |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 431 | if( d_len < s_len ) |
| 432 | s_len = d_len; |
| 433 | size_t excess_len = d_len - s_len; |
| 434 | size_t steps_x8 = s_len / 8; |
| 435 | size_t steps_x1 = s_len & 7; |
| 436 | |
| 437 | while( steps_x8-- ) |
| 438 | { |
| 439 | MULADDC_X8_INIT |
| 440 | MULADDC_X8_CORE |
| 441 | MULADDC_X8_STOP |
| 442 | } |
| 443 | |
| 444 | while( steps_x1-- ) |
| 445 | { |
| 446 | MULADDC_X1_INIT |
| 447 | MULADDC_X1_CORE |
| 448 | MULADDC_X1_STOP |
| 449 | } |
| 450 | |
| 451 | while( excess_len-- ) |
| 452 | { |
Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 453 | *d += c; |
| 454 | c = ( *d < c ); |
| 455 | d++; |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | return( c ); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Fast Montgomery initialization (thanks to Tom St Denis). |
| 463 | */ |
Tom Cosgrove | b7438d1 | 2022-09-15 15:05:59 +0100 | [diff] [blame] | 464 | mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N ) |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 465 | { |
| 466 | mbedtls_mpi_uint x = N[0]; |
| 467 | |
| 468 | x += ( ( N[0] + 2 ) & 4 ) << 1; |
| 469 | |
| 470 | for( unsigned int i = biL; i >= 8; i /= 2 ) |
| 471 | x *= ( 2 - ( N[0] * x ) ); |
| 472 | |
| 473 | return( ~x + 1 ); |
| 474 | } |
| 475 | |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 476 | void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, |
| 477 | const mbedtls_mpi_uint *A, |
| 478 | const mbedtls_mpi_uint *B, |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 479 | size_t B_limbs, |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 480 | const mbedtls_mpi_uint *N, |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 481 | size_t AN_limbs, |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 482 | mbedtls_mpi_uint mm, |
| 483 | mbedtls_mpi_uint *T ) |
| 484 | { |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 485 | memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL ); |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 486 | |
Tom Cosgrove | 67c9247 | 2022-09-02 13:28:59 +0100 | [diff] [blame] | 487 | for( size_t i = 0; i < AN_limbs; i++ ) |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 488 | { |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 489 | /* T = (T + u0*B + u1*N) / 2^biL */ |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 490 | mbedtls_mpi_uint u0 = A[i]; |
| 491 | mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm; |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 492 | |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 493 | (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 ); |
| 494 | (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 ); |
Tom Cosgrove | 67c9247 | 2022-09-02 13:28:59 +0100 | [diff] [blame] | 495 | |
| 496 | T++; |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 499 | /* |
| 500 | * The result we want is (T >= N) ? T - N : T. |
| 501 | * |
| 502 | * For better constant-time properties in this function, we always do the |
| 503 | * subtraction, with the result in X. |
| 504 | * |
| 505 | * We also look to see if there was any carry in the final additions in the |
| 506 | * loop above. |
| 507 | */ |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 508 | |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 509 | mbedtls_mpi_uint carry = T[AN_limbs]; |
| 510 | mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs ); |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 511 | |
| 512 | /* |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 513 | * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs): |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 514 | * |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 515 | * T can be in one of 3 ranges: |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 516 | * |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 517 | * 1) T < N : (carry, borrow) = (0, 1): we want T |
| 518 | * 2) N <= T < R : (carry, borrow) = (0, 0): we want X |
| 519 | * 3) T >= R : (carry, borrow) = (1, 1): we want X |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 520 | * |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 521 | * and (carry, borrow) = (1, 0) can't happen. |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 522 | * |
| 523 | * So the correct return value is already in X if (carry ^ borrow) = 0, |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 524 | * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1. |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 525 | */ |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 526 | mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) ); |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Minos Galanakis | a081c51 | 2022-10-24 12:16:28 +0100 | [diff] [blame] | 529 | int mbedtls_mpi_core_get_mont_r2_unsafe( mbedtls_mpi *X, |
Minos Galanakis | 51d638b | 2022-10-24 09:59:44 +0100 | [diff] [blame] | 530 | const mbedtls_mpi *N ) |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 531 | { |
| 532 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 533 | |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 534 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 1 ) ); |
| 535 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, N->n * 2 * biL ) ); |
| 536 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( X, X, N ) ); |
| 537 | MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( X, N->n ) ); |
| 538 | |
| 539 | cleanup: |
| 540 | return( ret ); |
| 541 | } |
| 542 | |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 543 | void mbedtls_mpi_core_ct_uint_table_lookup( mbedtls_mpi_uint *dest, |
Janos Follath | 8904a2d | 2022-10-31 15:32:28 +0000 | [diff] [blame] | 544 | const mbedtls_mpi_uint *table, |
| 545 | size_t limbs, |
| 546 | size_t count, |
| 547 | size_t index ) |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 548 | { |
Janos Follath | 8904a2d | 2022-10-31 15:32:28 +0000 | [diff] [blame] | 549 | for( size_t i = 0; i < count; i++, table += limbs ) |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 550 | { |
| 551 | unsigned char assign = mbedtls_ct_size_bool_eq( i, index ); |
Janos Follath | 8904a2d | 2022-10-31 15:32:28 +0000 | [diff] [blame] | 552 | mbedtls_mpi_core_cond_assign( dest, table, limbs, assign ); |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 556 | /* Fill X with n_bytes random bytes. |
| 557 | * X must already have room for those bytes. |
| 558 | * The ordering of the bytes returned from the RNG is suitable for |
Gilles Peskine | 22cdd0c | 2022-10-27 20:15:13 +0200 | [diff] [blame] | 559 | * deterministic ECDSA (see RFC 6979 §3.3 and the specification of |
| 560 | * mbedtls_mpi_core_random()). |
Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 561 | */ |
| 562 | int mbedtls_mpi_core_fill_random( |
| 563 | mbedtls_mpi_uint *X, size_t X_limbs, |
| 564 | size_t n_bytes, |
| 565 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 566 | { |
| 567 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 568 | const size_t limbs = CHARS_TO_LIMBS( n_bytes ); |
| 569 | const size_t overhead = ( limbs * ciL ) - n_bytes; |
| 570 | |
| 571 | if( X_limbs < limbs ) |
| 572 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 573 | |
| 574 | memset( X, 0, overhead ); |
| 575 | memset( (unsigned char *) X + limbs * ciL, 0, ( X_limbs - limbs ) * ciL ); |
| 576 | MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X + overhead, n_bytes ) ); |
| 577 | mbedtls_mpi_core_bigendian_to_host( X, limbs ); |
| 578 | |
| 579 | cleanup: |
| 580 | return( ret ); |
| 581 | } |
| 582 | |
Janos Follath | 2a8bcf8 | 2022-11-02 10:47:30 +0000 | [diff] [blame] | 583 | /* BEGIN MERGE SLOT 1 */ |
| 584 | |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 585 | static size_t mpi_exp_mod_get_window_size( size_t Ebits ) |
| 586 | { |
| 587 | size_t wsize = ( Ebits > 671 ) ? 6 : ( Ebits > 239 ) ? 5 : |
| 588 | ( Ebits > 79 ) ? 4 : ( Ebits > 23 ) ? 3 : 1; |
| 589 | |
| 590 | #if( MBEDTLS_MPI_WINDOW_SIZE < 6 ) |
| 591 | if( wsize > MBEDTLS_MPI_WINDOW_SIZE ) |
| 592 | wsize = MBEDTLS_MPI_WINDOW_SIZE; |
| 593 | #endif |
| 594 | |
| 595 | return( wsize ); |
| 596 | } |
| 597 | |
| 598 | int mbedtls_mpi_core_exp_mod( mbedtls_mpi_uint *X, |
| 599 | mbedtls_mpi_uint const *A, |
| 600 | const mbedtls_mpi_uint *N, |
| 601 | size_t n, |
| 602 | const mbedtls_mpi_uint *E, |
| 603 | size_t E_len, |
| 604 | const mbedtls_mpi_uint *RR ) |
| 605 | { |
| 606 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 607 | /* heap allocated memory pool */ |
| 608 | mbedtls_mpi_uint *mempool = NULL; |
| 609 | /* pointers to temporaries within memory pool */ |
| 610 | mbedtls_mpi_uint *Wtbl, *Wselect, *temp; |
| 611 | /* pointers to table entries */ |
| 612 | mbedtls_mpi_uint *Wcur, *Wlast, *W1; |
| 613 | |
| 614 | size_t wsize, welem; |
| 615 | mbedtls_mpi_uint one = 1, mm; |
| 616 | |
| 617 | mm = mbedtls_mpi_core_montmul_init( N ); /* Compute Montgomery constant */ |
| 618 | E += E_len; /* Skip to end of exponent buffer */ |
| 619 | |
| 620 | wsize = mpi_exp_mod_get_window_size( E_len * biL ); |
Janos Follath | bad42c4 | 2022-11-09 14:30:44 +0000 | [diff] [blame^] | 621 | welem = ( (size_t) 1 ) << wsize; |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 622 | |
| 623 | /* Allocate memory pool and set pointers to parts of it */ |
| 624 | const size_t table_limbs = welem * n; |
| 625 | const size_t temp_limbs = 2 * n + 1; |
| 626 | const size_t wselect_limbs = n; |
| 627 | const size_t total_limbs = table_limbs + temp_limbs + wselect_limbs; |
| 628 | |
| 629 | mempool = mbedtls_calloc( total_limbs, sizeof(mbedtls_mpi_uint) ); |
| 630 | if( mempool == NULL ) |
| 631 | { |
| 632 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 633 | goto cleanup; |
| 634 | } |
| 635 | |
| 636 | Wtbl = mempool; |
| 637 | Wselect = Wtbl + table_limbs; |
| 638 | temp = Wselect + wselect_limbs; |
| 639 | |
| 640 | /* |
| 641 | * Window precomputation |
| 642 | */ |
| 643 | |
| 644 | /* W[0] = 1 (in Montgomery presentation) */ |
| 645 | memset( Wtbl, 0, n * ciL ); Wtbl[0] = 1; |
| 646 | mbedtls_mpi_core_montmul( Wtbl, Wtbl, RR, n, N, n, mm, temp ); |
| 647 | Wcur = Wtbl + n; |
| 648 | /* W[1] = A * R^2 * R^-1 mod N = A * R mod N */ |
| 649 | memcpy( Wcur, A, n * ciL ); |
| 650 | mbedtls_mpi_core_montmul( Wcur, Wcur, RR, n, N, n, mm, temp ); |
| 651 | W1 = Wcur; |
| 652 | Wcur += n; |
| 653 | /* W[i+1] = W[i] * W[1], i >= 2 */ |
| 654 | Wlast = W1; |
| 655 | for( size_t i=2; i < welem; i++, Wlast += n, Wcur += n ) |
| 656 | mbedtls_mpi_core_montmul( Wcur, Wlast, W1, n, N, n, mm, temp ); |
| 657 | |
| 658 | /* |
| 659 | * Sliding window exponentiation |
| 660 | */ |
| 661 | |
| 662 | /* X = 1 (in Montgomery presentation) initially */ |
| 663 | memcpy( X, Wtbl, n * ciL ); |
| 664 | |
| 665 | size_t limb_bits_remaining = 0; |
Janos Follath | bad42c4 | 2022-11-09 14:30:44 +0000 | [diff] [blame^] | 666 | mbedtls_mpi_uint cur_limb, window = 0; |
| 667 | size_t window_bits = 0; |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 668 | while( 1 ) |
| 669 | { |
| 670 | size_t window_bits_missing = wsize - window_bits; |
| 671 | |
| 672 | const int no_more_bits = |
| 673 | ( limb_bits_remaining == 0 ) && ( E_len == 0 ); |
| 674 | const int window_full = |
| 675 | ( window_bits_missing == 0 ); |
| 676 | |
| 677 | /* Clear window if it's full or if we don't have further bits. */ |
| 678 | if( window_full || no_more_bits ) |
| 679 | { |
| 680 | if( window_bits == 0 ) |
| 681 | break; |
| 682 | /* Select table entry, square and multiply */ |
| 683 | mbedtls_mpi_core_ct_uint_table_lookup( Wselect, Wtbl, |
| 684 | n, welem, window ); |
| 685 | mbedtls_mpi_core_montmul( X, X, Wselect, n, N, n, mm, temp ); |
| 686 | window = window_bits = 0; |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | /* Load next exponent limb if necessary */ |
| 691 | if( limb_bits_remaining == 0 ) |
| 692 | { |
| 693 | cur_limb = *--E; |
| 694 | E_len--; |
| 695 | limb_bits_remaining = biL; |
| 696 | } |
| 697 | |
| 698 | /* Square */ |
| 699 | mbedtls_mpi_core_montmul( X, X, X, n, N, n, mm, temp ); |
| 700 | |
| 701 | /* Insert next exponent bit into window */ |
| 702 | window <<= 1; |
| 703 | window |= ( cur_limb >> ( biL - 1 ) ); |
| 704 | cur_limb <<= 1; |
| 705 | window_bits++; |
| 706 | limb_bits_remaining--; |
| 707 | } |
| 708 | |
| 709 | /* Convert X back to normal presentation */ |
| 710 | mbedtls_mpi_core_montmul( X, X, &one, 1, N, n, mm, temp ); |
| 711 | |
| 712 | ret = 0; |
| 713 | |
| 714 | cleanup: |
| 715 | |
| 716 | mbedtls_free( mempool ); |
| 717 | return( ret ); |
| 718 | } |
| 719 | |
Janos Follath | 2a8bcf8 | 2022-11-02 10:47:30 +0000 | [diff] [blame] | 720 | /* END MERGE SLOT 1 */ |
| 721 | |
| 722 | /* BEGIN MERGE SLOT 2 */ |
| 723 | |
| 724 | /* END MERGE SLOT 2 */ |
| 725 | |
| 726 | /* BEGIN MERGE SLOT 3 */ |
| 727 | |
| 728 | /* END MERGE SLOT 3 */ |
| 729 | |
| 730 | /* BEGIN MERGE SLOT 4 */ |
| 731 | |
| 732 | /* END MERGE SLOT 4 */ |
| 733 | |
| 734 | /* BEGIN MERGE SLOT 5 */ |
| 735 | |
| 736 | /* END MERGE SLOT 5 */ |
| 737 | |
| 738 | /* BEGIN MERGE SLOT 6 */ |
| 739 | |
| 740 | /* END MERGE SLOT 6 */ |
| 741 | |
| 742 | /* BEGIN MERGE SLOT 7 */ |
| 743 | |
| 744 | /* END MERGE SLOT 7 */ |
| 745 | |
| 746 | /* BEGIN MERGE SLOT 8 */ |
| 747 | |
| 748 | /* END MERGE SLOT 8 */ |
| 749 | |
| 750 | /* BEGIN MERGE SLOT 9 */ |
| 751 | |
| 752 | /* END MERGE SLOT 9 */ |
| 753 | |
| 754 | /* BEGIN MERGE SLOT 10 */ |
| 755 | |
| 756 | /* END MERGE SLOT 10 */ |
| 757 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 758 | #endif /* MBEDTLS_BIGNUM_C */ |