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 | { |
Dave Rodgman | 6d23ff6 | 2022-11-28 14:38:53 +0000 | [diff] [blame] | 86 | if ( MBEDTLS_IS_BIG_ENDIAN ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 87 | { |
Dave Rodgman | 6d23ff6 | 2022-11-28 14:38:53 +0000 | [diff] [blame] | 88 | /* Nothing to do on bigendian systems. */ |
| 89 | return( a ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 90 | } |
Dave Rodgman | 6d23ff6 | 2022-11-28 14:38:53 +0000 | [diff] [blame] | 91 | else |
| 92 | { |
| 93 | switch( sizeof(mbedtls_mpi_uint) ) |
| 94 | { |
| 95 | case 4: |
| 96 | return (mbedtls_mpi_uint) MBEDTLS_BSWAP32( (uint32_t)a ); |
| 97 | case 8: |
| 98 | return (mbedtls_mpi_uint) MBEDTLS_BSWAP64( (uint64_t)a ); |
| 99 | } |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 100 | |
Dave Rodgman | 6d23ff6 | 2022-11-28 14:38:53 +0000 | [diff] [blame] | 101 | /* Fall back to C-based reordering if we don't know the byte order |
| 102 | * or we couldn't use a compiler-specific builtin. */ |
| 103 | return( mpi_bigendian_to_host_c( a ) ); |
| 104 | } |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 107 | void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A, |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 108 | size_t A_limbs ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 109 | { |
| 110 | mbedtls_mpi_uint *cur_limb_left; |
| 111 | mbedtls_mpi_uint *cur_limb_right; |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 112 | if( A_limbs == 0 ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 113 | return; |
| 114 | |
| 115 | /* |
| 116 | * Traverse limbs and |
| 117 | * - adapt byte-order in each limb |
| 118 | * - swap the limbs themselves. |
| 119 | * For that, simultaneously traverse the limbs from left to right |
| 120 | * and from right to left, as long as the left index is not bigger |
| 121 | * than the right index (it's not a problem if limbs is odd and the |
| 122 | * indices coincide in the last iteration). |
| 123 | */ |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 124 | for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 125 | cur_limb_left <= cur_limb_right; |
| 126 | cur_limb_left++, cur_limb_right-- ) |
| 127 | { |
| 128 | mbedtls_mpi_uint tmp; |
| 129 | /* Note that if cur_limb_left == cur_limb_right, |
| 130 | * this code effectively swaps the bytes only once. */ |
Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 131 | tmp = mpi_bigendian_to_host( *cur_limb_left ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 132 | *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right ); |
| 133 | *cur_limb_right = tmp; |
| 134 | } |
| 135 | } |
| 136 | |
Gilles Peskine | 6f949ea | 2022-09-20 18:38:35 +0200 | [diff] [blame] | 137 | /* Whether min <= A, in constant time. |
| 138 | * A_limbs must be at least 1. */ |
| 139 | unsigned mbedtls_mpi_core_uint_le_mpi( mbedtls_mpi_uint min, |
| 140 | const mbedtls_mpi_uint *A, |
| 141 | size_t A_limbs ) |
| 142 | { |
| 143 | /* min <= least significant limb? */ |
| 144 | unsigned min_le_lsl = 1 ^ mbedtls_ct_mpi_uint_lt( A[0], min ); |
| 145 | |
Gilles Peskine | 6b7ce96 | 2022-12-15 15:04:33 +0100 | [diff] [blame] | 146 | /* limbs other than the least significant one are all zero? */ |
Gilles Peskine | 6f949ea | 2022-09-20 18:38:35 +0200 | [diff] [blame] | 147 | mbedtls_mpi_uint msll_mask = 0; |
| 148 | for( size_t i = 1; i < A_limbs; i++ ) |
| 149 | msll_mask |= A[i]; |
| 150 | /* The most significant limbs of A are not all zero iff msll_mask != 0. */ |
| 151 | unsigned msll_nonzero = mbedtls_ct_mpi_uint_mask( msll_mask ) & 1; |
| 152 | |
| 153 | /* min <= A iff the lowest limb of A is >= min or the other limbs |
| 154 | * are not all zero. */ |
| 155 | return( min_le_lsl | msll_nonzero ); |
| 156 | } |
| 157 | |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 158 | void mbedtls_mpi_core_cond_assign( mbedtls_mpi_uint *X, |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 159 | const mbedtls_mpi_uint *A, |
Gabor Mezei | 3eff425 | 2022-09-26 17:26:42 +0200 | [diff] [blame] | 160 | size_t limbs, |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 161 | unsigned char assign ) |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 162 | { |
Gabor Mezei | e9c013c | 2022-10-10 14:26:57 +0200 | [diff] [blame] | 163 | if( X == A ) |
| 164 | return; |
| 165 | |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 166 | mbedtls_ct_mpi_uint_cond_assign( limbs, X, A, assign ); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 169 | void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X, |
| 170 | mbedtls_mpi_uint *Y, |
Gabor Mezei | cfc0eb8 | 2022-09-15 20:15:34 +0200 | [diff] [blame] | 171 | size_t limbs, |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 172 | unsigned char swap ) |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 173 | { |
Gabor Mezei | e9c013c | 2022-10-10 14:26:57 +0200 | [diff] [blame] | 174 | if( X == Y ) |
| 175 | return; |
| 176 | |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 177 | /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */ |
| 178 | mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask( swap ); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 179 | |
Gabor Mezei | cfc0eb8 | 2022-09-15 20:15:34 +0200 | [diff] [blame] | 180 | for( size_t i = 0; i < limbs; i++ ) |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 181 | { |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 182 | mbedtls_mpi_uint tmp = X[i]; |
| 183 | X[i] = ( X[i] & ~limb_mask ) | ( Y[i] & limb_mask ); |
| 184 | Y[i] = ( Y[i] & ~limb_mask ) | ( tmp & limb_mask ); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 185 | } |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 188 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 189 | size_t X_limbs, |
| 190 | const unsigned char *input, |
| 191 | size_t input_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 192 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 193 | const size_t limbs = CHARS_TO_LIMBS( input_length ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 194 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 195 | if( X_limbs < limbs ) |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 196 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 197 | |
| 198 | if( X != NULL ) |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 199 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 200 | memset( X, 0, X_limbs * ciL ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 201 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 202 | for( size_t i = 0; i < input_length; i++ ) |
Janos Follath | ca5688e | 2022-08-19 12:05:28 +0100 | [diff] [blame] | 203 | { |
| 204 | size_t offset = ( ( i % ciL ) << 3 ); |
| 205 | X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset; |
| 206 | } |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 207 | } |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 208 | |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 209 | return( 0 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 212 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 213 | size_t X_limbs, |
| 214 | const unsigned char *input, |
| 215 | size_t input_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 216 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 217 | const size_t limbs = CHARS_TO_LIMBS( input_length ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 218 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 219 | if( X_limbs < limbs ) |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 220 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 221 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 222 | /* If X_limbs is 0, input_length must also be 0 (from previous test). |
| 223 | * Nothing to do. */ |
| 224 | if( X_limbs == 0 ) |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 225 | return( 0 ); |
| 226 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 227 | memset( X, 0, X_limbs * ciL ); |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 228 | |
| 229 | /* memcpy() with (NULL, 0) is undefined behaviour */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 230 | if( input_length != 0 ) |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 231 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 232 | size_t overhead = ( X_limbs * ciL ) - input_length; |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 233 | unsigned char *Xp = (unsigned char *) X; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 234 | memcpy( Xp + overhead, input, input_length ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 237 | mbedtls_mpi_core_bigendian_to_host( X, X_limbs ); |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 238 | |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 239 | return( 0 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 242 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A, |
| 243 | size_t A_limbs, |
| 244 | unsigned char *output, |
| 245 | size_t output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 246 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 247 | size_t stored_bytes = A_limbs * ciL; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 248 | size_t bytes_to_copy; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 249 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 250 | if( stored_bytes < output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 251 | { |
| 252 | bytes_to_copy = stored_bytes; |
| 253 | } |
| 254 | else |
| 255 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 256 | bytes_to_copy = output_length; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 257 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 258 | /* The output buffer is smaller than the allocated size of A. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 259 | * However A may fit if its leading bytes are zero. */ |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 260 | for( size_t i = bytes_to_copy; i < stored_bytes; i++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 261 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 262 | if( GET_BYTE( A, i ) != 0 ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 263 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 264 | } |
| 265 | } |
| 266 | |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 267 | for( size_t i = 0; i < bytes_to_copy; i++ ) |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 268 | output[i] = GET_BYTE( A, i ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 269 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 270 | if( stored_bytes < output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 271 | { |
| 272 | /* Write trailing 0 bytes */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 273 | memset( output + stored_bytes, 0, output_length - stored_bytes ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return( 0 ); |
| 277 | } |
| 278 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 279 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 280 | size_t X_limbs, |
| 281 | unsigned char *output, |
| 282 | size_t output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 283 | { |
| 284 | size_t stored_bytes; |
| 285 | size_t bytes_to_copy; |
| 286 | unsigned char *p; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 287 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 288 | stored_bytes = X_limbs * ciL; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 289 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 290 | if( stored_bytes < output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 291 | { |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 292 | /* There is enough space in the output buffer. Write initial |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 293 | * null bytes and record the position at which to start |
| 294 | * writing the significant bytes. In this case, the execution |
| 295 | * trace of this function does not depend on the value of the |
| 296 | * number. */ |
| 297 | bytes_to_copy = stored_bytes; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 298 | p = output + output_length - stored_bytes; |
| 299 | memset( output, 0, output_length - stored_bytes ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 300 | } |
| 301 | else |
| 302 | { |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 303 | /* The output buffer is smaller than the allocated size of X. |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 304 | * However X may fit if its leading bytes are zero. */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 305 | bytes_to_copy = output_length; |
| 306 | p = output; |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 307 | for( size_t i = bytes_to_copy; i < stored_bytes; i++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 308 | { |
| 309 | if( GET_BYTE( X, i ) != 0 ) |
| 310 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 311 | } |
| 312 | } |
| 313 | |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 314 | for( size_t i = 0; i < bytes_to_copy; i++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 315 | p[bytes_to_copy - i - 1] = GET_BYTE( X, i ); |
| 316 | |
| 317 | return( 0 ); |
| 318 | } |
| 319 | |
Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 320 | void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs, |
| 321 | size_t count ) |
| 322 | { |
| 323 | size_t i, v0, v1; |
| 324 | mbedtls_mpi_uint r0 = 0, r1; |
| 325 | |
| 326 | v0 = count / biL; |
| 327 | v1 = count & (biL - 1); |
| 328 | |
| 329 | if( v0 > limbs || ( v0 == limbs && v1 > 0 ) ) |
| 330 | { |
| 331 | memset( X, 0, limbs * ciL ); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * shift by count / limb_size |
| 337 | */ |
| 338 | if( v0 > 0 ) |
| 339 | { |
| 340 | for( i = 0; i < limbs - v0; i++ ) |
| 341 | X[i] = X[i + v0]; |
| 342 | |
| 343 | for( ; i < limbs; i++ ) |
| 344 | X[i] = 0; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * shift by count % limb_size |
| 349 | */ |
| 350 | if( v1 > 0 ) |
| 351 | { |
| 352 | for( i = limbs; i > 0; i-- ) |
| 353 | { |
| 354 | r1 = X[i - 1] << (biL - v1); |
| 355 | X[i - 1] >>= v1; |
| 356 | X[i - 1] |= r0; |
| 357 | r0 = r1; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 362 | mbedtls_mpi_uint mbedtls_mpi_core_add( mbedtls_mpi_uint *X, |
| 363 | const mbedtls_mpi_uint *A, |
| 364 | const mbedtls_mpi_uint *B, |
| 365 | size_t limbs ) |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 366 | { |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 367 | mbedtls_mpi_uint c = 0; |
Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 368 | |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 369 | for( size_t i = 0; i < limbs; i++ ) |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 370 | { |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 371 | mbedtls_mpi_uint t = c + A[i]; |
| 372 | c = ( t < A[i] ); |
| 373 | t += B[i]; |
| 374 | c += ( t < B[i] ); |
| 375 | X[i] = t; |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 376 | } |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 377 | |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 378 | return( c ); |
| 379 | } |
Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 380 | |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 381 | mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X, |
| 382 | const mbedtls_mpi_uint *A, |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 383 | size_t limbs, |
| 384 | unsigned cond ) |
| 385 | { |
Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 386 | mbedtls_mpi_uint c = 0; |
| 387 | |
Tom Cosgrove | 2701dea | 2022-09-15 15:00:07 +0100 | [diff] [blame] | 388 | /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */ |
| 389 | const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond ); |
Tom Cosgrove | 9354990 | 2022-08-30 17:41:23 +0100 | [diff] [blame] | 390 | |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 391 | for( size_t i = 0; i < limbs; i++ ) |
| 392 | { |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 393 | mbedtls_mpi_uint add = mask & A[i]; |
| 394 | mbedtls_mpi_uint t = c + X[i]; |
| 395 | c = ( t < X[i] ); |
Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 396 | t += add; |
| 397 | c += ( t < add ); |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 398 | X[i] = t; |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 399 | } |
Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 400 | |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 401 | return( c ); |
| 402 | } |
| 403 | |
| 404 | mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X, |
| 405 | const mbedtls_mpi_uint *A, |
| 406 | const mbedtls_mpi_uint *B, |
| 407 | size_t limbs ) |
| 408 | { |
| 409 | mbedtls_mpi_uint c = 0; |
| 410 | |
| 411 | for( size_t i = 0; i < limbs; i++ ) |
| 412 | { |
| 413 | mbedtls_mpi_uint z = ( A[i] < c ); |
| 414 | mbedtls_mpi_uint t = A[i] - c; |
| 415 | c = ( t < B[i] ) + z; |
| 416 | X[i] = t - B[i]; |
| 417 | } |
| 418 | |
| 419 | return( c ); |
| 420 | } |
| 421 | |
| 422 | mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, |
| 423 | const mbedtls_mpi_uint *s, size_t s_len, |
| 424 | mbedtls_mpi_uint b ) |
| 425 | { |
| 426 | mbedtls_mpi_uint c = 0; /* carry */ |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 427 | /* |
| 428 | * It is a documented precondition of this function that d_len >= s_len. |
| 429 | * If that's not the case, we swap these round: this turns what would be |
| 430 | * a buffer overflow into an incorrect result. |
| 431 | */ |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 432 | if( d_len < s_len ) |
| 433 | s_len = d_len; |
| 434 | size_t excess_len = d_len - s_len; |
| 435 | size_t steps_x8 = s_len / 8; |
| 436 | size_t steps_x1 = s_len & 7; |
| 437 | |
| 438 | while( steps_x8-- ) |
| 439 | { |
| 440 | MULADDC_X8_INIT |
| 441 | MULADDC_X8_CORE |
| 442 | MULADDC_X8_STOP |
| 443 | } |
| 444 | |
| 445 | while( steps_x1-- ) |
| 446 | { |
| 447 | MULADDC_X1_INIT |
| 448 | MULADDC_X1_CORE |
| 449 | MULADDC_X1_STOP |
| 450 | } |
| 451 | |
| 452 | while( excess_len-- ) |
| 453 | { |
Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 454 | *d += c; |
| 455 | c = ( *d < c ); |
| 456 | d++; |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | return( c ); |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Fast Montgomery initialization (thanks to Tom St Denis). |
| 464 | */ |
Tom Cosgrove | b7438d1 | 2022-09-15 15:05:59 +0100 | [diff] [blame] | 465 | 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] | 466 | { |
| 467 | mbedtls_mpi_uint x = N[0]; |
| 468 | |
| 469 | x += ( ( N[0] + 2 ) & 4 ) << 1; |
| 470 | |
| 471 | for( unsigned int i = biL; i >= 8; i /= 2 ) |
| 472 | x *= ( 2 - ( N[0] * x ) ); |
| 473 | |
| 474 | return( ~x + 1 ); |
| 475 | } |
| 476 | |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 477 | void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, |
| 478 | const mbedtls_mpi_uint *A, |
| 479 | const mbedtls_mpi_uint *B, |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 480 | size_t B_limbs, |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 481 | const mbedtls_mpi_uint *N, |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 482 | size_t AN_limbs, |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 483 | mbedtls_mpi_uint mm, |
| 484 | mbedtls_mpi_uint *T ) |
| 485 | { |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 486 | memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL ); |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 487 | |
Tom Cosgrove | 67c9247 | 2022-09-02 13:28:59 +0100 | [diff] [blame] | 488 | for( size_t i = 0; i < AN_limbs; i++ ) |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 489 | { |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 490 | /* T = (T + u0*B + u1*N) / 2^biL */ |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 491 | mbedtls_mpi_uint u0 = A[i]; |
| 492 | mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm; |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 493 | |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 494 | (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 ); |
| 495 | (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] | 496 | |
| 497 | T++; |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 500 | /* |
| 501 | * The result we want is (T >= N) ? T - N : T. |
| 502 | * |
| 503 | * For better constant-time properties in this function, we always do the |
| 504 | * subtraction, with the result in X. |
| 505 | * |
| 506 | * We also look to see if there was any carry in the final additions in the |
| 507 | * loop above. |
| 508 | */ |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 509 | |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 510 | mbedtls_mpi_uint carry = T[AN_limbs]; |
| 511 | 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] | 512 | |
| 513 | /* |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 514 | * 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] | 515 | * |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 516 | * T can be in one of 3 ranges: |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 517 | * |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 518 | * 1) T < N : (carry, borrow) = (0, 1): we want T |
| 519 | * 2) N <= T < R : (carry, borrow) = (0, 0): we want X |
| 520 | * 3) T >= R : (carry, borrow) = (1, 1): we want X |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 521 | * |
Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 522 | * and (carry, borrow) = (1, 0) can't happen. |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 523 | * |
| 524 | * 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] | 525 | * 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] | 526 | */ |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 527 | 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] | 528 | } |
| 529 | |
Minos Galanakis | a081c51 | 2022-10-24 12:16:28 +0100 | [diff] [blame] | 530 | int mbedtls_mpi_core_get_mont_r2_unsafe( mbedtls_mpi *X, |
Minos Galanakis | 51d638b | 2022-10-24 09:59:44 +0100 | [diff] [blame] | 531 | const mbedtls_mpi *N ) |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 532 | { |
| 533 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 534 | |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 535 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 1 ) ); |
| 536 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, N->n * 2 * biL ) ); |
| 537 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( X, X, N ) ); |
| 538 | MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( X, N->n ) ); |
| 539 | |
| 540 | cleanup: |
| 541 | return( ret ); |
| 542 | } |
| 543 | |
Janos Follath | 59cbd1b | 2022-10-28 18:13:43 +0100 | [diff] [blame] | 544 | MBEDTLS_STATIC_TESTABLE |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 545 | void mbedtls_mpi_core_ct_uint_table_lookup( mbedtls_mpi_uint *dest, |
Janos Follath | 8904a2d | 2022-10-31 15:32:28 +0000 | [diff] [blame] | 546 | const mbedtls_mpi_uint *table, |
| 547 | size_t limbs, |
| 548 | size_t count, |
| 549 | size_t index ) |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 550 | { |
Janos Follath | 8904a2d | 2022-10-31 15:32:28 +0000 | [diff] [blame] | 551 | for( size_t i = 0; i < count; i++, table += limbs ) |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 552 | { |
| 553 | unsigned char assign = mbedtls_ct_size_bool_eq( i, index ); |
Janos Follath | 8904a2d | 2022-10-31 15:32:28 +0000 | [diff] [blame] | 554 | mbedtls_mpi_core_cond_assign( dest, table, limbs, assign ); |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 558 | /* Fill X with n_bytes random bytes. |
| 559 | * X must already have room for those bytes. |
| 560 | * The ordering of the bytes returned from the RNG is suitable for |
Gilles Peskine | 22cdd0c | 2022-10-27 20:15:13 +0200 | [diff] [blame] | 561 | * deterministic ECDSA (see RFC 6979 §3.3 and the specification of |
| 562 | * mbedtls_mpi_core_random()). |
Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 563 | */ |
| 564 | int mbedtls_mpi_core_fill_random( |
| 565 | mbedtls_mpi_uint *X, size_t X_limbs, |
| 566 | size_t n_bytes, |
| 567 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 568 | { |
| 569 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 570 | const size_t limbs = CHARS_TO_LIMBS( n_bytes ); |
| 571 | const size_t overhead = ( limbs * ciL ) - n_bytes; |
| 572 | |
| 573 | if( X_limbs < limbs ) |
| 574 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 575 | |
| 576 | memset( X, 0, overhead ); |
| 577 | memset( (unsigned char *) X + limbs * ciL, 0, ( X_limbs - limbs ) * ciL ); |
| 578 | MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X + overhead, n_bytes ) ); |
| 579 | mbedtls_mpi_core_bigendian_to_host( X, limbs ); |
| 580 | |
| 581 | cleanup: |
| 582 | return( ret ); |
| 583 | } |
| 584 | |
Gilles Peskine | 70375b2 | 2022-09-21 15:47:23 +0200 | [diff] [blame] | 585 | int mbedtls_mpi_core_random( mbedtls_mpi_uint *X, |
| 586 | mbedtls_mpi_uint min, |
| 587 | const mbedtls_mpi_uint *N, |
| 588 | size_t limbs, |
| 589 | int (*f_rng)(void *, unsigned char *, size_t), |
| 590 | void *p_rng ) |
| 591 | { |
| 592 | unsigned ge_lower = 1, lt_upper = 0; |
| 593 | size_t n_bits = mbedtls_mpi_core_bitlen( N, limbs ); |
| 594 | size_t n_bytes = ( n_bits + 7 ) / 8; |
| 595 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 596 | |
| 597 | /* |
| 598 | * When min == 0, each try has at worst a probability 1/2 of failing |
| 599 | * (the msb has a probability 1/2 of being 0, and then the result will |
| 600 | * be < N), so after 30 tries failure probability is a most 2**(-30). |
| 601 | * |
| 602 | * When N is just below a power of 2, as is the case when generating |
| 603 | * a random scalar on most elliptic curves, 1 try is enough with |
| 604 | * overwhelming probability. When N is just above a power of 2, |
| 605 | * as when generating a random scalar on secp224k1, each try has |
| 606 | * a probability of failing that is almost 1/2. |
| 607 | * |
| 608 | * The probabilities are almost the same if min is nonzero but negligible |
| 609 | * compared to N. This is always the case when N is crypto-sized, but |
| 610 | * it's convenient to support small N for testing purposes. When N |
| 611 | * is small, use a higher repeat count, otherwise the probability of |
| 612 | * failure is macroscopic. |
| 613 | */ |
| 614 | int count = ( n_bytes > 4 ? 30 : 250 ); |
| 615 | |
| 616 | /* |
| 617 | * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA) |
| 618 | * when f_rng is a suitably parametrized instance of HMAC_DRBG: |
| 619 | * - use the same byte ordering; |
| 620 | * - keep the leftmost n_bits bits of the generated octet string; |
| 621 | * - try until result is in the desired range. |
| 622 | * This also avoids any bias, which is especially important for ECDSA. |
| 623 | */ |
| 624 | do |
| 625 | { |
| 626 | MBEDTLS_MPI_CHK( mbedtls_mpi_core_fill_random( X, limbs, |
| 627 | n_bytes, |
| 628 | f_rng, p_rng ) ); |
| 629 | mbedtls_mpi_core_shift_r( X, limbs, 8 * n_bytes - n_bits ); |
| 630 | |
| 631 | if( --count == 0 ) |
| 632 | { |
| 633 | ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
| 634 | goto cleanup; |
| 635 | } |
| 636 | |
| 637 | ge_lower = mbedtls_mpi_core_uint_le_mpi( min, X, limbs ); |
| 638 | lt_upper = mbedtls_mpi_core_lt_ct( X, N, limbs ); |
| 639 | } |
| 640 | while( ge_lower == 0 || lt_upper == 0 ); |
| 641 | |
| 642 | cleanup: |
| 643 | return( ret ); |
| 644 | } |
| 645 | |
Janos Follath | 2a8bcf8 | 2022-11-02 10:47:30 +0000 | [diff] [blame] | 646 | /* BEGIN MERGE SLOT 1 */ |
| 647 | |
Janos Follath | a77911e | 2022-10-08 09:48:20 +0100 | [diff] [blame] | 648 | static size_t exp_mod_get_window_size( size_t Ebits ) |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 649 | { |
| 650 | size_t wsize = ( Ebits > 671 ) ? 6 : ( Ebits > 239 ) ? 5 : |
Janos Follath | a77911e | 2022-10-08 09:48:20 +0100 | [diff] [blame] | 651 | ( Ebits > 79 ) ? 4 : 1; |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 652 | |
| 653 | #if( MBEDTLS_MPI_WINDOW_SIZE < 6 ) |
| 654 | if( wsize > MBEDTLS_MPI_WINDOW_SIZE ) |
| 655 | wsize = MBEDTLS_MPI_WINDOW_SIZE; |
| 656 | #endif |
| 657 | |
| 658 | return( wsize ); |
| 659 | } |
| 660 | |
Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 661 | size_t mbedtls_mpi_core_exp_mod_working_limbs( size_t AN_limbs, size_t E_limbs ) |
| 662 | { |
| 663 | const size_t wsize = exp_mod_get_window_size( E_limbs * biL ); |
| 664 | const size_t welem = ( (size_t) 1 ) << wsize; |
| 665 | |
| 666 | /* How big does each part of the working memory pool need to be? */ |
| 667 | const size_t table_limbs = welem * AN_limbs; |
| 668 | const size_t select_limbs = AN_limbs; |
| 669 | const size_t temp_limbs = 2 * AN_limbs + 1; |
| 670 | |
| 671 | return( table_limbs + select_limbs + temp_limbs ); |
| 672 | } |
| 673 | |
Gilles Peskine | 0de0a04 | 2022-11-16 20:12:49 +0100 | [diff] [blame] | 674 | static void exp_mod_precompute_window( const mbedtls_mpi_uint *A, |
| 675 | const mbedtls_mpi_uint *N, |
| 676 | size_t AN_limbs, |
| 677 | mbedtls_mpi_uint mm, |
| 678 | const mbedtls_mpi_uint *RR, |
| 679 | size_t welem, |
| 680 | mbedtls_mpi_uint *Wtable, |
| 681 | mbedtls_mpi_uint *temp ) |
| 682 | { |
Gilles Peskine | 0de0a04 | 2022-11-16 20:12:49 +0100 | [diff] [blame] | 683 | /* W[0] = 1 (in Montgomery presentation) */ |
| 684 | memset( Wtable, 0, AN_limbs * ciL ); |
| 685 | Wtable[0] = 1; |
| 686 | mbedtls_mpi_core_montmul( Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp ); |
Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 687 | |
Tom Cosgrove | ecda186 | 2022-12-06 10:46:30 +0000 | [diff] [blame] | 688 | /* W[1] = A (already in Montgomery presentation) */ |
Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 689 | mbedtls_mpi_uint *W1 = Wtable + AN_limbs; |
Tom Cosgrove | ecda186 | 2022-12-06 10:46:30 +0000 | [diff] [blame] | 690 | memcpy( W1, A, AN_limbs * ciL ); |
Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 691 | |
Gilles Peskine | 0de0a04 | 2022-11-16 20:12:49 +0100 | [diff] [blame] | 692 | /* W[i+1] = W[i] * W[1], i >= 2 */ |
Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 693 | mbedtls_mpi_uint *Wprev = W1; |
| 694 | for( size_t i = 2; i < welem; i++ ) |
| 695 | { |
| 696 | mbedtls_mpi_uint *Wcur = Wprev + AN_limbs; |
| 697 | mbedtls_mpi_core_montmul( Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp ); |
| 698 | Wprev = Wcur; |
| 699 | } |
Gilles Peskine | 0de0a04 | 2022-11-16 20:12:49 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Gilles Peskine | 7d89d35 | 2022-11-16 22:54:14 +0100 | [diff] [blame] | 702 | /* Exponentiation: X := A^E mod N. |
| 703 | * |
Tom Cosgrove | ecda186 | 2022-12-06 10:46:30 +0000 | [diff] [blame] | 704 | * A must already be in Montgomery form. |
| 705 | * |
Gilles Peskine | 7d89d35 | 2022-11-16 22:54:14 +0100 | [diff] [blame] | 706 | * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero. |
| 707 | * |
| 708 | * RR must contain 2^{2*biL} mod N. |
Janos Follath | 3321b58 | 2022-11-22 21:08:33 +0000 | [diff] [blame] | 709 | * |
| 710 | * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82 |
| 711 | * (The difference is that the body in our loop processes a single bit instead |
| 712 | * of a full window.) |
Gilles Peskine | 7d89d35 | 2022-11-16 22:54:14 +0100 | [diff] [blame] | 713 | */ |
Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 714 | void mbedtls_mpi_core_exp_mod( mbedtls_mpi_uint *X, |
| 715 | const mbedtls_mpi_uint *A, |
| 716 | const mbedtls_mpi_uint *N, |
| 717 | size_t AN_limbs, |
| 718 | const mbedtls_mpi_uint *E, |
| 719 | size_t E_limbs, |
| 720 | const mbedtls_mpi_uint *RR, |
| 721 | mbedtls_mpi_uint *T ) |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 722 | { |
Gilles Peskine | cf979b0 | 2022-11-16 20:04:00 +0100 | [diff] [blame] | 723 | const size_t wsize = exp_mod_get_window_size( E_limbs * biL ); |
| 724 | const size_t welem = ( (size_t) 1 ) << wsize; |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 725 | |
Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 726 | /* This is how we will use the temporary storage T, which must have space |
| 727 | * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */ |
| 728 | const size_t table_limbs = welem * AN_limbs; |
| 729 | const size_t select_limbs = AN_limbs; |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 730 | |
Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 731 | /* Pointers to specific parts of the temporary working memory pool */ |
| 732 | mbedtls_mpi_uint *const Wtable = T; |
| 733 | mbedtls_mpi_uint *const Wselect = Wtable + table_limbs; |
| 734 | mbedtls_mpi_uint *const temp = Wselect + select_limbs; |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 735 | |
| 736 | /* |
| 737 | * Window precomputation |
| 738 | */ |
| 739 | |
Gilles Peskine | cf979b0 | 2022-11-16 20:04:00 +0100 | [diff] [blame] | 740 | const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init( N ); |
| 741 | |
Gilles Peskine | 0de0a04 | 2022-11-16 20:12:49 +0100 | [diff] [blame] | 742 | /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */ |
| 743 | exp_mod_precompute_window( A, N, AN_limbs, |
| 744 | mm, RR, |
| 745 | welem, Wtable, temp ); |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 746 | |
| 747 | /* |
Janos Follath | 0ec6e3f | 2022-11-14 12:52:08 +0000 | [diff] [blame] | 748 | * Fixed window exponentiation |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 749 | */ |
| 750 | |
| 751 | /* X = 1 (in Montgomery presentation) initially */ |
Gilles Peskine | 07f2c69 | 2022-11-16 19:48:23 +0100 | [diff] [blame] | 752 | memcpy( X, Wtable, AN_limbs * ciL ); |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 753 | |
Gilles Peskine | c718a3c | 2022-11-16 20:42:09 +0100 | [diff] [blame] | 754 | /* We'll process the bits of E from most significant |
| 755 | * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant |
| 756 | * (limb_index=0, E_bit_index=0). */ |
| 757 | size_t E_limb_index = E_limbs; |
| 758 | size_t E_bit_index = 0; |
Gilles Peskine | 0b270a5 | 2022-11-16 22:54:03 +0100 | [diff] [blame] | 759 | /* At any given time, window contains window_bits bits from E. |
| 760 | * window_bits can go up to wsize. */ |
Janos Follath | bad42c4 | 2022-11-09 14:30:44 +0000 | [diff] [blame] | 761 | size_t window_bits = 0; |
Gilles Peskine | 0b270a5 | 2022-11-16 22:54:03 +0100 | [diff] [blame] | 762 | mbedtls_mpi_uint window = 0; |
Gilles Peskine | cf979b0 | 2022-11-16 20:04:00 +0100 | [diff] [blame] | 763 | |
Gilles Peskine | 3b63d09 | 2022-11-16 22:06:18 +0100 | [diff] [blame] | 764 | do |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 765 | { |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 766 | /* Square */ |
Janos Follath | 0ec6e3f | 2022-11-14 12:52:08 +0000 | [diff] [blame] | 767 | mbedtls_mpi_core_montmul( X, X, X, AN_limbs, N, AN_limbs, mm, temp ); |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 768 | |
Janos Follath | 3321b58 | 2022-11-22 21:08:33 +0000 | [diff] [blame] | 769 | /* Move to the next bit of the exponent */ |
Gilles Peskine | c718a3c | 2022-11-16 20:42:09 +0100 | [diff] [blame] | 770 | if( E_bit_index == 0 ) |
| 771 | { |
| 772 | --E_limb_index; |
| 773 | E_bit_index = biL - 1; |
| 774 | } |
| 775 | else |
| 776 | { |
| 777 | --E_bit_index; |
| 778 | } |
Janos Follath | 3321b58 | 2022-11-22 21:08:33 +0000 | [diff] [blame] | 779 | /* Insert next exponent bit into window */ |
Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 780 | ++window_bits; |
Gilles Peskine | c718a3c | 2022-11-16 20:42:09 +0100 | [diff] [blame] | 781 | window <<= 1; |
| 782 | window |= ( E[E_limb_index] >> E_bit_index ) & 1; |
Gilles Peskine | 3b63d09 | 2022-11-16 22:06:18 +0100 | [diff] [blame] | 783 | |
| 784 | /* Clear window if it's full. Also clear the window at the end, |
| 785 | * when we've finished processing the exponent. */ |
| 786 | if( window_bits == wsize || |
| 787 | ( E_bit_index == 0 && E_limb_index == 0 ) ) |
| 788 | { |
Gilles Peskine | 0b270a5 | 2022-11-16 22:54:03 +0100 | [diff] [blame] | 789 | /* Select Wtable[window] without leaking window through |
| 790 | * memory access patterns. */ |
Gilles Peskine | 3b63d09 | 2022-11-16 22:06:18 +0100 | [diff] [blame] | 791 | mbedtls_mpi_core_ct_uint_table_lookup( Wselect, Wtable, |
| 792 | AN_limbs, welem, window ); |
Gilles Peskine | 0b270a5 | 2022-11-16 22:54:03 +0100 | [diff] [blame] | 793 | /* Multiply X by the selected element. */ |
Janos Follath | 3321b58 | 2022-11-22 21:08:33 +0000 | [diff] [blame] | 794 | mbedtls_mpi_core_montmul( X, X, Wselect, AN_limbs, N, AN_limbs, mm, |
| 795 | temp ); |
Gilles Peskine | 3b63d09 | 2022-11-16 22:06:18 +0100 | [diff] [blame] | 796 | window = 0; |
| 797 | window_bits = 0; |
| 798 | } |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 799 | } |
Gilles Peskine | 3b63d09 | 2022-11-16 22:06:18 +0100 | [diff] [blame] | 800 | while( ! ( E_bit_index == 0 && E_limb_index == 0 ) ); |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 801 | } |
| 802 | |
Janos Follath | 2a8bcf8 | 2022-11-02 10:47:30 +0000 | [diff] [blame] | 803 | /* END MERGE SLOT 1 */ |
| 804 | |
| 805 | /* BEGIN MERGE SLOT 2 */ |
| 806 | |
| 807 | /* END MERGE SLOT 2 */ |
| 808 | |
| 809 | /* BEGIN MERGE SLOT 3 */ |
| 810 | |
Tom Cosgrove | f7ff4c9 | 2022-08-25 08:39:07 +0100 | [diff] [blame] | 811 | mbedtls_mpi_uint mbedtls_mpi_core_sub_int( mbedtls_mpi_uint *X, |
| 812 | const mbedtls_mpi_uint *A, |
| 813 | mbedtls_mpi_uint c, /* doubles as carry */ |
| 814 | size_t limbs ) |
Hanno Becker | d9b2348 | 2022-08-25 08:25:19 +0100 | [diff] [blame] | 815 | { |
Tom Cosgrove | f7ff4c9 | 2022-08-25 08:39:07 +0100 | [diff] [blame] | 816 | for( size_t i = 0; i < limbs; i++ ) |
Hanno Becker | d9b2348 | 2022-08-25 08:25:19 +0100 | [diff] [blame] | 817 | { |
Tom Cosgrove | f7ff4c9 | 2022-08-25 08:39:07 +0100 | [diff] [blame] | 818 | mbedtls_mpi_uint s = A[i]; |
| 819 | mbedtls_mpi_uint t = s - c; |
| 820 | c = ( t > s ); |
| 821 | X[i] = t; |
Hanno Becker | d9b2348 | 2022-08-25 08:25:19 +0100 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | return( c ); |
| 825 | } |
| 826 | |
Tom Cosgrove | 30f3b4d | 2022-12-12 16:54:57 +0000 | [diff] [blame] | 827 | mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct( const mbedtls_mpi_uint *A, |
| 828 | size_t limbs ) |
| 829 | { |
| 830 | mbedtls_mpi_uint bits = 0; |
| 831 | |
| 832 | for( size_t i = 0; i < limbs; i++ ) |
| 833 | bits |= A[i]; |
| 834 | |
| 835 | return( bits ); |
| 836 | } |
| 837 | |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 838 | void mbedtls_mpi_core_to_mont_rep( mbedtls_mpi_uint *X, |
| 839 | const mbedtls_mpi_uint *A, |
| 840 | const mbedtls_mpi_uint *N, |
| 841 | size_t AN_limbs, |
| 842 | mbedtls_mpi_uint mm, |
| 843 | const mbedtls_mpi_uint *rr, |
| 844 | mbedtls_mpi_uint *T ) |
| 845 | { |
| 846 | mbedtls_mpi_core_montmul( X, A, rr, AN_limbs, N, AN_limbs, mm, T ); |
| 847 | } |
| 848 | |
| 849 | void mbedtls_mpi_core_from_mont_rep( mbedtls_mpi_uint *X, |
| 850 | const mbedtls_mpi_uint *A, |
| 851 | const mbedtls_mpi_uint *N, |
| 852 | size_t AN_limbs, |
| 853 | mbedtls_mpi_uint mm, |
| 854 | mbedtls_mpi_uint *T ) |
| 855 | { |
| 856 | const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */ |
| 857 | |
| 858 | mbedtls_mpi_core_montmul( X, A, &Rinv, 1, N, AN_limbs, mm, T ); |
| 859 | } |
| 860 | |
Janos Follath | 2a8bcf8 | 2022-11-02 10:47:30 +0000 | [diff] [blame] | 861 | /* END MERGE SLOT 3 */ |
| 862 | |
| 863 | /* BEGIN MERGE SLOT 4 */ |
| 864 | |
| 865 | /* END MERGE SLOT 4 */ |
| 866 | |
| 867 | /* BEGIN MERGE SLOT 5 */ |
| 868 | |
| 869 | /* END MERGE SLOT 5 */ |
| 870 | |
| 871 | /* BEGIN MERGE SLOT 6 */ |
| 872 | |
| 873 | /* END MERGE SLOT 6 */ |
| 874 | |
| 875 | /* BEGIN MERGE SLOT 7 */ |
| 876 | |
| 877 | /* END MERGE SLOT 7 */ |
| 878 | |
| 879 | /* BEGIN MERGE SLOT 8 */ |
| 880 | |
| 881 | /* END MERGE SLOT 8 */ |
| 882 | |
| 883 | /* BEGIN MERGE SLOT 9 */ |
| 884 | |
| 885 | /* END MERGE SLOT 9 */ |
| 886 | |
| 887 | /* BEGIN MERGE SLOT 10 */ |
| 888 | |
| 889 | /* END MERGE SLOT 10 */ |
| 890 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 891 | #endif /* MBEDTLS_BIGNUM_C */ |