blob: 1f3a57c054c3a95619071887094ffef4a6b4bc0d [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 Rodgman880a6b32023-04-20 09:50:31 +010036inline size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010037{
Dave Rodgman880a6b32023-04-20 09:50:31 +010038 /* Note: the result is undefined for a == 0
39 * (because this is the behaviour of __builtin_clz).
40 */
Dave Rodgmanfe8a8cd2023-04-19 17:40:28 +010041#if defined(__has_builtin)
42#if __has_builtin(__builtin_clz)
43 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned int)) {
Dave Rodgman880a6b32023-04-20 09:50:31 +010044 return (size_t) __builtin_clz(a);
Dave Rodgmanfe8a8cd2023-04-19 17:40:28 +010045 }
46#endif
47#if __has_builtin(__builtin_clzl)
48 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long)) {
Dave Rodgman880a6b32023-04-20 09:50:31 +010049 return (size_t) __builtin_clzl(a);
Dave Rodgmanfe8a8cd2023-04-19 17:40:28 +010050 }
51#endif
52#if __has_builtin(__builtin_clzll)
53 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long long)) {
Dave Rodgman880a6b32023-04-20 09:50:31 +010054 return (size_t) __builtin_clzll(a);
Dave Rodgmanfe8a8cd2023-04-19 17:40:28 +010055 }
56#endif
57#endif
Janos Follath3ca07752022-08-09 11:45:47 +010058 size_t j;
59 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061 for (j = 0; j < biL; j++) {
62 if (a & mask) {
63 break;
64 }
Janos Follath3ca07752022-08-09 11:45:47 +010065
66 mask >>= 1;
67 }
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 return j;
Janos Follath3ca07752022-08-09 11:45:47 +010070}
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +010073{
Dave Rodgman880a6b32023-04-20 09:50:31 +010074 int i;
75 size_t j;
Janos Follath3ca07752022-08-09 11:45:47 +010076
Dave Rodgman880a6b32023-04-20 09:50:31 +010077 if (A_limbs != 0) {
78 for (i = (int) A_limbs - 1; i >= 0; i--) {
79 if (A[i] != 0) {
80 j = biL - mbedtls_mpi_core_clz(A[i]);
81 return (i * biL) + j;
82 }
Gilles Peskine449bd832023-01-11 14:50:10 +010083 }
84 }
Janos Follath3ca07752022-08-09 11:45:47 +010085
Dave Rodgman880a6b32023-04-20 09:50:31 +010086 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +010087}
88
Janos Follath3ca07752022-08-09 11:45:47 +010089/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
90 * into the storage form used by mbedtls_mpi. */
Gilles Peskine449bd832023-01-11 14:50:10 +010091static mbedtls_mpi_uint mpi_bigendian_to_host_c(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010092{
93 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010094 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010095 mbedtls_mpi_uint tmp = 0;
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 for (i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++) {
Janos Follath3ca07752022-08-09 11:45:47 +010098 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010099 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +0100100 }
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return tmp;
Janos Follath3ca07752022-08-09 11:45:47 +0100103}
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +0100106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (MBEDTLS_IS_BIG_ENDIAN) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000108 /* Nothing to do on bigendian systems. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return a;
110 } else {
111 switch (sizeof(mbedtls_mpi_uint)) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000112 case 4:
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return (mbedtls_mpi_uint) MBEDTLS_BSWAP32((uint32_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000114 case 8:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return (mbedtls_mpi_uint) MBEDTLS_BSWAP64((uint64_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000116 }
Janos Follath3ca07752022-08-09 11:45:47 +0100117
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000118 /* Fall back to C-based reordering if we don't know the byte order
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 * or we couldn't use a compiler-specific builtin. */
120 return mpi_bigendian_to_host_c(a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000121 }
Janos Follath3ca07752022-08-09 11:45:47 +0100122}
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
125 size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +0100126{
127 mbedtls_mpi_uint *cur_limb_left;
128 mbedtls_mpi_uint *cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if (A_limbs == 0) {
Janos Follath3ca07752022-08-09 11:45:47 +0100130 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 }
Janos Follath3ca07752022-08-09 11:45:47 +0100132
133 /*
134 * Traverse limbs and
135 * - adapt byte-order in each limb
136 * - swap the limbs themselves.
137 * For that, simultaneously traverse the limbs from left to right
138 * and from right to left, as long as the left index is not bigger
139 * than the right index (it's not a problem if limbs is odd and the
140 * indices coincide in the last iteration).
141 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
Janos Follath3ca07752022-08-09 11:45:47 +0100143 cur_limb_left <= cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 cur_limb_left++, cur_limb_right--) {
Janos Follath3ca07752022-08-09 11:45:47 +0100145 mbedtls_mpi_uint tmp;
146 /* Note that if cur_limb_left == cur_limb_right,
147 * this code effectively swaps the bytes only once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 tmp = mpi_bigendian_to_host(*cur_limb_left);
149 *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
Janos Follath3ca07752022-08-09 11:45:47 +0100150 *cur_limb_right = tmp;
151 }
152}
153
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200154/* Whether min <= A, in constant time.
155 * A_limbs must be at least 1. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156unsigned mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
157 const mbedtls_mpi_uint *A,
158 size_t A_limbs)
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200159{
160 /* min <= least significant limb? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 unsigned min_le_lsl = 1 ^ mbedtls_ct_mpi_uint_lt(A[0], min);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200162
Gilles Peskine6b7ce962022-12-15 15:04:33 +0100163 /* limbs other than the least significant one are all zero? */
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200164 mbedtls_mpi_uint msll_mask = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 for (size_t i = 1; i < A_limbs; i++) {
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200166 msll_mask |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 }
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200168 /* The most significant limbs of A are not all zero iff msll_mask != 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 unsigned msll_nonzero = mbedtls_ct_mpi_uint_mask(msll_mask) & 1;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200170
171 /* min <= A iff the lowest limb of A is >= min or the other limbs
172 * are not all zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return min_le_lsl | msll_nonzero;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200174}
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
177 const mbedtls_mpi_uint *A,
178 size_t limbs,
179 unsigned char assign)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200180{
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (X == A) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200182 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_ct_mpi_uint_cond_assign(limbs, X, A, assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200186}
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
189 mbedtls_mpi_uint *Y,
190 size_t limbs,
191 unsigned char swap)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200192{
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (X == Y) {
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
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200197 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask(swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 for (size_t i = 0; i < limbs; i++) {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200201 mbedtls_mpi_uint tmp = X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 X[i] = (X[i] & ~limb_mask) | (Y[i] & limb_mask);
203 Y[i] = (Y[i] & ~limb_mask) | (tmp & limb_mask);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200204 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200205}
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
208 size_t X_limbs,
209 const unsigned char *input,
210 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (X_limbs < limbs) {
215 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
216 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (X != NULL) {
219 memset(X, 0, X_limbs * ciL);
Janos Follath3ca07752022-08-09 11:45:47 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 for (size_t i = 0; i < input_length; i++) {
222 size_t offset = ((i % ciL) << 3);
223 X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
Janos Follathca5688e2022-08-19 12:05:28 +0100224 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200225 }
Janos Follath3ca07752022-08-09 11:45:47 +0100226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100228}
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
231 size_t X_limbs,
232 const unsigned char *input,
233 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100234{
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (X_limbs < limbs) {
238 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
239 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100240
Janos Follathb7a88ec2022-08-19 12:24:40 +0100241 /* If X_limbs is 0, input_length must also be 0 (from previous test).
242 * Nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (X_limbs == 0) {
244 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100245 }
246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 memset(X, 0, X_limbs * ciL);
Gabor Mezeic414ba32022-08-12 17:47:39 +0200248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 /* memcpy() with (NULL, 0) is undefined behaviour */
250 if (input_length != 0) {
251 size_t overhead = (X_limbs * ciL) - input_length;
252 unsigned char *Xp = (unsigned char *) X;
253 memcpy(Xp + overhead, input, input_length);
254 }
255
256 mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
257
258 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100259}
260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
262 size_t A_limbs,
263 unsigned char *output,
264 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100265{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100266 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100267 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (stored_bytes < output_length) {
Janos Follath3ca07752022-08-09 11:45:47 +0100270 bytes_to_copy = stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 } else {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100272 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100273
Janos Follathaf3f39c2022-08-22 09:06:32 +0100274 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100275 * However A may fit if its leading bytes are zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
277 if (GET_BYTE(A, i) != 0) {
278 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
279 }
Janos Follath3ca07752022-08-09 11:45:47 +0100280 }
281 }
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 for (size_t i = 0; i < bytes_to_copy; i++) {
284 output[i] = GET_BYTE(A, i);
Janos Follath3ca07752022-08-09 11:45:47 +0100285 }
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (stored_bytes < output_length) {
288 /* Write trailing 0 bytes */
289 memset(output + stored_bytes, 0, output_length - stored_bytes);
290 }
291
292 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100293}
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
296 size_t X_limbs,
297 unsigned char *output,
298 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100299{
300 size_t stored_bytes;
301 size_t bytes_to_copy;
302 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100303
Janos Follathb7a88ec2022-08-19 12:24:40 +0100304 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (stored_bytes < output_length) {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100307 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100308 * null bytes and record the position at which to start
309 * writing the significant bytes. In this case, the execution
310 * trace of this function does not depend on the value of the
311 * number. */
312 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100313 p = output + output_length - stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 memset(output, 0, output_length - stored_bytes);
315 } else {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100316 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100317 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100318 bytes_to_copy = output_length;
319 p = output;
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
321 if (GET_BYTE(X, i) != 0) {
322 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
323 }
Janos Follath3ca07752022-08-09 11:45:47 +0100324 }
325 }
326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 for (size_t i = 0; i < bytes_to_copy; i++) {
328 p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
329 }
Janos Follath3ca07752022-08-09 11:45:47 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100332}
333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
335 size_t count)
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200336{
337 size_t i, v0, v1;
338 mbedtls_mpi_uint r0 = 0, r1;
339
340 v0 = count / biL;
341 v1 = count & (biL - 1);
342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if (v0 > limbs || (v0 == limbs && v1 > 0)) {
344 memset(X, 0, limbs * ciL);
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200345 return;
346 }
347
348 /*
349 * shift by count / limb_size
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (v0 > 0) {
352 for (i = 0; i < limbs - v0; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200353 X[i] = X[i + v0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 for (; i < limbs; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200357 X[i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200359 }
360
361 /*
362 * shift by count % limb_size
363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (v1 > 0) {
365 for (i = limbs; i > 0; i--) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200366 r1 = X[i - 1] << (biL - v1);
367 X[i - 1] >>= v1;
368 X[i - 1] |= r0;
369 r0 = r1;
370 }
371 }
372}
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
375 const mbedtls_mpi_uint *A,
376 const mbedtls_mpi_uint *B,
377 size_t limbs)
Hanno Beckerc9887132022-08-24 12:54:36 +0100378{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100379 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 for (size_t i = 0; i < limbs; i++) {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100382 mbedtls_mpi_uint t = c + A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 c = (t < A[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100384 t += B[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 c += (t < B[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100386 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100387 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return c;
Hanno Beckerc9887132022-08-24 12:54:36 +0100390}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
393 const mbedtls_mpi_uint *A,
394 size_t limbs,
395 unsigned cond)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100396{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100397 mbedtls_mpi_uint c = 0;
398
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100399 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask(cond);
Tom Cosgrove93549902022-08-30 17:41:23 +0100401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100403 mbedtls_mpi_uint add = mask & A[i];
404 mbedtls_mpi_uint t = c + X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 c = (t < X[i]);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100406 t += add;
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 c += (t < add);
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100408 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100409 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100412}
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
415 const mbedtls_mpi_uint *A,
416 const mbedtls_mpi_uint *B,
417 size_t limbs)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100418{
419 mbedtls_mpi_uint c = 0;
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 for (size_t i = 0; i < limbs; i++) {
422 mbedtls_mpi_uint z = (A[i] < c);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100423 mbedtls_mpi_uint t = A[i] - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 c = (t < B[i]) + z;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100425 X[i] = t - B[i];
426 }
427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100429}
430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
432 const mbedtls_mpi_uint *s, size_t s_len,
433 mbedtls_mpi_uint b)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100434{
435 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100436 /*
437 * It is a documented precondition of this function that d_len >= s_len.
438 * If that's not the case, we swap these round: this turns what would be
439 * a buffer overflow into an incorrect result.
440 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (d_len < s_len) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100442 s_len = d_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100444 size_t excess_len = d_len - s_len;
445 size_t steps_x8 = s_len / 8;
446 size_t steps_x1 = s_len & 7;
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 while (steps_x8--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100449 MULADDC_X8_INIT
450 MULADDC_X8_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 MULADDC_X8_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100452 }
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 while (steps_x1--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100455 MULADDC_X1_INIT
456 MULADDC_X1_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 MULADDC_X1_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100458 }
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 while (excess_len--) {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100461 *d += c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 c = (*d < c);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100463 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100464 }
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100467}
468
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100469void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
470 const mbedtls_mpi_uint *A, size_t A_limbs,
471 const mbedtls_mpi_uint *B, size_t B_limbs)
Hanno Becker4ae890b2022-08-24 16:17:52 +0100472{
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100473 memset(X, 0, (A_limbs + B_limbs) * ciL);
474
475 for (size_t i = 0; i < B_limbs; i++) {
476 (void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]);
477 }
Hanno Becker4ae890b2022-08-24 16:17:52 +0100478}
479
Tom Cosgroveb4964862022-08-30 11:57:22 +0100480/*
481 * Fast Montgomery initialization (thanks to Tom St Denis).
482 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100484{
485 mbedtls_mpi_uint x = N[0];
486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 x += ((N[0] + 2) & 4) << 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 for (unsigned int i = biL; i >= 8; i /= 2) {
490 x *= (2 - (N[0] * x));
491 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 return ~x + 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100494}
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
497 const mbedtls_mpi_uint *A,
498 const mbedtls_mpi_uint *B,
499 size_t B_limbs,
500 const mbedtls_mpi_uint *N,
501 size_t AN_limbs,
502 mbedtls_mpi_uint mm,
503 mbedtls_mpi_uint *T)
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100504{
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 memset(T, 0, (2 * AN_limbs + 1) * ciL);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 for (size_t i = 0; i < AN_limbs; i++) {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100508 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100509 mbedtls_mpi_uint u0 = A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
513 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
Tom Cosgrove67c92472022-09-02 13:28:59 +0100514
515 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100516 }
517
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100518 /*
519 * The result we want is (T >= N) ? T - N : T.
520 *
521 * For better constant-time properties in this function, we always do the
522 * subtraction, with the result in X.
523 *
524 * We also look to see if there was any carry in the final additions in the
525 * loop above.
526 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100527
Tom Cosgrove72594632022-08-24 11:51:58 +0100528 mbedtls_mpi_uint carry = T[AN_limbs];
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100530
531 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100532 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100533 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100534 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100535 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100536 * 1) T < N : (carry, borrow) = (0, 1): we want T
537 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
538 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100539 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100540 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100541 *
542 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100543 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100544 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 mbedtls_ct_mpi_uint_cond_assign(AN_limbs, X, T, (unsigned char) (carry ^ borrow));
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100546}
547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
549 const mbedtls_mpi *N)
Hanno Beckerec440f22022-08-11 17:29:32 +0100550{
551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
554 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
555 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
556 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
Hanno Beckerec440f22022-08-11 17:29:32 +0100557
558cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return ret;
Hanno Beckerec440f22022-08-11 17:29:32 +0100560}
561
Janos Follath59cbd1b2022-10-28 18:13:43 +0100562MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100563void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
564 const mbedtls_mpi_uint *table,
565 size_t limbs,
566 size_t count,
567 size_t index)
Janos Follathe50f2f12022-10-26 15:14:33 +0100568{
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 for (size_t i = 0; i < count; i++, table += limbs) {
570 unsigned char assign = mbedtls_ct_size_bool_eq(i, index);
571 mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
Janos Follathe50f2f12022-10-26 15:14:33 +0100572 }
573}
574
Gilles Peskine009d1952022-09-09 21:00:00 +0200575/* Fill X with n_bytes random bytes.
576 * X must already have room for those bytes.
577 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200578 * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
579 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200580 */
581int mbedtls_mpi_core_fill_random(
582 mbedtls_mpi_uint *X, size_t X_limbs,
583 size_t n_bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Gilles Peskine009d1952022-09-09 21:00:00 +0200585{
586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 const size_t limbs = CHARS_TO_LIMBS(n_bytes);
588 const size_t overhead = (limbs * ciL) - n_bytes;
Gilles Peskine009d1952022-09-09 21:00:00 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (X_limbs < limbs) {
591 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
592 }
Gilles Peskine009d1952022-09-09 21:00:00 +0200593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 memset(X, 0, overhead);
595 memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
596 MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
597 mbedtls_mpi_core_bigendian_to_host(X, limbs);
Gilles Peskine009d1952022-09-09 21:00:00 +0200598
599cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 return ret;
Gilles Peskine009d1952022-09-09 21:00:00 +0200601}
602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
604 mbedtls_mpi_uint min,
605 const mbedtls_mpi_uint *N,
606 size_t limbs,
607 int (*f_rng)(void *, unsigned char *, size_t),
608 void *p_rng)
Gilles Peskine70375b22022-09-21 15:47:23 +0200609{
610 unsigned ge_lower = 1, lt_upper = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
612 size_t n_bytes = (n_bits + 7) / 8;
Gilles Peskine70375b22022-09-21 15:47:23 +0200613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
614
615 /*
616 * When min == 0, each try has at worst a probability 1/2 of failing
617 * (the msb has a probability 1/2 of being 0, and then the result will
618 * be < N), so after 30 tries failure probability is a most 2**(-30).
619 *
620 * When N is just below a power of 2, as is the case when generating
621 * a random scalar on most elliptic curves, 1 try is enough with
622 * overwhelming probability. When N is just above a power of 2,
623 * as when generating a random scalar on secp224k1, each try has
624 * a probability of failing that is almost 1/2.
625 *
626 * The probabilities are almost the same if min is nonzero but negligible
627 * compared to N. This is always the case when N is crypto-sized, but
628 * it's convenient to support small N for testing purposes. When N
629 * is small, use a higher repeat count, otherwise the probability of
630 * failure is macroscopic.
631 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 int count = (n_bytes > 4 ? 30 : 250);
Gilles Peskine70375b22022-09-21 15:47:23 +0200633
634 /*
635 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
636 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
637 * - use the same byte ordering;
638 * - keep the leftmost n_bits bits of the generated octet string;
639 * - try until result is in the desired range.
640 * This also avoids any bias, which is especially important for ECDSA.
641 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 do {
643 MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
644 n_bytes,
645 f_rng, p_rng));
646 mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
Gilles Peskine70375b22022-09-21 15:47:23 +0200647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (--count == 0) {
Gilles Peskine70375b22022-09-21 15:47:23 +0200649 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
650 goto cleanup;
651 }
652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
654 lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
655 } while (ge_lower == 0 || lt_upper == 0);
Gilles Peskine70375b22022-09-21 15:47:23 +0200656
657cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 return ret;
Gilles Peskine70375b22022-09-21 15:47:23 +0200659}
660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661static size_t exp_mod_get_window_size(size_t Ebits)
Janos Follathb6673f02022-09-30 14:13:14 +0100662{
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 size_t wsize = (Ebits > 671) ? 6 : (Ebits > 239) ? 5 :
664 (Ebits > 79) ? 4 : 1;
Janos Follathb6673f02022-09-30 14:13:14 +0100665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
667 if (wsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follathb6673f02022-09-30 14:13:14 +0100668 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 }
Janos Follathb6673f02022-09-30 14:13:14 +0100670#endif
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 return wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100673}
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000676{
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
678 const size_t welem = ((size_t) 1) << wsize;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000679
680 /* How big does each part of the working memory pool need to be? */
681 const size_t table_limbs = welem * AN_limbs;
682 const size_t select_limbs = AN_limbs;
683 const size_t temp_limbs = 2 * AN_limbs + 1;
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return table_limbs + select_limbs + temp_limbs;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000686}
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
689 const mbedtls_mpi_uint *N,
690 size_t AN_limbs,
691 mbedtls_mpi_uint mm,
692 const mbedtls_mpi_uint *RR,
693 size_t welem,
694 mbedtls_mpi_uint *Wtable,
695 mbedtls_mpi_uint *temp)
Gilles Peskine0de0a042022-11-16 20:12:49 +0100696{
Gilles Peskine0de0a042022-11-16 20:12:49 +0100697 /* W[0] = 1 (in Montgomery presentation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 memset(Wtable, 0, AN_limbs * ciL);
Gilles Peskine0de0a042022-11-16 20:12:49 +0100699 Wtable[0] = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100701
Tom Cosgroveecda1862022-12-06 10:46:30 +0000702 /* W[1] = A (already in Montgomery presentation) */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100703 mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 memcpy(W1, A, AN_limbs * ciL);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100705
Gilles Peskine0de0a042022-11-16 20:12:49 +0100706 /* W[i+1] = W[i] * W[1], i >= 2 */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100707 mbedtls_mpi_uint *Wprev = W1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 for (size_t i = 2; i < welem; i++) {
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100709 mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100711 Wprev = Wcur;
712 }
Gilles Peskine0de0a042022-11-16 20:12:49 +0100713}
714
Gilles Peskine7d89d352022-11-16 22:54:14 +0100715/* Exponentiation: X := A^E mod N.
716 *
Tom Cosgroveecda1862022-12-06 10:46:30 +0000717 * A must already be in Montgomery form.
718 *
Gilles Peskine7d89d352022-11-16 22:54:14 +0100719 * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
720 *
721 * RR must contain 2^{2*biL} mod N.
Janos Follath3321b582022-11-22 21:08:33 +0000722 *
723 * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
724 * (The difference is that the body in our loop processes a single bit instead
725 * of a full window.)
Gilles Peskine7d89d352022-11-16 22:54:14 +0100726 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
728 const mbedtls_mpi_uint *A,
729 const mbedtls_mpi_uint *N,
730 size_t AN_limbs,
731 const mbedtls_mpi_uint *E,
732 size_t E_limbs,
733 const mbedtls_mpi_uint *RR,
734 mbedtls_mpi_uint *T)
Janos Follathb6673f02022-09-30 14:13:14 +0100735{
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
737 const size_t welem = ((size_t) 1) << wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100738
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000739 /* This is how we will use the temporary storage T, which must have space
740 * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
741 const size_t table_limbs = welem * AN_limbs;
742 const size_t select_limbs = AN_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100743
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000744 /* Pointers to specific parts of the temporary working memory pool */
745 mbedtls_mpi_uint *const Wtable = T;
746 mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
747 mbedtls_mpi_uint *const temp = Wselect + select_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100748
749 /*
750 * Window precomputation
751 */
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
Gilles Peskinecf979b02022-11-16 20:04:00 +0100754
Gilles Peskine0de0a042022-11-16 20:12:49 +0100755 /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 exp_mod_precompute_window(A, N, AN_limbs,
757 mm, RR,
758 welem, Wtable, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100759
760 /*
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000761 * Fixed window exponentiation
Janos Follathb6673f02022-09-30 14:13:14 +0100762 */
763
764 /* X = 1 (in Montgomery presentation) initially */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 memcpy(X, Wtable, AN_limbs * ciL);
Janos Follathb6673f02022-09-30 14:13:14 +0100766
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100767 /* We'll process the bits of E from most significant
768 * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
769 * (limb_index=0, E_bit_index=0). */
770 size_t E_limb_index = E_limbs;
771 size_t E_bit_index = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100772 /* At any given time, window contains window_bits bits from E.
773 * window_bits can go up to wsize. */
Janos Follathbad42c42022-11-09 14:30:44 +0000774 size_t window_bits = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100775 mbedtls_mpi_uint window = 0;
Gilles Peskinecf979b02022-11-16 20:04:00 +0100776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 do {
Janos Follathb6673f02022-09-30 14:13:14 +0100778 /* Square */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100780
Janos Follath3321b582022-11-22 21:08:33 +0000781 /* Move to the next bit of the exponent */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (E_bit_index == 0) {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100783 --E_limb_index;
784 E_bit_index = biL - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 } else {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100786 --E_bit_index;
787 }
Janos Follath3321b582022-11-22 21:08:33 +0000788 /* Insert next exponent bit into window */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100789 ++window_bits;
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100790 window <<= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 window |= (E[E_limb_index] >> E_bit_index) & 1;
Gilles Peskine3b63d092022-11-16 22:06:18 +0100792
793 /* Clear window if it's full. Also clear the window at the end,
794 * when we've finished processing the exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (window_bits == wsize ||
796 (E_bit_index == 0 && E_limb_index == 0)) {
Gilles Peskine0b270a52022-11-16 22:54:03 +0100797 /* Select Wtable[window] without leaking window through
798 * memory access patterns. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
800 AN_limbs, welem, window);
Gilles Peskine0b270a52022-11-16 22:54:03 +0100801 /* Multiply X by the selected element. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
803 temp);
Gilles Peskine3b63d092022-11-16 22:06:18 +0100804 window = 0;
805 window_bits = 0;
806 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 } while (!(E_bit_index == 0 && E_limb_index == 0));
Janos Follathb6673f02022-09-30 14:13:14 +0100808}
809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
811 const mbedtls_mpi_uint *A,
812 mbedtls_mpi_uint c, /* doubles as carry */
813 size_t limbs)
Hanno Beckerd9b23482022-08-25 08:25:19 +0100814{
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100816 mbedtls_mpi_uint s = A[i];
817 mbedtls_mpi_uint t = s - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 c = (t > s);
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100819 X[i] = t;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100820 }
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 return c;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100823}
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
826 size_t limbs)
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000827{
828 mbedtls_mpi_uint bits = 0;
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000831 bits |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 }
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 return bits;
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000835}
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
838 const mbedtls_mpi_uint *A,
839 const mbedtls_mpi_uint *N,
840 size_t AN_limbs,
841 mbedtls_mpi_uint mm,
842 const mbedtls_mpi_uint *rr,
843 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000844{
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000846}
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
849 const mbedtls_mpi_uint *A,
850 const mbedtls_mpi_uint *N,
851 size_t AN_limbs,
852 mbedtls_mpi_uint mm,
853 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000854{
855 const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000858}
859
Janos Follath3ca07752022-08-09 11:45:47 +0100860#endif /* MBEDTLS_BIGNUM_C */