blob: 92a9d558a3c9a06191295e8093cc62a03cefcbcd [file] [log] [blame]
Janos Follath3ca07752022-08-09 11:45:47 +01001/*
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Core bignum functions
Janos Follath3ca07752022-08-09 11:45:47 +01003 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
22#if defined(MBEDTLS_BIGNUM_C)
23
24#include <string.h>
25
26#include "mbedtls/error.h"
27#include "mbedtls/platform_util.h"
Gabor Mezeie1d31c42022-09-12 16:25:24 +020028#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010029
Janos Follath3ca07752022-08-09 11:45:47 +010030#include "mbedtls/platform.h"
Janos Follath3ca07752022-08-09 11:45:47 +010031
32#include "bignum_core.h"
Tom Cosgrove958fd3d2022-08-24 11:08:51 +010033#include "bn_mul.h"
34#include "constant_time_internal.h"
Janos Follath3ca07752022-08-09 11:45:47 +010035
Gilles Peskine449bd832023-01-11 14:50:10 +010036size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010037{
38 size_t j;
39 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
40
Gilles Peskine449bd832023-01-11 14:50:10 +010041 for (j = 0; j < biL; j++) {
42 if (a & mask) {
43 break;
44 }
Janos Follath3ca07752022-08-09 11:45:47 +010045
46 mask >>= 1;
47 }
48
Gilles Peskine449bd832023-01-11 14:50:10 +010049 return j;
Janos Follath3ca07752022-08-09 11:45:47 +010050}
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +010053{
54 size_t i, j;
55
Gilles Peskine449bd832023-01-11 14:50:10 +010056 if (A_limbs == 0) {
57 return 0;
58 }
Janos Follath3ca07752022-08-09 11:45:47 +010059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 for (i = A_limbs - 1; i > 0; i--) {
61 if (A[i] != 0) {
Janos Follath3ca07752022-08-09 11:45:47 +010062 break;
Gilles Peskine449bd832023-01-11 14:50:10 +010063 }
64 }
Janos Follath3ca07752022-08-09 11:45:47 +010065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 j = biL - mbedtls_mpi_core_clz(A[i]);
Janos Follath3ca07752022-08-09 11:45:47 +010067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 return (i * biL) + j;
Janos Follath3ca07752022-08-09 11:45:47 +010069}
70
Janos Follath3ca07752022-08-09 11:45:47 +010071/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
72 * into the storage form used by mbedtls_mpi. */
Gilles Peskine449bd832023-01-11 14:50:10 +010073static mbedtls_mpi_uint mpi_bigendian_to_host_c(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010074{
75 uint8_t i;
Janos Follathb7a88ec2022-08-19 12:24:40 +010076 unsigned char *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010077 mbedtls_mpi_uint tmp = 0;
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 for (i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++) {
Janos Follath3ca07752022-08-09 11:45:47 +010080 tmp <<= CHAR_BIT;
Janos Follathb7a88ec2022-08-19 12:24:40 +010081 tmp |= (mbedtls_mpi_uint) *a_ptr;
Janos Follath3ca07752022-08-09 11:45:47 +010082 }
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084 return tmp;
Janos Follath3ca07752022-08-09 11:45:47 +010085}
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
Janos Follath3ca07752022-08-09 11:45:47 +010088{
Gilles Peskine449bd832023-01-11 14:50:10 +010089 if (MBEDTLS_IS_BIG_ENDIAN) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +000090 /* Nothing to do on bigendian systems. */
Gilles Peskine449bd832023-01-11 14:50:10 +010091 return a;
92 } else {
93 switch (sizeof(mbedtls_mpi_uint)) {
Dave Rodgman6d23ff62022-11-28 14:38:53 +000094 case 4:
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return (mbedtls_mpi_uint) MBEDTLS_BSWAP32((uint32_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +000096 case 8:
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return (mbedtls_mpi_uint) MBEDTLS_BSWAP64((uint64_t) a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +000098 }
Janos Follath3ca07752022-08-09 11:45:47 +010099
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000100 /* Fall back to C-based reordering if we don't know the byte order
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 * or we couldn't use a compiler-specific builtin. */
102 return mpi_bigendian_to_host_c(a);
Dave Rodgman6d23ff62022-11-28 14:38:53 +0000103 }
Janos Follath3ca07752022-08-09 11:45:47 +0100104}
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
107 size_t A_limbs)
Janos Follath3ca07752022-08-09 11:45:47 +0100108{
109 mbedtls_mpi_uint *cur_limb_left;
110 mbedtls_mpi_uint *cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (A_limbs == 0) {
Janos Follath3ca07752022-08-09 11:45:47 +0100112 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Janos Follath3ca07752022-08-09 11:45:47 +0100114
115 /*
116 * Traverse limbs and
117 * - adapt byte-order in each limb
118 * - swap the limbs themselves.
119 * For that, simultaneously traverse the limbs from left to right
120 * and from right to left, as long as the left index is not bigger
121 * than the right index (it's not a problem if limbs is odd and the
122 * indices coincide in the last iteration).
123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
Janos Follath3ca07752022-08-09 11:45:47 +0100125 cur_limb_left <= cur_limb_right;
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 cur_limb_left++, cur_limb_right--) {
Janos Follath3ca07752022-08-09 11:45:47 +0100127 mbedtls_mpi_uint tmp;
128 /* Note that if cur_limb_left == cur_limb_right,
129 * this code effectively swaps the bytes only once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 tmp = mpi_bigendian_to_host(*cur_limb_left);
131 *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
Janos Follath3ca07752022-08-09 11:45:47 +0100132 *cur_limb_right = tmp;
133 }
134}
135
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200136/* Whether min <= A, in constant time.
137 * A_limbs must be at least 1. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100138unsigned mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
139 const mbedtls_mpi_uint *A,
140 size_t A_limbs)
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200141{
142 /* min <= least significant limb? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 unsigned min_le_lsl = 1 ^ mbedtls_ct_mpi_uint_lt(A[0], min);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200144
Gilles Peskine6b7ce962022-12-15 15:04:33 +0100145 /* limbs other than the least significant one are all zero? */
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200146 mbedtls_mpi_uint msll_mask = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 for (size_t i = 1; i < A_limbs; i++) {
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200148 msll_mask |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 }
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200150 /* The most significant limbs of A are not all zero iff msll_mask != 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 unsigned msll_nonzero = mbedtls_ct_mpi_uint_mask(msll_mask) & 1;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200152
153 /* min <= A iff the lowest limb of A is >= min or the other limbs
154 * are not all zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return min_le_lsl | msll_nonzero;
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200156}
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
159 const mbedtls_mpi_uint *A,
160 size_t limbs,
161 unsigned char assign)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200162{
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (X == A) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200164 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_ct_mpi_uint_cond_assign(limbs, X, A, assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200168}
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
171 mbedtls_mpi_uint *Y,
172 size_t limbs,
173 unsigned char swap)
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200174{
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (X == Y) {
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200176 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 }
Gabor Mezeie9c013c2022-10-10 14:26:57 +0200178
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200179 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask(swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 for (size_t i = 0; i < limbs; i++) {
Gabor Mezeie5b85852022-09-30 13:54:02 +0200183 mbedtls_mpi_uint tmp = X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 X[i] = (X[i] & ~limb_mask) | (Y[i] & limb_mask);
185 Y[i] = (Y[i] & ~limb_mask) | (tmp & limb_mask);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200186 }
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200187}
188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
190 size_t X_limbs,
191 const unsigned char *input,
192 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100193{
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (X_limbs < limbs) {
197 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
198 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (X != NULL) {
201 memset(X, 0, X_limbs * ciL);
Janos Follath3ca07752022-08-09 11:45:47 +0100202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 for (size_t i = 0; i < input_length; i++) {
204 size_t offset = ((i % ciL) << 3);
205 X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
Janos Follathca5688e2022-08-19 12:05:28 +0100206 }
Gabor Mezeibf9da1d2022-08-12 14:11:56 +0200207 }
Janos Follath3ca07752022-08-09 11:45:47 +0100208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100210}
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
213 size_t X_limbs,
214 const unsigned char *input,
215 size_t input_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100216{
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 const size_t limbs = CHARS_TO_LIMBS(input_length);
Janos Follath3ca07752022-08-09 11:45:47 +0100218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (X_limbs < limbs) {
220 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
221 }
Janos Follath2ab2d3e2022-08-11 16:13:53 +0100222
Janos Follathb7a88ec2022-08-19 12:24:40 +0100223 /* If X_limbs is 0, input_length must also be 0 (from previous test).
224 * Nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (X_limbs == 0) {
226 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100227 }
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 memset(X, 0, X_limbs * ciL);
Gabor Mezeic414ba32022-08-12 17:47:39 +0200230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 /* memcpy() with (NULL, 0) is undefined behaviour */
232 if (input_length != 0) {
233 size_t overhead = (X_limbs * ciL) - input_length;
234 unsigned char *Xp = (unsigned char *) X;
235 memcpy(Xp + overhead, input, input_length);
236 }
237
238 mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
239
240 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100241}
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
244 size_t A_limbs,
245 unsigned char *output,
246 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100247{
Janos Follathb7a88ec2022-08-19 12:24:40 +0100248 size_t stored_bytes = A_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100249 size_t bytes_to_copy;
Janos Follath3ca07752022-08-09 11:45:47 +0100250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (stored_bytes < output_length) {
Janos Follath3ca07752022-08-09 11:45:47 +0100252 bytes_to_copy = stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 } else {
Janos Follathb7a88ec2022-08-19 12:24:40 +0100254 bytes_to_copy = output_length;
Janos Follath3ca07752022-08-09 11:45:47 +0100255
Janos Follathaf3f39c2022-08-22 09:06:32 +0100256 /* The output buffer is smaller than the allocated size of A.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100257 * However A may fit if its leading bytes are zero. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
259 if (GET_BYTE(A, i) != 0) {
260 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
261 }
Janos Follath3ca07752022-08-09 11:45:47 +0100262 }
263 }
264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 for (size_t i = 0; i < bytes_to_copy; i++) {
266 output[i] = GET_BYTE(A, i);
Janos Follath3ca07752022-08-09 11:45:47 +0100267 }
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (stored_bytes < output_length) {
270 /* Write trailing 0 bytes */
271 memset(output + stored_bytes, 0, output_length - stored_bytes);
272 }
273
274 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100275}
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
278 size_t X_limbs,
279 unsigned char *output,
280 size_t output_length)
Janos Follath3ca07752022-08-09 11:45:47 +0100281{
282 size_t stored_bytes;
283 size_t bytes_to_copy;
284 unsigned char *p;
Janos Follath3ca07752022-08-09 11:45:47 +0100285
Janos Follathb7a88ec2022-08-19 12:24:40 +0100286 stored_bytes = X_limbs * ciL;
Janos Follath3ca07752022-08-09 11:45:47 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (stored_bytes < output_length) {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100289 /* There is enough space in the output buffer. Write initial
Janos Follath3ca07752022-08-09 11:45:47 +0100290 * null bytes and record the position at which to start
291 * writing the significant bytes. In this case, the execution
292 * trace of this function does not depend on the value of the
293 * number. */
294 bytes_to_copy = stored_bytes;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100295 p = output + output_length - stored_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 memset(output, 0, output_length - stored_bytes);
297 } else {
Janos Follathaf3f39c2022-08-22 09:06:32 +0100298 /* The output buffer is smaller than the allocated size of X.
Janos Follath3ca07752022-08-09 11:45:47 +0100299 * However X may fit if its leading bytes are zero. */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100300 bytes_to_copy = output_length;
301 p = output;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
303 if (GET_BYTE(X, i) != 0) {
304 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
305 }
Janos Follath3ca07752022-08-09 11:45:47 +0100306 }
307 }
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 for (size_t i = 0; i < bytes_to_copy; i++) {
310 p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
311 }
Janos Follath3ca07752022-08-09 11:45:47 +0100312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return 0;
Janos Follath3ca07752022-08-09 11:45:47 +0100314}
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
317 size_t count)
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200318{
319 size_t i, v0, v1;
320 mbedtls_mpi_uint r0 = 0, r1;
321
322 v0 = count / biL;
323 v1 = count & (biL - 1);
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (v0 > limbs || (v0 == limbs && v1 > 0)) {
326 memset(X, 0, limbs * ciL);
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200327 return;
328 }
329
330 /*
331 * shift by count / limb_size
332 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (v0 > 0) {
334 for (i = 0; i < limbs - v0; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200335 X[i] = X[i + v0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 for (; i < limbs; i++) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200339 X[i] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 }
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200341 }
342
343 /*
344 * shift by count % limb_size
345 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (v1 > 0) {
347 for (i = limbs; i > 0; i--) {
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200348 r1 = X[i - 1] << (biL - v1);
349 X[i - 1] >>= v1;
350 X[i - 1] |= r0;
351 r0 = r1;
352 }
353 }
354}
355
Minos Galanakisec09e252023-04-20 14:22:16 +0100356void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
357 size_t count)
Minos Galanakisad808dd2023-04-20 12:18:41 +0100358{
Minos Galanakisec09e252023-04-20 14:22:16 +0100359 size_t i, v0, v1;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100360 mbedtls_mpi_uint r0 = 0, r1;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100361
Minos Galanakisec09e252023-04-20 14:22:16 +0100362 v0 = count / (biL);
363 v1 = count & (biL - 1);
Minos Galanakisad808dd2023-04-20 12:18:41 +0100364
Minos Galanakisec09e252023-04-20 14:22:16 +0100365 if (v0 > limbs || (v0 == limbs && v1 > 0)) {
366 memset(X, 0, limbs * ciL);
367 return;
368 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100369
370 /*
371 * shift by count / limb_size
372 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100373 if (v0 > 0) {
374 for (i = limbs; i > v0; i--) {
375 X[i - 1] = X[i - v0 - 1];
376 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100377
Minos Galanakisec09e252023-04-20 14:22:16 +0100378 for (; i > 0; i--) {
379 X[i - 1] = 0;
380 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100381 }
382
383 /*
384 * shift by count % limb_size
385 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100386 if (v1 > 0) {
387 for (i = v0; i < limbs; i++) {
388 r1 = X[i] >> (biL - v1);
389 X[i] <<= v1;
390 X[i] |= r0;
Minos Galanakisad808dd2023-04-20 12:18:41 +0100391 r0 = r1;
392 }
393 }
Minos Galanakisad808dd2023-04-20 12:18:41 +0100394}
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
397 const mbedtls_mpi_uint *A,
398 const mbedtls_mpi_uint *B,
399 size_t limbs)
Hanno Beckerc9887132022-08-24 12:54:36 +0100400{
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100401 mbedtls_mpi_uint c = 0;
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 for (size_t i = 0; i < limbs; i++) {
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100404 mbedtls_mpi_uint t = c + A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 c = (t < A[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100406 t += B[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 c += (t < B[i]);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100408 X[i] = t;
Hanno Beckerc9887132022-08-24 12:54:36 +0100409 }
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return c;
Hanno Beckerc9887132022-08-24 12:54:36 +0100412}
Gilles Peskinec279b2f2022-09-21 15:38:38 +0200413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
415 const mbedtls_mpi_uint *A,
416 size_t limbs,
417 unsigned cond)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100418{
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100419 mbedtls_mpi_uint c = 0;
420
Tom Cosgrove2701dea2022-09-15 15:00:07 +0100421 /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask(cond);
Tom Cosgrove93549902022-08-30 17:41:23 +0100423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100425 mbedtls_mpi_uint add = mask & A[i];
426 mbedtls_mpi_uint t = c + X[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 c = (t < X[i]);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100428 t += add;
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 c += (t < add);
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100430 X[i] = t;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100431 }
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100434}
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
437 const mbedtls_mpi_uint *A,
438 const mbedtls_mpi_uint *B,
439 size_t limbs)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100440{
441 mbedtls_mpi_uint c = 0;
442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 for (size_t i = 0; i < limbs; i++) {
444 mbedtls_mpi_uint z = (A[i] < c);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100445 mbedtls_mpi_uint t = A[i] - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 c = (t < B[i]) + z;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100447 X[i] = t - B[i];
448 }
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100451}
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
454 const mbedtls_mpi_uint *s, size_t s_len,
455 mbedtls_mpi_uint b)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100456{
457 mbedtls_mpi_uint c = 0; /* carry */
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100458 /*
459 * It is a documented precondition of this function that d_len >= s_len.
460 * If that's not the case, we swap these round: this turns what would be
461 * a buffer overflow into an incorrect result.
462 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (d_len < s_len) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100464 s_len = d_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100466 size_t excess_len = d_len - s_len;
467 size_t steps_x8 = s_len / 8;
468 size_t steps_x1 = s_len & 7;
469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 while (steps_x8--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100471 MULADDC_X8_INIT
472 MULADDC_X8_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 MULADDC_X8_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100474 }
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 while (steps_x1--) {
Tom Cosgroveb4964862022-08-30 11:57:22 +0100477 MULADDC_X1_INIT
478 MULADDC_X1_CORE
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 MULADDC_X1_STOP
Tom Cosgroveb4964862022-08-30 11:57:22 +0100480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 while (excess_len--) {
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100483 *d += c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 c = (*d < c);
Tom Cosgrovef0c8a8c2022-08-30 15:15:02 +0100485 d++;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100486 }
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return c;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100489}
490
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100491void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
492 const mbedtls_mpi_uint *A, size_t A_limbs,
493 const mbedtls_mpi_uint *B, size_t B_limbs)
Hanno Becker4ae890b2022-08-24 16:17:52 +0100494{
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100495 memset(X, 0, (A_limbs + B_limbs) * ciL);
496
497 for (size_t i = 0; i < B_limbs; i++) {
498 (void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]);
499 }
Hanno Becker4ae890b2022-08-24 16:17:52 +0100500}
501
Tom Cosgroveb4964862022-08-30 11:57:22 +0100502/*
503 * Fast Montgomery initialization (thanks to Tom St Denis).
504 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
Tom Cosgroveb4964862022-08-30 11:57:22 +0100506{
507 mbedtls_mpi_uint x = N[0];
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 x += ((N[0] + 2) & 4) << 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 for (unsigned int i = biL; i >= 8; i /= 2) {
512 x *= (2 - (N[0] * x));
513 }
Tom Cosgroveb4964862022-08-30 11:57:22 +0100514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return ~x + 1;
Tom Cosgroveb4964862022-08-30 11:57:22 +0100516}
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
519 const mbedtls_mpi_uint *A,
520 const mbedtls_mpi_uint *B,
521 size_t B_limbs,
522 const mbedtls_mpi_uint *N,
523 size_t AN_limbs,
524 mbedtls_mpi_uint mm,
525 mbedtls_mpi_uint *T)
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100526{
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 memset(T, 0, (2 * AN_limbs + 1) * ciL);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 for (size_t i = 0; i < AN_limbs; i++) {
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100530 /* T = (T + u0*B + u1*N) / 2^biL */
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100531 mbedtls_mpi_uint u0 = A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
535 (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
Tom Cosgrove67c92472022-09-02 13:28:59 +0100536
537 T++;
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100538 }
539
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100540 /*
541 * The result we want is (T >= N) ? T - N : T.
542 *
543 * For better constant-time properties in this function, we always do the
544 * subtraction, with the result in X.
545 *
546 * We also look to see if there was any carry in the final additions in the
547 * loop above.
548 */
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100549
Tom Cosgrove72594632022-08-24 11:51:58 +0100550 mbedtls_mpi_uint carry = T[AN_limbs];
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100552
553 /*
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100554 * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100555 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100556 * T can be in one of 3 ranges:
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100557 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100558 * 1) T < N : (carry, borrow) = (0, 1): we want T
559 * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
560 * 3) T >= R : (carry, borrow) = (1, 1): we want X
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100561 *
Tom Cosgrovef0b22312022-08-31 17:57:34 +0100562 * and (carry, borrow) = (1, 0) can't happen.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100563 *
564 * So the correct return value is already in X if (carry ^ borrow) = 0,
Tom Cosgrove72594632022-08-24 11:51:58 +0100565 * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100566 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_ct_mpi_uint_cond_assign(AN_limbs, X, T, (unsigned char) (carry ^ borrow));
Tom Cosgrove958fd3d2022-08-24 11:08:51 +0100568}
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
571 const mbedtls_mpi *N)
Hanno Beckerec440f22022-08-11 17:29:32 +0100572{
573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
576 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
577 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
578 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
Hanno Beckerec440f22022-08-11 17:29:32 +0100579
580cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return ret;
Hanno Beckerec440f22022-08-11 17:29:32 +0100582}
583
Janos Follath59cbd1b2022-10-28 18:13:43 +0100584MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100585void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
586 const mbedtls_mpi_uint *table,
587 size_t limbs,
588 size_t count,
589 size_t index)
Janos Follathe50f2f12022-10-26 15:14:33 +0100590{
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 for (size_t i = 0; i < count; i++, table += limbs) {
592 unsigned char assign = mbedtls_ct_size_bool_eq(i, index);
593 mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
Janos Follathe50f2f12022-10-26 15:14:33 +0100594 }
595}
596
Gilles Peskine009d1952022-09-09 21:00:00 +0200597/* Fill X with n_bytes random bytes.
598 * X must already have room for those bytes.
599 * The ordering of the bytes returned from the RNG is suitable for
Gilles Peskine22cdd0c2022-10-27 20:15:13 +0200600 * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
601 * mbedtls_mpi_core_random()).
Gilles Peskine009d1952022-09-09 21:00:00 +0200602 */
603int mbedtls_mpi_core_fill_random(
604 mbedtls_mpi_uint *X, size_t X_limbs,
605 size_t n_bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Gilles Peskine009d1952022-09-09 21:00:00 +0200607{
608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 const size_t limbs = CHARS_TO_LIMBS(n_bytes);
610 const size_t overhead = (limbs * ciL) - n_bytes;
Gilles Peskine009d1952022-09-09 21:00:00 +0200611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (X_limbs < limbs) {
613 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
614 }
Gilles Peskine009d1952022-09-09 21:00:00 +0200615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 memset(X, 0, overhead);
617 memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
618 MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
619 mbedtls_mpi_core_bigendian_to_host(X, limbs);
Gilles Peskine009d1952022-09-09 21:00:00 +0200620
621cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return ret;
Gilles Peskine009d1952022-09-09 21:00:00 +0200623}
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
626 mbedtls_mpi_uint min,
627 const mbedtls_mpi_uint *N,
628 size_t limbs,
629 int (*f_rng)(void *, unsigned char *, size_t),
630 void *p_rng)
Gilles Peskine70375b22022-09-21 15:47:23 +0200631{
632 unsigned ge_lower = 1, lt_upper = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
634 size_t n_bytes = (n_bits + 7) / 8;
Gilles Peskine70375b22022-09-21 15:47:23 +0200635 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
636
637 /*
638 * When min == 0, each try has at worst a probability 1/2 of failing
639 * (the msb has a probability 1/2 of being 0, and then the result will
640 * be < N), so after 30 tries failure probability is a most 2**(-30).
641 *
642 * When N is just below a power of 2, as is the case when generating
643 * a random scalar on most elliptic curves, 1 try is enough with
644 * overwhelming probability. When N is just above a power of 2,
645 * as when generating a random scalar on secp224k1, each try has
646 * a probability of failing that is almost 1/2.
647 *
648 * The probabilities are almost the same if min is nonzero but negligible
649 * compared to N. This is always the case when N is crypto-sized, but
650 * it's convenient to support small N for testing purposes. When N
651 * is small, use a higher repeat count, otherwise the probability of
652 * failure is macroscopic.
653 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 int count = (n_bytes > 4 ? 30 : 250);
Gilles Peskine70375b22022-09-21 15:47:23 +0200655
656 /*
657 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
658 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
659 * - use the same byte ordering;
660 * - keep the leftmost n_bits bits of the generated octet string;
661 * - try until result is in the desired range.
662 * This also avoids any bias, which is especially important for ECDSA.
663 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 do {
665 MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
666 n_bytes,
667 f_rng, p_rng));
668 mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
Gilles Peskine70375b22022-09-21 15:47:23 +0200669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (--count == 0) {
Gilles Peskine70375b22022-09-21 15:47:23 +0200671 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
672 goto cleanup;
673 }
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
676 lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
677 } while (ge_lower == 0 || lt_upper == 0);
Gilles Peskine70375b22022-09-21 15:47:23 +0200678
679cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 return ret;
Gilles Peskine70375b22022-09-21 15:47:23 +0200681}
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683static size_t exp_mod_get_window_size(size_t Ebits)
Janos Follathb6673f02022-09-30 14:13:14 +0100684{
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 size_t wsize = (Ebits > 671) ? 6 : (Ebits > 239) ? 5 :
686 (Ebits > 79) ? 4 : 1;
Janos Follathb6673f02022-09-30 14:13:14 +0100687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
689 if (wsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follathb6673f02022-09-30 14:13:14 +0100690 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 }
Janos Follathb6673f02022-09-30 14:13:14 +0100692#endif
693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100695}
696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000698{
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
700 const size_t welem = ((size_t) 1) << wsize;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000701
702 /* How big does each part of the working memory pool need to be? */
703 const size_t table_limbs = welem * AN_limbs;
704 const size_t select_limbs = AN_limbs;
705 const size_t temp_limbs = 2 * AN_limbs + 1;
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 return table_limbs + select_limbs + temp_limbs;
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000708}
709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
711 const mbedtls_mpi_uint *N,
712 size_t AN_limbs,
713 mbedtls_mpi_uint mm,
714 const mbedtls_mpi_uint *RR,
715 size_t welem,
716 mbedtls_mpi_uint *Wtable,
717 mbedtls_mpi_uint *temp)
Gilles Peskine0de0a042022-11-16 20:12:49 +0100718{
Gilles Peskine0de0a042022-11-16 20:12:49 +0100719 /* W[0] = 1 (in Montgomery presentation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 memset(Wtable, 0, AN_limbs * ciL);
Gilles Peskine0de0a042022-11-16 20:12:49 +0100721 Wtable[0] = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100723
Tom Cosgroveecda1862022-12-06 10:46:30 +0000724 /* W[1] = A (already in Montgomery presentation) */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100725 mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 memcpy(W1, A, AN_limbs * ciL);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100727
Gilles Peskine0de0a042022-11-16 20:12:49 +0100728 /* W[i+1] = W[i] * W[1], i >= 2 */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100729 mbedtls_mpi_uint *Wprev = W1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 for (size_t i = 2; i < welem; i++) {
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100731 mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100733 Wprev = Wcur;
734 }
Gilles Peskine0de0a042022-11-16 20:12:49 +0100735}
736
Gilles Peskine7d89d352022-11-16 22:54:14 +0100737/* Exponentiation: X := A^E mod N.
738 *
Tom Cosgroveecda1862022-12-06 10:46:30 +0000739 * A must already be in Montgomery form.
740 *
Gilles Peskine7d89d352022-11-16 22:54:14 +0100741 * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
742 *
743 * RR must contain 2^{2*biL} mod N.
Janos Follath3321b582022-11-22 21:08:33 +0000744 *
745 * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
746 * (The difference is that the body in our loop processes a single bit instead
747 * of a full window.)
Gilles Peskine7d89d352022-11-16 22:54:14 +0100748 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
750 const mbedtls_mpi_uint *A,
751 const mbedtls_mpi_uint *N,
752 size_t AN_limbs,
753 const mbedtls_mpi_uint *E,
754 size_t E_limbs,
755 const mbedtls_mpi_uint *RR,
756 mbedtls_mpi_uint *T)
Janos Follathb6673f02022-09-30 14:13:14 +0100757{
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
759 const size_t welem = ((size_t) 1) << wsize;
Janos Follathb6673f02022-09-30 14:13:14 +0100760
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000761 /* This is how we will use the temporary storage T, which must have space
762 * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
763 const size_t table_limbs = welem * AN_limbs;
764 const size_t select_limbs = AN_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100765
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000766 /* Pointers to specific parts of the temporary working memory pool */
767 mbedtls_mpi_uint *const Wtable = T;
768 mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
769 mbedtls_mpi_uint *const temp = Wselect + select_limbs;
Janos Follathb6673f02022-09-30 14:13:14 +0100770
771 /*
772 * Window precomputation
773 */
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
Gilles Peskinecf979b02022-11-16 20:04:00 +0100776
Gilles Peskine0de0a042022-11-16 20:12:49 +0100777 /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 exp_mod_precompute_window(A, N, AN_limbs,
779 mm, RR,
780 welem, Wtable, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100781
782 /*
Janos Follath0ec6e3f2022-11-14 12:52:08 +0000783 * Fixed window exponentiation
Janos Follathb6673f02022-09-30 14:13:14 +0100784 */
785
786 /* X = 1 (in Montgomery presentation) initially */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 memcpy(X, Wtable, AN_limbs * ciL);
Janos Follathb6673f02022-09-30 14:13:14 +0100788
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100789 /* We'll process the bits of E from most significant
790 * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
791 * (limb_index=0, E_bit_index=0). */
792 size_t E_limb_index = E_limbs;
793 size_t E_bit_index = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100794 /* At any given time, window contains window_bits bits from E.
795 * window_bits can go up to wsize. */
Janos Follathbad42c42022-11-09 14:30:44 +0000796 size_t window_bits = 0;
Gilles Peskine0b270a52022-11-16 22:54:03 +0100797 mbedtls_mpi_uint window = 0;
Gilles Peskinecf979b02022-11-16 20:04:00 +0100798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 do {
Janos Follathb6673f02022-09-30 14:13:14 +0100800 /* Square */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
Janos Follathb6673f02022-09-30 14:13:14 +0100802
Janos Follath3321b582022-11-22 21:08:33 +0000803 /* Move to the next bit of the exponent */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (E_bit_index == 0) {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100805 --E_limb_index;
806 E_bit_index = biL - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 } else {
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100808 --E_bit_index;
809 }
Janos Follath3321b582022-11-22 21:08:33 +0000810 /* Insert next exponent bit into window */
Gilles Peskined83b5cb2022-11-16 20:26:14 +0100811 ++window_bits;
Gilles Peskinec718a3c2022-11-16 20:42:09 +0100812 window <<= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 window |= (E[E_limb_index] >> E_bit_index) & 1;
Gilles Peskine3b63d092022-11-16 22:06:18 +0100814
815 /* Clear window if it's full. Also clear the window at the end,
816 * when we've finished processing the exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (window_bits == wsize ||
818 (E_bit_index == 0 && E_limb_index == 0)) {
Gilles Peskine0b270a52022-11-16 22:54:03 +0100819 /* Select Wtable[window] without leaking window through
820 * memory access patterns. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
822 AN_limbs, welem, window);
Gilles Peskine0b270a52022-11-16 22:54:03 +0100823 /* Multiply X by the selected element. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
825 temp);
Gilles Peskine3b63d092022-11-16 22:06:18 +0100826 window = 0;
827 window_bits = 0;
828 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 } while (!(E_bit_index == 0 && E_limb_index == 0));
Janos Follathb6673f02022-09-30 14:13:14 +0100830}
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
833 const mbedtls_mpi_uint *A,
834 mbedtls_mpi_uint c, /* doubles as carry */
835 size_t limbs)
Hanno Beckerd9b23482022-08-25 08:25:19 +0100836{
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100838 mbedtls_mpi_uint s = A[i];
839 mbedtls_mpi_uint t = s - c;
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 c = (t > s);
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100841 X[i] = t;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100842 }
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 return c;
Hanno Beckerd9b23482022-08-25 08:25:19 +0100845}
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
848 size_t limbs)
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000849{
850 mbedtls_mpi_uint bits = 0;
851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 for (size_t i = 0; i < limbs; i++) {
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000853 bits |= A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 }
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 return bits;
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000857}
858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
860 const mbedtls_mpi_uint *A,
861 const mbedtls_mpi_uint *N,
862 size_t AN_limbs,
863 mbedtls_mpi_uint mm,
864 const mbedtls_mpi_uint *rr,
865 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000866{
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000868}
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
871 const mbedtls_mpi_uint *A,
872 const mbedtls_mpi_uint *N,
873 size_t AN_limbs,
874 mbedtls_mpi_uint mm,
875 mbedtls_mpi_uint *T)
Tom Cosgrove786848b2022-12-13 10:45:19 +0000876{
877 const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000880}
881
Janos Follath3ca07752022-08-09 11:45:47 +0100882#endif /* MBEDTLS_BIGNUM_C */