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