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" |
| 28 | |
| 29 | #if defined(MBEDTLS_PLATFORM_C) |
| 30 | #include "mbedtls/platform.h" |
| 31 | #else |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #define mbedtls_printf printf |
| 35 | #define mbedtls_calloc calloc |
| 36 | #define mbedtls_free free |
| 37 | #endif |
| 38 | |
| 39 | #include "bignum_core.h" |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame^] | 40 | #include "bn_mul.h" |
| 41 | #include "constant_time_internal.h" |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 42 | |
Janos Follath | 2e328c8 | 2022-08-22 11:19:10 +0100 | [diff] [blame] | 43 | size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 44 | { |
| 45 | size_t j; |
| 46 | mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); |
| 47 | |
| 48 | for( j = 0; j < biL; j++ ) |
| 49 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 50 | if( a & mask ) break; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 51 | |
| 52 | mask >>= 1; |
| 53 | } |
| 54 | |
Gabor Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 55 | return( j ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 58 | 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] | 59 | { |
| 60 | size_t i, j; |
| 61 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 62 | if( A_limbs == 0 ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 63 | return( 0 ); |
| 64 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 65 | for( i = A_limbs - 1; i > 0; i-- ) |
| 66 | if( A[i] != 0 ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 67 | break; |
| 68 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 69 | j = biL - mbedtls_mpi_core_clz( A[i] ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 70 | |
| 71 | return( ( i * biL ) + j ); |
| 72 | } |
| 73 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 74 | /* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint |
| 75 | * into the storage form used by mbedtls_mpi. */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 76 | 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] | 77 | { |
| 78 | uint8_t i; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 79 | unsigned char *a_ptr; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 80 | mbedtls_mpi_uint tmp = 0; |
| 81 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 82 | 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] | 83 | { |
| 84 | tmp <<= CHAR_BIT; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 85 | tmp |= (mbedtls_mpi_uint) *a_ptr; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return( tmp ); |
| 89 | } |
| 90 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 91 | static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 92 | { |
| 93 | #if defined(__BYTE_ORDER__) |
| 94 | |
| 95 | /* Nothing to do on bigendian systems. */ |
| 96 | #if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ) |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 97 | return( a ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 98 | #endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */ |
| 99 | |
| 100 | #if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ) |
| 101 | |
| 102 | /* For GCC and Clang, have builtins for byte swapping. */ |
| 103 | #if defined(__GNUC__) && defined(__GNUC_PREREQ) |
| 104 | #if __GNUC_PREREQ(4,3) |
| 105 | #define have_bswap |
| 106 | #endif |
| 107 | #endif |
| 108 | |
| 109 | #if defined(__clang__) && defined(__has_builtin) |
| 110 | #if __has_builtin(__builtin_bswap32) && \ |
| 111 | __has_builtin(__builtin_bswap64) |
| 112 | #define have_bswap |
| 113 | #endif |
| 114 | #endif |
| 115 | |
| 116 | #if defined(have_bswap) |
| 117 | /* The compiler is hopefully able to statically evaluate this! */ |
| 118 | switch( sizeof(mbedtls_mpi_uint) ) |
| 119 | { |
| 120 | case 4: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 121 | return( __builtin_bswap32(a) ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 122 | case 8: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 123 | return( __builtin_bswap64(a) ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 124 | } |
| 125 | #endif |
| 126 | #endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */ |
| 127 | #endif /* __BYTE_ORDER__ */ |
| 128 | |
| 129 | /* Fall back to C-based reordering if we don't know the byte order |
| 130 | * or we couldn't use a compiler-specific builtin. */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 131 | return( mpi_bigendian_to_host_c( a ) ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 134 | void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A, |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 135 | size_t A_limbs ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 136 | { |
| 137 | mbedtls_mpi_uint *cur_limb_left; |
| 138 | mbedtls_mpi_uint *cur_limb_right; |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 139 | if( A_limbs == 0 ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 140 | return; |
| 141 | |
| 142 | /* |
| 143 | * Traverse limbs and |
| 144 | * - adapt byte-order in each limb |
| 145 | * - swap the limbs themselves. |
| 146 | * For that, simultaneously traverse the limbs from left to right |
| 147 | * and from right to left, as long as the left index is not bigger |
| 148 | * than the right index (it's not a problem if limbs is odd and the |
| 149 | * indices coincide in the last iteration). |
| 150 | */ |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 151 | for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 152 | cur_limb_left <= cur_limb_right; |
| 153 | cur_limb_left++, cur_limb_right-- ) |
| 154 | { |
| 155 | mbedtls_mpi_uint tmp; |
| 156 | /* Note that if cur_limb_left == cur_limb_right, |
| 157 | * this code effectively swaps the bytes only once. */ |
| 158 | tmp = mpi_bigendian_to_host( *cur_limb_left ); |
| 159 | *cur_limb_left = mpi_bigendian_to_host( *cur_limb_right ); |
| 160 | *cur_limb_right = tmp; |
| 161 | } |
| 162 | } |
| 163 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 164 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 165 | size_t X_limbs, |
| 166 | const unsigned char *input, |
| 167 | size_t input_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 168 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 169 | const size_t limbs = CHARS_TO_LIMBS( input_length ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 170 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 171 | if( X_limbs < limbs ) |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 172 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 173 | |
| 174 | if( X != NULL ) |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 175 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 176 | memset( X, 0, X_limbs * ciL ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 177 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 178 | for( size_t i = 0; i < input_length; i++ ) |
Janos Follath | ca5688e | 2022-08-19 12:05:28 +0100 | [diff] [blame] | 179 | { |
| 180 | size_t offset = ( ( i % ciL ) << 3 ); |
| 181 | X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset; |
| 182 | } |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 183 | } |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 184 | |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 185 | return( 0 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 188 | int mbedtls_mpi_core_read_be( 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 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 198 | /* If X_limbs is 0, input_length must also be 0 (from previous test). |
| 199 | * Nothing to do. */ |
| 200 | if( X_limbs == 0 ) |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 201 | return( 0 ); |
| 202 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 203 | memset( X, 0, X_limbs * ciL ); |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 204 | |
| 205 | /* memcpy() with (NULL, 0) is undefined behaviour */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 206 | if( input_length != 0 ) |
Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 207 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 208 | size_t overhead = ( X_limbs * ciL ) - input_length; |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 209 | unsigned char *Xp = (unsigned char *) X; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 210 | memcpy( Xp + overhead, input, input_length ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 213 | mbedtls_mpi_core_bigendian_to_host( X, X_limbs ); |
Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 214 | |
Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 215 | return( 0 ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 218 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A, |
| 219 | size_t A_limbs, |
| 220 | unsigned char *output, |
| 221 | size_t output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 222 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 223 | size_t stored_bytes = A_limbs * ciL; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 224 | size_t bytes_to_copy; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 225 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 226 | if( stored_bytes < output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 227 | { |
| 228 | bytes_to_copy = stored_bytes; |
| 229 | } |
| 230 | else |
| 231 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 232 | bytes_to_copy = output_length; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 233 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 234 | /* The output buffer is smaller than the allocated size of A. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 235 | * However A may fit if its leading bytes are zero. */ |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 236 | for( size_t i = bytes_to_copy; i < stored_bytes; i++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 237 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 238 | if( GET_BYTE( A, i ) != 0 ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 239 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 240 | } |
| 241 | } |
| 242 | |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 243 | for( size_t i = 0; i < bytes_to_copy; i++ ) |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 244 | output[i] = GET_BYTE( A, i ); |
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 | if( stored_bytes < output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 247 | { |
| 248 | /* Write trailing 0 bytes */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 249 | memset( output + stored_bytes, 0, output_length - stored_bytes ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return( 0 ); |
| 253 | } |
| 254 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 255 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 256 | size_t X_limbs, |
| 257 | unsigned char *output, |
| 258 | size_t output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 259 | { |
| 260 | size_t stored_bytes; |
| 261 | size_t bytes_to_copy; |
| 262 | unsigned char *p; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 263 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 264 | stored_bytes = X_limbs * ciL; |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 265 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 266 | if( stored_bytes < output_length ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 267 | { |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 268 | /* There is enough space in the output buffer. Write initial |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 269 | * null bytes and record the position at which to start |
| 270 | * writing the significant bytes. In this case, the execution |
| 271 | * trace of this function does not depend on the value of the |
| 272 | * number. */ |
| 273 | bytes_to_copy = stored_bytes; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 274 | p = output + output_length - stored_bytes; |
| 275 | memset( output, 0, output_length - stored_bytes ); |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 276 | } |
| 277 | else |
| 278 | { |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 279 | /* The output buffer is smaller than the allocated size of X. |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 280 | * However X may fit if its leading bytes are zero. */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 281 | bytes_to_copy = output_length; |
| 282 | p = output; |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 283 | for( size_t i = bytes_to_copy; i < stored_bytes; i++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 284 | { |
| 285 | if( GET_BYTE( X, i ) != 0 ) |
| 286 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
| 287 | } |
| 288 | } |
| 289 | |
Janos Follath | cc93908 | 2022-08-15 12:08:49 +0100 | [diff] [blame] | 290 | for( size_t i = 0; i < bytes_to_copy; i++ ) |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 291 | p[bytes_to_copy - i - 1] = GET_BYTE( X, i ); |
| 292 | |
| 293 | return( 0 ); |
| 294 | } |
| 295 | |
Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame^] | 296 | void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, |
| 297 | const mbedtls_mpi_uint *A, |
| 298 | const mbedtls_mpi_uint *B, |
| 299 | size_t B_len, |
| 300 | const mbedtls_mpi_uint *N, |
| 301 | size_t n, |
| 302 | mbedtls_mpi_uint mm, |
| 303 | mbedtls_mpi_uint *T ) |
| 304 | { |
| 305 | memset( T, 0, ( 2 * n + 1 ) * ciL ); |
| 306 | |
| 307 | for( size_t i = 0; i < n; i++, T++ ) |
| 308 | { |
| 309 | mbedtls_mpi_uint u0, u1; |
| 310 | /* T = (T + u0*B + u1*N) / 2^biL */ |
| 311 | u0 = A[i]; |
| 312 | u1 = ( T[0] + u0 * B[0] ) * mm; |
| 313 | |
| 314 | (void) mbedtls_mpi_core_mla( T, n + 2, B, B_len, u0 ); |
| 315 | (void) mbedtls_mpi_core_mla( T, n + 2, N, n, u1 ); |
| 316 | } |
| 317 | |
| 318 | /* It's possible that the result in T is > N, and so we might need to subtract N */ |
| 319 | |
| 320 | mbedtls_mpi_uint carry = T[n]; |
| 321 | mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, n ); |
| 322 | |
| 323 | /* |
| 324 | * Both carry and borrow can only be 0 or 1. |
| 325 | * |
| 326 | * If carry = 1, the result in T must be > N by definition, and the subtraction |
| 327 | * using only n limbs will create borrow, but that will have the correct |
| 328 | * final result. |
| 329 | * |
| 330 | * i.e. (carry, borrow) of (1, 1) => return X |
| 331 | * |
| 332 | * If carry = 0, then we want to use the result of the subtraction iff |
| 333 | * borrow = 0. |
| 334 | * |
| 335 | * i.e. (carry, borrow) of (0, 0) => return X |
| 336 | * (0, 1) => return T |
| 337 | * |
| 338 | * We've confirmed that the unit tests exercise this function with all 3 of |
| 339 | * the valid (carry, borrow) combinations (listed above), and that we don't |
| 340 | * see (carry, borrow) = (1, 0). |
| 341 | * |
| 342 | * So the correct return value is already in X if (carry ^ borrow) = 0, |
| 343 | * but is in (the lower n limbs of) T if (carry ^ borrow) = 1. |
| 344 | */ |
| 345 | mbedtls_ct_mpi_uint_cond_assign( n, X, T, (unsigned char) ( carry ^ borrow ) ); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Fast Montgomery initialization (thanks to Tom St Denis). |
| 350 | */ |
| 351 | mbedtls_mpi_uint mbedtls_mpi_montg_init( mbedtls_mpi_uint m0 ) |
| 352 | { |
| 353 | mbedtls_mpi_uint x = m0; |
| 354 | |
| 355 | x += ( ( m0 + 2 ) & 4 ) << 1; |
| 356 | |
| 357 | for( unsigned int i = biL; i >= 8; i /= 2 ) |
| 358 | x *= ( 2 - ( m0 * x ) ); |
| 359 | |
| 360 | return( ~x + 1 ); |
| 361 | } |
| 362 | |
| 363 | mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, |
| 364 | const mbedtls_mpi_uint *s, size_t s_len, |
| 365 | mbedtls_mpi_uint b ) |
| 366 | { |
| 367 | mbedtls_mpi_uint c = 0; /* carry */ |
| 368 | if( d_len < s_len ) |
| 369 | s_len = d_len; |
| 370 | size_t excess_len = d_len - s_len; |
| 371 | size_t steps_x8 = s_len / 8; |
| 372 | size_t steps_x1 = s_len & 7; |
| 373 | |
| 374 | while( steps_x8-- ) |
| 375 | { |
| 376 | MULADDC_X8_INIT |
| 377 | MULADDC_X8_CORE |
| 378 | MULADDC_X8_STOP |
| 379 | } |
| 380 | |
| 381 | while( steps_x1-- ) |
| 382 | { |
| 383 | MULADDC_X1_INIT |
| 384 | MULADDC_X1_CORE |
| 385 | MULADDC_X1_STOP |
| 386 | } |
| 387 | |
| 388 | while( excess_len-- ) |
| 389 | { |
| 390 | *d += c; c = ( *d < c ); d++; |
| 391 | } |
| 392 | |
| 393 | return( c ); |
| 394 | } |
| 395 | |
| 396 | mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d, |
| 397 | const mbedtls_mpi_uint *l, |
| 398 | const mbedtls_mpi_uint *r, |
| 399 | size_t n ) |
| 400 | { |
| 401 | mbedtls_mpi_uint c = 0; |
| 402 | |
| 403 | for( size_t i = 0; i < n; i++ ) |
| 404 | { |
| 405 | mbedtls_mpi_uint z = ( l[i] < c ); |
| 406 | mbedtls_mpi_uint t = l[i] - c; |
| 407 | c = ( t < r[i] ) + z; |
| 408 | d[i] = t - r[i]; |
| 409 | } |
| 410 | |
| 411 | return( c ); |
| 412 | } |
| 413 | |
| 414 | mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d, |
| 415 | const mbedtls_mpi_uint *r, |
| 416 | size_t n, |
| 417 | unsigned cond ) |
| 418 | { |
| 419 | mbedtls_mpi_uint c = 0, t; |
| 420 | for( size_t i = 0; i < n; i++ ) |
| 421 | { |
| 422 | mbedtls_mpi_uint add = cond * r[i]; |
| 423 | t = c; |
| 424 | t += d[i]; c = ( t < d[i] ); |
| 425 | t += add; c += ( t < add ); |
| 426 | d[i] = t; |
| 427 | } |
| 428 | return( c ); |
| 429 | } |
| 430 | |
Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 431 | #endif /* MBEDTLS_BIGNUM_C */ |