blob: 998c06c8fd55d4af1b3044aa6b2d913963d728ee [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 Rodgmanbbf88102023-04-21 12:54:40 +010036/**
37 * \brief Count leading zeros
38 *
39 * \warning The result is undefined if \p a == 0
40 *
41 * \param a The value to operate on
42 *
43 * \return The number of leading zeros, if \p a != 0. If \p a == 0, the result
44 * is undefined.
45 */
Dave Rodgman880a6b32023-04-20 09:50:31 +010046inline size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010047{
Dave Rodgmanfe8a8cd2023-04-19 17:40:28 +010048#if defined(__has_builtin)
49#if __has_builtin(__builtin_clz)
50 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned int)) {
Dave Rodgman880a6b32023-04-20 09:50:31 +010051 return (size_t) __builtin_clz(a);
Dave Rodgmanfe8a8cd2023-04-19 17:40:28 +010052 }
53#endif
54#if __has_builtin(__builtin_clzl)
55 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long)) {
Dave Rodgman880a6b32023-04-20 09:50:31 +010056 return (size_t) __builtin_clzl(a);
Dave Rodgmanfe8a8cd2023-04-19 17:40:28 +010057 }
58#endif
59#if __has_builtin(__builtin_clzll)
60 if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long long)) {
Dave Rodgman880a6b32023-04-20 09:50:31 +010061 return (size_t) __builtin_clzll(a);
Dave Rodgmanfe8a8cd2023-04-19 17:40:28 +010062 }
63#endif
64#endif
Janos Follath3ca07752022-08-09 11:45:47 +010065 size_t j;
66 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 for (j = 0; j < biL; j++) {
69 if (a & mask) {
70 break;
71 }
Janos Follath3ca07752022-08-09 11:45:47 +010072
73 mask >>= 1;
74 }
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 return j;
Janos Follath3ca07752022-08-09 11:45:47 +010077}
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +010080{
Dave Rodgman880a6b32023-04-20 09:50:31 +010081 int i;
82 size_t j;
Janos Follath3ca07752022-08-09 11:45:47 +010083
Dave Rodgman880a6b32023-04-20 09:50:31 +010084 if (A_limbs != 0) {
85 for (i = (int) A_limbs - 1; i >= 0; i--) {
86 if (A[i] != 0) {
87 j = biL - mbedtls_mpi_core_clz(A[i]);
88 return (i * biL) + j;
89 }
Gilles Peskine449bd832023-01-11 14:50:10 +010090 }
91 }
Janos Follath3ca07752022-08-09 11:45:47 +010092
Dave Rodgman880a6b32023-04-20 09:50:31 +010093 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +010094}
95
Janos Follath3ca07752022-08-09 11:45:47 +010096/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
97 * into the storage form used by mbedtls_mpi. */
Gilles Peskine449bd832023-01-11 14:50:10 +010098static mbedtls_mpi_uint mpi_bigendian_to_host_c(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010099{
100 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100101 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +0100102 mbedtls_mpi_uint tmp = 0;
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 for (i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++) {
Janos Follath3ca07752022-08-09 11:45:47 +0100105 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100106 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +0100107 }
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return tmp;
Janos Follath3ca07752022-08-09 11:45:47 +0100110}
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +0100113{
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (MBEDTLS_IS_BIG_ENDIAN) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000115 /* Nothing to do on bigendian systems. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 return a;
117 } else {
118 switch (sizeof(mbedtls_mpi_uint)) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000119 case 4:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return (mbedtls_mpi_uint) MBEDTLS_BSWAP32((uint32_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000121 case 8:
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 return (mbedtls_mpi_uint) MBEDTLS_BSWAP64((uint64_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000123 }
Janos Follath3ca07752022-08-09 11:45:47 +0100124
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000125 /* Fall back to C-based reordering if we don't know the byte order
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 * or we couldn't use a compiler-specific builtin. */
127 return mpi_bigendian_to_host_c(a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000128 }
Janos Follath3ca07752022-08-09 11:45:47 +0100129}
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
132 size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +0100133{
134 mbedtls_mpi_uint *cur_limb_left;
135 mbedtls_mpi_uint *cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (A_limbs == 0) {
Janos Follath3ca07752022-08-09 11:45:47 +0100137 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 }
Janos Follath3ca07752022-08-09 11:45:47 +0100139
140 /*
141 * Traverse limbs and
142 * - adapt byte-order in each limb
143 * - swap the limbs themselves.
144 * For that, simultaneously traverse the limbs from left to right
145 * and from right to left, as long as the left index is not bigger
146 * than the right index (it's not a problem if limbs is odd and the
147 * indices coincide in the last iteration).
148 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
Janos Follath3ca07752022-08-09 11:45:47 +0100150 cur_limb_left <= cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 cur_limb_left++, cur_limb_right--) {
Janos Follath3ca07752022-08-09 11:45:47 +0100152 mbedtls_mpi_uint tmp;
153 /* Note that if cur_limb_left == cur_limb_right,
154 * this code effectively swaps the bytes only once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 tmp = mpi_bigendian_to_host(*cur_limb_left);
156 *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
Janos Follath3ca07752022-08-09 11:45:47 +0100157 *cur_limb_right = tmp;
158 }
159}
160
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200161/* Whether min <= A, in constant time.
162 * A_limbs must be at least 1. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163unsigned mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
164 const mbedtls_mpi_uint *A,
165 size_t A_limbs)
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200166{
167 /* min <= least significant limb? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 unsigned min_le_lsl = 1 ^ mbedtls_ct_mpi_uint_lt(A[0], min);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200169
Gilles Peskine6b7ce962022-12-15 15:04:33 +0100170 /* limbs other than the least significant one are all zero? */
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200171 mbedtls_mpi_uint msll_mask = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 for (size_t i = 1; i < A_limbs; i++) {
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200173 msll_mask |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 }
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200175 /* The most significant limbs of A are not all zero iff msll_mask != 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 unsigned msll_nonzero = mbedtls_ct_mpi_uint_mask(msll_mask) & 1;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200177
178 /* min <= A iff the lowest limb of A is >= min or the other limbs
179 * are not all zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return min_le_lsl | msll_nonzero;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200181}
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
184 const mbedtls_mpi_uint *A,
185 size_t limbs,
186 unsigned char assign)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200187{
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (X == A) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200189 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 mbedtls_ct_mpi_uint_cond_assign(limbs, X, A, assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200193}
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
196 mbedtls_mpi_uint *Y,
197 size_t limbs,
198 unsigned char swap)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200199{
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (X == Y) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200201 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200203
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200204 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask(swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 for (size_t i = 0; i < limbs; i++) {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200208 mbedtls_mpi_uint tmp = X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 X[i] = (X[i] & ~limb_mask) | (Y[i] & limb_mask);
210 Y[i] = (Y[i] & ~limb_mask) | (tmp & limb_mask);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200211 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200212}
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
215 size_t X_limbs,
216 const unsigned char *input,
217 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100218{
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (X_limbs < limbs) {
222 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
223 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (X != NULL) {
226 memset(X, 0, X_limbs * ciL);
Janos Follath3ca07752022-08-09 11:45:47 +0100227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 for (size_t i = 0; i < input_length; i++) {
229 size_t offset = ((i % ciL) << 3);
230 X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
Janos Follathca5688e2022-08-19 12:05:28 +0100231 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200232 }
Janos Follath3ca07752022-08-09 11:45:47 +0100233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100235}
236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
238 size_t X_limbs,
239 const unsigned char *input,
240 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100241{
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (X_limbs < limbs) {
245 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
246 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100247
Janos Follathb7a88ec2022-08-19 12:24:40 +0100248 /* If X_limbs is 0, input_length must also be 0 (from previous test).
249 * Nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (X_limbs == 0) {
251 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100252 }
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 memset(X, 0, X_limbs * ciL);
Gabor Mezeic414ba32022-08-12 17:47:39 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 /* memcpy() with (NULL, 0) is undefined behaviour */
257 if (input_length != 0) {
258 size_t overhead = (X_limbs * ciL) - input_length;
259 unsigned char *Xp = (unsigned char *) X;
260 memcpy(Xp + overhead, input, input_length);
261 }
262
263 mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
264
265 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100266}
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
269 size_t A_limbs,
270 unsigned char *output,
271 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100272{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100273 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100274 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (stored_bytes < output_length) {
Janos Follath3ca07752022-08-09 11:45:47 +0100277 bytes_to_copy = stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 } else {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100279 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100280
Janos Follathaf3f39c2022-08-22 09:06:32 +0100281 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100282 * However A may fit if its leading bytes are zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
284 if (GET_BYTE(A, i) != 0) {
285 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
286 }
Janos Follath3ca07752022-08-09 11:45:47 +0100287 }
288 }
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 for (size_t i = 0; i < bytes_to_copy; i++) {
291 output[i] = GET_BYTE(A, i);
Janos Follath3ca07752022-08-09 11:45:47 +0100292 }
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (stored_bytes < output_length) {
295 /* Write trailing 0 bytes */
296 memset(output + stored_bytes, 0, output_length - stored_bytes);
297 }
298
299 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100300}
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
303 size_t X_limbs,
304 unsigned char *output,
305 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100306{
307 size_t stored_bytes;
308 size_t bytes_to_copy;
309 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100310
Janos Follathb7a88ec2022-08-19 12:24:40 +0100311 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if (stored_bytes < output_length) {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100314 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100315 * null bytes and record the position at which to start
316 * writing the significant bytes. In this case, the execution
317 * trace of this function does not depend on the value of the
318 * number. */
319 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100320 p = output + output_length - stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 memset(output, 0, output_length - stored_bytes);
322 } else {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100323 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100324 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100325 bytes_to_copy = output_length;
326 p = output;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
328 if (GET_BYTE(X, i) != 0) {
329 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
330 }
Janos Follath3ca07752022-08-09 11:45:47 +0100331 }
332 }
333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 for (size_t i = 0; i < bytes_to_copy; i++) {
335 p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
336 }
Janos Follath3ca07752022-08-09 11:45:47 +0100337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100339}
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
342 size_t count)
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200343{
344 size_t i, v0, v1;
345 mbedtls_mpi_uint r0 = 0, r1;
346
347 v0 = count / biL;
348 v1 = count & (biL - 1);
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (v0 > limbs || (v0 == limbs && v1 > 0)) {
351 memset(X, 0, limbs * ciL);
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200352 return;
353 }
354
355 /*
356 * shift by count / limb_size
357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (v0 > 0) {
359 for (i = 0; i < limbs - v0; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200360 X[i] = X[i + v0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 for (; i < limbs; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200364 X[i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200366 }
367
368 /*
369 * shift by count % limb_size
370 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (v1 > 0) {
372 for (i = limbs; i > 0; i--) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200373 r1 = X[i - 1] << (biL - v1);
374 X[i - 1] >>= v1;
375 X[i - 1] |= r0;
376 r0 = r1;
377 }
378 }
379}
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
382 const mbedtls_mpi_uint *A,
383 const mbedtls_mpi_uint *B,
384 size_t limbs)
Hanno Beckerc9887132022-08-24 12:54:36 +0100385{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100386 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 for (size_t i = 0; i < limbs; i++) {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100389 mbedtls_mpi_uint t = c + A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 c = (t < A[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100391 t += B[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 c += (t < B[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100393 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100394 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return c;
Hanno Beckerc9887132022-08-24 12:54:36 +0100397}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
400 const mbedtls_mpi_uint *A,
401 size_t limbs,
402 unsigned cond)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100403{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100404 mbedtls_mpi_uint c = 0;
405
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100406 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask(cond);
Tom Cosgrove93549902022-08-30 17:41:23 +0100408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100410 mbedtls_mpi_uint add = mask & A[i];
411 mbedtls_mpi_uint t = c + X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 c = (t < X[i]);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100413 t += add;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 c += (t < add);
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100415 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100416 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100419}
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
422 const mbedtls_mpi_uint *A,
423 const mbedtls_mpi_uint *B,
424 size_t limbs)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100425{
426 mbedtls_mpi_uint c = 0;
427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 for (size_t i = 0; i < limbs; i++) {
429 mbedtls_mpi_uint z = (A[i] < c);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100430 mbedtls_mpi_uint t = A[i] - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 c = (t < B[i]) + z;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100432 X[i] = t - B[i];
433 }
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100436}
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
439 const mbedtls_mpi_uint *s, size_t s_len,
440 mbedtls_mpi_uint b)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100441{
442 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100443 /*
444 * It is a documented precondition of this function that d_len >= s_len.
445 * If that's not the case, we swap these round: this turns what would be
446 * a buffer overflow into an incorrect result.
447 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (d_len < s_len) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100449 s_len = d_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100451 size_t excess_len = d_len - s_len;
452 size_t steps_x8 = s_len / 8;
453 size_t steps_x1 = s_len & 7;
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 while (steps_x8--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100456 MULADDC_X8_INIT
457 MULADDC_X8_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 MULADDC_X8_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100459 }
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 while (steps_x1--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100462 MULADDC_X1_INIT
463 MULADDC_X1_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 MULADDC_X1_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100465 }
466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 while (excess_len--) {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100468 *d += c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 c = (*d < c);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100470 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100471 }
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100474}
475
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100476void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
477 const mbedtls_mpi_uint *A, size_t A_limbs,
478 const mbedtls_mpi_uint *B, size_t B_limbs)
Hanno Becker4ae890b2022-08-24 16:17:52 +0100479{
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100480 memset(X, 0, (A_limbs + B_limbs) * ciL);
481
482 for (size_t i = 0; i < B_limbs; i++) {
483 (void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]);
484 }
Hanno Becker4ae890b2022-08-24 16:17:52 +0100485}
486
Tom Cosgroveb4964862022-08-30 11:57:22 +0100487/*
488 * Fast Montgomery initialization (thanks to Tom St Denis).
489 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100491{
492 mbedtls_mpi_uint x = N[0];
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 x += ((N[0] + 2) & 4) << 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 for (unsigned int i = biL; i >= 8; i /= 2) {
497 x *= (2 - (N[0] * x));
498 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 return ~x + 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100501}
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
504 const mbedtls_mpi_uint *A,
505 const mbedtls_mpi_uint *B,
506 size_t B_limbs,
507 const mbedtls_mpi_uint *N,
508 size_t AN_limbs,
509 mbedtls_mpi_uint mm,
510 mbedtls_mpi_uint *T)
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100511{
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 memset(T, 0, (2 * AN_limbs + 1) * ciL);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 for (size_t i = 0; i < AN_limbs; i++) {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100515 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100516 mbedtls_mpi_uint u0 = A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
520 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
Tom Cosgrove67c92472022-09-02 13:28:59 +0100521
522 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100523 }
524
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100525 /*
526 * The result we want is (T >= N) ? T - N : T.
527 *
528 * For better constant-time properties in this function, we always do the
529 * subtraction, with the result in X.
530 *
531 * We also look to see if there was any carry in the final additions in the
532 * loop above.
533 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100534
Tom Cosgrove72594632022-08-24 11:51:58 +0100535 mbedtls_mpi_uint carry = T[AN_limbs];
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100537
538 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100539 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100540 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100541 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100542 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100543 * 1) T < N : (carry, borrow) = (0, 1): we want T
544 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
545 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100546 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100547 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100548 *
549 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100550 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100551 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 mbedtls_ct_mpi_uint_cond_assign(AN_limbs, X, T, (unsigned char) (carry ^ borrow));
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100553}
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
556 const mbedtls_mpi *N)
Hanno Beckerec440f22022-08-11 17:29:32 +0100557{
558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
561 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
562 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
563 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
Hanno Beckerec440f22022-08-11 17:29:32 +0100564
565cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return ret;
Hanno Beckerec440f22022-08-11 17:29:32 +0100567}
568
Janos Follath59cbd1b2022-10-28 18:13:43 +0100569MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100570void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
571 const mbedtls_mpi_uint *table,
572 size_t limbs,
573 size_t count,
574 size_t index)
Janos Follathe50f2f12022-10-26 15:14:33 +0100575{
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 for (size_t i = 0; i < count; i++, table += limbs) {
577 unsigned char assign = mbedtls_ct_size_bool_eq(i, index);
578 mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
Janos Follathe50f2f12022-10-26 15:14:33 +0100579 }
580}
581
Gilles Peskine009d1952022-09-09 21:00:00 +0200582/* Fill X with n_bytes random bytes.
583 * X must already have room for those bytes.
584 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200585 * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
586 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200587 */
588int mbedtls_mpi_core_fill_random(
589 mbedtls_mpi_uint *X, size_t X_limbs,
590 size_t n_bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Gilles Peskine009d1952022-09-09 21:00:00 +0200592{
593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 const size_t limbs = CHARS_TO_LIMBS(n_bytes);
595 const size_t overhead = (limbs * ciL) - n_bytes;
Gilles Peskine009d1952022-09-09 21:00:00 +0200596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (X_limbs < limbs) {
598 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
599 }
Gilles Peskine009d1952022-09-09 21:00:00 +0200600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 memset(X, 0, overhead);
602 memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
603 MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
604 mbedtls_mpi_core_bigendian_to_host(X, limbs);
Gilles Peskine009d1952022-09-09 21:00:00 +0200605
606cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 return ret;
Gilles Peskine009d1952022-09-09 21:00:00 +0200608}
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
611 mbedtls_mpi_uint min,
612 const mbedtls_mpi_uint *N,
613 size_t limbs,
614 int (*f_rng)(void *, unsigned char *, size_t),
615 void *p_rng)
Gilles Peskine70375b22022-09-21 15:47:23 +0200616{
617 unsigned ge_lower = 1, lt_upper = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
619 size_t n_bytes = (n_bits + 7) / 8;
Gilles Peskine70375b22022-09-21 15:47:23 +0200620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
621
622 /*
623 * When min == 0, each try has at worst a probability 1/2 of failing
624 * (the msb has a probability 1/2 of being 0, and then the result will
625 * be < N), so after 30 tries failure probability is a most 2**(-30).
626 *
627 * When N is just below a power of 2, as is the case when generating
628 * a random scalar on most elliptic curves, 1 try is enough with
629 * overwhelming probability. When N is just above a power of 2,
630 * as when generating a random scalar on secp224k1, each try has
631 * a probability of failing that is almost 1/2.
632 *
633 * The probabilities are almost the same if min is nonzero but negligible
634 * compared to N. This is always the case when N is crypto-sized, but
635 * it's convenient to support small N for testing purposes. When N
636 * is small, use a higher repeat count, otherwise the probability of
637 * failure is macroscopic.
638 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 int count = (n_bytes > 4 ? 30 : 250);
Gilles Peskine70375b22022-09-21 15:47:23 +0200640
641 /*
642 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
643 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
644 * - use the same byte ordering;
645 * - keep the leftmost n_bits bits of the generated octet string;
646 * - try until result is in the desired range.
647 * This also avoids any bias, which is especially important for ECDSA.
648 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 do {
650 MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
651 n_bytes,
652 f_rng, p_rng));
653 mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
Gilles Peskine70375b22022-09-21 15:47:23 +0200654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (--count == 0) {
Gilles Peskine70375b22022-09-21 15:47:23 +0200656 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
657 goto cleanup;
658 }
659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
661 lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
662 } while (ge_lower == 0 || lt_upper == 0);
Gilles Peskine70375b22022-09-21 15:47:23 +0200663
664cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return ret;
Gilles Peskine70375b22022-09-21 15:47:23 +0200666}
667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668static size_t exp_mod_get_window_size(size_t Ebits)
Janos Follathb6673f02022-09-30 14:13:14 +0100669{
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 size_t wsize = (Ebits > 671) ? 6 : (Ebits > 239) ? 5 :
671 (Ebits > 79) ? 4 : 1;
Janos Follathb6673f02022-09-30 14:13:14 +0100672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
674 if (wsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follathb6673f02022-09-30 14:13:14 +0100675 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 }
Janos Follathb6673f02022-09-30 14:13:14 +0100677#endif
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100680}
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000683{
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
685 const size_t welem = ((size_t) 1) << wsize;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000686
687 /* How big does each part of the working memory pool need to be? */
688 const size_t table_limbs = welem * AN_limbs;
689 const size_t select_limbs = AN_limbs;
690 const size_t temp_limbs = 2 * AN_limbs + 1;
691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return table_limbs + select_limbs + temp_limbs;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000693}
694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
696 const mbedtls_mpi_uint *N,
697 size_t AN_limbs,
698 mbedtls_mpi_uint mm,
699 const mbedtls_mpi_uint *RR,
700 size_t welem,
701 mbedtls_mpi_uint *Wtable,
702 mbedtls_mpi_uint *temp)
Gilles Peskine0de0a042022-11-16 20:12:49 +0100703{
Gilles Peskine0de0a042022-11-16 20:12:49 +0100704 /* W[0] = 1 (in Montgomery presentation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 memset(Wtable, 0, AN_limbs * ciL);
Gilles Peskine0de0a042022-11-16 20:12:49 +0100706 Wtable[0] = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100708
Tom Cosgroveecda1862022-12-06 10:46:30 +0000709 /* W[1] = A (already in Montgomery presentation) */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100710 mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 memcpy(W1, A, AN_limbs * ciL);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100712
Gilles Peskine0de0a042022-11-16 20:12:49 +0100713 /* W[i+1] = W[i] * W[1], i >= 2 */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100714 mbedtls_mpi_uint *Wprev = W1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 for (size_t i = 2; i < welem; i++) {
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100716 mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100718 Wprev = Wcur;
719 }
Gilles Peskine0de0a042022-11-16 20:12:49 +0100720}
721
Gilles Peskine7d89d352022-11-16 22:54:14 +0100722/* Exponentiation: X := A^E mod N.
723 *
Tom Cosgroveecda1862022-12-06 10:46:30 +0000724 * A must already be in Montgomery form.
725 *
Gilles Peskine7d89d352022-11-16 22:54:14 +0100726 * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
727 *
728 * RR must contain 2^{2*biL} mod N.
Janos Follath3321b582022-11-22 21:08:33 +0000729 *
730 * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
731 * (The difference is that the body in our loop processes a single bit instead
732 * of a full window.)
Gilles Peskine7d89d352022-11-16 22:54:14 +0100733 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
735 const mbedtls_mpi_uint *A,
736 const mbedtls_mpi_uint *N,
737 size_t AN_limbs,
738 const mbedtls_mpi_uint *E,
739 size_t E_limbs,
740 const mbedtls_mpi_uint *RR,
741 mbedtls_mpi_uint *T)
Janos Follathb6673f02022-09-30 14:13:14 +0100742{
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
744 const size_t welem = ((size_t) 1) << wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100745
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000746 /* This is how we will use the temporary storage T, which must have space
747 * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
748 const size_t table_limbs = welem * AN_limbs;
749 const size_t select_limbs = AN_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100750
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000751 /* Pointers to specific parts of the temporary working memory pool */
752 mbedtls_mpi_uint *const Wtable = T;
753 mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
754 mbedtls_mpi_uint *const temp = Wselect + select_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100755
756 /*
757 * Window precomputation
758 */
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
Gilles Peskinecf979b02022-11-16 20:04:00 +0100761
Gilles Peskine0de0a042022-11-16 20:12:49 +0100762 /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 exp_mod_precompute_window(A, N, AN_limbs,
764 mm, RR,
765 welem, Wtable, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100766
767 /*
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000768 * Fixed window exponentiation
Janos Follathb6673f02022-09-30 14:13:14 +0100769 */
770
771 /* X = 1 (in Montgomery presentation) initially */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 memcpy(X, Wtable, AN_limbs * ciL);
Janos Follathb6673f02022-09-30 14:13:14 +0100773
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100774 /* We'll process the bits of E from most significant
775 * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
776 * (limb_index=0, E_bit_index=0). */
777 size_t E_limb_index = E_limbs;
778 size_t E_bit_index = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100779 /* At any given time, window contains window_bits bits from E.
780 * window_bits can go up to wsize. */
Janos Follathbad42c42022-11-09 14:30:44 +0000781 size_t window_bits = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100782 mbedtls_mpi_uint window = 0;
Gilles Peskinecf979b02022-11-16 20:04:00 +0100783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 do {
Janos Follathb6673f02022-09-30 14:13:14 +0100785 /* Square */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100787
Janos Follath3321b582022-11-22 21:08:33 +0000788 /* Move to the next bit of the exponent */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if (E_bit_index == 0) {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100790 --E_limb_index;
791 E_bit_index = biL - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 } else {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100793 --E_bit_index;
794 }
Janos Follath3321b582022-11-22 21:08:33 +0000795 /* Insert next exponent bit into window */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100796 ++window_bits;
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100797 window <<= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 window |= (E[E_limb_index] >> E_bit_index) & 1;
Gilles Peskine3b63d092022-11-16 22:06:18 +0100799
800 /* Clear window if it's full. Also clear the window at the end,
801 * when we've finished processing the exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (window_bits == wsize ||
803 (E_bit_index == 0 && E_limb_index == 0)) {
Gilles Peskine0b270a52022-11-16 22:54:03 +0100804 /* Select Wtable[window] without leaking window through
805 * memory access patterns. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
807 AN_limbs, welem, window);
Gilles Peskine0b270a52022-11-16 22:54:03 +0100808 /* Multiply X by the selected element. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
810 temp);
Gilles Peskine3b63d092022-11-16 22:06:18 +0100811 window = 0;
812 window_bits = 0;
813 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 } while (!(E_bit_index == 0 && E_limb_index == 0));
Janos Follathb6673f02022-09-30 14:13:14 +0100815}
816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
818 const mbedtls_mpi_uint *A,
819 mbedtls_mpi_uint c, /* doubles as carry */
820 size_t limbs)
Hanno Beckerd9b23482022-08-25 08:25:19 +0100821{
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100823 mbedtls_mpi_uint s = A[i];
824 mbedtls_mpi_uint t = s - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 c = (t > s);
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100826 X[i] = t;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100827 }
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 return c;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100830}
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
833 size_t limbs)
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000834{
835 mbedtls_mpi_uint bits = 0;
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000838 bits |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 }
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 return bits;
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000842}
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
845 const mbedtls_mpi_uint *A,
846 const mbedtls_mpi_uint *N,
847 size_t AN_limbs,
848 mbedtls_mpi_uint mm,
849 const mbedtls_mpi_uint *rr,
850 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000851{
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000853}
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
856 const mbedtls_mpi_uint *A,
857 const mbedtls_mpi_uint *N,
858 size_t AN_limbs,
859 mbedtls_mpi_uint mm,
860 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000861{
862 const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000865}
866
Janos Follath3ca07752022-08-09 11:45:47 +0100867#endif /* MBEDTLS_BIGNUM_C */