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