| 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 |
| Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "common.h" |
| 9 | |
| 10 | #if defined(MBEDTLS_BIGNUM_C) |
| 11 | |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include "mbedtls/error.h" |
| 15 | #include "mbedtls/platform_util.h" |
| Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 16 | #include "constant_time_internal.h" |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 17 | |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 18 | #include "mbedtls/platform.h" |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 19 | |
| 20 | #include "bignum_core.h" |
| Manuel Pégourié-Gonnard | 7fba466 | 2025-07-17 09:17:12 +0200 | [diff] [blame] | 21 | #include "bignum_core_invasive.h" |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 22 | #include "bn_mul.h" |
| 23 | #include "constant_time_internal.h" |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 24 | |
| Dave Rodgman | 914347b | 2023-04-27 14:20:30 +0100 | [diff] [blame] | 25 | size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a) |
| 26 | { |
| 27 | #if defined(__has_builtin) |
| Agathiyan Bragadeesh | e55a1e1 | 2023-07-17 15:00:19 +0100 | [diff] [blame] | 28 | #if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_clz) |
| 29 | #define core_clz __builtin_clz |
| 30 | #elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_clzl) |
| 31 | #define core_clz __builtin_clzl |
| 32 | #elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_clzll) |
| 33 | #define core_clz __builtin_clzll |
| Dave Rodgman | 914347b | 2023-04-27 14:20:30 +0100 | [diff] [blame] | 34 | #endif |
| 35 | #endif |
| Agathiyan Bragadeesh | e55a1e1 | 2023-07-17 15:00:19 +0100 | [diff] [blame] | 36 | #if defined(core_clz) |
| 37 | return (size_t) core_clz(a); |
| Agathiyan Bragadeesh | 271a953 | 2023-07-12 11:15:17 +0100 | [diff] [blame] | 38 | #else |
| Dave Rodgman | 914347b | 2023-04-27 14:20:30 +0100 | [diff] [blame] | 39 | size_t j; |
| 40 | mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); |
| 41 | |
| 42 | for (j = 0; j < biL; j++) { |
| 43 | if (a & mask) { |
| 44 | break; |
| 45 | } |
| 46 | |
| 47 | mask >>= 1; |
| 48 | } |
| 49 | |
| 50 | return j; |
| Agathiyan Bragadeesh | 271a953 | 2023-07-12 11:15:17 +0100 | [diff] [blame] | 51 | #endif |
| Dave Rodgman | 914347b | 2023-04-27 14:20:30 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | 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] | 55 | { |
| Dave Rodgman | 880a6b3 | 2023-04-20 09:50:31 +0100 | [diff] [blame] | 56 | int i; |
| 57 | size_t j; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 58 | |
| Dave Rodgman | 2e863ec | 2023-04-25 17:34:59 +0100 | [diff] [blame] | 59 | for (i = ((int) A_limbs) - 1; i >= 0; i--) { |
| 60 | if (A[i] != 0) { |
| 61 | j = biL - mbedtls_mpi_core_clz(A[i]); |
| 62 | return (i * biL) + j; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | } |
| 64 | } |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 65 | |
| Dave Rodgman | 880a6b3 | 2023-04-20 09:50:31 +0100 | [diff] [blame] | 66 | return 0; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a) |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 70 | { |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | if (MBEDTLS_IS_BIG_ENDIAN) { |
| Dave Rodgman | 6d23ff6 | 2022-11-28 14:38:53 +0000 | [diff] [blame] | 72 | /* Nothing to do on bigendian systems. */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | return a; |
| 74 | } else { |
| Dave Rodgman | 7e1e7be | 2023-09-05 18:12:33 +0100 | [diff] [blame] | 75 | #if defined(MBEDTLS_HAVE_INT32) |
| Dave Rodgman | b7b8c09 | 2023-09-05 20:35:19 +0100 | [diff] [blame] | 76 | return (mbedtls_mpi_uint) MBEDTLS_BSWAP32(a); |
| Dave Rodgman | 7e1e7be | 2023-09-05 18:12:33 +0100 | [diff] [blame] | 77 | #elif defined(MBEDTLS_HAVE_INT64) |
| Dave Rodgman | b7b8c09 | 2023-09-05 20:35:19 +0100 | [diff] [blame] | 78 | return (mbedtls_mpi_uint) MBEDTLS_BSWAP64(a); |
| Dave Rodgman | 7e1e7be | 2023-09-05 18:12:33 +0100 | [diff] [blame] | 79 | #endif |
| Dave Rodgman | 6d23ff6 | 2022-11-28 14:38:53 +0000 | [diff] [blame] | 80 | } |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A, |
| 84 | size_t A_limbs) |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 85 | { |
| 86 | mbedtls_mpi_uint *cur_limb_left; |
| 87 | mbedtls_mpi_uint *cur_limb_right; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | if (A_limbs == 0) { |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 89 | return; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | } |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * Traverse limbs and |
| 94 | * - adapt byte-order in each limb |
| 95 | * - swap the limbs themselves. |
| 96 | * For that, simultaneously traverse the limbs from left to right |
| 97 | * and from right to left, as long as the left index is not bigger |
| 98 | * than the right index (it's not a problem if limbs is odd and the |
| 99 | * indices coincide in the last iteration). |
| 100 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1); |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 102 | cur_limb_left <= cur_limb_right; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | cur_limb_left++, cur_limb_right--) { |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 104 | mbedtls_mpi_uint tmp; |
| 105 | /* Note that if cur_limb_left == cur_limb_right, |
| 106 | * this code effectively swaps the bytes only once. */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | tmp = mpi_bigendian_to_host(*cur_limb_left); |
| 108 | *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right); |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 109 | *cur_limb_right = tmp; |
| 110 | } |
| 111 | } |
| 112 | |
| Gilles Peskine | 6f949ea | 2022-09-20 18:38:35 +0200 | [diff] [blame] | 113 | /* Whether min <= A, in constant time. |
| 114 | * A_limbs must be at least 1. */ |
| Dave Rodgman | fd7fab4 | 2023-05-17 14:00:39 +0100 | [diff] [blame] | 115 | mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min, |
| 116 | const mbedtls_mpi_uint *A, |
| 117 | size_t A_limbs) |
| Gilles Peskine | 6f949ea | 2022-09-20 18:38:35 +0200 | [diff] [blame] | 118 | { |
| 119 | /* min <= least significant limb? */ |
| Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 120 | mbedtls_ct_condition_t min_le_lsl = mbedtls_ct_uint_ge(A[0], min); |
| Gilles Peskine | 6f949ea | 2022-09-20 18:38:35 +0200 | [diff] [blame] | 121 | |
| Gilles Peskine | 6b7ce96 | 2022-12-15 15:04:33 +0100 | [diff] [blame] | 122 | /* limbs other than the least significant one are all zero? */ |
| Dave Rodgman | fd7fab4 | 2023-05-17 14:00:39 +0100 | [diff] [blame] | 123 | mbedtls_ct_condition_t msll_mask = MBEDTLS_CT_FALSE; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | for (size_t i = 1; i < A_limbs; i++) { |
| Dave Rodgman | fd7fab4 | 2023-05-17 14:00:39 +0100 | [diff] [blame] | 125 | msll_mask = mbedtls_ct_bool_or(msll_mask, mbedtls_ct_bool(A[i])); |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | } |
| Gilles Peskine | 6f949ea | 2022-09-20 18:38:35 +0200 | [diff] [blame] | 127 | |
| 128 | /* min <= A iff the lowest limb of A is >= min or the other limbs |
| 129 | * are not all zero. */ |
| Dave Rodgman | fd7fab4 | 2023-05-17 14:00:39 +0100 | [diff] [blame] | 130 | return mbedtls_ct_bool_or(msll_mask, min_le_lsl); |
| Gilles Peskine | 6f949ea | 2022-09-20 18:38:35 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| Dave Rodgman | 8ac9a1d | 2023-05-17 15:16:22 +0100 | [diff] [blame] | 133 | mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A, |
| 134 | const mbedtls_mpi_uint *B, |
| 135 | size_t limbs) |
| Dave Rodgman | 7d4f019 | 2023-05-09 14:01:05 +0100 | [diff] [blame] | 136 | { |
| Dave Rodgman | 8ac9a1d | 2023-05-17 15:16:22 +0100 | [diff] [blame] | 137 | mbedtls_ct_condition_t ret = MBEDTLS_CT_FALSE, cond = MBEDTLS_CT_FALSE, done = MBEDTLS_CT_FALSE; |
| Dave Rodgman | 7d4f019 | 2023-05-09 14:01:05 +0100 | [diff] [blame] | 138 | |
| 139 | for (size_t i = limbs; i > 0; i--) { |
| 140 | /* |
| 141 | * If B[i - 1] < A[i - 1] then A < B is false and the result must |
| 142 | * remain 0. |
| 143 | * |
| 144 | * Again even if we can make a decision, we just mark the result and |
| 145 | * the fact that we are done and continue looping. |
| 146 | */ |
| Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 147 | cond = mbedtls_ct_uint_lt(B[i - 1], A[i - 1]); |
| Dave Rodgman | 8ac9a1d | 2023-05-17 15:16:22 +0100 | [diff] [blame] | 148 | done = mbedtls_ct_bool_or(done, cond); |
| Dave Rodgman | 7d4f019 | 2023-05-09 14:01:05 +0100 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * If A[i - 1] < B[i - 1] then A < B is true. |
| 152 | * |
| 153 | * Again even if we can make a decision, we just mark the result and |
| 154 | * the fact that we are done and continue looping. |
| 155 | */ |
| Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 156 | cond = mbedtls_ct_uint_lt(A[i - 1], B[i - 1]); |
| Dave Rodgman | 8ac9a1d | 2023-05-17 15:16:22 +0100 | [diff] [blame] | 157 | ret = mbedtls_ct_bool_or(ret, mbedtls_ct_bool_and(cond, mbedtls_ct_bool_not(done))); |
| 158 | done = mbedtls_ct_bool_or(done, cond); |
| Dave Rodgman | 7d4f019 | 2023-05-09 14:01:05 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
| 162 | * If all the limbs were equal, then the numbers are equal, A < B is false |
| 163 | * and leaving the result 0 is correct. |
| 164 | */ |
| 165 | |
| 166 | return ret; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X, |
| 170 | const mbedtls_mpi_uint *A, |
| 171 | size_t limbs, |
| Dave Rodgman | cd2e38b | 2023-05-17 13:31:55 +0100 | [diff] [blame] | 172 | mbedtls_ct_condition_t assign) |
| Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 173 | { |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | if (X == A) { |
| Gabor Mezei | e9c013c | 2022-10-10 14:26:57 +0200 | [diff] [blame] | 175 | return; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | } |
| Gabor Mezei | e9c013c | 2022-10-10 14:26:57 +0200 | [diff] [blame] | 177 | |
| Dave Rodgman | 3b25c40 | 2023-05-18 14:41:06 +0100 | [diff] [blame] | 178 | /* This function is very performance-sensitive for RSA. For this reason |
| 179 | * we have the loop below, instead of calling mbedtls_ct_memcpy_if |
| 180 | * (this is more optimal since here we don't have to handle the case where |
| 181 | * we copy awkwardly sized data). |
| 182 | */ |
| 183 | for (size_t i = 0; i < limbs; i++) { |
| 184 | X[i] = mbedtls_ct_mpi_uint_if(assign, A[i], X[i]); |
| 185 | } |
| Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X, |
| 189 | mbedtls_mpi_uint *Y, |
| 190 | size_t limbs, |
| Dave Rodgman | cd2e38b | 2023-05-17 13:31:55 +0100 | [diff] [blame] | 191 | mbedtls_ct_condition_t swap) |
| Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 192 | { |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | if (X == Y) { |
| Gabor Mezei | e9c013c | 2022-10-10 14:26:57 +0200 | [diff] [blame] | 194 | return; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | } |
| Gabor Mezei | e9c013c | 2022-10-10 14:26:57 +0200 | [diff] [blame] | 196 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | for (size_t i = 0; i < limbs; i++) { |
| Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 198 | mbedtls_mpi_uint tmp = X[i]; |
| Dave Rodgman | cd2e38b | 2023-05-17 13:31:55 +0100 | [diff] [blame] | 199 | X[i] = mbedtls_ct_mpi_uint_if(swap, Y[i], X[i]); |
| 200 | Y[i] = mbedtls_ct_mpi_uint_if(swap, tmp, Y[i]); |
| Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 201 | } |
| Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 204 | int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X, |
| 205 | size_t X_limbs, |
| 206 | const unsigned char *input, |
| 207 | size_t input_length) |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 208 | { |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | const size_t limbs = CHARS_TO_LIMBS(input_length); |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 210 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | if (X_limbs < limbs) { |
| 212 | return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL; |
| 213 | } |
| Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 214 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | if (X != NULL) { |
| 216 | memset(X, 0, X_limbs * ciL); |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 217 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | for (size_t i = 0; i < input_length; i++) { |
| 219 | size_t offset = ((i % ciL) << 3); |
| 220 | X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset; |
| Janos Follath | ca5688e | 2022-08-19 12:05:28 +0100 | [diff] [blame] | 221 | } |
| Gabor Mezei | bf9da1d | 2022-08-12 14:11:56 +0200 | [diff] [blame] | 222 | } |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 223 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 224 | return 0; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X, |
| 228 | size_t X_limbs, |
| 229 | const unsigned char *input, |
| 230 | size_t input_length) |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 231 | { |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | const size_t limbs = CHARS_TO_LIMBS(input_length); |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 233 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 234 | if (X_limbs < limbs) { |
| 235 | return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL; |
| 236 | } |
| Janos Follath | 2ab2d3e | 2022-08-11 16:13:53 +0100 | [diff] [blame] | 237 | |
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 238 | /* If X_limbs is 0, input_length must also be 0 (from previous test). |
| 239 | * Nothing to do. */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | if (X_limbs == 0) { |
| 241 | return 0; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | memset(X, 0, X_limbs * ciL); |
| Gabor Mezei | c414ba3 | 2022-08-12 17:47:39 +0200 | [diff] [blame] | 245 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | /* memcpy() with (NULL, 0) is undefined behaviour */ |
| 247 | if (input_length != 0) { |
| 248 | size_t overhead = (X_limbs * ciL) - input_length; |
| 249 | unsigned char *Xp = (unsigned char *) X; |
| 250 | memcpy(Xp + overhead, input, input_length); |
| 251 | } |
| 252 | |
| 253 | mbedtls_mpi_core_bigendian_to_host(X, X_limbs); |
| 254 | |
| 255 | return 0; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A, |
| 259 | size_t A_limbs, |
| 260 | unsigned char *output, |
| 261 | size_t output_length) |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 262 | { |
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 263 | size_t stored_bytes = A_limbs * ciL; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 264 | size_t bytes_to_copy; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 265 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | if (stored_bytes < output_length) { |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 267 | bytes_to_copy = stored_bytes; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | } else { |
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 269 | bytes_to_copy = output_length; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 270 | |
| Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 271 | /* The output buffer is smaller than the allocated size of A. |
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 272 | * However A may fit if its leading bytes are zero. */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | for (size_t i = bytes_to_copy; i < stored_bytes; i++) { |
| 274 | if (GET_BYTE(A, i) != 0) { |
| 275 | return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL; |
| 276 | } |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | for (size_t i = 0; i < bytes_to_copy; i++) { |
| 281 | output[i] = GET_BYTE(A, i); |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | if (stored_bytes < output_length) { |
| 285 | /* Write trailing 0 bytes */ |
| 286 | memset(output + stored_bytes, 0, output_length - stored_bytes); |
| 287 | } |
| 288 | |
| 289 | return 0; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X, |
| 293 | size_t X_limbs, |
| 294 | unsigned char *output, |
| 295 | size_t output_length) |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 296 | { |
| 297 | size_t stored_bytes; |
| 298 | size_t bytes_to_copy; |
| 299 | unsigned char *p; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 300 | |
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 301 | stored_bytes = X_limbs * ciL; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 302 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | if (stored_bytes < output_length) { |
| Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 304 | /* There is enough space in the output buffer. Write initial |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 305 | * null bytes and record the position at which to start |
| 306 | * writing the significant bytes. In this case, the execution |
| 307 | * trace of this function does not depend on the value of the |
| 308 | * number. */ |
| 309 | bytes_to_copy = stored_bytes; |
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 310 | p = output + output_length - stored_bytes; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | memset(output, 0, output_length - stored_bytes); |
| 312 | } else { |
| Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 313 | /* The output buffer is smaller than the allocated size of X. |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 314 | * However X may fit if its leading bytes are zero. */ |
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 315 | bytes_to_copy = output_length; |
| 316 | p = output; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | for (size_t i = bytes_to_copy; i < stored_bytes; i++) { |
| 318 | if (GET_BYTE(X, i) != 0) { |
| 319 | return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL; |
| 320 | } |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | for (size_t i = 0; i < bytes_to_copy; i++) { |
| 325 | p[bytes_to_copy - i - 1] = GET_BYTE(X, i); |
| 326 | } |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 327 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | return 0; |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs, |
| 332 | size_t count) |
| Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 333 | { |
| 334 | size_t i, v0, v1; |
| 335 | mbedtls_mpi_uint r0 = 0, r1; |
| 336 | |
| 337 | v0 = count / biL; |
| 338 | v1 = count & (biL - 1); |
| 339 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | if (v0 > limbs || (v0 == limbs && v1 > 0)) { |
| 341 | memset(X, 0, limbs * ciL); |
| Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * shift by count / limb_size |
| 347 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 348 | if (v0 > 0) { |
| 349 | for (i = 0; i < limbs - v0; i++) { |
| Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 350 | X[i] = X[i + v0]; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | } |
| Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 352 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | for (; i < limbs; i++) { |
| Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 354 | X[i] = 0; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | } |
| Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* |
| 359 | * shift by count % limb_size |
| 360 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 361 | if (v1 > 0) { |
| 362 | for (i = limbs; i > 0; i--) { |
| Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 363 | r1 = X[i - 1] << (biL - v1); |
| 364 | X[i - 1] >>= v1; |
| 365 | X[i - 1] |= r0; |
| 366 | r0 = r1; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame] | 371 | void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs, |
| 372 | size_t count) |
| Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 373 | { |
| Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame] | 374 | size_t i, v0, v1; |
| Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 375 | mbedtls_mpi_uint r0 = 0, r1; |
| Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 376 | |
| Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame] | 377 | v0 = count / (biL); |
| 378 | v1 = count & (biL - 1); |
| Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 379 | |
| Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 380 | /* |
| 381 | * shift by count / limb_size |
| 382 | */ |
| Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame] | 383 | if (v0 > 0) { |
| 384 | for (i = limbs; i > v0; i--) { |
| 385 | X[i - 1] = X[i - v0 - 1]; |
| 386 | } |
| Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 387 | |
| Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame] | 388 | for (; i > 0; i--) { |
| 389 | X[i - 1] = 0; |
| 390 | } |
| Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* |
| 394 | * shift by count % limb_size |
| 395 | */ |
| Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame] | 396 | if (v1 > 0) { |
| 397 | for (i = v0; i < limbs; i++) { |
| 398 | r1 = X[i] >> (biL - v1); |
| 399 | X[i] <<= v1; |
| 400 | X[i] |= r0; |
| Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 401 | r0 = r1; |
| 402 | } |
| 403 | } |
| Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X, |
| 407 | const mbedtls_mpi_uint *A, |
| 408 | const mbedtls_mpi_uint *B, |
| 409 | size_t limbs) |
| Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 410 | { |
| Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 411 | mbedtls_mpi_uint c = 0; |
| Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 412 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | for (size_t i = 0; i < limbs; i++) { |
| Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 414 | mbedtls_mpi_uint t = c + A[i]; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | c = (t < A[i]); |
| Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 416 | t += B[i]; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | c += (t < B[i]); |
| Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 418 | X[i] = t; |
| Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 419 | } |
| Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 420 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | return c; |
| Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 422 | } |
| Gilles Peskine | c279b2f | 2022-09-21 15:38:38 +0200 | [diff] [blame] | 423 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X, |
| 425 | const mbedtls_mpi_uint *A, |
| 426 | size_t limbs, |
| 427 | unsigned cond) |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 428 | { |
| Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 429 | mbedtls_mpi_uint c = 0; |
| 430 | |
| Dave Rodgman | b59b73e | 2023-05-17 15:17:12 +0100 | [diff] [blame] | 431 | mbedtls_ct_condition_t do_add = mbedtls_ct_bool(cond); |
| Tom Cosgrove | 9354990 | 2022-08-30 17:41:23 +0100 | [diff] [blame] | 432 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | for (size_t i = 0; i < limbs; i++) { |
| Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 434 | mbedtls_mpi_uint add = mbedtls_ct_mpi_uint_if_else_0(do_add, A[i]); |
| Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 435 | mbedtls_mpi_uint t = c + X[i]; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 436 | c = (t < X[i]); |
| Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 437 | t += add; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | c += (t < add); |
| Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 439 | X[i] = t; |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 440 | } |
| Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 441 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | return c; |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X, |
| 446 | const mbedtls_mpi_uint *A, |
| 447 | const mbedtls_mpi_uint *B, |
| 448 | size_t limbs) |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 449 | { |
| 450 | mbedtls_mpi_uint c = 0; |
| 451 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | for (size_t i = 0; i < limbs; i++) { |
| 453 | mbedtls_mpi_uint z = (A[i] < c); |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 454 | mbedtls_mpi_uint t = A[i] - c; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | c = (t < B[i]) + z; |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 456 | X[i] = t - B[i]; |
| 457 | } |
| 458 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | return c; |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len, |
| 463 | const mbedtls_mpi_uint *s, size_t s_len, |
| 464 | mbedtls_mpi_uint b) |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 465 | { |
| 466 | mbedtls_mpi_uint c = 0; /* carry */ |
| Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 467 | /* |
| 468 | * It is a documented precondition of this function that d_len >= s_len. |
| 469 | * If that's not the case, we swap these round: this turns what would be |
| 470 | * a buffer overflow into an incorrect result. |
| 471 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | if (d_len < s_len) { |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 473 | s_len = d_len; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | } |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 475 | size_t excess_len = d_len - s_len; |
| 476 | size_t steps_x8 = s_len / 8; |
| 477 | size_t steps_x1 = s_len & 7; |
| 478 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | while (steps_x8--) { |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 480 | MULADDC_X8_INIT |
| 481 | MULADDC_X8_CORE |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | MULADDC_X8_STOP |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | while (steps_x1--) { |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 486 | MULADDC_X1_INIT |
| 487 | MULADDC_X1_CORE |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | MULADDC_X1_STOP |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | while (excess_len--) { |
| Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 492 | *d += c; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | c = (*d < c); |
| Tom Cosgrove | f0c8a8c | 2022-08-30 15:15:02 +0100 | [diff] [blame] | 494 | d++; |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | return c; |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 498 | } |
| 499 | |
| Tom Cosgrove | 6af26f3 | 2022-08-24 16:40:55 +0100 | [diff] [blame] | 500 | void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X, |
| 501 | const mbedtls_mpi_uint *A, size_t A_limbs, |
| 502 | const mbedtls_mpi_uint *B, size_t B_limbs) |
| Hanno Becker | 4ae890b | 2022-08-24 16:17:52 +0100 | [diff] [blame] | 503 | { |
| Tom Cosgrove | 6af26f3 | 2022-08-24 16:40:55 +0100 | [diff] [blame] | 504 | memset(X, 0, (A_limbs + B_limbs) * ciL); |
| 505 | |
| 506 | for (size_t i = 0; i < B_limbs; i++) { |
| 507 | (void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]); |
| 508 | } |
| Hanno Becker | 4ae890b | 2022-08-24 16:17:52 +0100 | [diff] [blame] | 509 | } |
| 510 | |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 511 | /* |
| 512 | * Fast Montgomery initialization (thanks to Tom St Denis). |
| 513 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | 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] | 515 | { |
| 516 | mbedtls_mpi_uint x = N[0]; |
| 517 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | x += ((N[0] + 2) & 4) << 1; |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 519 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 520 | for (unsigned int i = biL; i >= 8; i /= 2) { |
| 521 | x *= (2 - (N[0] * x)); |
| 522 | } |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 523 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | return ~x + 1; |
| Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 525 | } |
| 526 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X, |
| 528 | const mbedtls_mpi_uint *A, |
| 529 | const mbedtls_mpi_uint *B, |
| 530 | size_t B_limbs, |
| 531 | const mbedtls_mpi_uint *N, |
| 532 | size_t AN_limbs, |
| 533 | mbedtls_mpi_uint mm, |
| 534 | mbedtls_mpi_uint *T) |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 535 | { |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 536 | memset(T, 0, (2 * AN_limbs + 1) * ciL); |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 537 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | for (size_t i = 0; i < AN_limbs; i++) { |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 539 | /* T = (T + u0*B + u1*N) / 2^biL */ |
| Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 540 | mbedtls_mpi_uint u0 = A[i]; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm; |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 542 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0); |
| 544 | (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] | 545 | |
| 546 | T++; |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 547 | } |
| 548 | |
| Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 549 | /* |
| 550 | * The result we want is (T >= N) ? T - N : T. |
| 551 | * |
| 552 | * For better constant-time properties in this function, we always do the |
| 553 | * subtraction, with the result in X. |
| 554 | * |
| 555 | * We also look to see if there was any carry in the final additions in the |
| 556 | * loop above. |
| 557 | */ |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 558 | |
| Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 559 | mbedtls_mpi_uint carry = T[AN_limbs]; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | 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] | 561 | |
| 562 | /* |
| Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 563 | * 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] | 564 | * |
| Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 565 | * T can be in one of 3 ranges: |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 566 | * |
| Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 567 | * 1) T < N : (carry, borrow) = (0, 1): we want T |
| 568 | * 2) N <= T < R : (carry, borrow) = (0, 0): we want X |
| 569 | * 3) T >= R : (carry, borrow) = (1, 1): we want X |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 570 | * |
| Tom Cosgrove | f0b2231 | 2022-08-31 17:57:34 +0100 | [diff] [blame] | 571 | * and (carry, borrow) = (1, 0) can't happen. |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 572 | * |
| 573 | * 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] | 574 | * 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] | 575 | */ |
| Dave Rodgman | 231a516 | 2023-05-17 15:13:14 +0100 | [diff] [blame] | 576 | mbedtls_ct_memcpy_if(mbedtls_ct_bool(carry ^ borrow), |
| 577 | (unsigned char *) X, |
| 578 | (unsigned char *) T, |
| 579 | NULL, |
| 580 | AN_limbs * sizeof(mbedtls_mpi_uint)); |
| Tom Cosgrove | 958fd3d | 2022-08-24 11:08:51 +0100 | [diff] [blame] | 581 | } |
| 582 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 583 | int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X, |
| 584 | const mbedtls_mpi *N) |
| Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 585 | { |
| 586 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 587 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1)); |
| 589 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL)); |
| 590 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N)); |
| 591 | MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n)); |
| Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 592 | |
| 593 | cleanup: |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | return ret; |
| Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 595 | } |
| 596 | |
| Janos Follath | 59cbd1b | 2022-10-28 18:13:43 +0100 | [diff] [blame] | 597 | MBEDTLS_STATIC_TESTABLE |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest, |
| 599 | const mbedtls_mpi_uint *table, |
| 600 | size_t limbs, |
| 601 | size_t count, |
| 602 | size_t index) |
| Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 603 | { |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | for (size_t i = 0; i < count; i++, table += limbs) { |
| Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 605 | mbedtls_ct_condition_t assign = mbedtls_ct_uint_eq(i, index); |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | mbedtls_mpi_core_cond_assign(dest, table, limbs, assign); |
| Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 610 | /* Fill X with n_bytes random bytes. |
| 611 | * X must already have room for those bytes. |
| 612 | * The ordering of the bytes returned from the RNG is suitable for |
| Gilles Peskine | 22cdd0c | 2022-10-27 20:15:13 +0200 | [diff] [blame] | 613 | * deterministic ECDSA (see RFC 6979 §3.3 and the specification of |
| 614 | * mbedtls_mpi_core_random()). |
| Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 615 | */ |
| 616 | int mbedtls_mpi_core_fill_random( |
| 617 | mbedtls_mpi_uint *X, size_t X_limbs, |
| 618 | size_t n_bytes, |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 619 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
| Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 620 | { |
| 621 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 622 | const size_t limbs = CHARS_TO_LIMBS(n_bytes); |
| 623 | const size_t overhead = (limbs * ciL) - n_bytes; |
| Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 624 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 625 | if (X_limbs < limbs) { |
| 626 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 627 | } |
| Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 628 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | memset(X, 0, overhead); |
| 630 | memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL); |
| 631 | MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes)); |
| 632 | mbedtls_mpi_core_bigendian_to_host(X, limbs); |
| Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 633 | |
| 634 | cleanup: |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 635 | return ret; |
| Gilles Peskine | 009d195 | 2022-09-09 21:00:00 +0200 | [diff] [blame] | 636 | } |
| 637 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | int mbedtls_mpi_core_random(mbedtls_mpi_uint *X, |
| 639 | mbedtls_mpi_uint min, |
| 640 | const mbedtls_mpi_uint *N, |
| 641 | size_t limbs, |
| 642 | int (*f_rng)(void *, unsigned char *, size_t), |
| 643 | void *p_rng) |
| Gilles Peskine | 70375b2 | 2022-09-21 15:47:23 +0200 | [diff] [blame] | 644 | { |
| Dave Rodgman | fd492ab | 2023-05-17 15:17:29 +0100 | [diff] [blame] | 645 | mbedtls_ct_condition_t ge_lower = MBEDTLS_CT_TRUE, lt_upper = MBEDTLS_CT_FALSE; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 646 | size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs); |
| 647 | size_t n_bytes = (n_bits + 7) / 8; |
| Gilles Peskine | 70375b2 | 2022-09-21 15:47:23 +0200 | [diff] [blame] | 648 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 649 | |
| 650 | /* |
| 651 | * When min == 0, each try has at worst a probability 1/2 of failing |
| 652 | * (the msb has a probability 1/2 of being 0, and then the result will |
| 653 | * be < N), so after 30 tries failure probability is a most 2**(-30). |
| 654 | * |
| 655 | * When N is just below a power of 2, as is the case when generating |
| 656 | * a random scalar on most elliptic curves, 1 try is enough with |
| 657 | * overwhelming probability. When N is just above a power of 2, |
| 658 | * as when generating a random scalar on secp224k1, each try has |
| 659 | * a probability of failing that is almost 1/2. |
| 660 | * |
| 661 | * The probabilities are almost the same if min is nonzero but negligible |
| 662 | * compared to N. This is always the case when N is crypto-sized, but |
| 663 | * it's convenient to support small N for testing purposes. When N |
| 664 | * is small, use a higher repeat count, otherwise the probability of |
| 665 | * failure is macroscopic. |
| 666 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | int count = (n_bytes > 4 ? 30 : 250); |
| Gilles Peskine | 70375b2 | 2022-09-21 15:47:23 +0200 | [diff] [blame] | 668 | |
| 669 | /* |
| 670 | * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA) |
| 671 | * when f_rng is a suitably parametrized instance of HMAC_DRBG: |
| 672 | * - use the same byte ordering; |
| 673 | * - keep the leftmost n_bits bits of the generated octet string; |
| 674 | * - try until result is in the desired range. |
| 675 | * This also avoids any bias, which is especially important for ECDSA. |
| 676 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | do { |
| 678 | MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs, |
| 679 | n_bytes, |
| 680 | f_rng, p_rng)); |
| 681 | mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits); |
| Gilles Peskine | 70375b2 | 2022-09-21 15:47:23 +0200 | [diff] [blame] | 682 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | if (--count == 0) { |
| Gilles Peskine | 70375b2 | 2022-09-21 15:47:23 +0200 | [diff] [blame] | 684 | ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
| 685 | goto cleanup; |
| 686 | } |
| 687 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 688 | ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs); |
| 689 | lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs); |
| Dave Rodgman | fd492ab | 2023-05-17 15:17:29 +0100 | [diff] [blame] | 690 | } while (mbedtls_ct_bool_and(ge_lower, lt_upper) == MBEDTLS_CT_FALSE); |
| Gilles Peskine | 70375b2 | 2022-09-21 15:47:23 +0200 | [diff] [blame] | 691 | |
| 692 | cleanup: |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | return ret; |
| Gilles Peskine | 70375b2 | 2022-09-21 15:47:23 +0200 | [diff] [blame] | 694 | } |
| 695 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | static size_t exp_mod_get_window_size(size_t Ebits) |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 697 | { |
| Dave Rodgman | 4883f10 | 2023-08-09 20:17:40 +0100 | [diff] [blame] | 698 | #if MBEDTLS_MPI_WINDOW_SIZE >= 6 |
| 699 | return (Ebits > 671) ? 6 : (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1; |
| 700 | #elif MBEDTLS_MPI_WINDOW_SIZE == 5 |
| 701 | return (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1; |
| 702 | #elif MBEDTLS_MPI_WINDOW_SIZE > 1 |
| 703 | return (Ebits > 79) ? MBEDTLS_MPI_WINDOW_SIZE : 1; |
| 704 | #else |
| 705 | (void) Ebits; |
| 706 | return 1; |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 707 | #endif |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 708 | } |
| 709 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 710 | size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs) |
| Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 711 | { |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 712 | const size_t wsize = exp_mod_get_window_size(E_limbs * biL); |
| 713 | const size_t welem = ((size_t) 1) << wsize; |
| Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 714 | |
| 715 | /* How big does each part of the working memory pool need to be? */ |
| 716 | const size_t table_limbs = welem * AN_limbs; |
| 717 | const size_t select_limbs = AN_limbs; |
| 718 | const size_t temp_limbs = 2 * AN_limbs + 1; |
| 719 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 720 | return table_limbs + select_limbs + temp_limbs; |
| Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 723 | static void exp_mod_precompute_window(const mbedtls_mpi_uint *A, |
| 724 | const mbedtls_mpi_uint *N, |
| 725 | size_t AN_limbs, |
| 726 | mbedtls_mpi_uint mm, |
| 727 | const mbedtls_mpi_uint *RR, |
| 728 | size_t welem, |
| 729 | mbedtls_mpi_uint *Wtable, |
| 730 | mbedtls_mpi_uint *temp) |
| Gilles Peskine | 0de0a04 | 2022-11-16 20:12:49 +0100 | [diff] [blame] | 731 | { |
| Gilles Peskine | 0de0a04 | 2022-11-16 20:12:49 +0100 | [diff] [blame] | 732 | /* W[0] = 1 (in Montgomery presentation) */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 733 | memset(Wtable, 0, AN_limbs * ciL); |
| Gilles Peskine | 0de0a04 | 2022-11-16 20:12:49 +0100 | [diff] [blame] | 734 | Wtable[0] = 1; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 735 | 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] | 736 | |
| Tom Cosgrove | ecda186 | 2022-12-06 10:46:30 +0000 | [diff] [blame] | 737 | /* W[1] = A (already in Montgomery presentation) */ |
| Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 738 | mbedtls_mpi_uint *W1 = Wtable + AN_limbs; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 739 | memcpy(W1, A, AN_limbs * ciL); |
| Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 740 | |
| Gilles Peskine | 0de0a04 | 2022-11-16 20:12:49 +0100 | [diff] [blame] | 741 | /* W[i+1] = W[i] * W[1], i >= 2 */ |
| Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 742 | mbedtls_mpi_uint *Wprev = W1; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 743 | for (size_t i = 2; i < welem; i++) { |
| Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 744 | mbedtls_mpi_uint *Wcur = Wprev + AN_limbs; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 745 | mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp); |
| Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 746 | Wprev = Wcur; |
| 747 | } |
| Gilles Peskine | 0de0a04 | 2022-11-16 20:12:49 +0100 | [diff] [blame] | 748 | } |
| 749 | |
| Janos Follath | 8786dd7 | 2024-08-20 10:21:54 +0100 | [diff] [blame] | 750 | #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C) |
| Janos Follath | 96cfd7a | 2024-08-22 18:30:06 +0100 | [diff] [blame] | 751 | void (*mbedtls_safe_codepath_hook)(void) = NULL; |
| 752 | void (*mbedtls_unsafe_codepath_hook)(void) = NULL; |
| Janos Follath | a112691 | 2024-08-20 09:56:16 +0100 | [diff] [blame] | 753 | #endif |
| 754 | |
| Janos Follath | bb3f295 | 2024-08-12 19:05:47 +0100 | [diff] [blame] | 755 | /* |
| 756 | * This function calculates the indices of the exponent where the exponentiation algorithm should |
| 757 | * start processing. |
| 758 | * |
| 759 | * Warning! If the parameter E_public has MBEDTLS_MPI_IS_PUBLIC as its value, |
| 760 | * this function is not constant time with respect to the exponent (parameter E). |
| 761 | */ |
| 762 | static inline void exp_mod_calc_first_bit_optionally_safe(const mbedtls_mpi_uint *E, |
| 763 | size_t E_limbs, |
| 764 | int E_public, |
| 765 | size_t *E_limb_index, |
| 766 | size_t *E_bit_index) |
| 767 | { |
| 768 | if (E_public == MBEDTLS_MPI_IS_PUBLIC) { |
| Janos Follath | afb2079 | 2024-08-20 10:41:55 +0100 | [diff] [blame] | 769 | /* |
| 770 | * Skip leading zero bits. |
| 771 | */ |
| Janos Follath | bb3f295 | 2024-08-12 19:05:47 +0100 | [diff] [blame] | 772 | size_t E_bits = mbedtls_mpi_core_bitlen(E, E_limbs); |
| Janos Follath | afb2079 | 2024-08-20 10:41:55 +0100 | [diff] [blame] | 773 | if (E_bits == 0) { |
| 774 | /* |
| 775 | * If E is 0 mbedtls_mpi_core_bitlen() returns 0. Even if that is the case, we will want |
| 776 | * to represent it as a single 0 bit and as such the bitlength will be 1. |
| 777 | */ |
| 778 | E_bits = 1; |
| Janos Follath | bb3f295 | 2024-08-12 19:05:47 +0100 | [diff] [blame] | 779 | } |
| Janos Follath | afb2079 | 2024-08-20 10:41:55 +0100 | [diff] [blame] | 780 | |
| 781 | *E_limb_index = E_bits / biL; |
| 782 | *E_bit_index = E_bits % biL; |
| 783 | |
| Janos Follath | 8786dd7 | 2024-08-20 10:21:54 +0100 | [diff] [blame] | 784 | #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C) |
| Manuel Pégourié-Gonnard | 126cfed | 2024-09-02 10:42:46 +0200 | [diff] [blame] | 785 | if (mbedtls_unsafe_codepath_hook != NULL) { |
| Janos Follath | 96cfd7a | 2024-08-22 18:30:06 +0100 | [diff] [blame] | 786 | mbedtls_unsafe_codepath_hook(); |
| Manuel Pégourié-Gonnard | 126cfed | 2024-09-02 10:42:46 +0200 | [diff] [blame] | 787 | } |
| Janos Follath | e0842aa | 2024-08-13 08:40:31 +0100 | [diff] [blame] | 788 | #endif |
| Janos Follath | bb3f295 | 2024-08-12 19:05:47 +0100 | [diff] [blame] | 789 | } else { |
| 790 | /* |
| 791 | * Here we need to be constant time with respect to E and can't do anything better than |
| 792 | * start at the first allocated bit. |
| 793 | */ |
| 794 | *E_limb_index = E_limbs; |
| 795 | *E_bit_index = 0; |
| Janos Follath | 8786dd7 | 2024-08-20 10:21:54 +0100 | [diff] [blame] | 796 | #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C) |
| Manuel Pégourié-Gonnard | 126cfed | 2024-09-02 10:42:46 +0200 | [diff] [blame] | 797 | if (mbedtls_safe_codepath_hook != NULL) { |
| Janos Follath | 96cfd7a | 2024-08-22 18:30:06 +0100 | [diff] [blame] | 798 | mbedtls_safe_codepath_hook(); |
| Manuel Pégourié-Gonnard | 126cfed | 2024-09-02 10:42:46 +0200 | [diff] [blame] | 799 | } |
| Janos Follath | e0842aa | 2024-08-13 08:40:31 +0100 | [diff] [blame] | 800 | #endif |
| Janos Follath | bb3f295 | 2024-08-12 19:05:47 +0100 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * Warning! If the parameter window_public has MBEDTLS_MPI_IS_PUBLIC as its value, this function is |
| 806 | * not constant time with respect to the window parameter and consequently the exponent of the |
| 807 | * exponentiation (parameter E of mbedtls_mpi_core_exp_mod_optionally_safe). |
| 808 | */ |
| 809 | static inline void exp_mod_table_lookup_optionally_safe(mbedtls_mpi_uint *Wselect, |
| 810 | mbedtls_mpi_uint *Wtable, |
| 811 | size_t AN_limbs, size_t welem, |
| 812 | mbedtls_mpi_uint window, |
| 813 | int window_public) |
| 814 | { |
| 815 | if (window_public == MBEDTLS_MPI_IS_PUBLIC) { |
| 816 | memcpy(Wselect, Wtable + window * AN_limbs, AN_limbs * ciL); |
| Janos Follath | 8786dd7 | 2024-08-20 10:21:54 +0100 | [diff] [blame] | 817 | #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C) |
| Manuel Pégourié-Gonnard | 126cfed | 2024-09-02 10:42:46 +0200 | [diff] [blame] | 818 | if (mbedtls_unsafe_codepath_hook != NULL) { |
| Janos Follath | 96cfd7a | 2024-08-22 18:30:06 +0100 | [diff] [blame] | 819 | mbedtls_unsafe_codepath_hook(); |
| Manuel Pégourié-Gonnard | 126cfed | 2024-09-02 10:42:46 +0200 | [diff] [blame] | 820 | } |
| Janos Follath | e0842aa | 2024-08-13 08:40:31 +0100 | [diff] [blame] | 821 | #endif |
| Janos Follath | bb3f295 | 2024-08-12 19:05:47 +0100 | [diff] [blame] | 822 | } else { |
| 823 | /* Select Wtable[window] without leaking window through |
| 824 | * memory access patterns. */ |
| 825 | mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable, |
| 826 | AN_limbs, welem, window); |
| Janos Follath | 8786dd7 | 2024-08-20 10:21:54 +0100 | [diff] [blame] | 827 | #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C) |
| Manuel Pégourié-Gonnard | 126cfed | 2024-09-02 10:42:46 +0200 | [diff] [blame] | 828 | if (mbedtls_safe_codepath_hook != NULL) { |
| Janos Follath | 96cfd7a | 2024-08-22 18:30:06 +0100 | [diff] [blame] | 829 | mbedtls_safe_codepath_hook(); |
| Manuel Pégourié-Gonnard | 126cfed | 2024-09-02 10:42:46 +0200 | [diff] [blame] | 830 | } |
| Janos Follath | e0842aa | 2024-08-13 08:40:31 +0100 | [diff] [blame] | 831 | #endif |
| Janos Follath | bb3f295 | 2024-08-12 19:05:47 +0100 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | |
| Gilles Peskine | 7d89d35 | 2022-11-16 22:54:14 +0100 | [diff] [blame] | 835 | /* Exponentiation: X := A^E mod N. |
| 836 | * |
| Janos Follath | 38ff70e | 2024-08-12 18:20:59 +0100 | [diff] [blame] | 837 | * Warning! If the parameter E_public has MBEDTLS_MPI_IS_PUBLIC as its value, |
| 838 | * this function is not constant time with respect to the exponent (parameter E). |
| 839 | * |
| Tom Cosgrove | ecda186 | 2022-12-06 10:46:30 +0000 | [diff] [blame] | 840 | * A must already be in Montgomery form. |
| 841 | * |
| Gilles Peskine | 7d89d35 | 2022-11-16 22:54:14 +0100 | [diff] [blame] | 842 | * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero. |
| 843 | * |
| 844 | * RR must contain 2^{2*biL} mod N. |
| Janos Follath | 3321b58 | 2022-11-22 21:08:33 +0000 | [diff] [blame] | 845 | * |
| 846 | * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82 |
| 847 | * (The difference is that the body in our loop processes a single bit instead |
| 848 | * of a full window.) |
| Gilles Peskine | 7d89d35 | 2022-11-16 22:54:14 +0100 | [diff] [blame] | 849 | */ |
| Janos Follath | 38ff70e | 2024-08-12 18:20:59 +0100 | [diff] [blame] | 850 | static void mbedtls_mpi_core_exp_mod_optionally_safe(mbedtls_mpi_uint *X, |
| 851 | const mbedtls_mpi_uint *A, |
| 852 | const mbedtls_mpi_uint *N, |
| 853 | size_t AN_limbs, |
| 854 | const mbedtls_mpi_uint *E, |
| 855 | size_t E_limbs, |
| Janos Follath | a5fc8f3 | 2024-08-12 20:11:06 +0100 | [diff] [blame] | 856 | int E_public, |
| Janos Follath | 38ff70e | 2024-08-12 18:20:59 +0100 | [diff] [blame] | 857 | const mbedtls_mpi_uint *RR, |
| Janos Follath | a5fc8f3 | 2024-08-12 20:11:06 +0100 | [diff] [blame] | 858 | mbedtls_mpi_uint *T) |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 859 | { |
| Janos Follath | 020b9ab | 2024-08-13 07:53:20 +0100 | [diff] [blame] | 860 | /* We'll process the bits of E from most significant |
| 861 | * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant |
| 862 | * (limb_index=0, E_bit_index=0). */ |
| Janos Follath | e86607c | 2024-08-22 17:07:58 +0100 | [diff] [blame] | 863 | size_t E_limb_index = E_limbs; |
| 864 | size_t E_bit_index = 0; |
| Janos Follath | 020b9ab | 2024-08-13 07:53:20 +0100 | [diff] [blame] | 865 | exp_mod_calc_first_bit_optionally_safe(E, E_limbs, E_public, |
| 866 | &E_limb_index, &E_bit_index); |
| 867 | |
| 868 | const size_t wsize = exp_mod_get_window_size(E_limb_index * biL); |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 869 | const size_t welem = ((size_t) 1) << wsize; |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 870 | |
| Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 871 | /* This is how we will use the temporary storage T, which must have space |
| 872 | * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */ |
| 873 | const size_t table_limbs = welem * AN_limbs; |
| 874 | const size_t select_limbs = AN_limbs; |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 875 | |
| Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 876 | /* Pointers to specific parts of the temporary working memory pool */ |
| 877 | mbedtls_mpi_uint *const Wtable = T; |
| 878 | mbedtls_mpi_uint *const Wselect = Wtable + table_limbs; |
| 879 | mbedtls_mpi_uint *const temp = Wselect + select_limbs; |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 880 | |
| 881 | /* |
| 882 | * Window precomputation |
| 883 | */ |
| 884 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N); |
| Gilles Peskine | cf979b0 | 2022-11-16 20:04:00 +0100 | [diff] [blame] | 886 | |
| Janos Follath | 2c62441 | 2024-08-15 15:53:07 +0100 | [diff] [blame] | 887 | /* Set Wtable[i] = A^i (in Montgomery representation) */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 888 | exp_mod_precompute_window(A, N, AN_limbs, |
| 889 | mm, RR, |
| 890 | welem, Wtable, temp); |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 891 | |
| 892 | /* |
| Janos Follath | 0ec6e3f | 2022-11-14 12:52:08 +0000 | [diff] [blame] | 893 | * Fixed window exponentiation |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 894 | */ |
| 895 | |
| 896 | /* X = 1 (in Montgomery presentation) initially */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | memcpy(X, Wtable, AN_limbs * ciL); |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 898 | |
| Gilles Peskine | 0b270a5 | 2022-11-16 22:54:03 +0100 | [diff] [blame] | 899 | /* At any given time, window contains window_bits bits from E. |
| 900 | * window_bits can go up to wsize. */ |
| Janos Follath | bad42c4 | 2022-11-09 14:30:44 +0000 | [diff] [blame] | 901 | size_t window_bits = 0; |
| Gilles Peskine | 0b270a5 | 2022-11-16 22:54:03 +0100 | [diff] [blame] | 902 | mbedtls_mpi_uint window = 0; |
| Gilles Peskine | cf979b0 | 2022-11-16 20:04:00 +0100 | [diff] [blame] | 903 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 904 | do { |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 905 | /* Square */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 906 | 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] | 907 | |
| Janos Follath | 3321b58 | 2022-11-22 21:08:33 +0000 | [diff] [blame] | 908 | /* Move to the next bit of the exponent */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 909 | if (E_bit_index == 0) { |
| Gilles Peskine | c718a3c | 2022-11-16 20:42:09 +0100 | [diff] [blame] | 910 | --E_limb_index; |
| 911 | E_bit_index = biL - 1; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 912 | } else { |
| Gilles Peskine | c718a3c | 2022-11-16 20:42:09 +0100 | [diff] [blame] | 913 | --E_bit_index; |
| 914 | } |
| Janos Follath | 3321b58 | 2022-11-22 21:08:33 +0000 | [diff] [blame] | 915 | /* Insert next exponent bit into window */ |
| Gilles Peskine | d83b5cb | 2022-11-16 20:26:14 +0100 | [diff] [blame] | 916 | ++window_bits; |
| Gilles Peskine | c718a3c | 2022-11-16 20:42:09 +0100 | [diff] [blame] | 917 | window <<= 1; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 918 | window |= (E[E_limb_index] >> E_bit_index) & 1; |
| Gilles Peskine | 3b63d09 | 2022-11-16 22:06:18 +0100 | [diff] [blame] | 919 | |
| 920 | /* Clear window if it's full. Also clear the window at the end, |
| 921 | * when we've finished processing the exponent. */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 922 | if (window_bits == wsize || |
| 923 | (E_bit_index == 0 && E_limb_index == 0)) { |
| Janos Follath | bb3f295 | 2024-08-12 19:05:47 +0100 | [diff] [blame] | 924 | |
| 925 | exp_mod_table_lookup_optionally_safe(Wselect, Wtable, AN_limbs, welem, |
| 926 | window, E_public); |
| Gilles Peskine | 0b270a5 | 2022-11-16 22:54:03 +0100 | [diff] [blame] | 927 | /* Multiply X by the selected element. */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 928 | mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm, |
| 929 | temp); |
| Gilles Peskine | 3b63d09 | 2022-11-16 22:06:18 +0100 | [diff] [blame] | 930 | window = 0; |
| 931 | window_bits = 0; |
| 932 | } |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 933 | } while (!(E_bit_index == 0 && E_limb_index == 0)); |
| Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 934 | } |
| 935 | |
| Manuel Pégourié-Gonnard | 75ed587 | 2024-06-18 12:52:45 +0200 | [diff] [blame] | 936 | void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X, |
| 937 | const mbedtls_mpi_uint *A, |
| 938 | const mbedtls_mpi_uint *N, size_t AN_limbs, |
| 939 | const mbedtls_mpi_uint *E, size_t E_limbs, |
| 940 | const mbedtls_mpi_uint *RR, |
| 941 | mbedtls_mpi_uint *T) |
| 942 | { |
| 943 | mbedtls_mpi_core_exp_mod_optionally_safe(X, |
| 944 | A, |
| 945 | N, |
| 946 | AN_limbs, |
| 947 | E, |
| 948 | E_limbs, |
| Janos Follath | a5fc8f3 | 2024-08-12 20:11:06 +0100 | [diff] [blame] | 949 | MBEDTLS_MPI_IS_SECRET, |
| Manuel Pégourié-Gonnard | 75ed587 | 2024-06-18 12:52:45 +0200 | [diff] [blame] | 950 | RR, |
| Janos Follath | a5fc8f3 | 2024-08-12 20:11:06 +0100 | [diff] [blame] | 951 | T); |
| Manuel Pégourié-Gonnard | 75ed587 | 2024-06-18 12:52:45 +0200 | [diff] [blame] | 952 | } |
| 953 | |
| Janos Follath | 38ff70e | 2024-08-12 18:20:59 +0100 | [diff] [blame] | 954 | void mbedtls_mpi_core_exp_mod_unsafe(mbedtls_mpi_uint *X, |
| 955 | const mbedtls_mpi_uint *A, |
| 956 | const mbedtls_mpi_uint *N, size_t AN_limbs, |
| 957 | const mbedtls_mpi_uint *E, size_t E_limbs, |
| 958 | const mbedtls_mpi_uint *RR, |
| 959 | mbedtls_mpi_uint *T) |
| 960 | { |
| 961 | mbedtls_mpi_core_exp_mod_optionally_safe(X, |
| 962 | A, |
| 963 | N, |
| 964 | AN_limbs, |
| 965 | E, |
| 966 | E_limbs, |
| Janos Follath | a5fc8f3 | 2024-08-12 20:11:06 +0100 | [diff] [blame] | 967 | MBEDTLS_MPI_IS_PUBLIC, |
| Janos Follath | 38ff70e | 2024-08-12 18:20:59 +0100 | [diff] [blame] | 968 | RR, |
| Janos Follath | a5fc8f3 | 2024-08-12 20:11:06 +0100 | [diff] [blame] | 969 | T); |
| Janos Follath | 38ff70e | 2024-08-12 18:20:59 +0100 | [diff] [blame] | 970 | } |
| 971 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 972 | mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X, |
| 973 | const mbedtls_mpi_uint *A, |
| 974 | mbedtls_mpi_uint c, /* doubles as carry */ |
| 975 | size_t limbs) |
| Hanno Becker | d9b2348 | 2022-08-25 08:25:19 +0100 | [diff] [blame] | 976 | { |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | for (size_t i = 0; i < limbs; i++) { |
| Tom Cosgrove | f7ff4c9 | 2022-08-25 08:39:07 +0100 | [diff] [blame] | 978 | mbedtls_mpi_uint s = A[i]; |
| 979 | mbedtls_mpi_uint t = s - c; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | c = (t > s); |
| Tom Cosgrove | f7ff4c9 | 2022-08-25 08:39:07 +0100 | [diff] [blame] | 981 | X[i] = t; |
| Hanno Becker | d9b2348 | 2022-08-25 08:25:19 +0100 | [diff] [blame] | 982 | } |
| 983 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | return c; |
| Hanno Becker | d9b2348 | 2022-08-25 08:25:19 +0100 | [diff] [blame] | 985 | } |
| 986 | |
| Janos Follath | adb9d2d | 2024-03-11 10:03:05 +0000 | [diff] [blame] | 987 | mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A, |
| Janos Follath | aec1a86 | 2024-02-21 11:24:20 +0000 | [diff] [blame] | 988 | size_t limbs) |
| Tom Cosgrove | 30f3b4d | 2022-12-12 16:54:57 +0000 | [diff] [blame] | 989 | { |
| Janos Follath | 23dc8b5 | 2024-03-11 10:39:57 +0000 | [diff] [blame] | 990 | volatile const mbedtls_mpi_uint *force_read_A = A; |
| Janos Follath | d6df0a5 | 2024-03-11 09:40:03 +0000 | [diff] [blame] | 991 | mbedtls_mpi_uint bits = 0; |
| Tom Cosgrove | 30f3b4d | 2022-12-12 16:54:57 +0000 | [diff] [blame] | 992 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 993 | for (size_t i = 0; i < limbs; i++) { |
| Janos Follath | adb9d2d | 2024-03-11 10:03:05 +0000 | [diff] [blame] | 994 | bits |= force_read_A[i]; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 995 | } |
| Tom Cosgrove | 30f3b4d | 2022-12-12 16:54:57 +0000 | [diff] [blame] | 996 | |
| Janos Follath | aec1a86 | 2024-02-21 11:24:20 +0000 | [diff] [blame] | 997 | return mbedtls_ct_bool(bits); |
| Tom Cosgrove | 30f3b4d | 2022-12-12 16:54:57 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1000 | void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X, |
| 1001 | const mbedtls_mpi_uint *A, |
| 1002 | const mbedtls_mpi_uint *N, |
| 1003 | size_t AN_limbs, |
| 1004 | mbedtls_mpi_uint mm, |
| 1005 | const mbedtls_mpi_uint *rr, |
| 1006 | mbedtls_mpi_uint *T) |
| Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 1007 | { |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1008 | mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T); |
| Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1011 | void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X, |
| 1012 | const mbedtls_mpi_uint *A, |
| 1013 | const mbedtls_mpi_uint *N, |
| 1014 | size_t AN_limbs, |
| 1015 | mbedtls_mpi_uint mm, |
| 1016 | mbedtls_mpi_uint *T) |
| Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 1017 | { |
| 1018 | const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */ |
| 1019 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1020 | mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T); |
| Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| Manuel Pégourié-Gonnard | 07a0577 | 2025-07-09 10:42:28 +0200 | [diff] [blame] | 1023 | /* |
| 1024 | * Compute X = A - B mod N. |
| 1025 | * Both A and B must be in [0, N) and so will the output. |
| 1026 | */ |
| 1027 | static void mpi_core_sub_mod(mbedtls_mpi_uint *X, |
| 1028 | const mbedtls_mpi_uint *A, |
| 1029 | const mbedtls_mpi_uint *B, |
| 1030 | const mbedtls_mpi_uint *N, |
| 1031 | size_t limbs) |
| 1032 | { |
| 1033 | mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, A, B, limbs); |
| 1034 | (void) mbedtls_mpi_core_add_if(X, N, limbs, (unsigned) c); |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Divide X by 2 mod N in place, assuming N is odd. |
| 1039 | * The input must be in [0, N) and so will the output. |
| 1040 | */ |
| Manuel Pégourié-Gonnard | 7fba466 | 2025-07-17 09:17:12 +0200 | [diff] [blame] | 1041 | MBEDTLS_STATIC_TESTABLE |
| 1042 | void mbedtls_mpi_core_div2_mod_odd(mbedtls_mpi_uint *X, |
| 1043 | const mbedtls_mpi_uint *N, |
| 1044 | size_t limbs) |
| Manuel Pégourié-Gonnard | 07a0577 | 2025-07-09 10:42:28 +0200 | [diff] [blame] | 1045 | { |
| 1046 | /* If X is odd, add N to make it even before shifting. */ |
| 1047 | unsigned odd = (unsigned) X[0] & 1; |
| 1048 | mbedtls_mpi_uint c = mbedtls_mpi_core_add_if(X, N, limbs, odd); |
| 1049 | mbedtls_mpi_core_shift_r(X, limbs, 1); |
| 1050 | X[limbs - 1] |= c << (biL - 1); |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * Constant-time GCD and modular inversion - odd modulus. |
| 1055 | * |
| 1056 | * Pre-conditions: see public documentation. |
| 1057 | * |
| 1058 | * See https://www.jstage.jst.go.jp/article/transinf/E106.D/9/E106.D_2022ICP0009/_pdf |
| Manuel Pégourié-Gonnard | ec35382 | 2025-07-24 12:22:16 +0200 | [diff] [blame] | 1059 | * |
| 1060 | * The paper gives two computationally equivalent algorithms: Alg 7 (readable) |
| 1061 | * and Alg 8 (constant-time). We use a third version that's hopefully both: |
| Manuel Pégourié-Gonnard | 9361550 | 2025-07-23 13:21:07 +0200 | [diff] [blame] | 1062 | * |
| 1063 | * u, v = A, N # N is called p in the paper but doesn't have to be prime |
| 1064 | * q, r = 0, 1 |
| 1065 | * repeat bits(A_limbs + N_limbs) times: |
| Manuel Pégourié-Gonnard | ec35382 | 2025-07-24 12:22:16 +0200 | [diff] [blame] | 1066 | * d = v - u # t1 in Alg 7 |
| 1067 | * t1 = (u and v both odd) ? u : d # t1 in Alg 8 |
| 1068 | * t2 = (u and v both odd) ? d : (u odd) ? v : u # t2 in Alg 8 |
| Manuel Pégourié-Gonnard | 9361550 | 2025-07-23 13:21:07 +0200 | [diff] [blame] | 1069 | * t2 >>= 1 |
| Manuel Pégourié-Gonnard | ec35382 | 2025-07-24 12:22:16 +0200 | [diff] [blame] | 1070 | * swap = t1 > t2 # similar to s, z in Alg 8 |
| 1071 | * u, v = (swap) ? t2, t1 : t1, t2 |
| Manuel Pégourié-Gonnard | 9361550 | 2025-07-23 13:21:07 +0200 | [diff] [blame] | 1072 | * |
| Manuel Pégourié-Gonnard | ec35382 | 2025-07-24 12:22:16 +0200 | [diff] [blame] | 1073 | * d = r - q mod N # t2 in Alg 7 |
| 1074 | * t1 = (u and v both odd) ? q : d # t3 in Alg 8 |
| 1075 | * t2 = (u and v both odd) ? d : (u odd) ? r : q # t4 Alg 8 |
| 1076 | * t2 /= 2 mod N # see below (pre_com) |
| 1077 | * q, r = (swap) ? t2, t1 : t1, t2 |
| 1078 | * return v, q # v: GCD, see Alg 6; q: no mult by pre_com, see below |
| Manuel Pégourié-Gonnard | 9361550 | 2025-07-23 13:21:07 +0200 | [diff] [blame] | 1079 | * |
| 1080 | * The ternary operators in the above pseudo-code need to be realised in a |
| Manuel Pégourié-Gonnard | ec35382 | 2025-07-24 12:22:16 +0200 | [diff] [blame] | 1081 | * constant-time fashion. We use conditional assign for t1, t2 and conditional |
| 1082 | * swap for the final update. (Note: the similarity between branches of Alg 7 |
| 1083 | * are highlighted in tables 2 and 3 and the surrounding text.) |
| Manuel Pégourié-Gonnard | 9361550 | 2025-07-23 13:21:07 +0200 | [diff] [blame] | 1084 | * |
| Manuel Pégourié-Gonnard | ec35382 | 2025-07-24 12:22:16 +0200 | [diff] [blame] | 1085 | * Also, we re-order operations, grouping things related to the inverse, which |
| 1086 | * facilitates making its computation optional, and requires fewer temporaries. |
| Manuel Pégourié-Gonnard | 9361550 | 2025-07-23 13:21:07 +0200 | [diff] [blame] | 1087 | * |
| Manuel Pégourié-Gonnard | ec35382 | 2025-07-24 12:22:16 +0200 | [diff] [blame] | 1088 | * The only actual change from the paper is dropping the trick with pre_com, |
| Manuel Pégourié-Gonnard | 9361550 | 2025-07-23 13:21:07 +0200 | [diff] [blame] | 1089 | * which I think complicates things for no benefit. |
| 1090 | * See the comment on the big I != NULL block below for details. |
| Manuel Pégourié-Gonnard | 07a0577 | 2025-07-09 10:42:28 +0200 | [diff] [blame] | 1091 | */ |
| 1092 | void mbedtls_mpi_core_gcd_modinv_odd(mbedtls_mpi_uint *G, |
| 1093 | mbedtls_mpi_uint *I, |
| 1094 | const mbedtls_mpi_uint *A, |
| 1095 | size_t A_limbs, |
| 1096 | const mbedtls_mpi_uint *N, |
| 1097 | size_t N_limbs, |
| 1098 | mbedtls_mpi_uint *T) |
| 1099 | { |
| Manuel Pégourié-Gonnard | 07a0577 | 2025-07-09 10:42:28 +0200 | [diff] [blame] | 1100 | /* GCD and modinv, names common to Alg 7 and Alg 8 */ |
| 1101 | mbedtls_mpi_uint *u = T + 0 * N_limbs; |
| 1102 | mbedtls_mpi_uint *v = G; |
| 1103 | |
| 1104 | /* GCD and modinv, my name (t1, t2 from Alg 7) */ |
| 1105 | mbedtls_mpi_uint *d = T + 1 * N_limbs; |
| 1106 | |
| 1107 | /* GCD and modinv, names from Alg 8 (note: t1, t2 from Alg 7 are d above) */ |
| 1108 | mbedtls_mpi_uint *t1 = T + 2 * N_limbs; |
| 1109 | mbedtls_mpi_uint *t2 = T + 3 * N_limbs; |
| 1110 | |
| 1111 | /* modinv only, names common to Alg 7 and Alg 8 */ |
| 1112 | mbedtls_mpi_uint *q = I; |
| 1113 | mbedtls_mpi_uint *r = I != NULL ? T + 4 * N_limbs : NULL; |
| 1114 | |
| 1115 | /* |
| 1116 | * Initial values: |
| 1117 | * u, v = A, N |
| 1118 | * q, r = 0, 1 |
| Manuel Pégourié-Gonnard | de5eeb5 | 2025-07-15 12:12:36 +0200 | [diff] [blame] | 1119 | * |
| 1120 | * We only write to G (aka v) after reading from inputs (A and N), which |
| 1121 | * allows aliasing, except with N when I != NULL, as then we'll be operating |
| 1122 | * mod N on q and r later - see the public documentation. |
| Manuel Pégourié-Gonnard | 07a0577 | 2025-07-09 10:42:28 +0200 | [diff] [blame] | 1123 | */ |
| Manuel Pégourié-Gonnard | efd242a | 2025-07-24 11:10:59 +0200 | [diff] [blame] | 1124 | if (A_limbs > N_limbs) { |
| 1125 | /* Violating this precondition should not result in memory errors. */ |
| 1126 | A_limbs = N_limbs; |
| 1127 | } |
| Manuel Pégourié-Gonnard | 07a0577 | 2025-07-09 10:42:28 +0200 | [diff] [blame] | 1128 | memcpy(u, A, A_limbs * ciL); |
| 1129 | memset((char *) u + A_limbs * ciL, 0, (N_limbs - A_limbs) * ciL); |
| 1130 | |
| Manuel Pégourié-Gonnard | efd242a | 2025-07-24 11:10:59 +0200 | [diff] [blame] | 1131 | /* Avoid possible UB with memcpy when src == dst. */ |
| Manuel Pégourié-Gonnard | de5eeb5 | 2025-07-15 12:12:36 +0200 | [diff] [blame] | 1132 | if (v != N) { |
| 1133 | memcpy(v, N, N_limbs * ciL); |
| 1134 | } |
| Manuel Pégourié-Gonnard | 07a0577 | 2025-07-09 10:42:28 +0200 | [diff] [blame] | 1135 | |
| 1136 | if (I != NULL) { |
| 1137 | memset(q, 0, N_limbs * ciL); |
| 1138 | |
| 1139 | memset(r, 0, N_limbs * ciL); |
| 1140 | r[0] = 1; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * At each step, out of u, v, v - u we keep one, shift another, and discard |
| 1145 | * the third, then update (u, v) with the ordered result. |
| 1146 | * Then we mirror those actions with q, r, r - q mod N. |
| 1147 | * |
| 1148 | * Loop invariants: |
| 1149 | * u <= v (on entry: A <= N) |
| 1150 | * GCD(u, v) == GCD(A, N) (on entry: trivial) |
| 1151 | * v = A * q mod N (on entry: N = A * 0 mod N) |
| 1152 | * u = A * r mod N (on entry: A = A * 1 mod N) |
| 1153 | * q, r in [0, N) (on entry: 0, 1) |
| 1154 | * |
| 1155 | * On exit: |
| 1156 | * u = 0 |
| 1157 | * v = GCD(A, N) = A * q mod N |
| 1158 | * if v == 1 then 1 = A * q mod N ie q is A's inverse mod N |
| 1159 | * r = 0 |
| 1160 | * |
| 1161 | * The exit state is a fixed point of the loop's body. |
| 1162 | * Alg 7 and Alg 8 use 2 * bitlen(N) iterations but Theorem 2 (above in the |
| 1163 | * paper) says bitlen(A) + bitlen(N) is actually enough. |
| 1164 | */ |
| 1165 | for (size_t i = 0; i < (A_limbs + N_limbs) * biL; i++) { |
| 1166 | /* s, z in Alg 8 - use meaningful names instead */ |
| 1167 | mbedtls_ct_condition_t u_odd = mbedtls_ct_bool(u[0] & 1); |
| 1168 | mbedtls_ct_condition_t v_odd = mbedtls_ct_bool(v[0] & 1); |
| 1169 | |
| 1170 | /* Other conditions that will be useful below */ |
| 1171 | mbedtls_ct_condition_t u_odd_v_odd = mbedtls_ct_bool_and(u_odd, v_odd); |
| 1172 | mbedtls_ct_condition_t v_even = mbedtls_ct_bool_not(v_odd); |
| 1173 | mbedtls_ct_condition_t u_odd_v_even = mbedtls_ct_bool_and(u_odd, v_even); |
| 1174 | |
| 1175 | /* This is called t1 in Alg 7 (no name in Alg 8). |
| 1176 | * We know that u <= v so there is no carry */ |
| 1177 | (void) mbedtls_mpi_core_sub(d, v, u, N_limbs); |
| 1178 | |
| 1179 | /* t1 (the thing that's kept) can be d (default) or u (if t2 is d) */ |
| 1180 | memcpy(t1, d, N_limbs * ciL); |
| 1181 | mbedtls_mpi_core_cond_assign(t1, u, N_limbs, u_odd_v_odd); |
| 1182 | |
| 1183 | /* t2 (the thing that's shifted) can be u (if even), or v (if even), |
| 1184 | * or d (which is even if both u and v were odd) */ |
| 1185 | memcpy(t2, u, N_limbs * ciL); |
| 1186 | mbedtls_mpi_core_cond_assign(t2, v, N_limbs, u_odd_v_even); |
| 1187 | mbedtls_mpi_core_cond_assign(t2, d, N_limbs, u_odd_v_odd); |
| 1188 | |
| 1189 | mbedtls_mpi_core_shift_r(t2, N_limbs, 1); // t2 is even |
| 1190 | |
| 1191 | /* Update u, v and re-order them if needed */ |
| 1192 | memcpy(u, t1, N_limbs * ciL); |
| 1193 | memcpy(v, t2, N_limbs * ciL); |
| 1194 | mbedtls_ct_condition_t swap = mbedtls_mpi_core_lt_ct(v, u, N_limbs); |
| 1195 | mbedtls_mpi_core_cond_swap(u, v, N_limbs, swap); |
| 1196 | |
| 1197 | /* Now, if modinv was requested, do the same with q, r, but: |
| 1198 | * - decisions still based on u and v (their initial values); |
| 1199 | * - operations are now mod N; |
| 1200 | * - we re-use t1, t2 for what the paper calls t3, t4 in Alg 8. |
| 1201 | * |
| 1202 | * Here we slightly diverge from the paper and instead do the obvious |
| 1203 | * thing that preserves the invariants involving q and r: mirror |
| 1204 | * operations on u and v, ie also divide by 2 here (mod N). |
| 1205 | * |
| 1206 | * The paper uses a trick where it replaces division by 2 with |
| Manuel Pégourié-Gonnard | ec35382 | 2025-07-24 12:22:16 +0200 | [diff] [blame] | 1207 | * multiplication by 2 here, and compensates in the end by multiplying |
| 1208 | * by pre_com, which is probably intended as an optimisation. |
| Manuel Pégourié-Gonnard | 07a0577 | 2025-07-09 10:42:28 +0200 | [diff] [blame] | 1209 | * |
| 1210 | * However I believe it's not actually an optimisation, since |
| 1211 | * constant-time modular multiplication by 2 (left-shift + conditional |
| 1212 | * subtract) is just as costly as constant-time modular division by 2 |
| 1213 | * (conditional add + right-shift). So, skip it and keep things simple. |
| 1214 | */ |
| 1215 | if (I != NULL) { |
| 1216 | /* This is called t2 in Alg 7 (no name in Alg 8). */ |
| 1217 | mpi_core_sub_mod(d, q, r, N, N_limbs); |
| 1218 | |
| 1219 | /* t3 (the thing that's kept) */ |
| 1220 | memcpy(t1, d, N_limbs * ciL); |
| 1221 | mbedtls_mpi_core_cond_assign(t1, r, N_limbs, u_odd_v_odd); |
| 1222 | |
| 1223 | /* t4 (the thing that's shifted) */ |
| 1224 | memcpy(t2, r, N_limbs * ciL); |
| 1225 | mbedtls_mpi_core_cond_assign(t2, q, N_limbs, u_odd_v_even); |
| 1226 | mbedtls_mpi_core_cond_assign(t2, d, N_limbs, u_odd_v_odd); |
| 1227 | |
| Manuel Pégourié-Gonnard | 7fba466 | 2025-07-17 09:17:12 +0200 | [diff] [blame] | 1228 | mbedtls_mpi_core_div2_mod_odd(t2, N, N_limbs); |
| Manuel Pégourié-Gonnard | 07a0577 | 2025-07-09 10:42:28 +0200 | [diff] [blame] | 1229 | |
| 1230 | /* Update and possibly swap */ |
| 1231 | memcpy(r, t1, N_limbs * ciL); |
| 1232 | memcpy(q, t2, N_limbs * ciL); |
| 1233 | mbedtls_mpi_core_cond_swap(r, q, N_limbs, swap); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | /* G and I already hold the correct values by virtue of being aliased */ |
| 1238 | } |
| 1239 | |
| Janos Follath | 3ca0775 | 2022-08-09 11:45:47 +0100 | [diff] [blame] | 1240 | #endif /* MBEDTLS_BIGNUM_C */ |