blob: f66739df92360d2c798f3b2ca4ebf63ee533a74b [file] [log] [blame]
Janos Follath3ca07752022-08-09 11:45:47 +01001/*
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Core bignum functions
Janos Follath3ca07752022-08-09 11:45:47 +01003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Janos Follath3ca07752022-08-09 11:45:47 +01006 */
7
8#include "common.h"
9
10#if defined(MBEDTLS_BIGNUM_C)
11
12#include <string.h>
13
14#include "mbedtls/error.h"
15#include "mbedtls/platform_util.h"
Gabor Mezeie1d31c42022-09-12 16:25:24 +020016#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010017
Janos Follath3ca07752022-08-09 11:45:47 +010018#include "mbedtls/platform.h"
Janos Follath3ca07752022-08-09 11:45:47 +010019
20#include "bignum_core.h"
Tom Cosgrove958fd3d2022-08-24 11:08:51 +010021#include "bn_mul.h"
22#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010023
Dave Rodgman914347b2023-04-27 14:20:30 +010024size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
25{
26#if defined(__has_builtin)
Agathiyan Bragadeeshe55a1e12023-07-17 15:00:19 +010027#if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_clz)
28 #define core_clz __builtin_clz
29#elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_clzl)
30 #define core_clz __builtin_clzl
31#elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_clzll)
32 #define core_clz __builtin_clzll
Dave Rodgman914347b2023-04-27 14:20:30 +010033#endif
34#endif
Agathiyan Bragadeeshe55a1e12023-07-17 15:00:19 +010035#if defined(core_clz)
36 return (size_t) core_clz(a);
Agathiyan Bragadeesh271a9532023-07-12 11:15:17 +010037#else
Dave Rodgman914347b2023-04-27 14:20:30 +010038 size_t j;
39 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
40
41 for (j = 0; j < biL; j++) {
42 if (a & mask) {
43 break;
44 }
45
46 mask >>= 1;
47 }
48
49 return j;
Agathiyan Bragadeesh271a9532023-07-12 11:15:17 +010050#endif
Dave Rodgman914347b2023-04-27 14:20:30 +010051}
52
Gilles Peskine449bd832023-01-11 14:50:10 +010053size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +010054{
Dave Rodgman880a6b32023-04-20 09:50:31 +010055 int i;
56 size_t j;
Janos Follath3ca07752022-08-09 11:45:47 +010057
Dave Rodgman2e863ec2023-04-25 17:34:59 +010058 for (i = ((int) A_limbs) - 1; i >= 0; i--) {
59 if (A[i] != 0) {
60 j = biL - mbedtls_mpi_core_clz(A[i]);
61 return (i * biL) + j;
Gilles Peskine449bd832023-01-11 14:50:10 +010062 }
63 }
Janos Follath3ca07752022-08-09 11:45:47 +010064
Dave Rodgman880a6b32023-04-20 09:50:31 +010065 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +010066}
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010069{
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if (MBEDTLS_IS_BIG_ENDIAN) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +000071 /* Nothing to do on bigendian systems. */
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return a;
73 } else {
Dave Rodgman7e1e7be2023-09-05 18:12:33 +010074#if defined(MBEDTLS_HAVE_INT32)
Dave Rodgmanb7b8c092023-09-05 20:35:19 +010075 return (mbedtls_mpi_uint) MBEDTLS_BSWAP32(a);
Dave Rodgman7e1e7be2023-09-05 18:12:33 +010076#elif defined(MBEDTLS_HAVE_INT64)
Dave Rodgmanb7b8c092023-09-05 20:35:19 +010077 return (mbedtls_mpi_uint) MBEDTLS_BSWAP64(a);
Dave Rodgman7e1e7be2023-09-05 18:12:33 +010078#endif
Dave Rodgman6d23ff62022-11-28 14:38:53 +000079 }
Janos Follath3ca07752022-08-09 11:45:47 +010080}
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
83 size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +010084{
85 mbedtls_mpi_uint *cur_limb_left;
86 mbedtls_mpi_uint *cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (A_limbs == 0) {
Janos Follath3ca07752022-08-09 11:45:47 +010088 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010089 }
Janos Follath3ca07752022-08-09 11:45:47 +010090
91 /*
92 * Traverse limbs and
93 * - adapt byte-order in each limb
94 * - swap the limbs themselves.
95 * For that, simultaneously traverse the limbs from left to right
96 * and from right to left, as long as the left index is not bigger
97 * than the right index (it's not a problem if limbs is odd and the
98 * indices coincide in the last iteration).
99 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
Janos Follath3ca07752022-08-09 11:45:47 +0100101 cur_limb_left <= cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 cur_limb_left++, cur_limb_right--) {
Janos Follath3ca07752022-08-09 11:45:47 +0100103 mbedtls_mpi_uint tmp;
104 /* Note that if cur_limb_left == cur_limb_right,
105 * this code effectively swaps the bytes only once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 tmp = mpi_bigendian_to_host(*cur_limb_left);
107 *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
Janos Follath3ca07752022-08-09 11:45:47 +0100108 *cur_limb_right = tmp;
109 }
110}
111
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200112/* Whether min <= A, in constant time.
113 * A_limbs must be at least 1. */
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100114mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
115 const mbedtls_mpi_uint *A,
116 size_t A_limbs)
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200117{
118 /* min <= least significant limb? */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100119 mbedtls_ct_condition_t min_le_lsl = mbedtls_ct_uint_ge(A[0], min);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200120
Gilles Peskine6b7ce962022-12-15 15:04:33 +0100121 /* limbs other than the least significant one are all zero? */
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100122 mbedtls_ct_condition_t msll_mask = MBEDTLS_CT_FALSE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 for (size_t i = 1; i < A_limbs; i++) {
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100124 msll_mask = mbedtls_ct_bool_or(msll_mask, mbedtls_ct_bool(A[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 }
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200126
127 /* min <= A iff the lowest limb of A is >= min or the other limbs
128 * are not all zero. */
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100129 return mbedtls_ct_bool_or(msll_mask, min_le_lsl);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200130}
131
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100132mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
133 const mbedtls_mpi_uint *B,
134 size_t limbs)
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100135{
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100136 mbedtls_ct_condition_t ret = MBEDTLS_CT_FALSE, cond = MBEDTLS_CT_FALSE, done = MBEDTLS_CT_FALSE;
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100137
138 for (size_t i = limbs; i > 0; i--) {
139 /*
140 * If B[i - 1] < A[i - 1] then A < B is false and the result must
141 * remain 0.
142 *
143 * Again even if we can make a decision, we just mark the result and
144 * the fact that we are done and continue looping.
145 */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100146 cond = mbedtls_ct_uint_lt(B[i - 1], A[i - 1]);
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100147 done = mbedtls_ct_bool_or(done, cond);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100148
149 /*
150 * If A[i - 1] < B[i - 1] then A < B is true.
151 *
152 * Again even if we can make a decision, we just mark the result and
153 * the fact that we are done and continue looping.
154 */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100155 cond = mbedtls_ct_uint_lt(A[i - 1], B[i - 1]);
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100156 ret = mbedtls_ct_bool_or(ret, mbedtls_ct_bool_and(cond, mbedtls_ct_bool_not(done)));
157 done = mbedtls_ct_bool_or(done, cond);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100158 }
159
160 /*
161 * If all the limbs were equal, then the numbers are equal, A < B is false
162 * and leaving the result 0 is correct.
163 */
164
165 return ret;
Janos Follath3ca07752022-08-09 11:45:47 +0100166}
167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
169 const mbedtls_mpi_uint *A,
170 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100171 mbedtls_ct_condition_t assign)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200172{
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (X == A) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200174 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200176
Dave Rodgman3b25c402023-05-18 14:41:06 +0100177 /* This function is very performance-sensitive for RSA. For this reason
178 * we have the loop below, instead of calling mbedtls_ct_memcpy_if
179 * (this is more optimal since here we don't have to handle the case where
180 * we copy awkwardly sized data).
181 */
182 for (size_t i = 0; i < limbs; i++) {
183 X[i] = mbedtls_ct_mpi_uint_if(assign, A[i], X[i]);
184 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200185}
186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
188 mbedtls_mpi_uint *Y,
189 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100190 mbedtls_ct_condition_t swap)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200191{
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (X == Y) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200193 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 for (size_t i = 0; i < limbs; i++) {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200197 mbedtls_mpi_uint tmp = X[i];
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100198 X[i] = mbedtls_ct_mpi_uint_if(swap, Y[i], X[i]);
199 Y[i] = mbedtls_ct_mpi_uint_if(swap, tmp, Y[i]);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200200 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200201}
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
204 size_t X_limbs,
205 const unsigned char *input,
206 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100207{
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (X_limbs < limbs) {
211 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
212 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (X != NULL) {
215 memset(X, 0, X_limbs * ciL);
Janos Follath3ca07752022-08-09 11:45:47 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 for (size_t i = 0; i < input_length; i++) {
218 size_t offset = ((i % ciL) << 3);
219 X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
Janos Follathca5688e2022-08-19 12:05:28 +0100220 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200221 }
Janos Follath3ca07752022-08-09 11:45:47 +0100222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100224}
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
227 size_t X_limbs,
228 const unsigned char *input,
229 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100230{
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if (X_limbs < limbs) {
234 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
235 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100236
Janos Follathb7a88ec2022-08-19 12:24:40 +0100237 /* If X_limbs is 0, input_length must also be 0 (from previous test).
238 * Nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (X_limbs == 0) {
240 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100241 }
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 memset(X, 0, X_limbs * ciL);
Gabor Mezeic414ba32022-08-12 17:47:39 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 /* memcpy() with (NULL, 0) is undefined behaviour */
246 if (input_length != 0) {
247 size_t overhead = (X_limbs * ciL) - input_length;
248 unsigned char *Xp = (unsigned char *) X;
249 memcpy(Xp + overhead, input, input_length);
250 }
251
252 mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
253
254 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100255}
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
258 size_t A_limbs,
259 unsigned char *output,
260 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100261{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100262 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100263 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (stored_bytes < output_length) {
Janos Follath3ca07752022-08-09 11:45:47 +0100266 bytes_to_copy = stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 } else {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100268 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100269
Janos Follathaf3f39c2022-08-22 09:06:32 +0100270 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100271 * However A may fit if its leading bytes are zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
273 if (GET_BYTE(A, i) != 0) {
274 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
275 }
Janos Follath3ca07752022-08-09 11:45:47 +0100276 }
277 }
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 for (size_t i = 0; i < bytes_to_copy; i++) {
280 output[i] = GET_BYTE(A, i);
Janos Follath3ca07752022-08-09 11:45:47 +0100281 }
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (stored_bytes < output_length) {
284 /* Write trailing 0 bytes */
285 memset(output + stored_bytes, 0, output_length - stored_bytes);
286 }
287
288 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100289}
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
292 size_t X_limbs,
293 unsigned char *output,
294 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100295{
296 size_t stored_bytes;
297 size_t bytes_to_copy;
298 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100299
Janos Follathb7a88ec2022-08-19 12:24:40 +0100300 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (stored_bytes < output_length) {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100303 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100304 * null bytes and record the position at which to start
305 * writing the significant bytes. In this case, the execution
306 * trace of this function does not depend on the value of the
307 * number. */
308 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100309 p = output + output_length - stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 memset(output, 0, output_length - stored_bytes);
311 } else {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100312 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100313 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100314 bytes_to_copy = output_length;
315 p = output;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
317 if (GET_BYTE(X, i) != 0) {
318 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
319 }
Janos Follath3ca07752022-08-09 11:45:47 +0100320 }
321 }
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 for (size_t i = 0; i < bytes_to_copy; i++) {
324 p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
325 }
Janos Follath3ca07752022-08-09 11:45:47 +0100326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100328}
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
331 size_t count)
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200332{
333 size_t i, v0, v1;
334 mbedtls_mpi_uint r0 = 0, r1;
335
336 v0 = count / biL;
337 v1 = count & (biL - 1);
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (v0 > limbs || (v0 == limbs && v1 > 0)) {
340 memset(X, 0, limbs * ciL);
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200341 return;
342 }
343
344 /*
345 * shift by count / limb_size
346 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (v0 > 0) {
348 for (i = 0; i < limbs - v0; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200349 X[i] = X[i + v0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 for (; i < limbs; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200353 X[i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200355 }
356
357 /*
358 * shift by count % limb_size
359 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (v1 > 0) {
361 for (i = limbs; i > 0; i--) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200362 r1 = X[i - 1] << (biL - v1);
363 X[i - 1] >>= v1;
364 X[i - 1] |= r0;
365 r0 = r1;
366 }
367 }
368}
369
Minos Galanakisec09e252023-04-20 14:22:16 +0100370void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
371 size_t count)
Minos Galanakisad808dd2023-04-20 12:18:41 +0100372{
Minos Galanakisec09e252023-04-20 14:22:16 +0100373 size_t i, v0, v1;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100374 mbedtls_mpi_uint r0 = 0, r1;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100375
Minos Galanakisec09e252023-04-20 14:22:16 +0100376 v0 = count / (biL);
377 v1 = count & (biL - 1);
Minos Galanakisad808dd2023-04-20 12:18:41 +0100378
Minos Galanakisad808dd2023-04-20 12:18:41 +0100379 /*
380 * shift by count / limb_size
381 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100382 if (v0 > 0) {
383 for (i = limbs; i > v0; i--) {
384 X[i - 1] = X[i - v0 - 1];
385 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100386
Minos Galanakisec09e252023-04-20 14:22:16 +0100387 for (; i > 0; i--) {
388 X[i - 1] = 0;
389 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100390 }
391
392 /*
393 * shift by count % limb_size
394 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100395 if (v1 > 0) {
396 for (i = v0; i < limbs; i++) {
397 r1 = X[i] >> (biL - v1);
398 X[i] <<= v1;
399 X[i] |= r0;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100400 r0 = r1;
401 }
402 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100403}
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
406 const mbedtls_mpi_uint *A,
407 const mbedtls_mpi_uint *B,
408 size_t limbs)
Hanno Beckerc9887132022-08-24 12:54:36 +0100409{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100410 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 for (size_t i = 0; i < limbs; i++) {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100413 mbedtls_mpi_uint t = c + A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 c = (t < A[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100415 t += B[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 c += (t < B[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100417 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100418 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return c;
Hanno Beckerc9887132022-08-24 12:54:36 +0100421}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
424 const mbedtls_mpi_uint *A,
425 size_t limbs,
426 unsigned cond)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100427{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100428 mbedtls_mpi_uint c = 0;
429
Dave Rodgmanb59b73e2023-05-17 15:17:12 +0100430 mbedtls_ct_condition_t do_add = mbedtls_ct_bool(cond);
Tom Cosgrove93549902022-08-30 17:41:23 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 for (size_t i = 0; i < limbs; i++) {
Dave Rodgman98ddc012023-08-10 12:11:31 +0100433 mbedtls_mpi_uint add = mbedtls_ct_mpi_uint_if_else_0(do_add, A[i]);
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100434 mbedtls_mpi_uint t = c + X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 c = (t < X[i]);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100436 t += add;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 c += (t < add);
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100438 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100439 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100442}
443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
445 const mbedtls_mpi_uint *A,
446 const mbedtls_mpi_uint *B,
447 size_t limbs)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100448{
449 mbedtls_mpi_uint c = 0;
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 for (size_t i = 0; i < limbs; i++) {
452 mbedtls_mpi_uint z = (A[i] < c);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100453 mbedtls_mpi_uint t = A[i] - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 c = (t < B[i]) + z;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100455 X[i] = t - B[i];
456 }
457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100459}
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
462 const mbedtls_mpi_uint *s, size_t s_len,
463 mbedtls_mpi_uint b)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100464{
465 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100466 /*
467 * It is a documented precondition of this function that d_len >= s_len.
468 * If that's not the case, we swap these round: this turns what would be
469 * a buffer overflow into an incorrect result.
470 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (d_len < s_len) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100472 s_len = d_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100474 size_t excess_len = d_len - s_len;
475 size_t steps_x8 = s_len / 8;
476 size_t steps_x1 = s_len & 7;
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 while (steps_x8--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100479 MULADDC_X8_INIT
480 MULADDC_X8_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 MULADDC_X8_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100482 }
483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 while (steps_x1--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100485 MULADDC_X1_INIT
486 MULADDC_X1_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 MULADDC_X1_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 while (excess_len--) {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100491 *d += c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 c = (*d < c);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100493 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100494 }
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100497}
498
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100499void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
500 const mbedtls_mpi_uint *A, size_t A_limbs,
501 const mbedtls_mpi_uint *B, size_t B_limbs)
Hanno Becker4ae890b2022-08-24 16:17:52 +0100502{
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100503 memset(X, 0, (A_limbs + B_limbs) * ciL);
504
505 for (size_t i = 0; i < B_limbs; i++) {
506 (void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]);
507 }
Hanno Becker4ae890b2022-08-24 16:17:52 +0100508}
509
Tom Cosgroveb4964862022-08-30 11:57:22 +0100510/*
511 * Fast Montgomery initialization (thanks to Tom St Denis).
512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100514{
515 mbedtls_mpi_uint x = N[0];
516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 x += ((N[0] + 2) & 4) << 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 for (unsigned int i = biL; i >= 8; i /= 2) {
520 x *= (2 - (N[0] * x));
521 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return ~x + 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100524}
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
527 const mbedtls_mpi_uint *A,
528 const mbedtls_mpi_uint *B,
529 size_t B_limbs,
530 const mbedtls_mpi_uint *N,
531 size_t AN_limbs,
532 mbedtls_mpi_uint mm,
533 mbedtls_mpi_uint *T)
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100534{
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 memset(T, 0, (2 * AN_limbs + 1) * ciL);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 for (size_t i = 0; i < AN_limbs; i++) {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100538 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100539 mbedtls_mpi_uint u0 = A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
543 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
Tom Cosgrove67c92472022-09-02 13:28:59 +0100544
545 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100546 }
547
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100548 /*
549 * The result we want is (T >= N) ? T - N : T.
550 *
551 * For better constant-time properties in this function, we always do the
552 * subtraction, with the result in X.
553 *
554 * We also look to see if there was any carry in the final additions in the
555 * loop above.
556 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100557
Tom Cosgrove72594632022-08-24 11:51:58 +0100558 mbedtls_mpi_uint carry = T[AN_limbs];
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100560
561 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100562 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100563 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100564 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100565 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100566 * 1) T < N : (carry, borrow) = (0, 1): we want T
567 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
568 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100569 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100570 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100571 *
572 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100573 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100574 */
Dave Rodgman231a5162023-05-17 15:13:14 +0100575 mbedtls_ct_memcpy_if(mbedtls_ct_bool(carry ^ borrow),
576 (unsigned char *) X,
577 (unsigned char *) T,
578 NULL,
579 AN_limbs * sizeof(mbedtls_mpi_uint));
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100580}
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
583 const mbedtls_mpi *N)
Hanno Beckerec440f22022-08-11 17:29:32 +0100584{
585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
588 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
589 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
590 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
Hanno Beckerec440f22022-08-11 17:29:32 +0100591
592cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return ret;
Hanno Beckerec440f22022-08-11 17:29:32 +0100594}
595
Janos Follath59cbd1b2022-10-28 18:13:43 +0100596MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100597void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
598 const mbedtls_mpi_uint *table,
599 size_t limbs,
600 size_t count,
601 size_t index)
Janos Follathe50f2f12022-10-26 15:14:33 +0100602{
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 for (size_t i = 0; i < count; i++, table += limbs) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100604 mbedtls_ct_condition_t assign = mbedtls_ct_uint_eq(i, index);
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
Janos Follathe50f2f12022-10-26 15:14:33 +0100606 }
607}
608
Gilles Peskine009d1952022-09-09 21:00:00 +0200609/* Fill X with n_bytes random bytes.
610 * X must already have room for those bytes.
611 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200612 * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
613 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200614 */
615int mbedtls_mpi_core_fill_random(
616 mbedtls_mpi_uint *X, size_t X_limbs,
617 size_t n_bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Gilles Peskine009d1952022-09-09 21:00:00 +0200619{
620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 const size_t limbs = CHARS_TO_LIMBS(n_bytes);
622 const size_t overhead = (limbs * ciL) - n_bytes;
Gilles Peskine009d1952022-09-09 21:00:00 +0200623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (X_limbs < limbs) {
625 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
626 }
Gilles Peskine009d1952022-09-09 21:00:00 +0200627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 memset(X, 0, overhead);
629 memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
630 MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
631 mbedtls_mpi_core_bigendian_to_host(X, limbs);
Gilles Peskine009d1952022-09-09 21:00:00 +0200632
633cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 return ret;
Gilles Peskine009d1952022-09-09 21:00:00 +0200635}
636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
638 mbedtls_mpi_uint min,
639 const mbedtls_mpi_uint *N,
640 size_t limbs,
641 int (*f_rng)(void *, unsigned char *, size_t),
642 void *p_rng)
Gilles Peskine70375b22022-09-21 15:47:23 +0200643{
Dave Rodgmanfd492ab2023-05-17 15:17:29 +0100644 mbedtls_ct_condition_t ge_lower = MBEDTLS_CT_TRUE, lt_upper = MBEDTLS_CT_FALSE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
646 size_t n_bytes = (n_bits + 7) / 8;
Gilles Peskine70375b22022-09-21 15:47:23 +0200647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
648
649 /*
650 * When min == 0, each try has at worst a probability 1/2 of failing
651 * (the msb has a probability 1/2 of being 0, and then the result will
652 * be < N), so after 30 tries failure probability is a most 2**(-30).
653 *
654 * When N is just below a power of 2, as is the case when generating
655 * a random scalar on most elliptic curves, 1 try is enough with
656 * overwhelming probability. When N is just above a power of 2,
657 * as when generating a random scalar on secp224k1, each try has
658 * a probability of failing that is almost 1/2.
659 *
660 * The probabilities are almost the same if min is nonzero but negligible
661 * compared to N. This is always the case when N is crypto-sized, but
662 * it's convenient to support small N for testing purposes. When N
663 * is small, use a higher repeat count, otherwise the probability of
664 * failure is macroscopic.
665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 int count = (n_bytes > 4 ? 30 : 250);
Gilles Peskine70375b22022-09-21 15:47:23 +0200667
668 /*
669 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
670 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
671 * - use the same byte ordering;
672 * - keep the leftmost n_bits bits of the generated octet string;
673 * - try until result is in the desired range.
674 * This also avoids any bias, which is especially important for ECDSA.
675 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 do {
677 MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
678 n_bytes,
679 f_rng, p_rng));
680 mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
Gilles Peskine70375b22022-09-21 15:47:23 +0200681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (--count == 0) {
Gilles Peskine70375b22022-09-21 15:47:23 +0200683 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
684 goto cleanup;
685 }
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
688 lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
Dave Rodgmanfd492ab2023-05-17 15:17:29 +0100689 } while (mbedtls_ct_bool_and(ge_lower, lt_upper) == MBEDTLS_CT_FALSE);
Gilles Peskine70375b22022-09-21 15:47:23 +0200690
691cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return ret;
Gilles Peskine70375b22022-09-21 15:47:23 +0200693}
694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695static size_t exp_mod_get_window_size(size_t Ebits)
Janos Follathb6673f02022-09-30 14:13:14 +0100696{
Dave Rodgman4883f102023-08-09 20:17:40 +0100697#if MBEDTLS_MPI_WINDOW_SIZE >= 6
698 return (Ebits > 671) ? 6 : (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1;
699#elif MBEDTLS_MPI_WINDOW_SIZE == 5
700 return (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1;
701#elif MBEDTLS_MPI_WINDOW_SIZE > 1
702 return (Ebits > 79) ? MBEDTLS_MPI_WINDOW_SIZE : 1;
703#else
704 (void) Ebits;
705 return 1;
Janos Follathb6673f02022-09-30 14:13:14 +0100706#endif
Janos Follathb6673f02022-09-30 14:13:14 +0100707}
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000710{
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
712 const size_t welem = ((size_t) 1) << wsize;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000713
714 /* How big does each part of the working memory pool need to be? */
715 const size_t table_limbs = welem * AN_limbs;
716 const size_t select_limbs = AN_limbs;
717 const size_t temp_limbs = 2 * AN_limbs + 1;
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return table_limbs + select_limbs + temp_limbs;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000720}
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
723 const mbedtls_mpi_uint *N,
724 size_t AN_limbs,
725 mbedtls_mpi_uint mm,
726 const mbedtls_mpi_uint *RR,
727 size_t welem,
728 mbedtls_mpi_uint *Wtable,
729 mbedtls_mpi_uint *temp)
Gilles Peskine0de0a042022-11-16 20:12:49 +0100730{
Gilles Peskine0de0a042022-11-16 20:12:49 +0100731 /* W[0] = 1 (in Montgomery presentation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 memset(Wtable, 0, AN_limbs * ciL);
Gilles Peskine0de0a042022-11-16 20:12:49 +0100733 Wtable[0] = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100735
Tom Cosgroveecda1862022-12-06 10:46:30 +0000736 /* W[1] = A (already in Montgomery presentation) */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100737 mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 memcpy(W1, A, AN_limbs * ciL);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100739
Gilles Peskine0de0a042022-11-16 20:12:49 +0100740 /* W[i+1] = W[i] * W[1], i >= 2 */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100741 mbedtls_mpi_uint *Wprev = W1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 for (size_t i = 2; i < welem; i++) {
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100743 mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100745 Wprev = Wcur;
746 }
Gilles Peskine0de0a042022-11-16 20:12:49 +0100747}
748
Gilles Peskine7d89d352022-11-16 22:54:14 +0100749/* Exponentiation: X := A^E mod N.
750 *
Tom Cosgroveecda1862022-12-06 10:46:30 +0000751 * A must already be in Montgomery form.
752 *
Gilles Peskine7d89d352022-11-16 22:54:14 +0100753 * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
754 *
755 * RR must contain 2^{2*biL} mod N.
Janos Follath3321b582022-11-22 21:08:33 +0000756 *
757 * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
758 * (The difference is that the body in our loop processes a single bit instead
759 * of a full window.)
Gilles Peskine7d89d352022-11-16 22:54:14 +0100760 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
762 const mbedtls_mpi_uint *A,
763 const mbedtls_mpi_uint *N,
764 size_t AN_limbs,
765 const mbedtls_mpi_uint *E,
766 size_t E_limbs,
767 const mbedtls_mpi_uint *RR,
768 mbedtls_mpi_uint *T)
Janos Follathb6673f02022-09-30 14:13:14 +0100769{
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
771 const size_t welem = ((size_t) 1) << wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100772
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000773 /* This is how we will use the temporary storage T, which must have space
774 * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
775 const size_t table_limbs = welem * AN_limbs;
776 const size_t select_limbs = AN_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100777
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000778 /* Pointers to specific parts of the temporary working memory pool */
779 mbedtls_mpi_uint *const Wtable = T;
780 mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
781 mbedtls_mpi_uint *const temp = Wselect + select_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100782
783 /*
784 * Window precomputation
785 */
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
Gilles Peskinecf979b02022-11-16 20:04:00 +0100788
Gilles Peskine0de0a042022-11-16 20:12:49 +0100789 /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 exp_mod_precompute_window(A, N, AN_limbs,
791 mm, RR,
792 welem, Wtable, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100793
794 /*
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000795 * Fixed window exponentiation
Janos Follathb6673f02022-09-30 14:13:14 +0100796 */
797
798 /* X = 1 (in Montgomery presentation) initially */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 memcpy(X, Wtable, AN_limbs * ciL);
Janos Follathb6673f02022-09-30 14:13:14 +0100800
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100801 /* We'll process the bits of E from most significant
802 * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
803 * (limb_index=0, E_bit_index=0). */
804 size_t E_limb_index = E_limbs;
805 size_t E_bit_index = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100806 /* At any given time, window contains window_bits bits from E.
807 * window_bits can go up to wsize. */
Janos Follathbad42c42022-11-09 14:30:44 +0000808 size_t window_bits = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100809 mbedtls_mpi_uint window = 0;
Gilles Peskinecf979b02022-11-16 20:04:00 +0100810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 do {
Janos Follathb6673f02022-09-30 14:13:14 +0100812 /* Square */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100814
Janos Follath3321b582022-11-22 21:08:33 +0000815 /* Move to the next bit of the exponent */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (E_bit_index == 0) {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100817 --E_limb_index;
818 E_bit_index = biL - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 } else {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100820 --E_bit_index;
821 }
Janos Follath3321b582022-11-22 21:08:33 +0000822 /* Insert next exponent bit into window */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100823 ++window_bits;
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100824 window <<= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 window |= (E[E_limb_index] >> E_bit_index) & 1;
Gilles Peskine3b63d092022-11-16 22:06:18 +0100826
827 /* Clear window if it's full. Also clear the window at the end,
828 * when we've finished processing the exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 if (window_bits == wsize ||
830 (E_bit_index == 0 && E_limb_index == 0)) {
Gilles Peskine0b270a52022-11-16 22:54:03 +0100831 /* Select Wtable[window] without leaking window through
832 * memory access patterns. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
834 AN_limbs, welem, window);
Gilles Peskine0b270a52022-11-16 22:54:03 +0100835 /* Multiply X by the selected element. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
837 temp);
Gilles Peskine3b63d092022-11-16 22:06:18 +0100838 window = 0;
839 window_bits = 0;
840 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 } while (!(E_bit_index == 0 && E_limb_index == 0));
Janos Follathb6673f02022-09-30 14:13:14 +0100842}
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
845 const mbedtls_mpi_uint *A,
846 mbedtls_mpi_uint c, /* doubles as carry */
847 size_t limbs)
Hanno Beckerd9b23482022-08-25 08:25:19 +0100848{
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100850 mbedtls_mpi_uint s = A[i];
851 mbedtls_mpi_uint t = s - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 c = (t > s);
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100853 X[i] = t;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100854 }
855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 return c;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100857}
858
Janos Follathaec1a862024-02-21 11:24:20 +0000859mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
860 size_t limbs)
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000861{
862 mbedtls_mpi_uint bits = 0;
863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000865 bits |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 }
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000867
Janos Follathaec1a862024-02-21 11:24:20 +0000868 return mbedtls_ct_bool(bits);
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000869}
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
872 const mbedtls_mpi_uint *A,
873 const mbedtls_mpi_uint *N,
874 size_t AN_limbs,
875 mbedtls_mpi_uint mm,
876 const mbedtls_mpi_uint *rr,
877 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000878{
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000880}
881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
883 const mbedtls_mpi_uint *A,
884 const mbedtls_mpi_uint *N,
885 size_t AN_limbs,
886 mbedtls_mpi_uint mm,
887 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000888{
889 const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000892}
893
Janos Follath3ca07752022-08-09 11:45:47 +0100894#endif /* MBEDTLS_BIGNUM_C */