blob: 75cf8c41e018a91f54076bc48226cfb56f2b4c5e [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
Gabor Mezei9a66ab12023-01-25 13:23:38 +010036#include "bignum_mod_raw_invasive.h"
37
Gilles Peskine449bd832023-01-11 14:50:10 +010038void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X,
39 const mbedtls_mpi_uint *A,
40 const mbedtls_mpi_mod_modulus *N,
41 unsigned char assign)
Gabor Mezei12071d42022-09-12 16:35:58 +020042{
Dave Rodgmancd2e38b2023-05-17 13:31:55 +010043 mbedtls_mpi_core_cond_assign(X, A, N->limbs, mbedtls_ct_bool(assign));
Gabor Mezei12071d42022-09-12 16:35:58 +020044}
45
Gilles Peskine449bd832023-01-11 14:50:10 +010046void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X,
47 mbedtls_mpi_uint *Y,
48 const mbedtls_mpi_mod_modulus *N,
49 unsigned char swap)
Gabor Mezei12071d42022-09-12 16:35:58 +020050{
Dave Rodgmancd2e38b2023-05-17 13:31:55 +010051 mbedtls_mpi_core_cond_swap(X, Y, N->limbs, mbedtls_ct_bool(swap));
Gabor Mezei12071d42022-09-12 16:35:58 +020052}
53
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053054int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X,
55 const mbedtls_mpi_mod_modulus *N,
56 const unsigned char *input,
57 size_t input_length,
58 mbedtls_mpi_mod_ext_rep ext_rep)
Janos Follath0ded6312022-08-09 13:34:54 +010059{
60 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062 switch (ext_rep) {
Janos Follath296ea662022-08-11 14:58:29 +010063 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053064 ret = mbedtls_mpi_core_read_le(X, N->limbs,
65 input, input_length);
Janos Follath296ea662022-08-11 14:58:29 +010066 break;
67 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053068 ret = mbedtls_mpi_core_read_be(X, N->limbs,
69 input, input_length);
Janos Follath296ea662022-08-11 14:58:29 +010070 break;
71 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath296ea662022-08-11 14:58:29 +010073 }
Janos Follath0ded6312022-08-09 13:34:54 +010074
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if (ret != 0) {
Janos Follath0ded6312022-08-09 13:34:54 +010076 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +010077 }
Janos Follath0ded6312022-08-09 13:34:54 +010078
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053079 if (!mbedtls_mpi_core_lt_ct(X, N->p, N->limbs)) {
Janos Follath0ded6312022-08-09 13:34:54 +010080 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
81 goto cleanup;
82 }
83
84cleanup:
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return ret;
Janos Follath0ded6312022-08-09 13:34:54 +010087}
88
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053089int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A,
90 const mbedtls_mpi_mod_modulus *N,
91 unsigned char *output,
92 size_t output_length,
93 mbedtls_mpi_mod_ext_rep ext_rep)
Janos Follath0ded6312022-08-09 13:34:54 +010094{
Gilles Peskine449bd832023-01-11 14:50:10 +010095 switch (ext_rep) {
Janos Follath296ea662022-08-11 14:58:29 +010096 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Mihir Raj Singh432cacf2023-01-11 21:12:46 +053097 return mbedtls_mpi_core_write_le(A, N->limbs,
98 output, output_length);
Janos Follath296ea662022-08-11 14:58:29 +010099 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530100 return mbedtls_mpi_core_write_be(A, N->limbs,
101 output, output_length);
Janos Follath296ea662022-08-11 14:58:29 +0100102 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath296ea662022-08-11 14:58:29 +0100104 }
Janos Follath0ded6312022-08-09 13:34:54 +0100105}
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
108 const mbedtls_mpi_uint *A,
109 const mbedtls_mpi_uint *B,
110 const mbedtls_mpi_mod_modulus *N)
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100111{
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, A, B, N->limbs);
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c);
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100115}
116
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +0100117MBEDTLS_STATIC_TESTABLE
Gabor Mezei9073f7d2023-01-23 19:05:37 +0100118void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X,
119 const mbedtls_mpi_mod_modulus *N)
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +0100120{
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +0100121 mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
122
123 (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c);
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +0100124}
125
Gabor Mezei627e5b12023-01-24 18:13:24 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
128 const mbedtls_mpi_uint *A,
129 const mbedtls_mpi_uint *B,
130 const mbedtls_mpi_mod_modulus *N,
131 mbedtls_mpi_uint *T)
Gabor Mezei979d34c2022-12-07 16:02:33 +0100132{
Minos Galanakisc7408a42023-06-25 20:56:59 +0100133 /* Standard (A * B) multiplication stored into pre-allocated T
Minos Galanakisc4e49582023-06-27 14:03:35 +0100134 * buffer of fixed limb size of (2N + 1).
Minos Galanakis53a16b32023-06-26 17:05:53 +0100135 *
Minos Galanakisc4e49582023-06-27 14:03:35 +0100136 * The space may not not fully filled by when
137 * MBEDTLS_MPI_MOD_REP_OPT_RED is used. */
138 const size_t T_limbs = BITS_TO_LIMBS(N->bits) * 2;
Minos Galanakis2ed8fb72023-06-14 16:01:47 +0100139 switch (N->int_rep) {
140 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
141 mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs,
142 N->rep.mont.mm, T);
143 break;
144 case MBEDTLS_MPI_MOD_REP_OPT_RED:
145 mbedtls_mpi_core_mul(T, A, N->limbs, B, N->limbs);
Minos Galanakis7b109322023-06-16 14:28:36 +0100146
147 /* Optimised Reduction */
Minos Galanakis2ed8fb72023-06-14 16:01:47 +0100148 (*N->rep.ored.modp)(T, T_limbs);
Minos Galanakis7b109322023-06-16 14:28:36 +0100149
Minos Galanakis8eb61042023-06-26 10:03:19 +0100150 /* Convert back to canonical representation */
Minos Galanakis2ed8fb72023-06-14 16:01:47 +0100151 mbedtls_mpi_mod_raw_fix_quasi_reduction(T, N);
152 memcpy(X, T, N->limbs * sizeof(mbedtls_mpi_uint));
153 break;
154 default:
155 break;
156 }
157
Gabor Mezei979d34c2022-12-07 16:02:33 +0100158}
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs)
Tom Cosgrove61292682022-12-08 09:44:10 +0000161{
162 /* mbedtls_mpi_mod_raw_inv_prime() needs a temporary for the exponent,
163 * which will be the same size as the modulus and input (AN_limbs),
164 * and additional space to pass to mbedtls_mpi_core_exp_mod(). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return AN_limbs +
166 mbedtls_mpi_core_exp_mod_working_limbs(AN_limbs, AN_limbs);
Tom Cosgrove61292682022-12-08 09:44:10 +0000167}
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X,
170 const mbedtls_mpi_uint *A,
171 const mbedtls_mpi_uint *N,
172 size_t AN_limbs,
173 const mbedtls_mpi_uint *RR,
174 mbedtls_mpi_uint *T)
Tom Cosgrove61292682022-12-08 09:44:10 +0000175{
176 /* Inversion by power: g^|G| = 1 => g^(-1) = g^(|G|-1), and
177 * |G| = N - 1, so we want
178 * g^(|G|-1) = g^(N - 2)
179 */
Tom Cosgrove5f099302022-12-09 10:58:15 +0000180
181 /* Use the first AN_limbs of T to hold N - 2 */
Tom Cosgrove61292682022-12-08 09:44:10 +0000182 mbedtls_mpi_uint *Nminus2 = T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 (void) mbedtls_mpi_core_sub_int(Nminus2, N, 2, AN_limbs);
Tom Cosgrove61292682022-12-08 09:44:10 +0000184
Tom Cosgrove5f099302022-12-09 10:58:15 +0000185 /* Rest of T is given to exp_mod for its working space */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_mpi_core_exp_mod(X,
187 A, N, AN_limbs, Nminus2, AN_limbs,
188 RR, T + AN_limbs);
Tom Cosgrove61292682022-12-08 09:44:10 +0000189}
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,
192 const mbedtls_mpi_uint *A,
193 const mbedtls_mpi_uint *B,
194 const mbedtls_mpi_mod_modulus *N)
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000195{
Werner Lewisd391b8c2022-11-08 15:53:47 +0000196 mbedtls_mpi_uint carry, borrow;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 carry = mbedtls_mpi_core_add(X, A, B, N->limbs);
198 borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
199 (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) (carry ^ borrow));
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000200}
Janos Follath5933f692022-11-02 14:35:17 +0000201
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100202int mbedtls_mpi_mod_raw_canonical_to_modulus_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_to_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
216int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
217 mbedtls_mpi_uint *X,
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 const mbedtls_mpi_mod_modulus *N)
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100219{
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 switch (N->int_rep) {
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100221 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return mbedtls_mpi_mod_raw_from_mont_rep(X, N);
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100223 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return 0;
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100225 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100227 }
228}
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,
231 mbedtls_mpi_uint min,
232 const mbedtls_mpi_mod_modulus *N,
233 int (*f_rng)(void *, unsigned char *, size_t),
234 void *p_rng)
Gilles Peskinea57cf982022-12-06 22:54:09 +0100235{
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 int ret = mbedtls_mpi_core_random(X, min, N->p, N->limbs, f_rng, p_rng);
237 if (ret != 0) {
238 return ret;
239 }
240 return mbedtls_mpi_mod_raw_canonical_to_modulus_rep(X, N);
Gilles Peskinea57cf982022-12-06 22:54:09 +0100241}
242
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530243int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X,
244 const mbedtls_mpi_mod_modulus *N)
Hanno Becker5ad4a932022-08-09 14:45:53 +0100245{
Minos Galanakisd9299c32022-11-01 16:19:07 +0000246 mbedtls_mpi_uint *T;
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530247 const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs(N->limbs);
Minos Galanakisd9299c32022-11-01 16:19:07 +0000248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if ((T = (mbedtls_mpi_uint *) mbedtls_calloc(t_limbs, ciL)) == NULL) {
250 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
251 }
Minos Galanakisd9299c32022-11-01 16:19:07 +0000252
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530253 mbedtls_mpi_core_to_mont_rep(X, X, N->p, N->limbs,
254 N->rep.mont.mm, N->rep.mont.rr, T);
Minos Galanakisd9299c32022-11-01 16:19:07 +0000255
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100256 mbedtls_zeroize_and_free(T, t_limbs * ciL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return 0;
Hanno Becker5ad4a932022-08-09 14:45:53 +0100258}
Janos Follath5933f692022-11-02 14:35:17 +0000259
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530260int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X,
261 const mbedtls_mpi_mod_modulus *N)
Hanno Becker5ad4a932022-08-09 14:45:53 +0100262{
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530263 const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs(N->limbs);
Minos Galanakisd9299c32022-11-01 16:19:07 +0000264 mbedtls_mpi_uint *T;
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if ((T = (mbedtls_mpi_uint *) mbedtls_calloc(t_limbs, ciL)) == NULL) {
267 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
268 }
Minos Galanakisd9299c32022-11-01 16:19:07 +0000269
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530270 mbedtls_mpi_core_from_mont_rep(X, X, N->p, N->limbs, N->rep.mont.mm, T);
Minos Galanakisd9299c32022-11-01 16:19:07 +0000271
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100272 mbedtls_zeroize_and_free(T, t_limbs * ciL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return 0;
Hanno Becker5ad4a932022-08-09 14:45:53 +0100274}
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,
277 const mbedtls_mpi_uint *A,
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530278 const mbedtls_mpi_mod_modulus *N)
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000279{
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530280 mbedtls_mpi_core_sub(X, N->p, A, N->limbs);
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000281
282 /* If A=0 initially, then X=N now. Detect this by
283 * subtracting N and catching the carry. */
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530284 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
285 (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) borrow);
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000286}
Janos Follath5933f692022-11-02 14:35:17 +0000287
Janos Follath0ded6312022-08-09 13:34:54 +0100288#endif /* MBEDTLS_BIGNUM_C */