blob: 6ac9e72dbb8df97de007d11b319572f1646407b0 [file] [log] [blame]
Janos Follath0ded6312022-08-09 13:34:54 +01001/*
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Low-level modular bignum functions
Janos Follath0ded6312022-08-09 13:34:54 +01003 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
22#if defined(MBEDTLS_BIGNUM_C)
23
24#include <string.h>
25
26#include "mbedtls/error.h"
27#include "mbedtls/platform_util.h"
28
Janos Follath0ded6312022-08-09 13:34:54 +010029#include "mbedtls/platform.h"
Janos Follath0ded6312022-08-09 13:34:54 +010030
31#include "bignum_core.h"
32#include "bignum_mod_raw.h"
33#include "bignum_mod.h"
34#include "constant_time_internal.h"
35
Gilles Peskine449bd832023-01-11 14:50:10 +010036void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X,
37 const mbedtls_mpi_uint *A,
38 const mbedtls_mpi_mod_modulus *N,
39 unsigned char assign)
Gabor Mezei12071d42022-09-12 16:35:58 +020040{
Gilles Peskine449bd832023-01-11 14:50:10 +010041 mbedtls_mpi_core_cond_assign(X, A, N->limbs, assign);
Gabor Mezei12071d42022-09-12 16:35:58 +020042}
43
Gilles Peskine449bd832023-01-11 14:50:10 +010044void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X,
45 mbedtls_mpi_uint *Y,
46 const mbedtls_mpi_mod_modulus *N,
47 unsigned char swap)
Gabor Mezei12071d42022-09-12 16:35:58 +020048{
Gilles Peskine449bd832023-01-11 14:50:10 +010049 mbedtls_mpi_core_cond_swap(X, Y, N->limbs, swap);
Gabor Mezei12071d42022-09-12 16:35:58 +020050}
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X,
53 const mbedtls_mpi_mod_modulus *m,
54 const unsigned char *input,
55 size_t input_length,
56 mbedtls_mpi_mod_ext_rep ext_rep)
Janos Follath0ded6312022-08-09 13:34:54 +010057{
58 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060 switch (ext_rep) {
Janos Follath296ea662022-08-11 14:58:29 +010061 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Gilles Peskine449bd832023-01-11 14:50:10 +010062 ret = mbedtls_mpi_core_read_le(X, m->limbs,
63 input, input_length);
Janos Follath296ea662022-08-11 14:58:29 +010064 break;
65 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Gilles Peskine449bd832023-01-11 14:50:10 +010066 ret = mbedtls_mpi_core_read_be(X, m->limbs,
67 input, input_length);
Janos Follath296ea662022-08-11 14:58:29 +010068 break;
69 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010070 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath296ea662022-08-11 14:58:29 +010071 }
Janos Follath0ded6312022-08-09 13:34:54 +010072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (ret != 0) {
Janos Follath0ded6312022-08-09 13:34:54 +010074 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +010075 }
Janos Follath0ded6312022-08-09 13:34:54 +010076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if (!mbedtls_mpi_core_lt_ct(X, m->p, m->limbs)) {
Janos Follath0ded6312022-08-09 13:34:54 +010078 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
79 goto cleanup;
80 }
81
82cleanup:
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084 return ret;
Janos Follath0ded6312022-08-09 13:34:54 +010085}
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A,
88 const mbedtls_mpi_mod_modulus *m,
89 unsigned char *output,
90 size_t output_length,
91 mbedtls_mpi_mod_ext_rep ext_rep)
Janos Follath0ded6312022-08-09 13:34:54 +010092{
Gilles Peskine449bd832023-01-11 14:50:10 +010093 switch (ext_rep) {
Janos Follath296ea662022-08-11 14:58:29 +010094 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return mbedtls_mpi_core_write_le(A, m->limbs,
96 output, output_length);
Janos Follath296ea662022-08-11 14:58:29 +010097 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return mbedtls_mpi_core_write_be(A, m->limbs,
99 output, output_length);
Janos Follath296ea662022-08-11 14:58:29 +0100100 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath296ea662022-08-11 14:58:29 +0100102 }
Janos Follath0ded6312022-08-09 13:34:54 +0100103}
104
Janos Follath5933f692022-11-02 14:35:17 +0000105/* BEGIN MERGE SLOT 1 */
106
107/* END MERGE SLOT 1 */
108
109/* BEGIN MERGE SLOT 2 */
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
112 const mbedtls_mpi_uint *A,
113 const mbedtls_mpi_uint *B,
114 const mbedtls_mpi_mod_modulus *N)
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100115{
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, A, B, N->limbs);
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c);
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100119}
120
Gabor Mezei627e5b12023-01-24 18:13:24 +0100121#if defined(MBEDTLS_TEST_HOOKS)
Gabor Mezei979d34c2022-12-07 16:02:33 +0100122
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +0100123MBEDTLS_STATIC_TESTABLE
Gabor Mezei9073f7d2023-01-23 19:05:37 +0100124void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X,
125 const mbedtls_mpi_mod_modulus *N)
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +0100126{
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +0100127 mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
128
129 (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c);
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +0100130}
131
Gabor Mezei627e5b12023-01-24 18:13:24 +0100132#endif /* MBEDTLS_TEST_HOOKS */
133
134void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
135 const mbedtls_mpi_uint *A,
136 const mbedtls_mpi_uint *B,
137 const mbedtls_mpi_mod_modulus *N,
138 mbedtls_mpi_uint *T)
139{
140 mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs,
141 N->rep.mont.mm, T);
142}
143
Janos Follath5933f692022-11-02 14:35:17 +0000144/* END MERGE SLOT 2 */
145
146/* BEGIN MERGE SLOT 3 */
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs)
Tom Cosgrove61292682022-12-08 09:44:10 +0000149{
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 Peskine449bd832023-01-11 14:50:10 +0100153 return AN_limbs +
154 mbedtls_mpi_core_exp_mod_working_limbs(AN_limbs, AN_limbs);
Tom Cosgrove61292682022-12-08 09:44:10 +0000155}
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157void 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 Cosgrove61292682022-12-08 09:44:10 +0000163{
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 Cosgrove5f099302022-12-09 10:58:15 +0000168
169 /* Use the first AN_limbs of T to hold N - 2 */
Tom Cosgrove61292682022-12-08 09:44:10 +0000170 mbedtls_mpi_uint *Nminus2 = T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 (void) mbedtls_mpi_core_sub_int(Nminus2, N, 2, AN_limbs);
Tom Cosgrove61292682022-12-08 09:44:10 +0000172
Tom Cosgrove5f099302022-12-09 10:58:15 +0000173 /* Rest of T is given to exp_mod for its working space */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_mpi_core_exp_mod(X,
175 A, N, AN_limbs, Nminus2, AN_limbs,
176 RR, T + AN_limbs);
Tom Cosgrove61292682022-12-08 09:44:10 +0000177}
178
Janos Follath5933f692022-11-02 14:35:17 +0000179/* END MERGE SLOT 3 */
180
181/* BEGIN MERGE SLOT 4 */
182
183/* END MERGE SLOT 4 */
184
185/* BEGIN MERGE SLOT 5 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,
187 const mbedtls_mpi_uint *A,
188 const mbedtls_mpi_uint *B,
189 const mbedtls_mpi_mod_modulus *N)
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000190{
Werner Lewisd391b8c2022-11-08 15:53:47 +0000191 mbedtls_mpi_uint carry, borrow;
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 carry = mbedtls_mpi_core_add(X, A, B, N->limbs);
193 borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
194 (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) (carry ^ borrow));
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000195}
Janos Follath5933f692022-11-02 14:35:17 +0000196/* END MERGE SLOT 5 */
197
198/* BEGIN MERGE SLOT 6 */
199
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100200int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(
201 mbedtls_mpi_uint *X,
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 const mbedtls_mpi_mod_modulus *N)
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100203{
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 switch (N->int_rep) {
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100205 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return mbedtls_mpi_mod_raw_to_mont_rep(X, N);
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100207 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return 0;
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100209 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100211 }
212}
213
214int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
215 mbedtls_mpi_uint *X,
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 const mbedtls_mpi_mod_modulus *N)
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100217{
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 switch (N->int_rep) {
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100219 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return mbedtls_mpi_mod_raw_from_mont_rep(X, N);
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100221 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return 0;
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100223 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100225 }
226}
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,
229 mbedtls_mpi_uint min,
230 const mbedtls_mpi_mod_modulus *N,
231 int (*f_rng)(void *, unsigned char *, size_t),
232 void *p_rng)
Gilles Peskinea57cf982022-12-06 22:54:09 +0100233{
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 int ret = mbedtls_mpi_core_random(X, min, N->p, N->limbs, f_rng, p_rng);
235 if (ret != 0) {
236 return ret;
237 }
238 return mbedtls_mpi_mod_raw_canonical_to_modulus_rep(X, N);
Gilles Peskinea57cf982022-12-06 22:54:09 +0100239}
240
Janos Follath5933f692022-11-02 14:35:17 +0000241/* END MERGE SLOT 6 */
242
243/* BEGIN MERGE SLOT 7 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X,
245 const mbedtls_mpi_mod_modulus *m)
Hanno Becker5ad4a932022-08-09 14:45:53 +0100246{
Minos Galanakisd9299c32022-11-01 16:19:07 +0000247 mbedtls_mpi_uint *T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs(m->limbs);
Minos Galanakisd9299c32022-11-01 16:19:07 +0000249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if ((T = (mbedtls_mpi_uint *) mbedtls_calloc(t_limbs, ciL)) == NULL) {
251 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
252 }
Minos Galanakisd9299c32022-11-01 16:19:07 +0000253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_mpi_core_to_mont_rep(X, X, m->p, m->limbs,
255 m->rep.mont.mm, m->rep.mont.rr, T);
Minos Galanakisd9299c32022-11-01 16:19:07 +0000256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 mbedtls_platform_zeroize(T, t_limbs * ciL);
258 mbedtls_free(T);
259 return 0;
Hanno Becker5ad4a932022-08-09 14:45:53 +0100260}
Janos Follath5933f692022-11-02 14:35:17 +0000261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X,
263 const mbedtls_mpi_mod_modulus *m)
Hanno Becker5ad4a932022-08-09 14:45:53 +0100264{
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs(m->limbs);
Minos Galanakisd9299c32022-11-01 16:19:07 +0000266 mbedtls_mpi_uint *T;
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if ((T = (mbedtls_mpi_uint *) mbedtls_calloc(t_limbs, ciL)) == NULL) {
269 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
270 }
Minos Galanakisd9299c32022-11-01 16:19:07 +0000271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 mbedtls_mpi_core_from_mont_rep(X, X, m->p, m->limbs, m->rep.mont.mm, T);
Minos Galanakisd9299c32022-11-01 16:19:07 +0000273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_platform_zeroize(T, t_limbs * ciL);
275 mbedtls_free(T);
276 return 0;
Hanno Becker5ad4a932022-08-09 14:45:53 +0100277}
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,
280 const mbedtls_mpi_uint *A,
281 const mbedtls_mpi_mod_modulus *m)
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000282{
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 mbedtls_mpi_core_sub(X, m->p, A, m->limbs);
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000284
285 /* If A=0 initially, then X=N now. Detect this by
286 * subtracting N and catching the carry. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, X, m->p, m->limbs);
288 (void) mbedtls_mpi_core_add_if(X, m->p, m->limbs, (unsigned) borrow);
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000289}
Janos Follath5933f692022-11-02 14:35:17 +0000290/* END MERGE SLOT 7 */
291
292/* BEGIN MERGE SLOT 8 */
293
294/* END MERGE SLOT 8 */
295
296/* BEGIN MERGE SLOT 9 */
297
298/* END MERGE SLOT 9 */
299
300/* BEGIN MERGE SLOT 10 */
301
302/* END MERGE SLOT 10 */
303
Janos Follath0ded6312022-08-09 13:34:54 +0100304#endif /* MBEDTLS_BIGNUM_C */