Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 1 | /* |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 2 | * Low-level modular bignum functions |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +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 | |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 30 | |
| 31 | #include "bignum_core.h" |
| 32 | #include "bignum_mod_raw.h" |
| 33 | #include "bignum_mod.h" |
| 34 | #include "constant_time_internal.h" |
| 35 | |
Gabor Mezei | 9a66ab1 | 2023-01-25 13:23:38 +0100 | [diff] [blame] | 36 | #include "bignum_mod_raw_invasive.h" |
| 37 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X, |
| 39 | const mbedtls_mpi_uint *A, |
| 40 | const mbedtls_mpi_mod_modulus *N, |
| 41 | unsigned char assign) |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 42 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | mbedtls_mpi_core_cond_assign(X, A, N->limbs, assign); |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X, |
| 47 | mbedtls_mpi_uint *Y, |
| 48 | const mbedtls_mpi_mod_modulus *N, |
| 49 | unsigned char swap) |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 50 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | mbedtls_mpi_core_cond_swap(X, Y, N->limbs, swap); |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 54 | int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X, |
| 55 | const mbedtls_mpi_mod_modulus *N, |
| 56 | const unsigned char *input, |
| 57 | size_t input_length, |
| 58 | mbedtls_mpi_mod_ext_rep ext_rep) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 59 | { |
| 60 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 61 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | switch (ext_rep) { |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 63 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 64 | ret = mbedtls_mpi_core_read_le(X, N->limbs, |
| 65 | input, input_length); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 66 | break; |
| 67 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 68 | ret = mbedtls_mpi_core_read_be(X, N->limbs, |
| 69 | input, input_length); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 70 | break; |
| 71 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 73 | } |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 74 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | if (ret != 0) { |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 76 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | } |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 78 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 79 | if (!mbedtls_mpi_core_lt_ct(X, N->p, N->limbs)) { |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 80 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 81 | goto cleanup; |
| 82 | } |
| 83 | |
| 84 | cleanup: |
| 85 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | return ret; |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 89 | int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A, |
| 90 | const mbedtls_mpi_mod_modulus *N, |
| 91 | unsigned char *output, |
| 92 | size_t output_length, |
| 93 | mbedtls_mpi_mod_ext_rep ext_rep) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 94 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | switch (ext_rep) { |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 96 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 97 | return mbedtls_mpi_core_write_le(A, N->limbs, |
| 98 | output, output_length); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 99 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 100 | return mbedtls_mpi_core_write_be(A, N->limbs, |
| 101 | output, output_length); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 102 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 104 | } |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X, |
| 108 | const mbedtls_mpi_uint *A, |
| 109 | const mbedtls_mpi_uint *B, |
| 110 | const mbedtls_mpi_mod_modulus *N) |
Gabor Mezei | 4c7cf7d | 2022-11-09 14:07:43 +0100 | [diff] [blame] | 111 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, A, B, N->limbs); |
Gabor Mezei | 4c7cf7d | 2022-11-09 14:07:43 +0100 | [diff] [blame] | 113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c); |
Gabor Mezei | 4c7cf7d | 2022-11-09 14:07:43 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Gabor Mezei | aaa1d2a | 2023-01-23 16:13:43 +0100 | [diff] [blame] | 117 | MBEDTLS_STATIC_TESTABLE |
Gabor Mezei | 9073f7d | 2023-01-23 19:05:37 +0100 | [diff] [blame] | 118 | void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, |
| 119 | const mbedtls_mpi_mod_modulus *N) |
Gabor Mezei | aaa1d2a | 2023-01-23 16:13:43 +0100 | [diff] [blame] | 120 | { |
Gabor Mezei | aaa1d2a | 2023-01-23 16:13:43 +0100 | [diff] [blame] | 121 | mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, X, N->p, N->limbs); |
| 122 | |
| 123 | (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c); |
Gabor Mezei | aaa1d2a | 2023-01-23 16:13:43 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Gabor Mezei | 627e5b1 | 2023-01-24 18:13:24 +0100 | [diff] [blame] | 126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X, |
| 128 | const mbedtls_mpi_uint *A, |
| 129 | const mbedtls_mpi_uint *B, |
| 130 | const mbedtls_mpi_mod_modulus *N, |
| 131 | mbedtls_mpi_uint *T) |
Gabor Mezei | 979d34c | 2022-12-07 16:02:33 +0100 | [diff] [blame] | 132 | { |
Minos Galanakis | 2ed8fb7 | 2023-06-14 16:01:47 +0100 | [diff] [blame^] | 133 | const size_t T_limbs = (N->limbs * 2); |
| 134 | switch (N->int_rep) { |
| 135 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
| 136 | mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs, |
| 137 | N->rep.mont.mm, T); |
| 138 | break; |
| 139 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
| 140 | mbedtls_mpi_core_mul(T, A, N->limbs, B, N->limbs); |
| 141 | (*N->rep.ored.modp)(T, T_limbs); |
| 142 | mbedtls_mpi_mod_raw_fix_quasi_reduction(T, N); |
| 143 | memcpy(X, T, N->limbs * sizeof(mbedtls_mpi_uint)); |
| 144 | break; |
| 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | |
Gabor Mezei | 979d34c | 2022-12-07 16:02:33 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs) |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 152 | { |
| 153 | /* mbedtls_mpi_mod_raw_inv_prime() needs a temporary for the exponent, |
| 154 | * which will be the same size as the modulus and input (AN_limbs), |
| 155 | * and additional space to pass to mbedtls_mpi_core_exp_mod(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | return AN_limbs + |
| 157 | mbedtls_mpi_core_exp_mod_working_limbs(AN_limbs, AN_limbs); |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X, |
| 161 | const mbedtls_mpi_uint *A, |
| 162 | const mbedtls_mpi_uint *N, |
| 163 | size_t AN_limbs, |
| 164 | const mbedtls_mpi_uint *RR, |
| 165 | mbedtls_mpi_uint *T) |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 166 | { |
| 167 | /* Inversion by power: g^|G| = 1 => g^(-1) = g^(|G|-1), and |
| 168 | * |G| = N - 1, so we want |
| 169 | * g^(|G|-1) = g^(N - 2) |
| 170 | */ |
Tom Cosgrove | 5f09930 | 2022-12-09 10:58:15 +0000 | [diff] [blame] | 171 | |
| 172 | /* Use the first AN_limbs of T to hold N - 2 */ |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 173 | mbedtls_mpi_uint *Nminus2 = T; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | (void) mbedtls_mpi_core_sub_int(Nminus2, N, 2, AN_limbs); |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 175 | |
Tom Cosgrove | 5f09930 | 2022-12-09 10:58:15 +0000 | [diff] [blame] | 176 | /* Rest of T is given to exp_mod for its working space */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | mbedtls_mpi_core_exp_mod(X, |
| 178 | A, N, AN_limbs, Nminus2, AN_limbs, |
| 179 | RR, T + AN_limbs); |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X, |
| 183 | const mbedtls_mpi_uint *A, |
| 184 | const mbedtls_mpi_uint *B, |
| 185 | const mbedtls_mpi_mod_modulus *N) |
Hanno Becker | a45b6fe | 2022-11-01 13:14:28 +0000 | [diff] [blame] | 186 | { |
Werner Lewis | d391b8c | 2022-11-08 15:53:47 +0000 | [diff] [blame] | 187 | mbedtls_mpi_uint carry, borrow; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | carry = mbedtls_mpi_core_add(X, A, B, N->limbs); |
| 189 | borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs); |
| 190 | (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) (carry ^ borrow)); |
Hanno Becker | a45b6fe | 2022-11-01 13:14:28 +0000 | [diff] [blame] | 191 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 192 | |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 193 | int mbedtls_mpi_mod_raw_canonical_to_modulus_rep( |
| 194 | mbedtls_mpi_uint *X, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | const mbedtls_mpi_mod_modulus *N) |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 196 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | switch (N->int_rep) { |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 198 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | return mbedtls_mpi_mod_raw_to_mont_rep(X, N); |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 200 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | return 0; |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 202 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | int mbedtls_mpi_mod_raw_modulus_to_canonical_rep( |
| 208 | mbedtls_mpi_uint *X, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | const mbedtls_mpi_mod_modulus *N) |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 210 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | switch (N->int_rep) { |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 212 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 213 | return mbedtls_mpi_mod_raw_from_mont_rep(X, N); |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 214 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | return 0; |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 216 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X, |
| 222 | mbedtls_mpi_uint min, |
| 223 | const mbedtls_mpi_mod_modulus *N, |
| 224 | int (*f_rng)(void *, unsigned char *, size_t), |
| 225 | void *p_rng) |
Gilles Peskine | a57cf98 | 2022-12-06 22:54:09 +0100 | [diff] [blame] | 226 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | int ret = mbedtls_mpi_core_random(X, min, N->p, N->limbs, f_rng, p_rng); |
| 228 | if (ret != 0) { |
| 229 | return ret; |
| 230 | } |
| 231 | return mbedtls_mpi_mod_raw_canonical_to_modulus_rep(X, N); |
Gilles Peskine | a57cf98 | 2022-12-06 22:54:09 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 234 | int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X, |
| 235 | const mbedtls_mpi_mod_modulus *N) |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 236 | { |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 237 | mbedtls_mpi_uint *T; |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 238 | const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs(N->limbs); |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | if ((T = (mbedtls_mpi_uint *) mbedtls_calloc(t_limbs, ciL)) == NULL) { |
| 241 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 242 | } |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 243 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 244 | mbedtls_mpi_core_to_mont_rep(X, X, N->p, N->limbs, |
| 245 | N->rep.mont.mm, N->rep.mont.rr, T); |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | mbedtls_platform_zeroize(T, t_limbs * ciL); |
| 248 | mbedtls_free(T); |
| 249 | return 0; |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 250 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 251 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 252 | int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X, |
| 253 | const mbedtls_mpi_mod_modulus *N) |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 254 | { |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 255 | const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs(N->limbs); |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 256 | mbedtls_mpi_uint *T; |
| 257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | if ((T = (mbedtls_mpi_uint *) mbedtls_calloc(t_limbs, ciL)) == NULL) { |
| 259 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 260 | } |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 261 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 262 | mbedtls_mpi_core_from_mont_rep(X, X, N->p, N->limbs, N->rep.mont.mm, T); |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | mbedtls_platform_zeroize(T, t_limbs * ciL); |
| 265 | mbedtls_free(T); |
| 266 | return 0; |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 267 | } |
Minos Galanakis | 21fe8bd | 2022-12-07 18:06:05 +0000 | [diff] [blame] | 268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X, |
| 270 | const mbedtls_mpi_uint *A, |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 271 | const mbedtls_mpi_mod_modulus *N) |
Minos Galanakis | 21fe8bd | 2022-12-07 18:06:05 +0000 | [diff] [blame] | 272 | { |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 273 | mbedtls_mpi_core_sub(X, N->p, A, N->limbs); |
Minos Galanakis | 21fe8bd | 2022-12-07 18:06:05 +0000 | [diff] [blame] | 274 | |
| 275 | /* If A=0 initially, then X=N now. Detect this by |
| 276 | * subtracting N and catching the carry. */ |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 277 | mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs); |
| 278 | (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) borrow); |
Minos Galanakis | 21fe8bd | 2022-12-07 18:06:05 +0000 | [diff] [blame] | 279 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 280 | |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 281 | #endif /* MBEDTLS_BIGNUM_C */ |