blob: 9b6c414eb4c91e258c06944a4683463c2e431479 [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
Gilles Peskine449bd832023-01-11 14:50:10 +010036size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010037{
Dave Rodgmanfe8a8cd2023-04-19 17:40:28 +010038#if defined(__has_builtin)
39#if __has_builtin(__builtin_clz)
40 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned int)) {
41 // __builtin_clz is undefined if a == 0
42 if (a == 0) {
43 return sizeof(mbedtls_mpi_uint) * 8;
44 } else {
45 return (size_t) __builtin_clz(a);
46 }
47 }
48#endif
49#if __has_builtin(__builtin_clzl)
50 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long)) {
51 if (a == 0) {
52 return sizeof(mbedtls_mpi_uint) * 8;
53 } else {
54 return (size_t) __builtin_clzl(a);
55 }
56 }
57#endif
58#if __has_builtin(__builtin_clzll)
59 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long long)) {
60 if (a == 0) {
61 return sizeof(mbedtls_mpi_uint) * 8;
62 } else {
63 return (size_t) __builtin_clzll(a);
64 }
65 }
66#endif
67#endif
Janos Follath3ca07752022-08-09 11:45:47 +010068 size_t j;
69 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071 for (j = 0; j < biL; j++) {
72 if (a & mask) {
73 break;
74 }
Janos Follath3ca07752022-08-09 11:45:47 +010075
76 mask >>= 1;
77 }
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return j;
Janos Follath3ca07752022-08-09 11:45:47 +010080}
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +010083{
84 size_t i, j;
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (A_limbs == 0) {
87 return 0;
88 }
Janos Follath3ca07752022-08-09 11:45:47 +010089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 for (i = A_limbs - 1; i > 0; i--) {
91 if (A[i] != 0) {
Janos Follath3ca07752022-08-09 11:45:47 +010092 break;
Gilles Peskine449bd832023-01-11 14:50:10 +010093 }
94 }
Janos Follath3ca07752022-08-09 11:45:47 +010095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 j = biL - mbedtls_mpi_core_clz(A[i]);
Janos Follath3ca07752022-08-09 11:45:47 +010097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return (i * biL) + j;
Janos Follath3ca07752022-08-09 11:45:47 +010099}
100
Janos Follath3ca07752022-08-09 11:45:47 +0100101/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
102 * into the storage form used by mbedtls_mpi. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103static mbedtls_mpi_uint mpi_bigendian_to_host_c(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +0100104{
105 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100106 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +0100107 mbedtls_mpi_uint tmp = 0;
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 for (i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++) {
Janos Follath3ca07752022-08-09 11:45:47 +0100110 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100111 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +0100112 }
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return tmp;
Janos Follath3ca07752022-08-09 11:45:47 +0100115}
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +0100118{
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (MBEDTLS_IS_BIG_ENDIAN) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000120 /* Nothing to do on bigendian systems. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return a;
122 } else {
123 switch (sizeof(mbedtls_mpi_uint)) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000124 case 4:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return (mbedtls_mpi_uint) MBEDTLS_BSWAP32((uint32_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000126 case 8:
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 return (mbedtls_mpi_uint) MBEDTLS_BSWAP64((uint64_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000128 }
Janos Follath3ca07752022-08-09 11:45:47 +0100129
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000130 /* Fall back to C-based reordering if we don't know the byte order
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 * or we couldn't use a compiler-specific builtin. */
132 return mpi_bigendian_to_host_c(a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000133 }
Janos Follath3ca07752022-08-09 11:45:47 +0100134}
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
137 size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +0100138{
139 mbedtls_mpi_uint *cur_limb_left;
140 mbedtls_mpi_uint *cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (A_limbs == 0) {
Janos Follath3ca07752022-08-09 11:45:47 +0100142 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 }
Janos Follath3ca07752022-08-09 11:45:47 +0100144
145 /*
146 * Traverse limbs and
147 * - adapt byte-order in each limb
148 * - swap the limbs themselves.
149 * For that, simultaneously traverse the limbs from left to right
150 * and from right to left, as long as the left index is not bigger
151 * than the right index (it's not a problem if limbs is odd and the
152 * indices coincide in the last iteration).
153 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
Janos Follath3ca07752022-08-09 11:45:47 +0100155 cur_limb_left <= cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 cur_limb_left++, cur_limb_right--) {
Janos Follath3ca07752022-08-09 11:45:47 +0100157 mbedtls_mpi_uint tmp;
158 /* Note that if cur_limb_left == cur_limb_right,
159 * this code effectively swaps the bytes only once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 tmp = mpi_bigendian_to_host(*cur_limb_left);
161 *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
Janos Follath3ca07752022-08-09 11:45:47 +0100162 *cur_limb_right = tmp;
163 }
164}
165
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200166/* Whether min <= A, in constant time.
167 * A_limbs must be at least 1. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168unsigned mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
169 const mbedtls_mpi_uint *A,
170 size_t A_limbs)
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200171{
172 /* min <= least significant limb? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 unsigned min_le_lsl = 1 ^ mbedtls_ct_mpi_uint_lt(A[0], min);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200174
Gilles Peskine6b7ce962022-12-15 15:04:33 +0100175 /* limbs other than the least significant one are all zero? */
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200176 mbedtls_mpi_uint msll_mask = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 for (size_t i = 1; i < A_limbs; i++) {
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200178 msll_mask |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 }
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200180 /* The most significant limbs of A are not all zero iff msll_mask != 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 unsigned msll_nonzero = mbedtls_ct_mpi_uint_mask(msll_mask) & 1;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200182
183 /* min <= A iff the lowest limb of A is >= min or the other limbs
184 * are not all zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return min_le_lsl | msll_nonzero;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200186}
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
189 const mbedtls_mpi_uint *A,
190 size_t limbs,
191 unsigned char assign)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200192{
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (X == A) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200194 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 mbedtls_ct_mpi_uint_cond_assign(limbs, X, A, assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200198}
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
201 mbedtls_mpi_uint *Y,
202 size_t limbs,
203 unsigned char swap)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200204{
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (X == Y) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200206 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200208
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200209 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask(swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 for (size_t i = 0; i < limbs; i++) {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200213 mbedtls_mpi_uint tmp = X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 X[i] = (X[i] & ~limb_mask) | (Y[i] & limb_mask);
215 Y[i] = (Y[i] & ~limb_mask) | (tmp & limb_mask);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200216 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200217}
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
220 size_t X_limbs,
221 const unsigned char *input,
222 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100223{
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (X_limbs < limbs) {
227 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
228 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (X != NULL) {
231 memset(X, 0, X_limbs * ciL);
Janos Follath3ca07752022-08-09 11:45:47 +0100232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 for (size_t i = 0; i < input_length; i++) {
234 size_t offset = ((i % ciL) << 3);
235 X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
Janos Follathca5688e2022-08-19 12:05:28 +0100236 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200237 }
Janos Follath3ca07752022-08-09 11:45:47 +0100238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100240}
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
243 size_t X_limbs,
244 const unsigned char *input,
245 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100246{
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (X_limbs < limbs) {
250 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
251 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100252
Janos Follathb7a88ec2022-08-19 12:24:40 +0100253 /* If X_limbs is 0, input_length must also be 0 (from previous test).
254 * Nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (X_limbs == 0) {
256 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100257 }
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 memset(X, 0, X_limbs * ciL);
Gabor Mezeic414ba32022-08-12 17:47:39 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 /* memcpy() with (NULL, 0) is undefined behaviour */
262 if (input_length != 0) {
263 size_t overhead = (X_limbs * ciL) - input_length;
264 unsigned char *Xp = (unsigned char *) X;
265 memcpy(Xp + overhead, input, input_length);
266 }
267
268 mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
269
270 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100271}
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
274 size_t A_limbs,
275 unsigned char *output,
276 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100277{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100278 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100279 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (stored_bytes < output_length) {
Janos Follath3ca07752022-08-09 11:45:47 +0100282 bytes_to_copy = stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 } else {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100284 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100285
Janos Follathaf3f39c2022-08-22 09:06:32 +0100286 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100287 * However A may fit if its leading bytes are zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
289 if (GET_BYTE(A, i) != 0) {
290 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
291 }
Janos Follath3ca07752022-08-09 11:45:47 +0100292 }
293 }
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 for (size_t i = 0; i < bytes_to_copy; i++) {
296 output[i] = GET_BYTE(A, i);
Janos Follath3ca07752022-08-09 11:45:47 +0100297 }
298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (stored_bytes < output_length) {
300 /* Write trailing 0 bytes */
301 memset(output + stored_bytes, 0, output_length - stored_bytes);
302 }
303
304 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100305}
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
308 size_t X_limbs,
309 unsigned char *output,
310 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100311{
312 size_t stored_bytes;
313 size_t bytes_to_copy;
314 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100315
Janos Follathb7a88ec2022-08-19 12:24:40 +0100316 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (stored_bytes < output_length) {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100319 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100320 * null bytes and record the position at which to start
321 * writing the significant bytes. In this case, the execution
322 * trace of this function does not depend on the value of the
323 * number. */
324 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100325 p = output + output_length - stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 memset(output, 0, output_length - stored_bytes);
327 } else {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100328 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100329 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100330 bytes_to_copy = output_length;
331 p = output;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
333 if (GET_BYTE(X, i) != 0) {
334 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
335 }
Janos Follath3ca07752022-08-09 11:45:47 +0100336 }
337 }
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 for (size_t i = 0; i < bytes_to_copy; i++) {
340 p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
341 }
Janos Follath3ca07752022-08-09 11:45:47 +0100342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100344}
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
347 size_t count)
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200348{
349 size_t i, v0, v1;
350 mbedtls_mpi_uint r0 = 0, r1;
351
352 v0 = count / biL;
353 v1 = count & (biL - 1);
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (v0 > limbs || (v0 == limbs && v1 > 0)) {
356 memset(X, 0, limbs * ciL);
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200357 return;
358 }
359
360 /*
361 * shift by count / limb_size
362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (v0 > 0) {
364 for (i = 0; i < limbs - v0; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200365 X[i] = X[i + v0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 for (; i < limbs; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200369 X[i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200371 }
372
373 /*
374 * shift by count % limb_size
375 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (v1 > 0) {
377 for (i = limbs; i > 0; i--) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200378 r1 = X[i - 1] << (biL - v1);
379 X[i - 1] >>= v1;
380 X[i - 1] |= r0;
381 r0 = r1;
382 }
383 }
384}
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
387 const mbedtls_mpi_uint *A,
388 const mbedtls_mpi_uint *B,
389 size_t limbs)
Hanno Beckerc9887132022-08-24 12:54:36 +0100390{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100391 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 for (size_t i = 0; i < limbs; i++) {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100394 mbedtls_mpi_uint t = c + A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 c = (t < A[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100396 t += B[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 c += (t < B[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100398 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100399 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return c;
Hanno Beckerc9887132022-08-24 12:54:36 +0100402}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
405 const mbedtls_mpi_uint *A,
406 size_t limbs,
407 unsigned cond)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100408{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100409 mbedtls_mpi_uint c = 0;
410
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100411 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask(cond);
Tom Cosgrove93549902022-08-30 17:41:23 +0100413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100415 mbedtls_mpi_uint add = mask & A[i];
416 mbedtls_mpi_uint t = c + X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 c = (t < X[i]);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100418 t += add;
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 c += (t < add);
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100420 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100421 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100424}
425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
427 const mbedtls_mpi_uint *A,
428 const mbedtls_mpi_uint *B,
429 size_t limbs)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100430{
431 mbedtls_mpi_uint c = 0;
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 for (size_t i = 0; i < limbs; i++) {
434 mbedtls_mpi_uint z = (A[i] < c);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100435 mbedtls_mpi_uint t = A[i] - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 c = (t < B[i]) + z;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100437 X[i] = t - B[i];
438 }
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100441}
442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
444 const mbedtls_mpi_uint *s, size_t s_len,
445 mbedtls_mpi_uint b)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100446{
447 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100448 /*
449 * It is a documented precondition of this function that d_len >= s_len.
450 * If that's not the case, we swap these round: this turns what would be
451 * a buffer overflow into an incorrect result.
452 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (d_len < s_len) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100454 s_len = d_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100456 size_t excess_len = d_len - s_len;
457 size_t steps_x8 = s_len / 8;
458 size_t steps_x1 = s_len & 7;
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 while (steps_x8--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100461 MULADDC_X8_INIT
462 MULADDC_X8_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 MULADDC_X8_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100464 }
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 while (steps_x1--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100467 MULADDC_X1_INIT
468 MULADDC_X1_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 MULADDC_X1_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100470 }
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 while (excess_len--) {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100473 *d += c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 c = (*d < c);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100475 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100476 }
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100479}
480
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100481void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
482 const mbedtls_mpi_uint *A, size_t A_limbs,
483 const mbedtls_mpi_uint *B, size_t B_limbs)
Hanno Becker4ae890b2022-08-24 16:17:52 +0100484{
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100485 memset(X, 0, (A_limbs + B_limbs) * ciL);
486
487 for (size_t i = 0; i < B_limbs; i++) {
488 (void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]);
489 }
Hanno Becker4ae890b2022-08-24 16:17:52 +0100490}
491
Tom Cosgroveb4964862022-08-30 11:57:22 +0100492/*
493 * Fast Montgomery initialization (thanks to Tom St Denis).
494 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100496{
497 mbedtls_mpi_uint x = N[0];
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 x += ((N[0] + 2) & 4) << 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 for (unsigned int i = biL; i >= 8; i /= 2) {
502 x *= (2 - (N[0] * x));
503 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return ~x + 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100506}
507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
509 const mbedtls_mpi_uint *A,
510 const mbedtls_mpi_uint *B,
511 size_t B_limbs,
512 const mbedtls_mpi_uint *N,
513 size_t AN_limbs,
514 mbedtls_mpi_uint mm,
515 mbedtls_mpi_uint *T)
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100516{
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 memset(T, 0, (2 * AN_limbs + 1) * ciL);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 for (size_t i = 0; i < AN_limbs; i++) {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100520 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100521 mbedtls_mpi_uint u0 = A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
525 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
Tom Cosgrove67c92472022-09-02 13:28:59 +0100526
527 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100528 }
529
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100530 /*
531 * The result we want is (T >= N) ? T - N : T.
532 *
533 * For better constant-time properties in this function, we always do the
534 * subtraction, with the result in X.
535 *
536 * We also look to see if there was any carry in the final additions in the
537 * loop above.
538 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100539
Tom Cosgrove72594632022-08-24 11:51:58 +0100540 mbedtls_mpi_uint carry = T[AN_limbs];
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100542
543 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100544 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100545 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100546 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100547 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100548 * 1) T < N : (carry, borrow) = (0, 1): we want T
549 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
550 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100551 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100552 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100553 *
554 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100555 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100556 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_ct_mpi_uint_cond_assign(AN_limbs, X, T, (unsigned char) (carry ^ borrow));
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100558}
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
561 const mbedtls_mpi *N)
Hanno Beckerec440f22022-08-11 17:29:32 +0100562{
563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
566 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
567 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
568 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
Hanno Beckerec440f22022-08-11 17:29:32 +0100569
570cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return ret;
Hanno Beckerec440f22022-08-11 17:29:32 +0100572}
573
Janos Follath59cbd1b2022-10-28 18:13:43 +0100574MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100575void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
576 const mbedtls_mpi_uint *table,
577 size_t limbs,
578 size_t count,
579 size_t index)
Janos Follathe50f2f12022-10-26 15:14:33 +0100580{
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 for (size_t i = 0; i < count; i++, table += limbs) {
582 unsigned char assign = mbedtls_ct_size_bool_eq(i, index);
583 mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
Janos Follathe50f2f12022-10-26 15:14:33 +0100584 }
585}
586
Gilles Peskine009d1952022-09-09 21:00:00 +0200587/* Fill X with n_bytes random bytes.
588 * X must already have room for those bytes.
589 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200590 * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
591 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200592 */
593int mbedtls_mpi_core_fill_random(
594 mbedtls_mpi_uint *X, size_t X_limbs,
595 size_t n_bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Gilles Peskine009d1952022-09-09 21:00:00 +0200597{
598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 const size_t limbs = CHARS_TO_LIMBS(n_bytes);
600 const size_t overhead = (limbs * ciL) - n_bytes;
Gilles Peskine009d1952022-09-09 21:00:00 +0200601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 if (X_limbs < limbs) {
603 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
604 }
Gilles Peskine009d1952022-09-09 21:00:00 +0200605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 memset(X, 0, overhead);
607 memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
608 MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
609 mbedtls_mpi_core_bigendian_to_host(X, limbs);
Gilles Peskine009d1952022-09-09 21:00:00 +0200610
611cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return ret;
Gilles Peskine009d1952022-09-09 21:00:00 +0200613}
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
616 mbedtls_mpi_uint min,
617 const mbedtls_mpi_uint *N,
618 size_t limbs,
619 int (*f_rng)(void *, unsigned char *, size_t),
620 void *p_rng)
Gilles Peskine70375b22022-09-21 15:47:23 +0200621{
622 unsigned ge_lower = 1, lt_upper = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
624 size_t n_bytes = (n_bits + 7) / 8;
Gilles Peskine70375b22022-09-21 15:47:23 +0200625 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
626
627 /*
628 * When min == 0, each try has at worst a probability 1/2 of failing
629 * (the msb has a probability 1/2 of being 0, and then the result will
630 * be < N), so after 30 tries failure probability is a most 2**(-30).
631 *
632 * When N is just below a power of 2, as is the case when generating
633 * a random scalar on most elliptic curves, 1 try is enough with
634 * overwhelming probability. When N is just above a power of 2,
635 * as when generating a random scalar on secp224k1, each try has
636 * a probability of failing that is almost 1/2.
637 *
638 * The probabilities are almost the same if min is nonzero but negligible
639 * compared to N. This is always the case when N is crypto-sized, but
640 * it's convenient to support small N for testing purposes. When N
641 * is small, use a higher repeat count, otherwise the probability of
642 * failure is macroscopic.
643 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 int count = (n_bytes > 4 ? 30 : 250);
Gilles Peskine70375b22022-09-21 15:47:23 +0200645
646 /*
647 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
648 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
649 * - use the same byte ordering;
650 * - keep the leftmost n_bits bits of the generated octet string;
651 * - try until result is in the desired range.
652 * This also avoids any bias, which is especially important for ECDSA.
653 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 do {
655 MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
656 n_bytes,
657 f_rng, p_rng));
658 mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
Gilles Peskine70375b22022-09-21 15:47:23 +0200659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (--count == 0) {
Gilles Peskine70375b22022-09-21 15:47:23 +0200661 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
662 goto cleanup;
663 }
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
666 lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
667 } while (ge_lower == 0 || lt_upper == 0);
Gilles Peskine70375b22022-09-21 15:47:23 +0200668
669cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 return ret;
Gilles Peskine70375b22022-09-21 15:47:23 +0200671}
672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673static size_t exp_mod_get_window_size(size_t Ebits)
Janos Follathb6673f02022-09-30 14:13:14 +0100674{
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 size_t wsize = (Ebits > 671) ? 6 : (Ebits > 239) ? 5 :
676 (Ebits > 79) ? 4 : 1;
Janos Follathb6673f02022-09-30 14:13:14 +0100677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
679 if (wsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follathb6673f02022-09-30 14:13:14 +0100680 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 }
Janos Follathb6673f02022-09-30 14:13:14 +0100682#endif
683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100685}
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000688{
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
690 const size_t welem = ((size_t) 1) << wsize;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000691
692 /* How big does each part of the working memory pool need to be? */
693 const size_t table_limbs = welem * AN_limbs;
694 const size_t select_limbs = AN_limbs;
695 const size_t temp_limbs = 2 * AN_limbs + 1;
696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return table_limbs + select_limbs + temp_limbs;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000698}
699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
701 const mbedtls_mpi_uint *N,
702 size_t AN_limbs,
703 mbedtls_mpi_uint mm,
704 const mbedtls_mpi_uint *RR,
705 size_t welem,
706 mbedtls_mpi_uint *Wtable,
707 mbedtls_mpi_uint *temp)
Gilles Peskine0de0a042022-11-16 20:12:49 +0100708{
Gilles Peskine0de0a042022-11-16 20:12:49 +0100709 /* W[0] = 1 (in Montgomery presentation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 memset(Wtable, 0, AN_limbs * ciL);
Gilles Peskine0de0a042022-11-16 20:12:49 +0100711 Wtable[0] = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100713
Tom Cosgroveecda1862022-12-06 10:46:30 +0000714 /* W[1] = A (already in Montgomery presentation) */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100715 mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 memcpy(W1, A, AN_limbs * ciL);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100717
Gilles Peskine0de0a042022-11-16 20:12:49 +0100718 /* W[i+1] = W[i] * W[1], i >= 2 */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100719 mbedtls_mpi_uint *Wprev = W1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 for (size_t i = 2; i < welem; i++) {
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100721 mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100723 Wprev = Wcur;
724 }
Gilles Peskine0de0a042022-11-16 20:12:49 +0100725}
726
Gilles Peskine7d89d352022-11-16 22:54:14 +0100727/* Exponentiation: X := A^E mod N.
728 *
Tom Cosgroveecda1862022-12-06 10:46:30 +0000729 * A must already be in Montgomery form.
730 *
Gilles Peskine7d89d352022-11-16 22:54:14 +0100731 * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
732 *
733 * RR must contain 2^{2*biL} mod N.
Janos Follath3321b582022-11-22 21:08:33 +0000734 *
735 * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
736 * (The difference is that the body in our loop processes a single bit instead
737 * of a full window.)
Gilles Peskine7d89d352022-11-16 22:54:14 +0100738 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
740 const mbedtls_mpi_uint *A,
741 const mbedtls_mpi_uint *N,
742 size_t AN_limbs,
743 const mbedtls_mpi_uint *E,
744 size_t E_limbs,
745 const mbedtls_mpi_uint *RR,
746 mbedtls_mpi_uint *T)
Janos Follathb6673f02022-09-30 14:13:14 +0100747{
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
749 const size_t welem = ((size_t) 1) << wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100750
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000751 /* This is how we will use the temporary storage T, which must have space
752 * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
753 const size_t table_limbs = welem * AN_limbs;
754 const size_t select_limbs = AN_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100755
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000756 /* Pointers to specific parts of the temporary working memory pool */
757 mbedtls_mpi_uint *const Wtable = T;
758 mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
759 mbedtls_mpi_uint *const temp = Wselect + select_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100760
761 /*
762 * Window precomputation
763 */
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
Gilles Peskinecf979b02022-11-16 20:04:00 +0100766
Gilles Peskine0de0a042022-11-16 20:12:49 +0100767 /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 exp_mod_precompute_window(A, N, AN_limbs,
769 mm, RR,
770 welem, Wtable, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100771
772 /*
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000773 * Fixed window exponentiation
Janos Follathb6673f02022-09-30 14:13:14 +0100774 */
775
776 /* X = 1 (in Montgomery presentation) initially */
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 memcpy(X, Wtable, AN_limbs * ciL);
Janos Follathb6673f02022-09-30 14:13:14 +0100778
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100779 /* We'll process the bits of E from most significant
780 * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
781 * (limb_index=0, E_bit_index=0). */
782 size_t E_limb_index = E_limbs;
783 size_t E_bit_index = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100784 /* At any given time, window contains window_bits bits from E.
785 * window_bits can go up to wsize. */
Janos Follathbad42c42022-11-09 14:30:44 +0000786 size_t window_bits = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100787 mbedtls_mpi_uint window = 0;
Gilles Peskinecf979b02022-11-16 20:04:00 +0100788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 do {
Janos Follathb6673f02022-09-30 14:13:14 +0100790 /* Square */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100792
Janos Follath3321b582022-11-22 21:08:33 +0000793 /* Move to the next bit of the exponent */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (E_bit_index == 0) {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100795 --E_limb_index;
796 E_bit_index = biL - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 } else {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100798 --E_bit_index;
799 }
Janos Follath3321b582022-11-22 21:08:33 +0000800 /* Insert next exponent bit into window */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100801 ++window_bits;
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100802 window <<= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 window |= (E[E_limb_index] >> E_bit_index) & 1;
Gilles Peskine3b63d092022-11-16 22:06:18 +0100804
805 /* Clear window if it's full. Also clear the window at the end,
806 * when we've finished processing the exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (window_bits == wsize ||
808 (E_bit_index == 0 && E_limb_index == 0)) {
Gilles Peskine0b270a52022-11-16 22:54:03 +0100809 /* Select Wtable[window] without leaking window through
810 * memory access patterns. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
812 AN_limbs, welem, window);
Gilles Peskine0b270a52022-11-16 22:54:03 +0100813 /* Multiply X by the selected element. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
815 temp);
Gilles Peskine3b63d092022-11-16 22:06:18 +0100816 window = 0;
817 window_bits = 0;
818 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 } while (!(E_bit_index == 0 && E_limb_index == 0));
Janos Follathb6673f02022-09-30 14:13:14 +0100820}
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
823 const mbedtls_mpi_uint *A,
824 mbedtls_mpi_uint c, /* doubles as carry */
825 size_t limbs)
Hanno Beckerd9b23482022-08-25 08:25:19 +0100826{
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100828 mbedtls_mpi_uint s = A[i];
829 mbedtls_mpi_uint t = s - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 c = (t > s);
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100831 X[i] = t;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100832 }
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 return c;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100835}
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
838 size_t limbs)
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000839{
840 mbedtls_mpi_uint bits = 0;
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000843 bits |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 }
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 return bits;
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000847}
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
850 const mbedtls_mpi_uint *A,
851 const mbedtls_mpi_uint *N,
852 size_t AN_limbs,
853 mbedtls_mpi_uint mm,
854 const mbedtls_mpi_uint *rr,
855 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000856{
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000858}
859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
861 const mbedtls_mpi_uint *A,
862 const mbedtls_mpi_uint *N,
863 size_t AN_limbs,
864 mbedtls_mpi_uint mm,
865 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000866{
867 const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000870}
871
Janos Follath3ca07752022-08-09 11:45:47 +0100872#endif /* MBEDTLS_BIGNUM_C */