blob: 75806cf24b8502a52d59c562948418f0bf5ec838 [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
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"
Gabor Mezeie1d31c42022-09-12 16:25:24 +020028#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010029
Janos Follath3ca07752022-08-09 11:45:47 +010030#include "mbedtls/platform.h"
Janos Follath3ca07752022-08-09 11:45:47 +010031
32#include "bignum_core.h"
Tom Cosgrove958fd3d2022-08-24 11:08:51 +010033#include "bn_mul.h"
34#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010035
Dave Rodgman914347b2023-04-27 14:20:30 +010036size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
37{
38#if defined(__has_builtin)
39#if __has_builtin(__builtin_clz)
40 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned int)) {
41 return (size_t) __builtin_clz(a);
42 }
43#endif
44#if __has_builtin(__builtin_clzl)
45 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long)) {
46 return (size_t) __builtin_clzl(a);
47 }
48#endif
49#if __has_builtin(__builtin_clzll)
50 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long long)) {
51 return (size_t) __builtin_clzll(a);
52 }
53#endif
54#endif
55 size_t j;
56 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
57
58 for (j = 0; j < biL; j++) {
59 if (a & mask) {
60 break;
61 }
62
63 mask >>= 1;
64 }
65
66 return j;
67}
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +010070{
Dave Rodgman880a6b32023-04-20 09:50:31 +010071 int i;
72 size_t j;
Janos Follath3ca07752022-08-09 11:45:47 +010073
Dave Rodgman2e863ec2023-04-25 17:34:59 +010074 for (i = ((int) A_limbs) - 1; i >= 0; i--) {
75 if (A[i] != 0) {
76 j = biL - mbedtls_mpi_core_clz(A[i]);
77 return (i * biL) + j;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 }
79 }
Janos Follath3ca07752022-08-09 11:45:47 +010080
Dave Rodgman880a6b32023-04-20 09:50:31 +010081 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +010082}
83
Janos Follath3ca07752022-08-09 11:45:47 +010084/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
85 * into the storage form used by mbedtls_mpi. */
Gilles Peskine449bd832023-01-11 14:50:10 +010086static mbedtls_mpi_uint mpi_bigendian_to_host_c(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010087{
88 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010089 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010090 mbedtls_mpi_uint tmp = 0;
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 for (i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++) {
Janos Follath3ca07752022-08-09 11:45:47 +010093 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010094 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010095 }
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return tmp;
Janos Follath3ca07752022-08-09 11:45:47 +010098}
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +0100101{
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (MBEDTLS_IS_BIG_ENDIAN) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000103 /* Nothing to do on bigendian systems. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return a;
105 } else {
106 switch (sizeof(mbedtls_mpi_uint)) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000107 case 4:
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return (mbedtls_mpi_uint) MBEDTLS_BSWAP32((uint32_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000109 case 8:
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 return (mbedtls_mpi_uint) MBEDTLS_BSWAP64((uint64_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000111 }
Janos Follath3ca07752022-08-09 11:45:47 +0100112
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000113 /* Fall back to C-based reordering if we don't know the byte order
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 * or we couldn't use a compiler-specific builtin. */
115 return mpi_bigendian_to_host_c(a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000116 }
Janos Follath3ca07752022-08-09 11:45:47 +0100117}
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
120 size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +0100121{
122 mbedtls_mpi_uint *cur_limb_left;
123 mbedtls_mpi_uint *cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (A_limbs == 0) {
Janos Follath3ca07752022-08-09 11:45:47 +0100125 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 }
Janos Follath3ca07752022-08-09 11:45:47 +0100127
128 /*
129 * Traverse limbs and
130 * - adapt byte-order in each limb
131 * - swap the limbs themselves.
132 * For that, simultaneously traverse the limbs from left to right
133 * and from right to left, as long as the left index is not bigger
134 * than the right index (it's not a problem if limbs is odd and the
135 * indices coincide in the last iteration).
136 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
Janos Follath3ca07752022-08-09 11:45:47 +0100138 cur_limb_left <= cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 cur_limb_left++, cur_limb_right--) {
Janos Follath3ca07752022-08-09 11:45:47 +0100140 mbedtls_mpi_uint tmp;
141 /* Note that if cur_limb_left == cur_limb_right,
142 * this code effectively swaps the bytes only once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 tmp = mpi_bigendian_to_host(*cur_limb_left);
144 *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
Janos Follath3ca07752022-08-09 11:45:47 +0100145 *cur_limb_right = tmp;
146 }
147}
148
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200149/* Whether min <= A, in constant time.
150 * A_limbs must be at least 1. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151unsigned mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
152 const mbedtls_mpi_uint *A,
153 size_t A_limbs)
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200154{
155 /* min <= least significant limb? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 unsigned min_le_lsl = 1 ^ mbedtls_ct_mpi_uint_lt(A[0], min);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200157
Gilles Peskine6b7ce962022-12-15 15:04:33 +0100158 /* limbs other than the least significant one are all zero? */
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200159 mbedtls_mpi_uint msll_mask = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 for (size_t i = 1; i < A_limbs; i++) {
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200161 msll_mask |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 }
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200163 /* The most significant limbs of A are not all zero iff msll_mask != 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 unsigned msll_nonzero = mbedtls_ct_mpi_uint_mask(msll_mask) & 1;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200165
166 /* min <= A iff the lowest limb of A is >= min or the other limbs
167 * are not all zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return min_le_lsl | msll_nonzero;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200169}
170
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100171unsigned mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
172 const mbedtls_mpi_uint *B,
173 size_t limbs)
174{
175 unsigned ret, cond, done;
176
177 /* The value of any of these variables is either 0 or 1 for the rest of
178 * their scope. */
179 ret = cond = done = 0;
180
181 for (size_t i = limbs; i > 0; i--) {
182 /*
183 * If B[i - 1] < A[i - 1] then A < B is false and the result must
184 * remain 0.
185 *
186 * Again even if we can make a decision, we just mark the result and
187 * the fact that we are done and continue looping.
188 */
189 cond = mbedtls_ct_mpi_uint_lt(B[i - 1], A[i - 1]);
190 done |= cond;
191
192 /*
193 * If A[i - 1] < B[i - 1] then A < B is true.
194 *
195 * Again even if we can make a decision, we just mark the result and
196 * the fact that we are done and continue looping.
197 */
198 cond = mbedtls_ct_mpi_uint_lt(A[i - 1], B[i - 1]);
199 ret |= cond & (1 - done);
200 done |= cond;
201 }
202
203 /*
204 * If all the limbs were equal, then the numbers are equal, A < B is false
205 * and leaving the result 0 is correct.
206 */
207
208 return ret;
209}
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
212 const mbedtls_mpi_uint *A,
213 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100214 mbedtls_ct_condition_t assign)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200215{
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (X == A) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200217 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200219
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100220 mbedtls_ct_memcpy_if(assign, (unsigned char *) X, (unsigned char *) A, NULL,
221 limbs * sizeof(mbedtls_mpi_uint));
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200222}
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
225 mbedtls_mpi_uint *Y,
226 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100227 mbedtls_ct_condition_t swap)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200228{
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (X == Y) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200230 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 for (size_t i = 0; i < limbs; i++) {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200234 mbedtls_mpi_uint tmp = X[i];
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100235 X[i] = mbedtls_ct_mpi_uint_if(swap, Y[i], X[i]);
236 Y[i] = mbedtls_ct_mpi_uint_if(swap, tmp, Y[i]);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200237 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200238}
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
241 size_t X_limbs,
242 const unsigned char *input,
243 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100244{
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (X_limbs < limbs) {
248 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
249 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (X != NULL) {
252 memset(X, 0, X_limbs * ciL);
Janos Follath3ca07752022-08-09 11:45:47 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 for (size_t i = 0; i < input_length; i++) {
255 size_t offset = ((i % ciL) << 3);
256 X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
Janos Follathca5688e2022-08-19 12:05:28 +0100257 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200258 }
Janos Follath3ca07752022-08-09 11:45:47 +0100259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100261}
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
264 size_t X_limbs,
265 const unsigned char *input,
266 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100267{
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (X_limbs < limbs) {
271 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
272 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100273
Janos Follathb7a88ec2022-08-19 12:24:40 +0100274 /* If X_limbs is 0, input_length must also be 0 (from previous test).
275 * Nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (X_limbs == 0) {
277 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100278 }
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 memset(X, 0, X_limbs * ciL);
Gabor Mezeic414ba32022-08-12 17:47:39 +0200281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 /* memcpy() with (NULL, 0) is undefined behaviour */
283 if (input_length != 0) {
284 size_t overhead = (X_limbs * ciL) - input_length;
285 unsigned char *Xp = (unsigned char *) X;
286 memcpy(Xp + overhead, input, input_length);
287 }
288
289 mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
290
291 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100292}
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
295 size_t A_limbs,
296 unsigned char *output,
297 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100298{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100299 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100300 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (stored_bytes < output_length) {
Janos Follath3ca07752022-08-09 11:45:47 +0100303 bytes_to_copy = stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 } else {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100305 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100306
Janos Follathaf3f39c2022-08-22 09:06:32 +0100307 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100308 * However A may fit if its leading bytes are zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
310 if (GET_BYTE(A, i) != 0) {
311 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
312 }
Janos Follath3ca07752022-08-09 11:45:47 +0100313 }
314 }
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 for (size_t i = 0; i < bytes_to_copy; i++) {
317 output[i] = GET_BYTE(A, i);
Janos Follath3ca07752022-08-09 11:45:47 +0100318 }
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (stored_bytes < output_length) {
321 /* Write trailing 0 bytes */
322 memset(output + stored_bytes, 0, output_length - stored_bytes);
323 }
324
325 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100326}
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
329 size_t X_limbs,
330 unsigned char *output,
331 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100332{
333 size_t stored_bytes;
334 size_t bytes_to_copy;
335 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100336
Janos Follathb7a88ec2022-08-19 12:24:40 +0100337 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (stored_bytes < output_length) {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100340 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100341 * null bytes and record the position at which to start
342 * writing the significant bytes. In this case, the execution
343 * trace of this function does not depend on the value of the
344 * number. */
345 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100346 p = output + output_length - stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 memset(output, 0, output_length - stored_bytes);
348 } else {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100349 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100350 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100351 bytes_to_copy = output_length;
352 p = output;
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
354 if (GET_BYTE(X, i) != 0) {
355 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
356 }
Janos Follath3ca07752022-08-09 11:45:47 +0100357 }
358 }
359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 for (size_t i = 0; i < bytes_to_copy; i++) {
361 p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
362 }
Janos Follath3ca07752022-08-09 11:45:47 +0100363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100365}
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
368 size_t count)
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200369{
370 size_t i, v0, v1;
371 mbedtls_mpi_uint r0 = 0, r1;
372
373 v0 = count / biL;
374 v1 = count & (biL - 1);
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (v0 > limbs || (v0 == limbs && v1 > 0)) {
377 memset(X, 0, limbs * ciL);
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200378 return;
379 }
380
381 /*
382 * shift by count / limb_size
383 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (v0 > 0) {
385 for (i = 0; i < limbs - v0; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200386 X[i] = X[i + v0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 for (; i < limbs; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200390 X[i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200392 }
393
394 /*
395 * shift by count % limb_size
396 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (v1 > 0) {
398 for (i = limbs; i > 0; i--) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200399 r1 = X[i - 1] << (biL - v1);
400 X[i - 1] >>= v1;
401 X[i - 1] |= r0;
402 r0 = r1;
403 }
404 }
405}
406
Minos Galanakisec09e252023-04-20 14:22:16 +0100407void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
408 size_t count)
Minos Galanakisad808dd2023-04-20 12:18:41 +0100409{
Minos Galanakisec09e252023-04-20 14:22:16 +0100410 size_t i, v0, v1;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100411 mbedtls_mpi_uint r0 = 0, r1;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100412
Minos Galanakisec09e252023-04-20 14:22:16 +0100413 v0 = count / (biL);
414 v1 = count & (biL - 1);
Minos Galanakisad808dd2023-04-20 12:18:41 +0100415
Minos Galanakisad808dd2023-04-20 12:18:41 +0100416 /*
417 * shift by count / limb_size
418 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100419 if (v0 > 0) {
420 for (i = limbs; i > v0; i--) {
421 X[i - 1] = X[i - v0 - 1];
422 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100423
Minos Galanakisec09e252023-04-20 14:22:16 +0100424 for (; i > 0; i--) {
425 X[i - 1] = 0;
426 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100427 }
428
429 /*
430 * shift by count % limb_size
431 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100432 if (v1 > 0) {
433 for (i = v0; i < limbs; i++) {
434 r1 = X[i] >> (biL - v1);
435 X[i] <<= v1;
436 X[i] |= r0;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100437 r0 = r1;
438 }
439 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100440}
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
443 const mbedtls_mpi_uint *A,
444 const mbedtls_mpi_uint *B,
445 size_t limbs)
Hanno Beckerc9887132022-08-24 12:54:36 +0100446{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100447 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 for (size_t i = 0; i < limbs; i++) {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100450 mbedtls_mpi_uint t = c + A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 c = (t < A[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100452 t += B[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 c += (t < B[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100454 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100455 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return c;
Hanno Beckerc9887132022-08-24 12:54:36 +0100458}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
461 const mbedtls_mpi_uint *A,
462 size_t limbs,
463 unsigned cond)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100464{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100465 mbedtls_mpi_uint c = 0;
466
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100467 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask(cond);
Tom Cosgrove93549902022-08-30 17:41:23 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100471 mbedtls_mpi_uint add = mask & A[i];
472 mbedtls_mpi_uint t = c + X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 c = (t < X[i]);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100474 t += add;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 c += (t < add);
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100476 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100477 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100480}
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
483 const mbedtls_mpi_uint *A,
484 const mbedtls_mpi_uint *B,
485 size_t limbs)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100486{
487 mbedtls_mpi_uint c = 0;
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 for (size_t i = 0; i < limbs; i++) {
490 mbedtls_mpi_uint z = (A[i] < c);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100491 mbedtls_mpi_uint t = A[i] - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 c = (t < B[i]) + z;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100493 X[i] = t - B[i];
494 }
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100497}
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
500 const mbedtls_mpi_uint *s, size_t s_len,
501 mbedtls_mpi_uint b)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100502{
503 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100504 /*
505 * It is a documented precondition of this function that d_len >= s_len.
506 * If that's not the case, we swap these round: this turns what would be
507 * a buffer overflow into an incorrect result.
508 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (d_len < s_len) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100510 s_len = d_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100512 size_t excess_len = d_len - s_len;
513 size_t steps_x8 = s_len / 8;
514 size_t steps_x1 = s_len & 7;
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 while (steps_x8--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100517 MULADDC_X8_INIT
518 MULADDC_X8_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 MULADDC_X8_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 while (steps_x1--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100523 MULADDC_X1_INIT
524 MULADDC_X1_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 MULADDC_X1_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100526 }
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 while (excess_len--) {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100529 *d += c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 c = (*d < c);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100531 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100532 }
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100535}
536
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100537void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
538 const mbedtls_mpi_uint *A, size_t A_limbs,
539 const mbedtls_mpi_uint *B, size_t B_limbs)
Hanno Becker4ae890b2022-08-24 16:17:52 +0100540{
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100541 memset(X, 0, (A_limbs + B_limbs) * ciL);
542
543 for (size_t i = 0; i < B_limbs; i++) {
544 (void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]);
545 }
Hanno Becker4ae890b2022-08-24 16:17:52 +0100546}
547
Tom Cosgroveb4964862022-08-30 11:57:22 +0100548/*
549 * Fast Montgomery initialization (thanks to Tom St Denis).
550 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100551mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100552{
553 mbedtls_mpi_uint x = N[0];
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 x += ((N[0] + 2) & 4) << 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 for (unsigned int i = biL; i >= 8; i /= 2) {
558 x *= (2 - (N[0] * x));
559 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return ~x + 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100562}
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
565 const mbedtls_mpi_uint *A,
566 const mbedtls_mpi_uint *B,
567 size_t B_limbs,
568 const mbedtls_mpi_uint *N,
569 size_t AN_limbs,
570 mbedtls_mpi_uint mm,
571 mbedtls_mpi_uint *T)
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100572{
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 memset(T, 0, (2 * AN_limbs + 1) * ciL);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 for (size_t i = 0; i < AN_limbs; i++) {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100576 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100577 mbedtls_mpi_uint u0 = A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
581 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
Tom Cosgrove67c92472022-09-02 13:28:59 +0100582
583 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100584 }
585
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100586 /*
587 * The result we want is (T >= N) ? T - N : T.
588 *
589 * For better constant-time properties in this function, we always do the
590 * subtraction, with the result in X.
591 *
592 * We also look to see if there was any carry in the final additions in the
593 * loop above.
594 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100595
Tom Cosgrove72594632022-08-24 11:51:58 +0100596 mbedtls_mpi_uint carry = T[AN_limbs];
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100598
599 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100600 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100601 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100602 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100603 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100604 * 1) T < N : (carry, borrow) = (0, 1): we want T
605 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
606 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100607 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100608 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100609 *
610 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100611 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100612 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 mbedtls_ct_mpi_uint_cond_assign(AN_limbs, X, T, (unsigned char) (carry ^ borrow));
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100614}
615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
617 const mbedtls_mpi *N)
Hanno Beckerec440f22022-08-11 17:29:32 +0100618{
619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
622 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
623 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
624 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
Hanno Beckerec440f22022-08-11 17:29:32 +0100625
626cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return ret;
Hanno Beckerec440f22022-08-11 17:29:32 +0100628}
629
Janos Follath59cbd1b2022-10-28 18:13:43 +0100630MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100631void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
632 const mbedtls_mpi_uint *table,
633 size_t limbs,
634 size_t count,
635 size_t index)
Janos Follathe50f2f12022-10-26 15:14:33 +0100636{
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 for (size_t i = 0; i < count; i++, table += limbs) {
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100638 mbedtls_ct_condition_t assign = mbedtls_ct_bool_eq(i, index);
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
Janos Follathe50f2f12022-10-26 15:14:33 +0100640 }
641}
642
Gilles Peskine009d1952022-09-09 21:00:00 +0200643/* Fill X with n_bytes random bytes.
644 * X must already have room for those bytes.
645 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200646 * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
647 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200648 */
649int mbedtls_mpi_core_fill_random(
650 mbedtls_mpi_uint *X, size_t X_limbs,
651 size_t n_bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Gilles Peskine009d1952022-09-09 21:00:00 +0200653{
654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 const size_t limbs = CHARS_TO_LIMBS(n_bytes);
656 const size_t overhead = (limbs * ciL) - n_bytes;
Gilles Peskine009d1952022-09-09 21:00:00 +0200657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (X_limbs < limbs) {
659 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
660 }
Gilles Peskine009d1952022-09-09 21:00:00 +0200661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 memset(X, 0, overhead);
663 memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
664 MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
665 mbedtls_mpi_core_bigendian_to_host(X, limbs);
Gilles Peskine009d1952022-09-09 21:00:00 +0200666
667cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 return ret;
Gilles Peskine009d1952022-09-09 21:00:00 +0200669}
670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
672 mbedtls_mpi_uint min,
673 const mbedtls_mpi_uint *N,
674 size_t limbs,
675 int (*f_rng)(void *, unsigned char *, size_t),
676 void *p_rng)
Gilles Peskine70375b22022-09-21 15:47:23 +0200677{
678 unsigned ge_lower = 1, lt_upper = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
680 size_t n_bytes = (n_bits + 7) / 8;
Gilles Peskine70375b22022-09-21 15:47:23 +0200681 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
682
683 /*
684 * When min == 0, each try has at worst a probability 1/2 of failing
685 * (the msb has a probability 1/2 of being 0, and then the result will
686 * be < N), so after 30 tries failure probability is a most 2**(-30).
687 *
688 * When N is just below a power of 2, as is the case when generating
689 * a random scalar on most elliptic curves, 1 try is enough with
690 * overwhelming probability. When N is just above a power of 2,
691 * as when generating a random scalar on secp224k1, each try has
692 * a probability of failing that is almost 1/2.
693 *
694 * The probabilities are almost the same if min is nonzero but negligible
695 * compared to N. This is always the case when N is crypto-sized, but
696 * it's convenient to support small N for testing purposes. When N
697 * is small, use a higher repeat count, otherwise the probability of
698 * failure is macroscopic.
699 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 int count = (n_bytes > 4 ? 30 : 250);
Gilles Peskine70375b22022-09-21 15:47:23 +0200701
702 /*
703 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
704 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
705 * - use the same byte ordering;
706 * - keep the leftmost n_bits bits of the generated octet string;
707 * - try until result is in the desired range.
708 * This also avoids any bias, which is especially important for ECDSA.
709 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 do {
711 MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
712 n_bytes,
713 f_rng, p_rng));
714 mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
Gilles Peskine70375b22022-09-21 15:47:23 +0200715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (--count == 0) {
Gilles Peskine70375b22022-09-21 15:47:23 +0200717 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
718 goto cleanup;
719 }
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
722 lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
723 } while (ge_lower == 0 || lt_upper == 0);
Gilles Peskine70375b22022-09-21 15:47:23 +0200724
725cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 return ret;
Gilles Peskine70375b22022-09-21 15:47:23 +0200727}
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729static size_t exp_mod_get_window_size(size_t Ebits)
Janos Follathb6673f02022-09-30 14:13:14 +0100730{
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 size_t wsize = (Ebits > 671) ? 6 : (Ebits > 239) ? 5 :
732 (Ebits > 79) ? 4 : 1;
Janos Follathb6673f02022-09-30 14:13:14 +0100733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
735 if (wsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follathb6673f02022-09-30 14:13:14 +0100736 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 }
Janos Follathb6673f02022-09-30 14:13:14 +0100738#endif
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 return wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100741}
742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000744{
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
746 const size_t welem = ((size_t) 1) << wsize;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000747
748 /* How big does each part of the working memory pool need to be? */
749 const size_t table_limbs = welem * AN_limbs;
750 const size_t select_limbs = AN_limbs;
751 const size_t temp_limbs = 2 * AN_limbs + 1;
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 return table_limbs + select_limbs + temp_limbs;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000754}
755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
757 const mbedtls_mpi_uint *N,
758 size_t AN_limbs,
759 mbedtls_mpi_uint mm,
760 const mbedtls_mpi_uint *RR,
761 size_t welem,
762 mbedtls_mpi_uint *Wtable,
763 mbedtls_mpi_uint *temp)
Gilles Peskine0de0a042022-11-16 20:12:49 +0100764{
Gilles Peskine0de0a042022-11-16 20:12:49 +0100765 /* W[0] = 1 (in Montgomery presentation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 memset(Wtable, 0, AN_limbs * ciL);
Gilles Peskine0de0a042022-11-16 20:12:49 +0100767 Wtable[0] = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100769
Tom Cosgroveecda1862022-12-06 10:46:30 +0000770 /* W[1] = A (already in Montgomery presentation) */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100771 mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 memcpy(W1, A, AN_limbs * ciL);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100773
Gilles Peskine0de0a042022-11-16 20:12:49 +0100774 /* W[i+1] = W[i] * W[1], i >= 2 */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100775 mbedtls_mpi_uint *Wprev = W1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 for (size_t i = 2; i < welem; i++) {
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100777 mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100779 Wprev = Wcur;
780 }
Gilles Peskine0de0a042022-11-16 20:12:49 +0100781}
782
Gilles Peskine7d89d352022-11-16 22:54:14 +0100783/* Exponentiation: X := A^E mod N.
784 *
Tom Cosgroveecda1862022-12-06 10:46:30 +0000785 * A must already be in Montgomery form.
786 *
Gilles Peskine7d89d352022-11-16 22:54:14 +0100787 * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
788 *
789 * RR must contain 2^{2*biL} mod N.
Janos Follath3321b582022-11-22 21:08:33 +0000790 *
791 * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
792 * (The difference is that the body in our loop processes a single bit instead
793 * of a full window.)
Gilles Peskine7d89d352022-11-16 22:54:14 +0100794 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
796 const mbedtls_mpi_uint *A,
797 const mbedtls_mpi_uint *N,
798 size_t AN_limbs,
799 const mbedtls_mpi_uint *E,
800 size_t E_limbs,
801 const mbedtls_mpi_uint *RR,
802 mbedtls_mpi_uint *T)
Janos Follathb6673f02022-09-30 14:13:14 +0100803{
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
805 const size_t welem = ((size_t) 1) << wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100806
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000807 /* This is how we will use the temporary storage T, which must have space
808 * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
809 const size_t table_limbs = welem * AN_limbs;
810 const size_t select_limbs = AN_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100811
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000812 /* Pointers to specific parts of the temporary working memory pool */
813 mbedtls_mpi_uint *const Wtable = T;
814 mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
815 mbedtls_mpi_uint *const temp = Wselect + select_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100816
817 /*
818 * Window precomputation
819 */
820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
Gilles Peskinecf979b02022-11-16 20:04:00 +0100822
Gilles Peskine0de0a042022-11-16 20:12:49 +0100823 /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 exp_mod_precompute_window(A, N, AN_limbs,
825 mm, RR,
826 welem, Wtable, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100827
828 /*
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000829 * Fixed window exponentiation
Janos Follathb6673f02022-09-30 14:13:14 +0100830 */
831
832 /* X = 1 (in Montgomery presentation) initially */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 memcpy(X, Wtable, AN_limbs * ciL);
Janos Follathb6673f02022-09-30 14:13:14 +0100834
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100835 /* We'll process the bits of E from most significant
836 * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
837 * (limb_index=0, E_bit_index=0). */
838 size_t E_limb_index = E_limbs;
839 size_t E_bit_index = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100840 /* At any given time, window contains window_bits bits from E.
841 * window_bits can go up to wsize. */
Janos Follathbad42c42022-11-09 14:30:44 +0000842 size_t window_bits = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100843 mbedtls_mpi_uint window = 0;
Gilles Peskinecf979b02022-11-16 20:04:00 +0100844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 do {
Janos Follathb6673f02022-09-30 14:13:14 +0100846 /* Square */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100848
Janos Follath3321b582022-11-22 21:08:33 +0000849 /* Move to the next bit of the exponent */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (E_bit_index == 0) {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100851 --E_limb_index;
852 E_bit_index = biL - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 } else {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100854 --E_bit_index;
855 }
Janos Follath3321b582022-11-22 21:08:33 +0000856 /* Insert next exponent bit into window */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100857 ++window_bits;
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100858 window <<= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 window |= (E[E_limb_index] >> E_bit_index) & 1;
Gilles Peskine3b63d092022-11-16 22:06:18 +0100860
861 /* Clear window if it's full. Also clear the window at the end,
862 * when we've finished processing the exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (window_bits == wsize ||
864 (E_bit_index == 0 && E_limb_index == 0)) {
Gilles Peskine0b270a52022-11-16 22:54:03 +0100865 /* Select Wtable[window] without leaking window through
866 * memory access patterns. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
868 AN_limbs, welem, window);
Gilles Peskine0b270a52022-11-16 22:54:03 +0100869 /* Multiply X by the selected element. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
871 temp);
Gilles Peskine3b63d092022-11-16 22:06:18 +0100872 window = 0;
873 window_bits = 0;
874 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 } while (!(E_bit_index == 0 && E_limb_index == 0));
Janos Follathb6673f02022-09-30 14:13:14 +0100876}
877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
879 const mbedtls_mpi_uint *A,
880 mbedtls_mpi_uint c, /* doubles as carry */
881 size_t limbs)
Hanno Beckerd9b23482022-08-25 08:25:19 +0100882{
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100884 mbedtls_mpi_uint s = A[i];
885 mbedtls_mpi_uint t = s - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 c = (t > s);
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100887 X[i] = t;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100888 }
889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 return c;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100891}
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
894 size_t limbs)
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000895{
896 mbedtls_mpi_uint bits = 0;
897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000899 bits |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 }
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 return bits;
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000903}
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
906 const mbedtls_mpi_uint *A,
907 const mbedtls_mpi_uint *N,
908 size_t AN_limbs,
909 mbedtls_mpi_uint mm,
910 const mbedtls_mpi_uint *rr,
911 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000912{
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000914}
915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
917 const mbedtls_mpi_uint *A,
918 const mbedtls_mpi_uint *N,
919 size_t AN_limbs,
920 mbedtls_mpi_uint mm,
921 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000922{
923 const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000926}
927
Janos Follath3ca07752022-08-09 11:45:47 +0100928#endif /* MBEDTLS_BIGNUM_C */