Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 1 | /** |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 2 | * Modular bignum functions |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [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 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 24 | #include <string.h> |
| 25 | |
| 26 | #include "mbedtls/platform_util.h" |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 27 | #include "mbedtls/error.h" |
| 28 | #include "mbedtls/bignum.h" |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 29 | |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 31 | |
Janos Follath | d1baedb | 2022-08-09 13:44:53 +0100 | [diff] [blame] | 32 | #include "bignum_core.h" |
| 33 | #include "bignum_mod.h" |
| 34 | #include "bignum_mod_raw.h" |
| 35 | #include "constant_time_internal.h" |
| 36 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 37 | int mbedtls_mpi_mod_residue_setup(mbedtls_mpi_mod_residue *r, |
| 38 | const mbedtls_mpi_mod_modulus *N, |
| 39 | mbedtls_mpi_uint *p, |
| 40 | size_t p_limbs) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 41 | { |
Mihir Raj Singh | b13a589 | 2023-01-11 19:49:00 +0530 | [diff] [blame] | 42 | if (p_limbs != N->limbs || !mbedtls_mpi_core_lt_ct(p, N->p, N->limbs)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 44 | } |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 45 | |
Mihir Raj Singh | b13a589 | 2023-01-11 19:49:00 +0530 | [diff] [blame] | 46 | r->limbs = N->limbs; |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 47 | r->p = p; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 48 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | return 0; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | void mbedtls_mpi_mod_residue_release(mbedtls_mpi_mod_residue *r) |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 53 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | if (r == NULL) { |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 55 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | } |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 57 | |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 58 | r->limbs = 0; |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 59 | r->p = NULL; |
| 60 | } |
| 61 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 62 | void mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *N) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 63 | { |
Mihir Raj Singh | b6fa940 | 2023-01-11 19:55:14 +0530 | [diff] [blame] | 64 | if (N == NULL) { |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 65 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | } |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 67 | |
Mihir Raj Singh | b6fa940 | 2023-01-11 19:55:14 +0530 | [diff] [blame] | 68 | N->p = NULL; |
| 69 | N->limbs = 0; |
| 70 | N->bits = 0; |
| 71 | N->int_rep = MBEDTLS_MPI_MOD_REP_INVALID; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 74 | void mbedtls_mpi_mod_modulus_free(mbedtls_mpi_mod_modulus *N) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 75 | { |
Mihir Raj Singh | 928a07b | 2023-01-11 20:08:34 +0530 | [diff] [blame] | 76 | if (N == NULL) { |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 77 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | } |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 79 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 80 | switch (N->int_rep) { |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 81 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 82 | if (N->rep.mont.rr != NULL) { |
| 83 | mbedtls_platform_zeroize((mbedtls_mpi_uint *) N->rep.mont.rr, |
| 84 | N->limbs * sizeof(mbedtls_mpi_uint)); |
| 85 | mbedtls_free((mbedtls_mpi_uint *) N->rep.mont.rr); |
Mihir Raj Singh | 928a07b | 2023-01-11 20:08:34 +0530 | [diff] [blame] | 86 | N->rep.mont.rr = NULL; |
Minos Galanakis | 4d4c98b | 2022-10-27 15:58:02 +0100 | [diff] [blame] | 87 | } |
Mihir Raj Singh | 928a07b | 2023-01-11 20:08:34 +0530 | [diff] [blame] | 88 | N->rep.mont.mm = 0; |
Minos Galanakis | 771c470 | 2022-10-27 12:22:22 +0100 | [diff] [blame] | 89 | break; |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 90 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
Minos Galanakis | be1bf15 | 2023-06-09 14:47:55 +0100 | [diff] [blame] | 91 | N->rep.ored.modp = NULL; |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 92 | break; |
| 93 | case MBEDTLS_MPI_MOD_REP_INVALID: |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 94 | break; |
| 95 | } |
| 96 | |
Mihir Raj Singh | 928a07b | 2023-01-11 20:08:34 +0530 | [diff] [blame] | 97 | N->p = NULL; |
| 98 | N->limbs = 0; |
| 99 | N->bits = 0; |
| 100 | N->int_rep = MBEDTLS_MPI_MOD_REP_INVALID; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | static int set_mont_const_square(const mbedtls_mpi_uint **X, |
| 104 | const mbedtls_mpi_uint *A, |
| 105 | size_t limbs) |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 106 | { |
| 107 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 108 | mbedtls_mpi N; |
| 109 | mbedtls_mpi RR; |
Minos Galanakis | 4d4c98b | 2022-10-27 15:58:02 +0100 | [diff] [blame] | 110 | *X = NULL; |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | mbedtls_mpi_init(&N); |
| 113 | mbedtls_mpi_init(&RR); |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | if (A == NULL || limbs == 0 || limbs >= (MBEDTLS_MPI_MAX_LIMBS / 2) - 2) { |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 116 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | } |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | if (mbedtls_mpi_grow(&N, limbs)) { |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 120 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | } |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | memcpy(N.p, A, sizeof(mbedtls_mpi_uint) * limbs); |
Minos Galanakis | 771c470 | 2022-10-27 12:22:22 +0100 | [diff] [blame] | 124 | |
Minos Galanakis | 4d4c98b | 2022-10-27 15:58:02 +0100 | [diff] [blame] | 125 | ret = mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N); |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | if (ret == 0) { |
Minos Galanakis | 4d4c98b | 2022-10-27 15:58:02 +0100 | [diff] [blame] | 128 | *X = RR.p; |
| 129 | RR.p = NULL; |
| 130 | } |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 131 | |
| 132 | cleanup: |
| 133 | mbedtls_mpi_free(&N); |
| 134 | mbedtls_mpi_free(&RR); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | ret = (ret != 0) ? MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED : 0; |
| 136 | return ret; |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Minos Galanakis | f055ad6 | 2023-05-09 15:44:46 +0100 | [diff] [blame] | 139 | static inline void standard_modulus_setup(mbedtls_mpi_mod_modulus *N, |
Minos Galanakis | 0f718c9 | 2023-05-19 14:22:06 +0100 | [diff] [blame] | 140 | const mbedtls_mpi_uint *p, |
| 141 | size_t p_limbs, |
| 142 | mbedtls_mpi_mod_rep_selector int_rep) |
Minos Galanakis | f055ad6 | 2023-05-09 15:44:46 +0100 | [diff] [blame] | 143 | { |
| 144 | N->p = p; |
| 145 | N->limbs = p_limbs; |
| 146 | N->bits = mbedtls_mpi_core_bitlen(p, p_limbs); |
| 147 | N->int_rep = int_rep; |
| 148 | } |
| 149 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 150 | int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N, |
| 151 | const mbedtls_mpi_uint *p, |
Minos Galanakis | 88e16df | 2023-05-09 14:11:43 +0100 | [diff] [blame] | 152 | size_t p_limbs) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 153 | { |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 154 | int ret = 0; |
Minos Galanakis | f055ad6 | 2023-05-09 15:44:46 +0100 | [diff] [blame] | 155 | standard_modulus_setup(N, p, p_limbs, MBEDTLS_MPI_MOD_REP_MONTGOMERY); |
Minos Galanakis | 88e16df | 2023-05-09 14:11:43 +0100 | [diff] [blame] | 156 | N->rep.mont.mm = mbedtls_mpi_core_montmul_init(N->p); |
| 157 | ret = set_mont_const_square(&N->rep.mont.rr, N->p, N->limbs); |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 158 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 159 | if (ret != 0) { |
| 160 | mbedtls_mpi_mod_modulus_free(N); |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | return ret; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Minos Galanakis | bbe9db4 | 2023-05-09 10:37:21 +0100 | [diff] [blame] | 166 | int mbedtls_mpi_mod_optred_modulus_setup(mbedtls_mpi_mod_modulus *N, |
| 167 | const mbedtls_mpi_uint *p, |
| 168 | size_t p_limbs, |
Minos Galanakis | de87461 | 2023-06-13 16:59:26 +0100 | [diff] [blame^] | 169 | int (*modp)(mbedtls_mpi_uint *X, |
| 170 | size_t X_limbs)) |
Minos Galanakis | bbe9db4 | 2023-05-09 10:37:21 +0100 | [diff] [blame] | 171 | { |
Minos Galanakis | f055ad6 | 2023-05-09 15:44:46 +0100 | [diff] [blame] | 172 | standard_modulus_setup(N, p, p_limbs, MBEDTLS_MPI_MOD_REP_OPT_RED); |
Minos Galanakis | be1bf15 | 2023-06-09 14:47:55 +0100 | [diff] [blame] | 173 | N->rep.ored.modp = modp; |
Minos Galanakis | bbe9db4 | 2023-05-09 10:37:21 +0100 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | int mbedtls_mpi_mod_mul(mbedtls_mpi_mod_residue *X, |
| 178 | const mbedtls_mpi_mod_residue *A, |
| 179 | const mbedtls_mpi_mod_residue *B, |
| 180 | const mbedtls_mpi_mod_modulus *N) |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 181 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | if (N->limbs == 0) { |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 183 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | } |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) { |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 187 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | } |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | mbedtls_mpi_uint *T = mbedtls_calloc(N->limbs * 2 + 1, ciL); |
| 191 | if (T == NULL) { |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 192 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | } |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | mbedtls_mpi_mod_raw_mul(X->p, A->p, B->p, N, T); |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | mbedtls_free(T); |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | return 0; |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | int mbedtls_mpi_mod_sub(mbedtls_mpi_mod_residue *X, |
| 203 | const mbedtls_mpi_mod_residue *A, |
| 204 | const mbedtls_mpi_mod_residue *B, |
| 205 | const mbedtls_mpi_mod_modulus *N) |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 206 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) { |
| 208 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 209 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | mbedtls_mpi_mod_raw_sub(X->p, A->p, B->p, N); |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 213 | return 0; |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 214 | } |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | static int mbedtls_mpi_mod_inv_mont(mbedtls_mpi_mod_residue *X, |
| 217 | const mbedtls_mpi_mod_residue *A, |
| 218 | const mbedtls_mpi_mod_modulus *N, |
| 219 | mbedtls_mpi_uint *working_memory) |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 220 | { |
| 221 | /* Input already in Montgomery form, so there's little to do */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | mbedtls_mpi_mod_raw_inv_prime(X->p, A->p, |
| 223 | N->p, N->limbs, |
| 224 | N->rep.mont.rr, |
| 225 | working_memory); |
| 226 | return 0; |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | static int mbedtls_mpi_mod_inv_non_mont(mbedtls_mpi_mod_residue *X, |
| 230 | const mbedtls_mpi_mod_residue *A, |
| 231 | const mbedtls_mpi_mod_modulus *N, |
| 232 | mbedtls_mpi_uint *working_memory) |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 233 | { |
| 234 | /* Need to convert input into Montgomery form */ |
| 235 | |
| 236 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 237 | |
| 238 | mbedtls_mpi_mod_modulus Nmont; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | mbedtls_mpi_mod_modulus_init(&Nmont); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 240 | |
Minos Galanakis | 88e16df | 2023-05-09 14:11:43 +0100 | [diff] [blame] | 241 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_modulus_setup(&Nmont, N->p, N->limbs)); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 242 | |
| 243 | /* We'll use X->p to hold the Montgomery form of the input A->p */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | mbedtls_mpi_core_to_mont_rep(X->p, A->p, Nmont.p, Nmont.limbs, |
| 245 | Nmont.rep.mont.mm, Nmont.rep.mont.rr, |
| 246 | working_memory); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | mbedtls_mpi_mod_raw_inv_prime(X->p, X->p, |
| 249 | Nmont.p, Nmont.limbs, |
| 250 | Nmont.rep.mont.rr, |
| 251 | working_memory); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 252 | |
| 253 | /* And convert back from Montgomery form */ |
| 254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | mbedtls_mpi_core_from_mont_rep(X->p, X->p, Nmont.p, Nmont.limbs, |
| 256 | Nmont.rep.mont.mm, working_memory); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 257 | |
| 258 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | mbedtls_mpi_mod_modulus_free(&Nmont); |
| 260 | return ret; |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | int mbedtls_mpi_mod_inv(mbedtls_mpi_mod_residue *X, |
| 264 | const mbedtls_mpi_mod_residue *A, |
| 265 | const mbedtls_mpi_mod_modulus *N) |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 266 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | if (X->limbs != N->limbs || A->limbs != N->limbs) { |
| 268 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 269 | } |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 270 | |
| 271 | /* Zero has the same value regardless of Montgomery form or not */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 272 | if (mbedtls_mpi_core_check_zero_ct(A->p, A->limbs) == 0) { |
| 273 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 274 | } |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 275 | |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 276 | size_t working_limbs = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | mbedtls_mpi_mod_raw_inv_prime_working_limbs(N->limbs); |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | mbedtls_mpi_uint *working_memory = mbedtls_calloc(working_limbs, |
| 280 | sizeof(mbedtls_mpi_uint)); |
| 281 | if (working_memory == NULL) { |
| 282 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 283 | } |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 284 | |
| 285 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | switch (N->int_rep) { |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 288 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | ret = mbedtls_mpi_mod_inv_mont(X, A, N, working_memory); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 290 | break; |
| 291 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | ret = mbedtls_mpi_mod_inv_non_mont(X, A, N, working_memory); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 293 | break; |
| 294 | default: |
| 295 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 296 | break; |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | mbedtls_platform_zeroize(working_memory, |
| 300 | working_limbs * sizeof(mbedtls_mpi_uint)); |
| 301 | mbedtls_free(working_memory); |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 302 | |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 303 | return ret; |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 304 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | int mbedtls_mpi_mod_add(mbedtls_mpi_mod_residue *X, |
| 307 | const mbedtls_mpi_mod_residue *A, |
| 308 | const mbedtls_mpi_mod_residue *B, |
| 309 | const mbedtls_mpi_mod_modulus *N) |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 310 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) { |
| 312 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 313 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 314 | |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 315 | mbedtls_mpi_mod_raw_add(X->p, A->p, B->p, N); |
| 316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | return 0; |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 318 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | int mbedtls_mpi_mod_random(mbedtls_mpi_mod_residue *X, |
| 321 | mbedtls_mpi_uint min, |
| 322 | const mbedtls_mpi_mod_modulus *N, |
| 323 | int (*f_rng)(void *, unsigned char *, size_t), |
| 324 | void *p_rng) |
Gilles Peskine | b1eea02 | 2022-12-07 22:59:27 +0100 | [diff] [blame] | 325 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | if (X->limbs != N->limbs) { |
| 327 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 328 | } |
| 329 | return mbedtls_mpi_mod_raw_random(X->p, min, N, f_rng, p_rng); |
Gilles Peskine | b1eea02 | 2022-12-07 22:59:27 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 332 | int mbedtls_mpi_mod_read(mbedtls_mpi_mod_residue *r, |
| 333 | const mbedtls_mpi_mod_modulus *N, |
| 334 | const unsigned char *buf, |
| 335 | size_t buflen, |
| 336 | mbedtls_mpi_mod_ext_rep ext_rep) |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 337 | { |
| 338 | int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 339 | |
Janos Follath | 75b9f0f | 2022-11-26 14:28:50 +0000 | [diff] [blame] | 340 | /* Do our best to check if r and m have been set up */ |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 341 | if (r->limbs == 0 || N->limbs == 0) { |
Janos Follath | 75b9f0f | 2022-11-26 14:28:50 +0000 | [diff] [blame] | 342 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | } |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 344 | if (r->limbs != N->limbs) { |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 345 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 346 | } |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 347 | |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 348 | ret = mbedtls_mpi_mod_raw_read(r->p, N, buf, buflen, ext_rep); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | if (ret != 0) { |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 350 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | } |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 352 | |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 353 | r->limbs = N->limbs; |
Minos Galanakis | 8b37545 | 2022-11-24 11:04:11 +0000 | [diff] [blame] | 354 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 355 | ret = mbedtls_mpi_mod_raw_canonical_to_modulus_rep(r->p, N); |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 356 | |
| 357 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | return ret; |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 361 | int mbedtls_mpi_mod_write(const mbedtls_mpi_mod_residue *r, |
| 362 | const mbedtls_mpi_mod_modulus *N, |
| 363 | unsigned char *buf, |
| 364 | size_t buflen, |
| 365 | mbedtls_mpi_mod_ext_rep ext_rep) |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 366 | { |
Janos Follath | 75b9f0f | 2022-11-26 14:28:50 +0000 | [diff] [blame] | 367 | /* Do our best to check if r and m have been set up */ |
Mihir Raj Singh | a43290d | 2023-01-11 20:46:18 +0530 | [diff] [blame] | 368 | if (r->limbs == 0 || N->limbs == 0) { |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 369 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | } |
Mihir Raj Singh | a43290d | 2023-01-11 20:46:18 +0530 | [diff] [blame] | 371 | if (r->limbs != N->limbs) { |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 372 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Janos Follath | 8dfc8c4 | 2022-11-26 15:39:02 +0000 | [diff] [blame] | 373 | } |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 374 | |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 375 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 376 | mbedtls_mpi_uint *working_memory = r->p; |
| 377 | size_t working_memory_len = sizeof(mbedtls_mpi_uint) * r->limbs; |
| 378 | |
Mihir Raj Singh | a43290d | 2023-01-11 20:46:18 +0530 | [diff] [blame] | 379 | if (N->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY) { |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 380 | |
| 381 | working_memory = mbedtls_calloc(r->limbs, sizeof(mbedtls_mpi_uint)); |
| 382 | |
| 383 | if (working_memory == NULL) { |
| 384 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 385 | goto cleanup; |
| 386 | } |
| 387 | |
| 388 | memcpy(working_memory, r->p, working_memory_len); |
| 389 | |
| 390 | ret = mbedtls_mpi_mod_raw_from_mont_rep(working_memory, N); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | if (ret != 0) { |
| 392 | goto cleanup; |
| 393 | } |
| 394 | } |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 395 | |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 396 | ret = mbedtls_mpi_mod_raw_write(working_memory, N, buf, buflen, ext_rep); |
Janos Follath | 8dfc8c4 | 2022-11-26 15:39:02 +0000 | [diff] [blame] | 397 | |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 398 | cleanup: |
Janos Follath | 8dfc8c4 | 2022-11-26 15:39:02 +0000 | [diff] [blame] | 399 | |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 400 | if (N->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY && |
| 401 | working_memory != NULL) { |
| 402 | |
| 403 | mbedtls_platform_zeroize(working_memory, working_memory_len); |
| 404 | mbedtls_free(working_memory); |
| 405 | } |
| 406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 407 | return ret; |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 408 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 409 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 410 | #endif /* MBEDTLS_BIGNUM_C */ |