blob: 0f575fca6e5d3779256d086cc9b9fe9465ace331 [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
Mihir Raj Singhcd17ff02023-01-11 20:56:13 +053052int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
53 const mbedtls_mpi_mod_modulus *N,
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:
Mihir Raj Singhcd17ff02023-01-11 20:56:13 +053062 ret = mbedtls_mpi_core_read_le( X, N->limbs,
63 input, input_length );
Janos Follath296ea662022-08-11 14:58:29 +010064 break;
65 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Mihir Raj Singhcd17ff02023-01-11 20:56:13 +053066 ret = mbedtls_mpi_core_read_be( X, N->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
Mihir Raj Singhcd17ff02023-01-11 20:56:13 +053077 if( !mbedtls_mpi_core_lt_ct( X, N->p, N->limbs ) )
78 {
Janos Follath0ded6312022-08-09 13:34:54 +010079 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
80 goto cleanup;
81 }
82
83cleanup:
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return ret;
Janos Follath0ded6312022-08-09 13:34:54 +010086}
87
Mihir Raj Singh01e861f2023-01-11 21:00:42 +053088int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
89 const mbedtls_mpi_mod_modulus *N,
90 unsigned char *output,
91 size_t output_length,
92 mbedtls_mpi_mod_ext_rep ext_rep )
Janos Follath0ded6312022-08-09 13:34:54 +010093{
Gilles Peskine449bd832023-01-11 14:50:10 +010094 switch (ext_rep) {
Janos Follath296ea662022-08-11 14:58:29 +010095 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Mihir Raj Singh01e861f2023-01-11 21:00:42 +053096 return( mbedtls_mpi_core_write_le( A, N->limbs,
97 output, output_length ) );
Janos Follath296ea662022-08-11 14:58:29 +010098 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Mihir Raj Singh01e861f2023-01-11 21:00:42 +053099 return( mbedtls_mpi_core_write_be( A, N->limbs,
100 output, output_length ) );
Janos Follath296ea662022-08-11 14:58:29 +0100101 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath296ea662022-08-11 14:58:29 +0100103 }
Janos Follath0ded6312022-08-09 13:34:54 +0100104}
105
Janos Follath5933f692022-11-02 14:35:17 +0000106/* BEGIN MERGE SLOT 1 */
107
108/* END MERGE SLOT 1 */
109
110/* BEGIN MERGE SLOT 2 */
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
113 const mbedtls_mpi_uint *A,
114 const mbedtls_mpi_uint *B,
115 const mbedtls_mpi_mod_modulus *N)
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100116{
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, A, B, N->limbs);
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c);
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100120}
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
123 const mbedtls_mpi_uint *A,
124 const mbedtls_mpi_uint *B,
125 const mbedtls_mpi_mod_modulus *N,
126 mbedtls_mpi_uint *T)
Gabor Mezei979d34c2022-12-07 16:02:33 +0100127{
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs,
129 N->rep.mont.mm, T);
Gabor Mezei979d34c2022-12-07 16:02:33 +0100130}
131
Janos Follath5933f692022-11-02 14:35:17 +0000132/* END MERGE SLOT 2 */
133
134/* BEGIN MERGE SLOT 3 */
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs)
Tom Cosgrove61292682022-12-08 09:44:10 +0000137{
138 /* mbedtls_mpi_mod_raw_inv_prime() needs a temporary for the exponent,
139 * which will be the same size as the modulus and input (AN_limbs),
140 * and additional space to pass to mbedtls_mpi_core_exp_mod(). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return AN_limbs +
142 mbedtls_mpi_core_exp_mod_working_limbs(AN_limbs, AN_limbs);
Tom Cosgrove61292682022-12-08 09:44:10 +0000143}
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X,
146 const mbedtls_mpi_uint *A,
147 const mbedtls_mpi_uint *N,
148 size_t AN_limbs,
149 const mbedtls_mpi_uint *RR,
150 mbedtls_mpi_uint *T)
Tom Cosgrove61292682022-12-08 09:44:10 +0000151{
152 /* Inversion by power: g^|G| = 1 => g^(-1) = g^(|G|-1), and
153 * |G| = N - 1, so we want
154 * g^(|G|-1) = g^(N - 2)
155 */
Tom Cosgrove5f099302022-12-09 10:58:15 +0000156
157 /* Use the first AN_limbs of T to hold N - 2 */
Tom Cosgrove61292682022-12-08 09:44:10 +0000158 mbedtls_mpi_uint *Nminus2 = T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 (void) mbedtls_mpi_core_sub_int(Nminus2, N, 2, AN_limbs);
Tom Cosgrove61292682022-12-08 09:44:10 +0000160
Tom Cosgrove5f099302022-12-09 10:58:15 +0000161 /* Rest of T is given to exp_mod for its working space */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_mpi_core_exp_mod(X,
163 A, N, AN_limbs, Nminus2, AN_limbs,
164 RR, T + AN_limbs);
Tom Cosgrove61292682022-12-08 09:44:10 +0000165}
166
Janos Follath5933f692022-11-02 14:35:17 +0000167/* END MERGE SLOT 3 */
168
169/* BEGIN MERGE SLOT 4 */
170
171/* END MERGE SLOT 4 */
172
173/* BEGIN MERGE SLOT 5 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,
175 const mbedtls_mpi_uint *A,
176 const mbedtls_mpi_uint *B,
177 const mbedtls_mpi_mod_modulus *N)
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000178{
Werner Lewisd391b8c2022-11-08 15:53:47 +0000179 mbedtls_mpi_uint carry, borrow;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 carry = mbedtls_mpi_core_add(X, A, B, N->limbs);
181 borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
182 (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) (carry ^ borrow));
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000183}
Janos Follath5933f692022-11-02 14:35:17 +0000184/* END MERGE SLOT 5 */
185
186/* BEGIN MERGE SLOT 6 */
187
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100188int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(
189 mbedtls_mpi_uint *X,
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 const mbedtls_mpi_mod_modulus *N)
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100191{
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 switch (N->int_rep) {
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100193 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return mbedtls_mpi_mod_raw_to_mont_rep(X, N);
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100195 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return 0;
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100197 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100199 }
200}
201
202int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
203 mbedtls_mpi_uint *X,
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 const mbedtls_mpi_mod_modulus *N)
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100205{
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 switch (N->int_rep) {
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100207 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return mbedtls_mpi_mod_raw_from_mont_rep(X, N);
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100209 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return 0;
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100211 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100213 }
214}
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,
217 mbedtls_mpi_uint min,
218 const mbedtls_mpi_mod_modulus *N,
219 int (*f_rng)(void *, unsigned char *, size_t),
220 void *p_rng)
Gilles Peskinea57cf982022-12-06 22:54:09 +0100221{
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 int ret = mbedtls_mpi_core_random(X, min, N->p, N->limbs, f_rng, p_rng);
223 if (ret != 0) {
224 return ret;
225 }
226 return mbedtls_mpi_mod_raw_canonical_to_modulus_rep(X, N);
Gilles Peskinea57cf982022-12-06 22:54:09 +0100227}
228
Janos Follath5933f692022-11-02 14:35:17 +0000229/* END MERGE SLOT 6 */
230
231/* BEGIN MERGE SLOT 7 */
Mihir Raj Singh37ece722023-01-11 21:06:16 +0530232int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
233 const mbedtls_mpi_mod_modulus *N )
Hanno Becker5ad4a932022-08-09 14:45:53 +0100234{
Minos Galanakisd9299c32022-11-01 16:19:07 +0000235 mbedtls_mpi_uint *T;
Mihir Raj Singh37ece722023-01-11 21:06:16 +0530236 const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs( N->limbs );
Minos Galanakisd9299c32022-11-01 16:19:07 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if ((T = (mbedtls_mpi_uint *) mbedtls_calloc(t_limbs, ciL)) == NULL) {
239 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
240 }
Minos Galanakisd9299c32022-11-01 16:19:07 +0000241
Mihir Raj Singh37ece722023-01-11 21:06:16 +0530242 mbedtls_mpi_core_to_mont_rep( X, X, N->p, N->limbs,
243 N->rep.mont.mm, N->rep.mont.rr, T );
Minos Galanakisd9299c32022-11-01 16:19:07 +0000244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 mbedtls_platform_zeroize(T, t_limbs * ciL);
246 mbedtls_free(T);
247 return 0;
Hanno Becker5ad4a932022-08-09 14:45:53 +0100248}
Janos Follath5933f692022-11-02 14:35:17 +0000249
Mihir Raj Singhb0354c52023-01-11 21:10:22 +0530250int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
251 const mbedtls_mpi_mod_modulus *N )
Hanno Becker5ad4a932022-08-09 14:45:53 +0100252{
Mihir Raj Singhb0354c52023-01-11 21:10:22 +0530253 const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs( N->limbs );
Minos Galanakisd9299c32022-11-01 16:19:07 +0000254 mbedtls_mpi_uint *T;
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if ((T = (mbedtls_mpi_uint *) mbedtls_calloc(t_limbs, ciL)) == NULL) {
257 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
258 }
Minos Galanakisd9299c32022-11-01 16:19:07 +0000259
Mihir Raj Singhb0354c52023-01-11 21:10:22 +0530260 mbedtls_mpi_core_from_mont_rep( X, X, N->p, N->limbs, N->rep.mont.mm, T );
Minos Galanakisd9299c32022-11-01 16:19:07 +0000261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_platform_zeroize(T, t_limbs * ciL);
263 mbedtls_free(T);
264 return 0;
Hanno Becker5ad4a932022-08-09 14:45:53 +0100265}
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,
268 const mbedtls_mpi_uint *A,
269 const mbedtls_mpi_mod_modulus *m)
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000270{
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_mpi_core_sub(X, m->p, A, m->limbs);
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000272
273 /* If A=0 initially, then X=N now. Detect this by
274 * subtracting N and catching the carry. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, X, m->p, m->limbs);
276 (void) mbedtls_mpi_core_add_if(X, m->p, m->limbs, (unsigned) borrow);
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000277}
Janos Follath5933f692022-11-02 14:35:17 +0000278/* END MERGE SLOT 7 */
279
280/* BEGIN MERGE SLOT 8 */
281
282/* END MERGE SLOT 8 */
283
284/* BEGIN MERGE SLOT 9 */
285
286/* END MERGE SLOT 9 */
287
288/* BEGIN MERGE SLOT 10 */
289
290/* END MERGE SLOT 10 */
291
Janos Follath0ded6312022-08-09 13:34:54 +0100292#endif /* MBEDTLS_BIGNUM_C */