blob: ae0b94acebe2de51795a4eba90a350fb6f1fc86d [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)
Agathiyan Bragadeeshe55a1e12023-07-17 15:00:19 +010039#if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_clz)
40 #define core_clz __builtin_clz
41#elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_clzl)
42 #define core_clz __builtin_clzl
43#elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_clzll)
44 #define core_clz __builtin_clzll
Dave Rodgman914347b2023-04-27 14:20:30 +010045#endif
46#endif
Agathiyan Bragadeeshe55a1e12023-07-17 15:00:19 +010047#if defined(core_clz)
48 return (size_t) core_clz(a);
Agathiyan Bragadeesh271a9532023-07-12 11:15:17 +010049#else
Dave Rodgman914347b2023-04-27 14:20:30 +010050 size_t j;
51 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
52
53 for (j = 0; j < biL; j++) {
54 if (a & mask) {
55 break;
56 }
57
58 mask >>= 1;
59 }
60
61 return j;
Agathiyan Bragadeesh271a9532023-07-12 11:15:17 +010062#endif
Dave Rodgman914347b2023-04-27 14:20:30 +010063}
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +010066{
Dave Rodgman880a6b32023-04-20 09:50:31 +010067 int i;
68 size_t j;
Janos Follath3ca07752022-08-09 11:45:47 +010069
Dave Rodgman2e863ec2023-04-25 17:34:59 +010070 for (i = ((int) A_limbs) - 1; i >= 0; i--) {
71 if (A[i] != 0) {
72 j = biL - mbedtls_mpi_core_clz(A[i]);
73 return (i * biL) + j;
Gilles Peskine449bd832023-01-11 14:50:10 +010074 }
75 }
Janos Follath3ca07752022-08-09 11:45:47 +010076
Dave Rodgman880a6b32023-04-20 09:50:31 +010077 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +010078}
79
Janos Follath3ca07752022-08-09 11:45:47 +010080/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
81 * into the storage form used by mbedtls_mpi. */
Gilles Peskine449bd832023-01-11 14:50:10 +010082static mbedtls_mpi_uint mpi_bigendian_to_host_c(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010083{
84 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010085 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010086 mbedtls_mpi_uint tmp = 0;
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 for (i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++) {
Janos Follath3ca07752022-08-09 11:45:47 +010089 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010090 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010091 }
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return tmp;
Janos Follath3ca07752022-08-09 11:45:47 +010094}
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010097{
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (MBEDTLS_IS_BIG_ENDIAN) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +000099 /* Nothing to do on bigendian systems. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return a;
101 } else {
102 switch (sizeof(mbedtls_mpi_uint)) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000103 case 4:
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return (mbedtls_mpi_uint) MBEDTLS_BSWAP32((uint32_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000105 case 8:
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 return (mbedtls_mpi_uint) MBEDTLS_BSWAP64((uint64_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000107 }
Janos Follath3ca07752022-08-09 11:45:47 +0100108
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000109 /* Fall back to C-based reordering if we don't know the byte order
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 * or we couldn't use a compiler-specific builtin. */
111 return mpi_bigendian_to_host_c(a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000112 }
Janos Follath3ca07752022-08-09 11:45:47 +0100113}
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
116 size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +0100117{
118 mbedtls_mpi_uint *cur_limb_left;
119 mbedtls_mpi_uint *cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (A_limbs == 0) {
Janos Follath3ca07752022-08-09 11:45:47 +0100121 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 }
Janos Follath3ca07752022-08-09 11:45:47 +0100123
124 /*
125 * Traverse limbs and
126 * - adapt byte-order in each limb
127 * - swap the limbs themselves.
128 * For that, simultaneously traverse the limbs from left to right
129 * and from right to left, as long as the left index is not bigger
130 * than the right index (it's not a problem if limbs is odd and the
131 * indices coincide in the last iteration).
132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
Janos Follath3ca07752022-08-09 11:45:47 +0100134 cur_limb_left <= cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 cur_limb_left++, cur_limb_right--) {
Janos Follath3ca07752022-08-09 11:45:47 +0100136 mbedtls_mpi_uint tmp;
137 /* Note that if cur_limb_left == cur_limb_right,
138 * this code effectively swaps the bytes only once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 tmp = mpi_bigendian_to_host(*cur_limb_left);
140 *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
Janos Follath3ca07752022-08-09 11:45:47 +0100141 *cur_limb_right = tmp;
142 }
143}
144
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200145/* Whether min <= A, in constant time.
146 * A_limbs must be at least 1. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147unsigned mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
148 const mbedtls_mpi_uint *A,
149 size_t A_limbs)
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200150{
151 /* min <= least significant limb? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 unsigned min_le_lsl = 1 ^ mbedtls_ct_mpi_uint_lt(A[0], min);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200153
Gilles Peskine6b7ce962022-12-15 15:04:33 +0100154 /* limbs other than the least significant one are all zero? */
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200155 mbedtls_mpi_uint msll_mask = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 for (size_t i = 1; i < A_limbs; i++) {
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200157 msll_mask |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 }
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200159 /* The most significant limbs of A are not all zero iff msll_mask != 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 unsigned msll_nonzero = mbedtls_ct_mpi_uint_mask(msll_mask) & 1;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200161
162 /* min <= A iff the lowest limb of A is >= min or the other limbs
163 * are not all zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return min_le_lsl | msll_nonzero;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200165}
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
168 const mbedtls_mpi_uint *A,
169 size_t limbs,
170 unsigned char assign)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200171{
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (X == A) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200173 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 mbedtls_ct_mpi_uint_cond_assign(limbs, X, A, assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200177}
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
180 mbedtls_mpi_uint *Y,
181 size_t limbs,
182 unsigned char swap)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200183{
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (X == Y) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200185 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200187
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200188 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask(swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 for (size_t i = 0; i < limbs; i++) {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200192 mbedtls_mpi_uint tmp = X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 X[i] = (X[i] & ~limb_mask) | (Y[i] & limb_mask);
194 Y[i] = (Y[i] & ~limb_mask) | (tmp & limb_mask);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200195 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200196}
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
199 size_t X_limbs,
200 const unsigned char *input,
201 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100202{
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (X_limbs < limbs) {
206 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
207 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 if (X != NULL) {
210 memset(X, 0, X_limbs * ciL);
Janos Follath3ca07752022-08-09 11:45:47 +0100211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 for (size_t i = 0; i < input_length; i++) {
213 size_t offset = ((i % ciL) << 3);
214 X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
Janos Follathca5688e2022-08-19 12:05:28 +0100215 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200216 }
Janos Follath3ca07752022-08-09 11:45:47 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100219}
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
222 size_t X_limbs,
223 const unsigned char *input,
224 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100225{
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (X_limbs < limbs) {
229 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
230 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100231
Janos Follathb7a88ec2022-08-19 12:24:40 +0100232 /* If X_limbs is 0, input_length must also be 0 (from previous test).
233 * Nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (X_limbs == 0) {
235 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100236 }
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 memset(X, 0, X_limbs * ciL);
Gabor Mezeic414ba32022-08-12 17:47:39 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 /* memcpy() with (NULL, 0) is undefined behaviour */
241 if (input_length != 0) {
242 size_t overhead = (X_limbs * ciL) - input_length;
243 unsigned char *Xp = (unsigned char *) X;
244 memcpy(Xp + overhead, input, input_length);
245 }
246
247 mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
248
249 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100250}
251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
253 size_t A_limbs,
254 unsigned char *output,
255 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100256{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100257 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100258 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (stored_bytes < output_length) {
Janos Follath3ca07752022-08-09 11:45:47 +0100261 bytes_to_copy = stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 } else {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100263 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100264
Janos Follathaf3f39c2022-08-22 09:06:32 +0100265 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100266 * However A may fit if its leading bytes are zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
268 if (GET_BYTE(A, i) != 0) {
269 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
270 }
Janos Follath3ca07752022-08-09 11:45:47 +0100271 }
272 }
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 for (size_t i = 0; i < bytes_to_copy; i++) {
275 output[i] = GET_BYTE(A, i);
Janos Follath3ca07752022-08-09 11:45:47 +0100276 }
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (stored_bytes < output_length) {
279 /* Write trailing 0 bytes */
280 memset(output + stored_bytes, 0, output_length - stored_bytes);
281 }
282
283 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100284}
285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
287 size_t X_limbs,
288 unsigned char *output,
289 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100290{
291 size_t stored_bytes;
292 size_t bytes_to_copy;
293 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100294
Janos Follathb7a88ec2022-08-19 12:24:40 +0100295 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (stored_bytes < output_length) {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100298 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100299 * null bytes and record the position at which to start
300 * writing the significant bytes. In this case, the execution
301 * trace of this function does not depend on the value of the
302 * number. */
303 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100304 p = output + output_length - stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 memset(output, 0, output_length - stored_bytes);
306 } else {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100307 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100308 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100309 bytes_to_copy = output_length;
310 p = output;
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
312 if (GET_BYTE(X, i) != 0) {
313 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
314 }
Janos Follath3ca07752022-08-09 11:45:47 +0100315 }
316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 for (size_t i = 0; i < bytes_to_copy; i++) {
319 p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
320 }
Janos Follath3ca07752022-08-09 11:45:47 +0100321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100323}
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
326 size_t count)
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200327{
328 size_t i, v0, v1;
329 mbedtls_mpi_uint r0 = 0, r1;
330
331 v0 = count / biL;
332 v1 = count & (biL - 1);
333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (v0 > limbs || (v0 == limbs && v1 > 0)) {
335 memset(X, 0, limbs * ciL);
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200336 return;
337 }
338
339 /*
340 * shift by count / limb_size
341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (v0 > 0) {
343 for (i = 0; i < limbs - v0; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200344 X[i] = X[i + v0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 for (; i < limbs; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200348 X[i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200350 }
351
352 /*
353 * shift by count % limb_size
354 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (v1 > 0) {
356 for (i = limbs; i > 0; i--) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200357 r1 = X[i - 1] << (biL - v1);
358 X[i - 1] >>= v1;
359 X[i - 1] |= r0;
360 r0 = r1;
361 }
362 }
363}
364
Minos Galanakisec09e252023-04-20 14:22:16 +0100365void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
366 size_t count)
Minos Galanakisad808dd2023-04-20 12:18:41 +0100367{
Minos Galanakisec09e252023-04-20 14:22:16 +0100368 size_t i, v0, v1;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100369 mbedtls_mpi_uint r0 = 0, r1;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100370
Minos Galanakisec09e252023-04-20 14:22:16 +0100371 v0 = count / (biL);
372 v1 = count & (biL - 1);
Minos Galanakisad808dd2023-04-20 12:18:41 +0100373
Minos Galanakisad808dd2023-04-20 12:18:41 +0100374 /*
375 * shift by count / limb_size
376 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100377 if (v0 > 0) {
378 for (i = limbs; i > v0; i--) {
379 X[i - 1] = X[i - v0 - 1];
380 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100381
Minos Galanakisec09e252023-04-20 14:22:16 +0100382 for (; i > 0; i--) {
383 X[i - 1] = 0;
384 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100385 }
386
387 /*
388 * shift by count % limb_size
389 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100390 if (v1 > 0) {
391 for (i = v0; i < limbs; i++) {
392 r1 = X[i] >> (biL - v1);
393 X[i] <<= v1;
394 X[i] |= r0;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100395 r0 = r1;
396 }
397 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100398}
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
401 const mbedtls_mpi_uint *A,
402 const mbedtls_mpi_uint *B,
403 size_t limbs)
Hanno Beckerc9887132022-08-24 12:54:36 +0100404{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100405 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 for (size_t i = 0; i < limbs; i++) {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100408 mbedtls_mpi_uint t = c + A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 c = (t < A[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100410 t += B[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 c += (t < B[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100412 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100413 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return c;
Hanno Beckerc9887132022-08-24 12:54:36 +0100416}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
419 const mbedtls_mpi_uint *A,
420 size_t limbs,
421 unsigned cond)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100422{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100423 mbedtls_mpi_uint c = 0;
424
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100425 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask(cond);
Tom Cosgrove93549902022-08-30 17:41:23 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100429 mbedtls_mpi_uint add = mask & A[i];
430 mbedtls_mpi_uint t = c + X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 c = (t < X[i]);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100432 t += add;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 c += (t < add);
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100434 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100435 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100438}
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
441 const mbedtls_mpi_uint *A,
442 const mbedtls_mpi_uint *B,
443 size_t limbs)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100444{
445 mbedtls_mpi_uint c = 0;
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 for (size_t i = 0; i < limbs; i++) {
448 mbedtls_mpi_uint z = (A[i] < c);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100449 mbedtls_mpi_uint t = A[i] - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 c = (t < B[i]) + z;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100451 X[i] = t - B[i];
452 }
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100455}
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
458 const mbedtls_mpi_uint *s, size_t s_len,
459 mbedtls_mpi_uint b)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100460{
461 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100462 /*
463 * It is a documented precondition of this function that d_len >= s_len.
464 * If that's not the case, we swap these round: this turns what would be
465 * a buffer overflow into an incorrect result.
466 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (d_len < s_len) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100468 s_len = d_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100470 size_t excess_len = d_len - s_len;
471 size_t steps_x8 = s_len / 8;
472 size_t steps_x1 = s_len & 7;
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 while (steps_x8--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100475 MULADDC_X8_INIT
476 MULADDC_X8_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 MULADDC_X8_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100478 }
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 while (steps_x1--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100481 MULADDC_X1_INIT
482 MULADDC_X1_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 MULADDC_X1_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100484 }
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 while (excess_len--) {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100487 *d += c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 c = (*d < c);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100489 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100490 }
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100493}
494
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100495void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
496 const mbedtls_mpi_uint *A, size_t A_limbs,
497 const mbedtls_mpi_uint *B, size_t B_limbs)
Hanno Becker4ae890b2022-08-24 16:17:52 +0100498{
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100499 memset(X, 0, (A_limbs + B_limbs) * ciL);
500
501 for (size_t i = 0; i < B_limbs; i++) {
502 (void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]);
503 }
Hanno Becker4ae890b2022-08-24 16:17:52 +0100504}
505
Tom Cosgroveb4964862022-08-30 11:57:22 +0100506/*
507 * Fast Montgomery initialization (thanks to Tom St Denis).
508 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100510{
511 mbedtls_mpi_uint x = N[0];
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 x += ((N[0] + 2) & 4) << 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 for (unsigned int i = biL; i >= 8; i /= 2) {
516 x *= (2 - (N[0] * x));
517 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return ~x + 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100520}
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
523 const mbedtls_mpi_uint *A,
524 const mbedtls_mpi_uint *B,
525 size_t B_limbs,
526 const mbedtls_mpi_uint *N,
527 size_t AN_limbs,
528 mbedtls_mpi_uint mm,
529 mbedtls_mpi_uint *T)
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100530{
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 memset(T, 0, (2 * AN_limbs + 1) * ciL);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 for (size_t i = 0; i < AN_limbs; i++) {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100534 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100535 mbedtls_mpi_uint u0 = A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
539 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
Tom Cosgrove67c92472022-09-02 13:28:59 +0100540
541 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100542 }
543
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100544 /*
545 * The result we want is (T >= N) ? T - N : T.
546 *
547 * For better constant-time properties in this function, we always do the
548 * subtraction, with the result in X.
549 *
550 * We also look to see if there was any carry in the final additions in the
551 * loop above.
552 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100553
Tom Cosgrove72594632022-08-24 11:51:58 +0100554 mbedtls_mpi_uint carry = T[AN_limbs];
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100556
557 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100558 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100559 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100560 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100561 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100562 * 1) T < N : (carry, borrow) = (0, 1): we want T
563 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
564 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100565 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100566 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100567 *
568 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100569 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100570 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 mbedtls_ct_mpi_uint_cond_assign(AN_limbs, X, T, (unsigned char) (carry ^ borrow));
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100572}
573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
575 const mbedtls_mpi *N)
Hanno Beckerec440f22022-08-11 17:29:32 +0100576{
577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
580 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
581 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
582 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
Hanno Beckerec440f22022-08-11 17:29:32 +0100583
584cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return ret;
Hanno Beckerec440f22022-08-11 17:29:32 +0100586}
587
Janos Follath59cbd1b2022-10-28 18:13:43 +0100588MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100589void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
590 const mbedtls_mpi_uint *table,
591 size_t limbs,
592 size_t count,
593 size_t index)
Janos Follathe50f2f12022-10-26 15:14:33 +0100594{
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 for (size_t i = 0; i < count; i++, table += limbs) {
596 unsigned char assign = mbedtls_ct_size_bool_eq(i, index);
597 mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
Janos Follathe50f2f12022-10-26 15:14:33 +0100598 }
599}
600
Gilles Peskine009d1952022-09-09 21:00:00 +0200601/* Fill X with n_bytes random bytes.
602 * X must already have room for those bytes.
603 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200604 * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
605 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200606 */
607int mbedtls_mpi_core_fill_random(
608 mbedtls_mpi_uint *X, size_t X_limbs,
609 size_t n_bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Gilles Peskine009d1952022-09-09 21:00:00 +0200611{
612 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 const size_t limbs = CHARS_TO_LIMBS(n_bytes);
614 const size_t overhead = (limbs * ciL) - n_bytes;
Gilles Peskine009d1952022-09-09 21:00:00 +0200615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (X_limbs < limbs) {
617 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
618 }
Gilles Peskine009d1952022-09-09 21:00:00 +0200619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 memset(X, 0, overhead);
621 memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
622 MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
623 mbedtls_mpi_core_bigendian_to_host(X, limbs);
Gilles Peskine009d1952022-09-09 21:00:00 +0200624
625cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return ret;
Gilles Peskine009d1952022-09-09 21:00:00 +0200627}
628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
630 mbedtls_mpi_uint min,
631 const mbedtls_mpi_uint *N,
632 size_t limbs,
633 int (*f_rng)(void *, unsigned char *, size_t),
634 void *p_rng)
Gilles Peskine70375b22022-09-21 15:47:23 +0200635{
636 unsigned ge_lower = 1, lt_upper = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
638 size_t n_bytes = (n_bits + 7) / 8;
Gilles Peskine70375b22022-09-21 15:47:23 +0200639 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
640
641 /*
642 * When min == 0, each try has at worst a probability 1/2 of failing
643 * (the msb has a probability 1/2 of being 0, and then the result will
644 * be < N), so after 30 tries failure probability is a most 2**(-30).
645 *
646 * When N is just below a power of 2, as is the case when generating
647 * a random scalar on most elliptic curves, 1 try is enough with
648 * overwhelming probability. When N is just above a power of 2,
649 * as when generating a random scalar on secp224k1, each try has
650 * a probability of failing that is almost 1/2.
651 *
652 * The probabilities are almost the same if min is nonzero but negligible
653 * compared to N. This is always the case when N is crypto-sized, but
654 * it's convenient to support small N for testing purposes. When N
655 * is small, use a higher repeat count, otherwise the probability of
656 * failure is macroscopic.
657 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 int count = (n_bytes > 4 ? 30 : 250);
Gilles Peskine70375b22022-09-21 15:47:23 +0200659
660 /*
661 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
662 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
663 * - use the same byte ordering;
664 * - keep the leftmost n_bits bits of the generated octet string;
665 * - try until result is in the desired range.
666 * This also avoids any bias, which is especially important for ECDSA.
667 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 do {
669 MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
670 n_bytes,
671 f_rng, p_rng));
672 mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
Gilles Peskine70375b22022-09-21 15:47:23 +0200673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (--count == 0) {
Gilles Peskine70375b22022-09-21 15:47:23 +0200675 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
676 goto cleanup;
677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
680 lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
681 } while (ge_lower == 0 || lt_upper == 0);
Gilles Peskine70375b22022-09-21 15:47:23 +0200682
683cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return ret;
Gilles Peskine70375b22022-09-21 15:47:23 +0200685}
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687static size_t exp_mod_get_window_size(size_t Ebits)
Janos Follathb6673f02022-09-30 14:13:14 +0100688{
Dave Rodgman4883f102023-08-09 20:17:40 +0100689#if MBEDTLS_MPI_WINDOW_SIZE >= 6
690 return (Ebits > 671) ? 6 : (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1;
691#elif MBEDTLS_MPI_WINDOW_SIZE == 5
692 return (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1;
693#elif MBEDTLS_MPI_WINDOW_SIZE > 1
694 return (Ebits > 79) ? MBEDTLS_MPI_WINDOW_SIZE : 1;
695#else
696 (void) Ebits;
697 return 1;
Janos Follathb6673f02022-09-30 14:13:14 +0100698#endif
Janos Follathb6673f02022-09-30 14:13:14 +0100699}
700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000702{
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
704 const size_t welem = ((size_t) 1) << wsize;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000705
706 /* How big does each part of the working memory pool need to be? */
707 const size_t table_limbs = welem * AN_limbs;
708 const size_t select_limbs = AN_limbs;
709 const size_t temp_limbs = 2 * AN_limbs + 1;
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return table_limbs + select_limbs + temp_limbs;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000712}
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
715 const mbedtls_mpi_uint *N,
716 size_t AN_limbs,
717 mbedtls_mpi_uint mm,
718 const mbedtls_mpi_uint *RR,
719 size_t welem,
720 mbedtls_mpi_uint *Wtable,
721 mbedtls_mpi_uint *temp)
Gilles Peskine0de0a042022-11-16 20:12:49 +0100722{
Gilles Peskine0de0a042022-11-16 20:12:49 +0100723 /* W[0] = 1 (in Montgomery presentation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 memset(Wtable, 0, AN_limbs * ciL);
Gilles Peskine0de0a042022-11-16 20:12:49 +0100725 Wtable[0] = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100727
Tom Cosgroveecda1862022-12-06 10:46:30 +0000728 /* W[1] = A (already in Montgomery presentation) */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100729 mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 memcpy(W1, A, AN_limbs * ciL);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100731
Gilles Peskine0de0a042022-11-16 20:12:49 +0100732 /* W[i+1] = W[i] * W[1], i >= 2 */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100733 mbedtls_mpi_uint *Wprev = W1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 for (size_t i = 2; i < welem; i++) {
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100735 mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100737 Wprev = Wcur;
738 }
Gilles Peskine0de0a042022-11-16 20:12:49 +0100739}
740
Gilles Peskine7d89d352022-11-16 22:54:14 +0100741/* Exponentiation: X := A^E mod N.
742 *
Tom Cosgroveecda1862022-12-06 10:46:30 +0000743 * A must already be in Montgomery form.
744 *
Gilles Peskine7d89d352022-11-16 22:54:14 +0100745 * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
746 *
747 * RR must contain 2^{2*biL} mod N.
Janos Follath3321b582022-11-22 21:08:33 +0000748 *
749 * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
750 * (The difference is that the body in our loop processes a single bit instead
751 * of a full window.)
Gilles Peskine7d89d352022-11-16 22:54:14 +0100752 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
754 const mbedtls_mpi_uint *A,
755 const mbedtls_mpi_uint *N,
756 size_t AN_limbs,
757 const mbedtls_mpi_uint *E,
758 size_t E_limbs,
759 const mbedtls_mpi_uint *RR,
760 mbedtls_mpi_uint *T)
Janos Follathb6673f02022-09-30 14:13:14 +0100761{
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
763 const size_t welem = ((size_t) 1) << wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100764
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000765 /* This is how we will use the temporary storage T, which must have space
766 * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
767 const size_t table_limbs = welem * AN_limbs;
768 const size_t select_limbs = AN_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100769
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000770 /* Pointers to specific parts of the temporary working memory pool */
771 mbedtls_mpi_uint *const Wtable = T;
772 mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
773 mbedtls_mpi_uint *const temp = Wselect + select_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100774
775 /*
776 * Window precomputation
777 */
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
Gilles Peskinecf979b02022-11-16 20:04:00 +0100780
Gilles Peskine0de0a042022-11-16 20:12:49 +0100781 /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 exp_mod_precompute_window(A, N, AN_limbs,
783 mm, RR,
784 welem, Wtable, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100785
786 /*
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000787 * Fixed window exponentiation
Janos Follathb6673f02022-09-30 14:13:14 +0100788 */
789
790 /* X = 1 (in Montgomery presentation) initially */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 memcpy(X, Wtable, AN_limbs * ciL);
Janos Follathb6673f02022-09-30 14:13:14 +0100792
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100793 /* We'll process the bits of E from most significant
794 * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
795 * (limb_index=0, E_bit_index=0). */
796 size_t E_limb_index = E_limbs;
797 size_t E_bit_index = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100798 /* At any given time, window contains window_bits bits from E.
799 * window_bits can go up to wsize. */
Janos Follathbad42c42022-11-09 14:30:44 +0000800 size_t window_bits = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100801 mbedtls_mpi_uint window = 0;
Gilles Peskinecf979b02022-11-16 20:04:00 +0100802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 do {
Janos Follathb6673f02022-09-30 14:13:14 +0100804 /* Square */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100806
Janos Follath3321b582022-11-22 21:08:33 +0000807 /* Move to the next bit of the exponent */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (E_bit_index == 0) {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100809 --E_limb_index;
810 E_bit_index = biL - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 } else {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100812 --E_bit_index;
813 }
Janos Follath3321b582022-11-22 21:08:33 +0000814 /* Insert next exponent bit into window */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100815 ++window_bits;
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100816 window <<= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 window |= (E[E_limb_index] >> E_bit_index) & 1;
Gilles Peskine3b63d092022-11-16 22:06:18 +0100818
819 /* Clear window if it's full. Also clear the window at the end,
820 * when we've finished processing the exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 if (window_bits == wsize ||
822 (E_bit_index == 0 && E_limb_index == 0)) {
Gilles Peskine0b270a52022-11-16 22:54:03 +0100823 /* Select Wtable[window] without leaking window through
824 * memory access patterns. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
826 AN_limbs, welem, window);
Gilles Peskine0b270a52022-11-16 22:54:03 +0100827 /* Multiply X by the selected element. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
829 temp);
Gilles Peskine3b63d092022-11-16 22:06:18 +0100830 window = 0;
831 window_bits = 0;
832 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 } while (!(E_bit_index == 0 && E_limb_index == 0));
Janos Follathb6673f02022-09-30 14:13:14 +0100834}
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
837 const mbedtls_mpi_uint *A,
838 mbedtls_mpi_uint c, /* doubles as carry */
839 size_t limbs)
Hanno Beckerd9b23482022-08-25 08:25:19 +0100840{
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100842 mbedtls_mpi_uint s = A[i];
843 mbedtls_mpi_uint t = s - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 c = (t > s);
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100845 X[i] = t;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100846 }
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 return c;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100849}
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
852 size_t limbs)
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000853{
854 mbedtls_mpi_uint bits = 0;
855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000857 bits |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 }
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 return bits;
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000861}
862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
864 const mbedtls_mpi_uint *A,
865 const mbedtls_mpi_uint *N,
866 size_t AN_limbs,
867 mbedtls_mpi_uint mm,
868 const mbedtls_mpi_uint *rr,
869 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000870{
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000872}
873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
875 const mbedtls_mpi_uint *A,
876 const mbedtls_mpi_uint *N,
877 size_t AN_limbs,
878 mbedtls_mpi_uint mm,
879 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000880{
881 const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000884}
885
Janos Follath3ca07752022-08-09 11:45:47 +0100886#endif /* MBEDTLS_BIGNUM_C */