blob: 4d6782972b79fab012c2e2f30925997d77df5b5e [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Modular bignum functions
Gabor Mezeif049dbf2022-07-18 23:02:33 +02003 *
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 Mezeib9030702022-07-18 23:09:45 +020024#include <string.h>
25
26#include "mbedtls/platform_util.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020027#include "mbedtls/error.h"
28#include "mbedtls/bignum.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020029
Janos Follathba5c1392022-07-19 13:42:07 +010030#include "mbedtls/platform.h"
Janos Follathba5c1392022-07-19 13:42:07 +010031
Janos Follathd1baedb2022-08-09 13:44:53 +010032#include "bignum_core.h"
33#include "bignum_mod.h"
34#include "bignum_mod_raw.h"
35#include "constant_time_internal.h"
36
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053037int 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 Mezeif049dbf2022-07-18 23:02:33 +020041{
Mihir Raj Singhb13a5892023-01-11 19:49:00 +053042 if (p_limbs != N->limbs || !mbedtls_mpi_core_lt_ct(p, N->p, N->limbs)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010043 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
44 }
Gabor Mezeif049dbf2022-07-18 23:02:33 +020045
Mihir Raj Singhb13a5892023-01-11 19:49:00 +053046 r->limbs = N->limbs;
Janos Follath8b718b52022-07-25 11:31:02 +010047 r->p = p;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 return 0;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020050}
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052void mbedtls_mpi_mod_residue_release(mbedtls_mpi_mod_residue *r)
Gabor Mezei37b06362022-08-02 17:22:18 +020053{
Gilles Peskine449bd832023-01-11 14:50:10 +010054 if (r == NULL) {
Gabor Mezei37b06362022-08-02 17:22:18 +020055 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010056 }
Gabor Mezei37b06362022-08-02 17:22:18 +020057
Gabor Mezeifd65e822022-08-12 18:09:12 +020058 r->limbs = 0;
Gabor Mezei37b06362022-08-02 17:22:18 +020059 r->p = NULL;
60}
61
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053062void mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *N)
Gabor Mezeif049dbf2022-07-18 23:02:33 +020063{
Mihir Raj Singhb6fa9402023-01-11 19:55:14 +053064 if (N == NULL) {
Gabor Mezeif049dbf2022-07-18 23:02:33 +020065 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010066 }
Gabor Mezeif049dbf2022-07-18 23:02:33 +020067
Mihir Raj Singhb6fa9402023-01-11 19:55:14 +053068 N->p = NULL;
69 N->limbs = 0;
70 N->bits = 0;
71 N->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020072}
73
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053074void mbedtls_mpi_mod_modulus_free(mbedtls_mpi_mod_modulus *N)
Gabor Mezeif049dbf2022-07-18 23:02:33 +020075{
Mihir Raj Singh928a07b2023-01-11 20:08:34 +053076 if (N == NULL) {
Gabor Mezeif049dbf2022-07-18 23:02:33 +020077 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 }
Gabor Mezeif049dbf2022-07-18 23:02:33 +020079
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053080 switch (N->int_rep) {
Janos Follathba5c1392022-07-19 13:42:07 +010081 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053082 if (N->rep.mont.rr != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +010083 mbedtls_zeroize_and_free((mbedtls_mpi_uint *) N->rep.mont.rr,
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053084 N->limbs * sizeof(mbedtls_mpi_uint));
Mihir Raj Singh928a07b2023-01-11 20:08:34 +053085 N->rep.mont.rr = NULL;
Minos Galanakis4d4c98b2022-10-27 15:58:02 +010086 }
Mihir Raj Singh928a07b2023-01-11 20:08:34 +053087 N->rep.mont.mm = 0;
Minos Galanakis771c4702022-10-27 12:22:22 +010088 break;
Janos Follathba5c1392022-07-19 13:42:07 +010089 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Minos Galanakisbe1bf152023-06-09 14:47:55 +010090 N->rep.ored.modp = NULL;
Janos Follath296ea662022-08-11 14:58:29 +010091 break;
92 case MBEDTLS_MPI_MOD_REP_INVALID:
Janos Follathba5c1392022-07-19 13:42:07 +010093 break;
94 }
95
Mihir Raj Singh928a07b2023-01-11 20:08:34 +053096 N->p = NULL;
97 N->limbs = 0;
98 N->bits = 0;
99 N->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200100}
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102static int set_mont_const_square(const mbedtls_mpi_uint **X,
103 const mbedtls_mpi_uint *A,
104 size_t limbs)
Minos Galanakis8b333632022-10-11 11:28:24 +0100105{
106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
107 mbedtls_mpi N;
108 mbedtls_mpi RR;
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100109 *X = NULL;
Minos Galanakis8b333632022-10-11 11:28:24 +0100110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_mpi_init(&N);
112 mbedtls_mpi_init(&RR);
Minos Galanakis8b333632022-10-11 11:28:24 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (A == NULL || limbs == 0 || limbs >= (MBEDTLS_MPI_MAX_LIMBS / 2) - 2) {
Minos Galanakis8b333632022-10-11 11:28:24 +0100115 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 }
Minos Galanakis8b333632022-10-11 11:28:24 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (mbedtls_mpi_grow(&N, limbs)) {
Minos Galanakis8b333632022-10-11 11:28:24 +0100119 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 }
Minos Galanakis8b333632022-10-11 11:28:24 +0100121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 memcpy(N.p, A, sizeof(mbedtls_mpi_uint) * limbs);
Minos Galanakis771c4702022-10-27 12:22:22 +0100123
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100124 ret = mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N);
Minos Galanakis8b333632022-10-11 11:28:24 +0100125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (ret == 0) {
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100127 *X = RR.p;
128 RR.p = NULL;
129 }
Minos Galanakis8b333632022-10-11 11:28:24 +0100130
131cleanup:
132 mbedtls_mpi_free(&N);
133 mbedtls_mpi_free(&RR);
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 ret = (ret != 0) ? MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED : 0;
135 return ret;
Minos Galanakis8b333632022-10-11 11:28:24 +0100136}
137
Minos Galanakisf055ad62023-05-09 15:44:46 +0100138static inline void standard_modulus_setup(mbedtls_mpi_mod_modulus *N,
Minos Galanakis0f718c92023-05-19 14:22:06 +0100139 const mbedtls_mpi_uint *p,
140 size_t p_limbs,
141 mbedtls_mpi_mod_rep_selector int_rep)
Minos Galanakisf055ad62023-05-09 15:44:46 +0100142{
143 N->p = p;
144 N->limbs = p_limbs;
145 N->bits = mbedtls_mpi_core_bitlen(p, p_limbs);
146 N->int_rep = int_rep;
147}
148
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530149int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N,
150 const mbedtls_mpi_uint *p,
Minos Galanakis88e16df2023-05-09 14:11:43 +0100151 size_t p_limbs)
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200152{
Janos Follathba5c1392022-07-19 13:42:07 +0100153 int ret = 0;
Minos Galanakisf055ad62023-05-09 15:44:46 +0100154 standard_modulus_setup(N, p, p_limbs, MBEDTLS_MPI_MOD_REP_MONTGOMERY);
Minos Galanakis88e16df2023-05-09 14:11:43 +0100155 N->rep.mont.mm = mbedtls_mpi_core_montmul_init(N->p);
156 ret = set_mont_const_square(&N->rep.mont.rr, N->p, N->limbs);
Janos Follathba5c1392022-07-19 13:42:07 +0100157
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530158 if (ret != 0) {
159 mbedtls_mpi_mod_modulus_free(N);
Janos Follathba5c1392022-07-19 13:42:07 +0100160 }
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return ret;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200163}
164
Minos Galanakisbbe9db42023-05-09 10:37:21 +0100165int mbedtls_mpi_mod_optred_modulus_setup(mbedtls_mpi_mod_modulus *N,
166 const mbedtls_mpi_uint *p,
167 size_t p_limbs,
Minos Galanakis2a03fd32023-06-21 15:23:29 +0100168 mbedtls_mpi_modp_fn modp)
Minos Galanakisbbe9db42023-05-09 10:37:21 +0100169{
Minos Galanakisf055ad62023-05-09 15:44:46 +0100170 standard_modulus_setup(N, p, p_limbs, MBEDTLS_MPI_MOD_REP_OPT_RED);
Minos Galanakisbe1bf152023-06-09 14:47:55 +0100171 N->rep.ored.modp = modp;
Minos Galanakisbbe9db42023-05-09 10:37:21 +0100172 return 0;
173}
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int mbedtls_mpi_mod_mul(mbedtls_mpi_mod_residue *X,
176 const mbedtls_mpi_mod_residue *A,
177 const mbedtls_mpi_mod_residue *B,
178 const mbedtls_mpi_mod_modulus *N)
Gabor Mezei9db81e92022-12-13 10:51:37 +0100179{
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (N->limbs == 0) {
Gabor Mezei9db81e92022-12-13 10:51:37 +0100181 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 }
Gabor Mezei9db81e92022-12-13 10:51:37 +0100183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) {
Gabor Mezei9db81e92022-12-13 10:51:37 +0100185 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 }
Gabor Mezei9db81e92022-12-13 10:51:37 +0100187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 mbedtls_mpi_uint *T = mbedtls_calloc(N->limbs * 2 + 1, ciL);
189 if (T == NULL) {
Gabor Mezei9db81e92022-12-13 10:51:37 +0100190 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 }
Gabor Mezei9db81e92022-12-13 10:51:37 +0100192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_mpi_mod_raw_mul(X->p, A->p, B->p, N, T);
Gabor Mezei9db81e92022-12-13 10:51:37 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 mbedtls_free(T);
Gabor Mezei9db81e92022-12-13 10:51:37 +0100196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return 0;
Gabor Mezei9db81e92022-12-13 10:51:37 +0100198}
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200int mbedtls_mpi_mod_sub(mbedtls_mpi_mod_residue *X,
201 const mbedtls_mpi_mod_residue *A,
202 const mbedtls_mpi_mod_residue *B,
203 const mbedtls_mpi_mod_modulus *N)
Tom Cosgrove62b20482022-12-01 14:27:37 +0000204{
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) {
206 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
207 }
Janos Follath5933f692022-11-02 14:35:17 +0000208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_mpi_mod_raw_sub(X->p, A->p, B->p, N);
Tom Cosgrove62b20482022-12-01 14:27:37 +0000210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return 0;
Tom Cosgrove62b20482022-12-01 14:27:37 +0000212}
Tom Cosgrove4302d022022-12-13 10:46:39 +0000213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214static int mbedtls_mpi_mod_inv_mont(mbedtls_mpi_mod_residue *X,
215 const mbedtls_mpi_mod_residue *A,
216 const mbedtls_mpi_mod_modulus *N,
217 mbedtls_mpi_uint *working_memory)
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000218{
219 /* Input already in Montgomery form, so there's little to do */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_mpi_mod_raw_inv_prime(X->p, A->p,
221 N->p, N->limbs,
222 N->rep.mont.rr,
223 working_memory);
224 return 0;
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000225}
226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227static int mbedtls_mpi_mod_inv_non_mont(mbedtls_mpi_mod_residue *X,
228 const mbedtls_mpi_mod_residue *A,
229 const mbedtls_mpi_mod_modulus *N,
230 mbedtls_mpi_uint *working_memory)
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000231{
232 /* Need to convert input into Montgomery form */
233
234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
235
236 mbedtls_mpi_mod_modulus Nmont;
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_mpi_mod_modulus_init(&Nmont);
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000238
Minos Galanakis88e16df2023-05-09 14:11:43 +0100239 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_modulus_setup(&Nmont, N->p, N->limbs));
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000240
241 /* We'll use X->p to hold the Montgomery form of the input A->p */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_mpi_core_to_mont_rep(X->p, A->p, Nmont.p, Nmont.limbs,
243 Nmont.rep.mont.mm, Nmont.rep.mont.rr,
244 working_memory);
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 mbedtls_mpi_mod_raw_inv_prime(X->p, X->p,
247 Nmont.p, Nmont.limbs,
248 Nmont.rep.mont.rr,
249 working_memory);
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000250
251 /* And convert back from Montgomery form */
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_mpi_core_from_mont_rep(X->p, X->p, Nmont.p, Nmont.limbs,
254 Nmont.rep.mont.mm, working_memory);
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000255
256cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 mbedtls_mpi_mod_modulus_free(&Nmont);
258 return ret;
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000259}
260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261int mbedtls_mpi_mod_inv(mbedtls_mpi_mod_residue *X,
262 const mbedtls_mpi_mod_residue *A,
263 const mbedtls_mpi_mod_modulus *N)
Tom Cosgrove4302d022022-12-13 10:46:39 +0000264{
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (X->limbs != N->limbs || A->limbs != N->limbs) {
266 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
267 }
Tom Cosgrove4302d022022-12-13 10:46:39 +0000268
269 /* Zero has the same value regardless of Montgomery form or not */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (mbedtls_mpi_core_check_zero_ct(A->p, A->limbs) == 0) {
271 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
272 }
Tom Cosgrove4302d022022-12-13 10:46:39 +0000273
Tom Cosgrove4302d022022-12-13 10:46:39 +0000274 size_t working_limbs =
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_mpi_mod_raw_inv_prime_working_limbs(N->limbs);
Tom Cosgrove4302d022022-12-13 10:46:39 +0000276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 mbedtls_mpi_uint *working_memory = mbedtls_calloc(working_limbs,
278 sizeof(mbedtls_mpi_uint));
279 if (working_memory == NULL) {
280 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
281 }
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000282
283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 switch (N->int_rep) {
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000286 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 ret = mbedtls_mpi_mod_inv_mont(X, A, N, working_memory);
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000288 break;
289 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 ret = mbedtls_mpi_mod_inv_non_mont(X, A, N, working_memory);
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000291 break;
292 default:
293 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
294 break;
Tom Cosgrove4302d022022-12-13 10:46:39 +0000295 }
296
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100297 mbedtls_zeroize_and_free(working_memory,
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 working_limbs * sizeof(mbedtls_mpi_uint));
Tom Cosgrove4302d022022-12-13 10:46:39 +0000299
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000300 return ret;
Tom Cosgrove4302d022022-12-13 10:46:39 +0000301}
Janos Follath5933f692022-11-02 14:35:17 +0000302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303int mbedtls_mpi_mod_add(mbedtls_mpi_mod_residue *X,
304 const mbedtls_mpi_mod_residue *A,
305 const mbedtls_mpi_mod_residue *B,
306 const mbedtls_mpi_mod_modulus *N)
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000307{
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) {
309 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
310 }
Janos Follath5933f692022-11-02 14:35:17 +0000311
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000312 mbedtls_mpi_mod_raw_add(X->p, A->p, B->p, N);
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return 0;
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000315}
Janos Follath5933f692022-11-02 14:35:17 +0000316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317int mbedtls_mpi_mod_random(mbedtls_mpi_mod_residue *X,
318 mbedtls_mpi_uint min,
319 const mbedtls_mpi_mod_modulus *N,
320 int (*f_rng)(void *, unsigned char *, size_t),
321 void *p_rng)
Gilles Peskineb1eea022022-12-07 22:59:27 +0100322{
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (X->limbs != N->limbs) {
324 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
325 }
326 return mbedtls_mpi_mod_raw_random(X->p, min, N, f_rng, p_rng);
Gilles Peskineb1eea022022-12-07 22:59:27 +0100327}
328
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530329int mbedtls_mpi_mod_read(mbedtls_mpi_mod_residue *r,
330 const mbedtls_mpi_mod_modulus *N,
331 const unsigned char *buf,
332 size_t buflen,
333 mbedtls_mpi_mod_ext_rep ext_rep)
Minos Galanakis81f4b112022-11-10 14:40:38 +0000334{
335 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath5933f692022-11-02 14:35:17 +0000336
Janos Follath75b9f0f2022-11-26 14:28:50 +0000337 /* Do our best to check if r and m have been set up */
Mihir Raj Singhfdc314b2023-01-11 20:32:59 +0530338 if (r->limbs == 0 || N->limbs == 0) {
Janos Follath75b9f0f2022-11-26 14:28:50 +0000339 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 }
Mihir Raj Singhfdc314b2023-01-11 20:32:59 +0530341 if (r->limbs != N->limbs) {
Minos Galanakis81f4b112022-11-10 14:40:38 +0000342 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 }
Minos Galanakis81f4b112022-11-10 14:40:38 +0000344
Mihir Raj Singhfdc314b2023-01-11 20:32:59 +0530345 ret = mbedtls_mpi_mod_raw_read(r->p, N, buf, buflen, ext_rep);
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (ret != 0) {
Minos Galanakis81f4b112022-11-10 14:40:38 +0000347 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 }
Minos Galanakis81f4b112022-11-10 14:40:38 +0000349
Mihir Raj Singhfdc314b2023-01-11 20:32:59 +0530350 r->limbs = N->limbs;
Minos Galanakis8b375452022-11-24 11:04:11 +0000351
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530352 ret = mbedtls_mpi_mod_raw_canonical_to_modulus_rep(r->p, N);
Minos Galanakis81f4b112022-11-10 14:40:38 +0000353
354cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return ret;
Minos Galanakis81f4b112022-11-10 14:40:38 +0000356}
357
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530358int mbedtls_mpi_mod_write(const mbedtls_mpi_mod_residue *r,
359 const mbedtls_mpi_mod_modulus *N,
360 unsigned char *buf,
361 size_t buflen,
362 mbedtls_mpi_mod_ext_rep ext_rep)
Minos Galanakis81f4b112022-11-10 14:40:38 +0000363{
Janos Follath75b9f0f2022-11-26 14:28:50 +0000364 /* Do our best to check if r and m have been set up */
Mihir Raj Singha43290d2023-01-11 20:46:18 +0530365 if (r->limbs == 0 || N->limbs == 0) {
Gabor Mezei2f73edb2023-03-27 15:49:24 +0200366 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 }
Mihir Raj Singha43290d2023-01-11 20:46:18 +0530368 if (r->limbs != N->limbs) {
Gabor Mezei2f73edb2023-03-27 15:49:24 +0200369 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath8dfc8c42022-11-26 15:39:02 +0000370 }
Minos Galanakis81f4b112022-11-10 14:40:38 +0000371
Gabor Mezei2f73edb2023-03-27 15:49:24 +0200372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
373 mbedtls_mpi_uint *working_memory = r->p;
374 size_t working_memory_len = sizeof(mbedtls_mpi_uint) * r->limbs;
375
Mihir Raj Singha43290d2023-01-11 20:46:18 +0530376 if (N->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY) {
Gabor Mezei2f73edb2023-03-27 15:49:24 +0200377
378 working_memory = mbedtls_calloc(r->limbs, sizeof(mbedtls_mpi_uint));
379
380 if (working_memory == NULL) {
381 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
382 goto cleanup;
383 }
384
385 memcpy(working_memory, r->p, working_memory_len);
386
387 ret = mbedtls_mpi_mod_raw_from_mont_rep(working_memory, N);
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (ret != 0) {
389 goto cleanup;
390 }
391 }
Minos Galanakis81f4b112022-11-10 14:40:38 +0000392
Gabor Mezei2f73edb2023-03-27 15:49:24 +0200393 ret = mbedtls_mpi_mod_raw_write(working_memory, N, buf, buflen, ext_rep);
Janos Follath8dfc8c42022-11-26 15:39:02 +0000394
Minos Galanakis81f4b112022-11-10 14:40:38 +0000395cleanup:
Janos Follath8dfc8c42022-11-26 15:39:02 +0000396
Gabor Mezei2f73edb2023-03-27 15:49:24 +0200397 if (N->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY &&
398 working_memory != NULL) {
399
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100400 mbedtls_zeroize_and_free(working_memory, working_memory_len);
Gabor Mezei2f73edb2023-03-27 15:49:24 +0200401 }
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return ret;
Minos Galanakis81f4b112022-11-10 14:40:38 +0000404}
Janos Follath5933f692022-11-02 14:35:17 +0000405
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200406#endif /* MBEDTLS_BIGNUM_C */