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: |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 91 | mbedtls_free(N->rep.ored); |
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, |
| 140 | const mbedtls_mpi_uint *p, |
| 141 | size_t p_limbs, |
| 142 | mbedtls_mpi_mod_rep_selector int_rep) |
| 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, |
| 169 | mbedtls_mpi_opt_red_struct *ored) |
| 170 | { |
Minos Galanakis | f055ad6 | 2023-05-09 15:44:46 +0100 | [diff] [blame^] | 171 | standard_modulus_setup(N, p, p_limbs, MBEDTLS_MPI_MOD_REP_OPT_RED); |
Minos Galanakis | bbe9db4 | 2023-05-09 10:37:21 +0100 | [diff] [blame] | 172 | N->rep.ored =ored ; |
| 173 | return 0; |
| 174 | } |
| 175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | int mbedtls_mpi_mod_mul(mbedtls_mpi_mod_residue *X, |
| 177 | const mbedtls_mpi_mod_residue *A, |
| 178 | const mbedtls_mpi_mod_residue *B, |
| 179 | const mbedtls_mpi_mod_modulus *N) |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 180 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | if (N->limbs == 0) { |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 182 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | } |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | 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] | 186 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 187 | } |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | mbedtls_mpi_uint *T = mbedtls_calloc(N->limbs * 2 + 1, ciL); |
| 190 | if (T == NULL) { |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 191 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | } |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 194 | 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] | 195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | mbedtls_free(T); |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | return 0; |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | int mbedtls_mpi_mod_sub(mbedtls_mpi_mod_residue *X, |
| 202 | const mbedtls_mpi_mod_residue *A, |
| 203 | const mbedtls_mpi_mod_residue *B, |
| 204 | const mbedtls_mpi_mod_modulus *N) |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 205 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) { |
| 207 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 208 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | mbedtls_mpi_mod_raw_sub(X->p, A->p, B->p, N); |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 212 | return 0; |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 213 | } |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | static int mbedtls_mpi_mod_inv_mont(mbedtls_mpi_mod_residue *X, |
| 216 | const mbedtls_mpi_mod_residue *A, |
| 217 | const mbedtls_mpi_mod_modulus *N, |
| 218 | mbedtls_mpi_uint *working_memory) |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 219 | { |
| 220 | /* Input already in Montgomery form, so there's little to do */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | mbedtls_mpi_mod_raw_inv_prime(X->p, A->p, |
| 222 | N->p, N->limbs, |
| 223 | N->rep.mont.rr, |
| 224 | working_memory); |
| 225 | return 0; |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | static int mbedtls_mpi_mod_inv_non_mont(mbedtls_mpi_mod_residue *X, |
| 229 | const mbedtls_mpi_mod_residue *A, |
| 230 | const mbedtls_mpi_mod_modulus *N, |
| 231 | mbedtls_mpi_uint *working_memory) |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 232 | { |
| 233 | /* Need to convert input into Montgomery form */ |
| 234 | |
| 235 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 236 | |
| 237 | mbedtls_mpi_mod_modulus Nmont; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | mbedtls_mpi_mod_modulus_init(&Nmont); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 239 | |
Minos Galanakis | 88e16df | 2023-05-09 14:11:43 +0100 | [diff] [blame] | 240 | 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] | 241 | |
| 242 | /* 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] | 243 | mbedtls_mpi_core_to_mont_rep(X->p, A->p, Nmont.p, Nmont.limbs, |
| 244 | Nmont.rep.mont.mm, Nmont.rep.mont.rr, |
| 245 | working_memory); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | mbedtls_mpi_mod_raw_inv_prime(X->p, X->p, |
| 248 | Nmont.p, Nmont.limbs, |
| 249 | Nmont.rep.mont.rr, |
| 250 | working_memory); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 251 | |
| 252 | /* And convert back from Montgomery form */ |
| 253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | mbedtls_mpi_core_from_mont_rep(X->p, X->p, Nmont.p, Nmont.limbs, |
| 255 | Nmont.rep.mont.mm, working_memory); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 256 | |
| 257 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | mbedtls_mpi_mod_modulus_free(&Nmont); |
| 259 | return ret; |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | int mbedtls_mpi_mod_inv(mbedtls_mpi_mod_residue *X, |
| 263 | const mbedtls_mpi_mod_residue *A, |
| 264 | const mbedtls_mpi_mod_modulus *N) |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 265 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | if (X->limbs != N->limbs || A->limbs != N->limbs) { |
| 267 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 268 | } |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 269 | |
| 270 | /* Zero has the same value regardless of Montgomery form or not */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | if (mbedtls_mpi_core_check_zero_ct(A->p, A->limbs) == 0) { |
| 272 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 273 | } |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 274 | |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 275 | size_t working_limbs = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | mbedtls_mpi_mod_raw_inv_prime_working_limbs(N->limbs); |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | mbedtls_mpi_uint *working_memory = mbedtls_calloc(working_limbs, |
| 279 | sizeof(mbedtls_mpi_uint)); |
| 280 | if (working_memory == NULL) { |
| 281 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 282 | } |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 283 | |
| 284 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | switch (N->int_rep) { |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 287 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | ret = mbedtls_mpi_mod_inv_mont(X, A, N, working_memory); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 289 | break; |
| 290 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | ret = mbedtls_mpi_mod_inv_non_mont(X, A, N, working_memory); |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 292 | break; |
| 293 | default: |
| 294 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 295 | break; |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | mbedtls_platform_zeroize(working_memory, |
| 299 | working_limbs * sizeof(mbedtls_mpi_uint)); |
| 300 | mbedtls_free(working_memory); |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 301 | |
Tom Cosgrove | a9e0f95 | 2022-12-13 11:57:57 +0000 | [diff] [blame] | 302 | return ret; |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 303 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | int mbedtls_mpi_mod_add(mbedtls_mpi_mod_residue *X, |
| 306 | const mbedtls_mpi_mod_residue *A, |
| 307 | const mbedtls_mpi_mod_residue *B, |
| 308 | const mbedtls_mpi_mod_modulus *N) |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 309 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) { |
| 311 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 312 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 313 | |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 314 | mbedtls_mpi_mod_raw_add(X->p, A->p, B->p, N); |
| 315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | return 0; |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 317 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | int mbedtls_mpi_mod_random(mbedtls_mpi_mod_residue *X, |
| 320 | mbedtls_mpi_uint min, |
| 321 | const mbedtls_mpi_mod_modulus *N, |
| 322 | int (*f_rng)(void *, unsigned char *, size_t), |
| 323 | void *p_rng) |
Gilles Peskine | b1eea02 | 2022-12-07 22:59:27 +0100 | [diff] [blame] | 324 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | if (X->limbs != N->limbs) { |
| 326 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 327 | } |
| 328 | 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] | 329 | } |
| 330 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 331 | int mbedtls_mpi_mod_read(mbedtls_mpi_mod_residue *r, |
| 332 | const mbedtls_mpi_mod_modulus *N, |
| 333 | const unsigned char *buf, |
| 334 | size_t buflen, |
| 335 | mbedtls_mpi_mod_ext_rep ext_rep) |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 336 | { |
| 337 | int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 338 | |
Janos Follath | 75b9f0f | 2022-11-26 14:28:50 +0000 | [diff] [blame] | 339 | /* 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] | 340 | if (r->limbs == 0 || N->limbs == 0) { |
Janos Follath | 75b9f0f | 2022-11-26 14:28:50 +0000 | [diff] [blame] | 341 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 342 | } |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 343 | if (r->limbs != N->limbs) { |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 344 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | } |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 346 | |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 347 | 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] | 348 | if (ret != 0) { |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 349 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | } |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 351 | |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 352 | r->limbs = N->limbs; |
Minos Galanakis | 8b37545 | 2022-11-24 11:04:11 +0000 | [diff] [blame] | 353 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 354 | ret = mbedtls_mpi_mod_raw_canonical_to_modulus_rep(r->p, N); |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 355 | |
| 356 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | return ret; |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 360 | int mbedtls_mpi_mod_write(const mbedtls_mpi_mod_residue *r, |
| 361 | const mbedtls_mpi_mod_modulus *N, |
| 362 | unsigned char *buf, |
| 363 | size_t buflen, |
| 364 | mbedtls_mpi_mod_ext_rep ext_rep) |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 365 | { |
Janos Follath | 75b9f0f | 2022-11-26 14:28:50 +0000 | [diff] [blame] | 366 | /* 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] | 367 | if (r->limbs == 0 || N->limbs == 0) { |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 368 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | } |
Mihir Raj Singh | a43290d | 2023-01-11 20:46:18 +0530 | [diff] [blame] | 370 | if (r->limbs != N->limbs) { |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 371 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Janos Follath | 8dfc8c4 | 2022-11-26 15:39:02 +0000 | [diff] [blame] | 372 | } |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 373 | |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 374 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 375 | mbedtls_mpi_uint *working_memory = r->p; |
| 376 | size_t working_memory_len = sizeof(mbedtls_mpi_uint) * r->limbs; |
| 377 | |
Mihir Raj Singh | a43290d | 2023-01-11 20:46:18 +0530 | [diff] [blame] | 378 | if (N->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY) { |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 379 | |
| 380 | working_memory = mbedtls_calloc(r->limbs, sizeof(mbedtls_mpi_uint)); |
| 381 | |
| 382 | if (working_memory == NULL) { |
| 383 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 384 | goto cleanup; |
| 385 | } |
| 386 | |
| 387 | memcpy(working_memory, r->p, working_memory_len); |
| 388 | |
| 389 | ret = mbedtls_mpi_mod_raw_from_mont_rep(working_memory, N); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | if (ret != 0) { |
| 391 | goto cleanup; |
| 392 | } |
| 393 | } |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 394 | |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 395 | 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] | 396 | |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 397 | cleanup: |
Janos Follath | 8dfc8c4 | 2022-11-26 15:39:02 +0000 | [diff] [blame] | 398 | |
Gabor Mezei | 2f73edb | 2023-03-27 15:49:24 +0200 | [diff] [blame] | 399 | if (N->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY && |
| 400 | working_memory != NULL) { |
| 401 | |
| 402 | mbedtls_platform_zeroize(working_memory, working_memory_len); |
| 403 | mbedtls_free(working_memory); |
| 404 | } |
| 405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | return ret; |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 407 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 408 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 409 | #endif /* MBEDTLS_BIGNUM_C */ |