blob: 2421c1a3ecfa98e357f6f487fea159027316de14 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Multi-precision integer library
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
Simon Butcher15b15d12015-11-26 19:35:03 +000019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcher15b15d12015-11-26 19:35:03 +000021 * The following sources were referenced in the design of this Multi-precision
22 * Integer library:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcher15b15d12015-11-26 19:35:03 +000024 * [1] Handbook of Applied Cryptography - 1997
25 * Menezes, van Oorschot and Vanstone
26 *
27 * [2] Multi-Precision Math
28 * Tom St Denis
29 * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
30 *
31 * [3] GNU Multi-Precision Arithmetic Library
32 * https://gmplib.org/manual/index.html
33 *
Simon Butcherf5ba0452015-12-27 23:01:55 +000034 */
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Gilles Peskinedb09ef62020-06-03 01:43:33 +020036#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/bignum.h"
Janos Follath4614b9a2022-07-21 15:34:47 +010041#include "bignum_core.h"
Chris Jones4c5819c2021-03-03 17:45:34 +000042#include "bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050043#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000044#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020045#include "constant_time_internal.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Dave Rodgman351c71b2021-12-06 17:50:53 +000047#include <limits.h>
Rich Evans00ab4702015-02-06 13:43:58 +000048#include <string.h>
49
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051
Gilles Peskine449bd832023-01-11 14:50:10 +010052#define MPI_VALIDATE_RET(cond) \
53 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA)
54#define MPI_VALIDATE(cond) \
55 MBEDTLS_INTERNAL_VALIDATE(cond)
Gabor Mezei66669142022-08-03 12:52:26 +020056
Gilles Peskine449bd832023-01-11 14:50:10 +010057#define MPI_SIZE_T_MAX ((size_t) -1) /* SIZE_T_MAX is not standard */
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010058
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050059/* Implementation that should never be optimized out by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +010060static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n)
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050061{
Gilles Peskine449bd832023-01-11 14:50:10 +010062 mbedtls_platform_zeroize(v, ciL * n);
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050063}
64
Paul Bakker5121ce52009-01-03 21:22:43 +000065/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000066 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000067 */
Gilles Peskine449bd832023-01-11 14:50:10 +010068void mbedtls_mpi_init(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
Gilles Peskine449bd832023-01-11 14:50:10 +010070 MPI_VALIDATE(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Paul Bakker6c591fa2011-05-05 11:49:20 +000072 X->s = 1;
73 X->n = 0;
74 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000075}
76
77/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000078 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000079 */
Gilles Peskine449bd832023-01-11 14:50:10 +010080void mbedtls_mpi_free(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +000081{
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if (X == NULL) {
Paul Bakker6c591fa2011-05-05 11:49:20 +000083 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010084 }
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (X->p != NULL) {
87 mbedtls_mpi_zeroize(X->p, X->n);
88 mbedtls_free(X->p);
Paul Bakker5121ce52009-01-03 21:22:43 +000089 }
90
Paul Bakker6c591fa2011-05-05 11:49:20 +000091 X->s = 1;
92 X->n = 0;
93 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000094}
95
96/*
97 * Enlarge to the specified number of limbs
98 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Paul Bakker5121ce52009-01-03 21:22:43 +0000100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_mpi_uint *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
105 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
106 }
Paul Bakkerf9688572011-05-05 10:00:45 +0000107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if (X->n < nblimbs) {
109 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
110 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
111 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (X->p != NULL) {
114 memcpy(p, X->p, X->n * ciL);
115 mbedtls_mpi_zeroize(X->p, X->n);
116 mbedtls_free(X->p);
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 }
118
119 X->n = nblimbs;
120 X->p = p;
121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000124}
125
126/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100127 * Resize down as much as possible,
128 * while keeping at least the specified number of limbs
129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100133 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
137 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
138 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100139
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100140 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (X->n <= nblimbs) {
142 return mbedtls_mpi_grow(X, nblimbs);
143 }
Gilles Peskine322752b2020-01-21 13:59:51 +0100144 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 for (i = X->n - 1; i > 0; i--) {
147 if (X->p[i] != 0) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100148 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 }
150 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100151 i++;
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (i < nblimbs) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100154 i = nblimbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
158 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
159 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if (X->p != NULL) {
162 memcpy(p, X->p, i * ciL);
163 mbedtls_mpi_zeroize(X->p, X->n);
164 mbedtls_free(X->p);
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100165 }
166
167 X->n = i;
168 X->p = p;
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return 0;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100171}
172
Gilles Peskineed32b572021-06-02 22:17:52 +0200173/* Resize X to have exactly n limbs and set it to 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
Gilles Peskineed32b572021-06-02 22:17:52 +0200175{
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (limbs == 0) {
177 mbedtls_mpi_free(X);
178 return 0;
179 } else if (X->n == limbs) {
180 memset(X->p, 0, limbs * ciL);
Gilles Peskineed32b572021-06-02 22:17:52 +0200181 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return 0;
183 } else {
184 mbedtls_mpi_free(X);
185 return mbedtls_mpi_grow(X, limbs);
Gilles Peskineed32b572021-06-02 22:17:52 +0200186 }
187}
188
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100189/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200190 * Copy the contents of Y into X.
191 *
192 * This function is not constant-time. Leading zeros in Y may be removed.
193 *
194 * Ensure that X does not shrink. This is not guaranteed by the public API,
195 * but some code in the bignum module relies on this property, for example
196 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100200 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000201 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 MPI_VALIDATE_RET(X != NULL);
203 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (X == Y) {
206 return 0;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200207 }
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 if (Y->n == 0) {
210 if (X->n != 0) {
211 X->s = 1;
212 memset(X->p, 0, X->n * ciL);
213 }
214 return 0;
215 }
216
217 for (i = Y->n - 1; i > 0; i--) {
218 if (Y->p[i] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 }
221 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 i++;
223
224 X->s = Y->s;
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (X->n < i) {
227 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
228 } else {
229 memset(X->p + i, 0, (X->n - i) * ciL);
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100230 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 memcpy(X->p, Y->p, i * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234cleanup:
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000237}
238
239/*
240 * Swap the contents of X and Y
241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 MPI_VALIDATE(X != NULL);
246 MPI_VALIDATE(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 memcpy(&T, X, sizeof(mbedtls_mpi));
249 memcpy(X, Y, sizeof(mbedtls_mpi));
250 memcpy(Y, &T, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +0000251}
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100254{
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (z >= 0) {
256 return z;
257 }
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100258 /* Take care to handle the most negative value (-2^(biL-1)) correctly.
259 * A naive -z would have undefined behavior.
260 * Write this in a way that makes popular compilers happy (GCC, Clang,
261 * MSVC). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100263}
264
Paul Bakker5121ce52009-01-03 21:22:43 +0000265/*
266 * Set value from integer
267 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100268int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000269{
Janos Follath24eed8d2019-11-22 13:21:35 +0000270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
274 memset(X->p, 0, X->n * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 X->p[0] = mpi_sint_abs(z);
277 X->s = (z < 0) ? -1 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000278
279cleanup:
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000282}
283
284/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000285 * Get a specific bit
286 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000288{
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (X->n * biL <= pos) {
292 return 0;
293 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return (X->p[pos / biL] >> (pos % biL)) & 0x01;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000296}
297
298/*
299 * Set a bit to a specific value of 0 or 1
300 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000302{
303 int ret = 0;
304 size_t off = pos / biL;
305 size_t idx = pos % biL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 MPI_VALIDATE_RET(X != NULL);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (val != 0 && val != 1) {
309 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000310 }
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (X->n * biL <= pos) {
313 if (val == 0) {
314 return 0;
315 }
316
317 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
318 }
319
320 X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000322
323cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return ret;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000326}
327
328/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200329 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000332{
Paul Bakker23986e52011-04-24 08:57:21 +0000333 size_t i, j, count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 MBEDTLS_INTERNAL_VALIDATE_RET(X != NULL, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 for (i = 0; i < X->n; i++) {
337 for (j = 0; j < biL; j++, count++) {
338 if (((X->p[i] >> j) & 1) != 0) {
339 return count;
340 }
341 }
342 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000345}
346
347/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200348 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000351{
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return mbedtls_mpi_core_bitlen(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000353}
354
355/*
356 * Return the total size in bytes
357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000359{
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return (mbedtls_mpi_bitlen(X) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000361}
362
363/*
364 * Convert an ASCII character to digit value
365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
Paul Bakker5121ce52009-01-03 21:22:43 +0000367{
368 *d = 255;
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (c >= 0x30 && c <= 0x39) {
371 *d = c - 0x30;
372 }
373 if (c >= 0x41 && c <= 0x46) {
374 *d = c - 0x37;
375 }
376 if (c >= 0x61 && c <= 0x66) {
377 *d = c - 0x57;
378 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (*d >= (mbedtls_mpi_uint) radix) {
381 return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
382 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000385}
386
387/*
388 * Import from an ASCII string
389 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Paul Bakker5121ce52009-01-03 21:22:43 +0000391{
Janos Follath24eed8d2019-11-22 13:21:35 +0000392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000393 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200394 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_mpi_uint d;
396 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 MPI_VALIDATE_RET(X != NULL);
398 MPI_VALIDATE_RET(s != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (radix < 2 || radix > 16) {
401 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine7cba8592021-06-08 18:32:34 +0200402 }
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 mbedtls_mpi_init(&T);
405
406 if (s[0] == 0) {
407 mbedtls_mpi_free(X);
408 return 0;
409 }
410
411 if (s[0] == '-') {
Gilles Peskine80f56732021-04-03 18:26:13 +0200412 ++s;
413 sign = -1;
414 }
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 slen = strlen(s);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (radix == 16) {
419 if (slen > MPI_SIZE_T_MAX >> 2) {
420 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 n = BITS_TO_LIMBS(slen << 2);
424
425 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
426 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
427
428 for (i = slen, j = 0; i > 0; i--, j++) {
429 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
430 X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
431 }
432 } else {
433 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
434
435 for (i = 0; i < slen; i++) {
436 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
437 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
438 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 }
440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
Gilles Peskine80f56732021-04-03 18:26:13 +0200443 X->s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 }
Gilles Peskine80f56732021-04-03 18:26:13 +0200445
Paul Bakker5121ce52009-01-03 21:22:43 +0000446cleanup:
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000451}
452
453/*
Ron Eldora16fa292018-11-20 14:07:01 +0200454 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100456static int mpi_write_hlp(mbedtls_mpi *X, int radix,
457 char **p, const size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000458{
Janos Follath24eed8d2019-11-22 13:21:35 +0000459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200461 size_t length = 0;
462 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 do {
465 if (length >= buflen) {
466 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Ron Eldora16fa292018-11-20 14:07:01 +0200467 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
470 MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
Ron Eldora16fa292018-11-20 14:07:01 +0200471 /*
472 * Write the residue in the current position, as an ASCII character.
473 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if (r < 0xA) {
475 *(--p_end) = (char) ('0' + r);
476 } else {
477 *(--p_end) = (char) ('A' + (r - 0xA));
478 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Ron Eldora16fa292018-11-20 14:07:01 +0200480 length++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 } while (mbedtls_mpi_cmp_int(X, 0) != 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 memmove(*p, p_end, length);
Ron Eldora16fa292018-11-20 14:07:01 +0200484 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486cleanup:
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000489}
490
491/*
492 * Export into an ASCII string
493 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
495 char *buf, size_t buflen, size_t *olen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000496{
Paul Bakker23986e52011-04-24 08:57:21 +0000497 int ret = 0;
498 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 MPI_VALIDATE_RET(X != NULL);
502 MPI_VALIDATE_RET(olen != NULL);
503 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (radix < 2 || radix > 16) {
506 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
507 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */
510 if (radix >= 4) {
511 n >>= 1; /* Number of 4-adic digits necessary to present
Hanno Becker23cfea02019-02-04 09:45:07 +0000512 * `n`. If radix > 4, this might be a strict
513 * overapproximation of the number of
514 * radix-adic digits needed to present `n`. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 }
516 if (radix >= 16) {
517 n >>= 1; /* Number of hexadecimal digits necessary to
Hanno Becker23cfea02019-02-04 09:45:07 +0000518 * present `n`. */
519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 }
Janos Follath80470622019-03-06 13:43:02 +0000521 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000522 n += 1; /* Compensate for the divisions above, which round down `n`
523 * in case it's not even. */
524 n += 1; /* Potential '-'-sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 n += (n & 1); /* Make n even to have enough space for hexadecimal writing,
Hanno Becker23cfea02019-02-04 09:45:07 +0000526 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (buflen < n) {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100529 *olen = n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 }
532
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100533 p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (X->s == -1) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000538 buflen--;
539 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (radix == 16) {
Paul Bakker23986e52011-04-24 08:57:21 +0000542 int c;
543 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 for (i = X->n, k = 0; i > 0; i--) {
546 for (j = ciL; j > 0; j--) {
547 c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (c == 0 && k == 0 && (i + j) != 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000553 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000554 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 k = 1;
556 }
557 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 } else {
559 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (T.s == -1) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000562 T.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 }
567
568 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100569 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571cleanup:
572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000576}
577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000579/*
580 * Read X from an opened file
581 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100582int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Paul Bakker5121ce52009-01-03 21:22:43 +0000583{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000585 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000587 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000588 * Buffer should have space for (short) label and decimal formatted MPI,
589 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000590 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 MPI_VALIDATE_RET(X != NULL);
594 MPI_VALIDATE_RET(fin != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (radix < 2 || radix > 16) {
597 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
598 }
Hanno Becker73d7d792018-12-11 10:35:51 +0000599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 memset(s, 0, sizeof(s));
601 if (fgets(s, sizeof(s) - 1, fin) == NULL) {
602 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
603 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 slen = strlen(s);
606 if (slen == sizeof(s) - 2) {
607 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
608 }
Paul Bakkercb37aa52011-11-30 16:00:20 +0000609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (slen > 0 && s[slen - 1] == '\n') {
611 slen--; s[slen] = '\0';
612 }
613 if (slen > 0 && s[slen - 1] == '\r') {
614 slen--; s[slen] = '\0';
615 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
617 p = s + slen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 while (p-- > s) {
619 if (mpi_get_digit(&d, radix, *p) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 }
622 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return mbedtls_mpi_read_string(X, radix, p + 1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000625}
626
627/*
628 * Write X into an opened file (or stdout if fout == NULL)
629 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Paul Bakker5121ce52009-01-03 21:22:43 +0000631{
Janos Follath24eed8d2019-11-22 13:21:35 +0000632 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000633 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000634 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000635 * Buffer should have space for (short) label and decimal formatted MPI,
636 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000637 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
639 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (radix < 2 || radix > 16) {
642 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
643 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 memset(s, 0, sizeof(s));
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (p == NULL) {
650 p = "";
651 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 plen = strlen(p);
654 slen = strlen(s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 s[slen++] = '\r';
656 s[slen++] = '\n';
657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (fout != NULL) {
659 if (fwrite(p, 1, plen, fout) != plen ||
660 fwrite(s, 1, slen, fout) != slen) {
661 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
662 }
663 } else {
664 mbedtls_printf("%s%s", p, s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
667cleanup:
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673/*
Janos Follatha778a942019-02-13 10:28:28 +0000674 * Import X from unsigned binary data, little endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100675 *
676 * This function is guaranteed to return an MPI with exactly the necessary
677 * number of limbs (in particular, it does not skip 0s in the input).
Janos Follatha778a942019-02-13 10:28:28 +0000678 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
680 const unsigned char *buf, size_t buflen)
Janos Follatha778a942019-02-13 10:28:28 +0000681{
Janos Follath24eed8d2019-11-22 13:21:35 +0000682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 const size_t limbs = CHARS_TO_LIMBS(buflen);
Janos Follatha778a942019-02-13 10:28:28 +0000684
685 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Janos Follatha778a942019-02-13 10:28:28 +0000687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
Janos Follatha778a942019-02-13 10:28:28 +0000689
690cleanup:
691
Janos Follath171a7ef2019-02-15 16:17:45 +0000692 /*
693 * This function is also used to import keys. However, wiping the buffers
694 * upon failure is not necessary because failure only can happen before any
695 * input is copied.
696 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return ret;
Janos Follatha778a942019-02-13 10:28:28 +0000698}
699
700/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 * Import X from unsigned binary data, big endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100702 *
703 * This function is guaranteed to return an MPI with exactly the necessary
704 * number of limbs (in particular, it does not skip 0s in the input).
Paul Bakker5121ce52009-01-03 21:22:43 +0000705 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000707{
Janos Follath24eed8d2019-11-22 13:21:35 +0000708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 const size_t limbs = CHARS_TO_LIMBS(buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 MPI_VALIDATE_RET(X != NULL);
712 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000713
Hanno Becker073c1992017-10-17 15:17:27 +0100714 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719cleanup:
720
Janos Follath171a7ef2019-02-15 16:17:45 +0000721 /*
722 * This function is also used to import keys. However, wiping the buffers
723 * upon failure is not necessary because failure only can happen before any
724 * input is copied.
725 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000727}
728
729/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000730 * Export X into unsigned binary data, little endian
731 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
733 unsigned char *buf, size_t buflen)
Janos Follathe344d0f2019-02-19 16:17:40 +0000734{
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
Janos Follathe344d0f2019-02-19 16:17:40 +0000736}
737
738/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 * Export X into unsigned binary data, big endian
740 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
742 unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000743{
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000745}
746
747/*
748 * Left-shift: X <<= count
749 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000751{
Janos Follath24eed8d2019-11-22 13:21:35 +0000752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000753 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_mpi_uint r0 = 0, r1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 v0 = count / (biL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000758 t1 = count & (biL - 1);
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 i = mbedtls_mpi_bitlen(X) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (X->n * biL < i) {
763 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
764 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
766 ret = 0;
767
768 /*
769 * shift by count / limb_size
770 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (v0 > 0) {
772 for (i = X->n; i > v0; i--) {
Paul Bakker23986e52011-04-24 08:57:21 +0000773 X->p[i - 1] = X->p[i - v0 - 1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 for (; i > 0; i--) {
Paul Bakker23986e52011-04-24 08:57:21 +0000777 X->p[i - 1] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 }
780
781 /*
782 * shift by count % limb_size
783 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if (t1 > 0) {
785 for (i = v0; i < X->n; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 r1 = X->p[i] >> (biL - t1);
787 X->p[i] <<= t1;
788 X->p[i] |= r0;
789 r0 = r1;
790 }
791 }
792
793cleanup:
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000796}
797
798/*
799 * Right-shift: X >>= count
800 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000802{
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 MPI_VALIDATE_RET(X != NULL);
804 if (X->n != 0) {
805 mbedtls_mpi_core_shift_r(X->p, X->n, count);
806 }
807 return 0;
Gilles Peskine66414202022-09-21 15:36:16 +0200808}
809
Paul Bakker5121ce52009-01-03 21:22:43 +0000810/*
811 * Compare unsigned values
812 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000814{
Paul Bakker23986e52011-04-24 08:57:21 +0000815 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 MPI_VALIDATE_RET(X != NULL);
817 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 for (i = X->n; i > 0; i--) {
820 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 }
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 for (j = Y->n; j > 0; j--) {
826 if (Y->p[j - 1] != 0) {
827 break;
828 }
829 }
830
831 if (i == 0 && j == 0) {
832 return 0;
833 }
834
835 if (i > j) {
836 return 1;
837 }
838 if (j > i) {
839 return -1;
840 }
841
842 for (; i > 0; i--) {
843 if (X->p[i - 1] > Y->p[i - 1]) {
844 return 1;
845 }
846 if (X->p[i - 1] < Y->p[i - 1]) {
847 return -1;
848 }
849 }
850
851 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000852}
853
854/*
855 * Compare signed values
856 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000858{
Paul Bakker23986e52011-04-24 08:57:21 +0000859 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 MPI_VALIDATE_RET(X != NULL);
861 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 for (i = X->n; i > 0; i--) {
864 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000865 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 }
868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 for (j = Y->n; j > 0; j--) {
870 if (Y->p[j - 1] != 0) {
871 break;
872 }
873 }
874
875 if (i == 0 && j == 0) {
876 return 0;
877 }
878
879 if (i > j) {
880 return X->s;
881 }
882 if (j > i) {
883 return -Y->s;
884 }
885
886 if (X->s > 0 && Y->s < 0) {
887 return 1;
888 }
889 if (Y->s > 0 && X->s < 0) {
890 return -1;
891 }
892
893 for (; i > 0; i--) {
894 if (X->p[i - 1] > Y->p[i - 1]) {
895 return X->s;
896 }
897 if (X->p[i - 1] < Y->p[i - 1]) {
898 return -X->s;
899 }
900 }
901
902 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000903}
904
Janos Follathee6abce2019-09-05 14:47:19 +0100905/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000906 * Compare signed values
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_mpi Y;
911 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 *p = mpi_sint_abs(z);
915 Y.s = (z < 0) ? -1 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 Y.n = 1;
917 Y.p = p;
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 return mbedtls_mpi_cmp_mpi(X, &Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000920}
921
922/*
923 * Unsigned addition: X = |A| + |B| (HAC 14.7)
924 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +0000926{
Janos Follath24eed8d2019-11-22 13:21:35 +0000927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100928 size_t j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 MPI_VALIDATE_RET(X != NULL);
930 MPI_VALIDATE_RET(A != NULL);
931 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (X == B) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 }
936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (X != A) {
938 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
939 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200940
Paul Bakkerf7ca7b92009-06-20 10:31:06 +0000941 /*
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100942 * X must always be positive as a result of unsigned additions.
Paul Bakkerf7ca7b92009-06-20 10:31:06 +0000943 */
944 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 for (j = B->n; j > 0; j--) {
947 if (B->p[j - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000948 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 }
950 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Gilles Peskinedb14a9d2022-11-15 22:59:00 +0100952 /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
953 * and B is 0 (of any size). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (j == 0) {
955 return 0;
956 }
Gilles Peskinedb14a9d2022-11-15 22:59:00 +0100957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100960 /* j is the number of non-zero limbs of B. Add those to X. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100962 mbedtls_mpi_uint *p = X->p;
963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_mpi_uint c = mbedtls_mpi_core_add(p, p, B->p, j);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100965
966 p += j;
967
968 /* Now propagate any carry */
Paul Bakker5121ce52009-01-03 21:22:43 +0000969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 while (c != 0) {
971 if (j >= X->n) {
972 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100973 p = X->p + j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 }
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 *p += c; c = (*p < c); j++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000977 }
978
979cleanup:
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000982}
983
Paul Bakker5121ce52009-01-03 21:22:43 +0000984/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +0200985 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +0000988{
Janos Follath24eed8d2019-11-22 13:21:35 +0000989 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000990 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +0200991 mbedtls_mpi_uint carry;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 MPI_VALIDATE_RET(X != NULL);
993 MPI_VALIDATE_RET(A != NULL);
994 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 for (n = B->n; n > 0; n--) {
997 if (B->p[n - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 }
1000 }
1001 if (n > A->n) {
Gilles Peskinec8a91772021-01-27 22:30:43 +01001002 /* B >= (2^ciL)^n > A */
1003 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1004 goto cleanup;
1005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001008
1009 /* Set the high limbs of X to match A. Don't touch the lower limbs
1010 * because X might be aliased to B, and we must not overwrite the
1011 * significant digits of B. */
Aaron M. Uckoaf67d2c2023-01-17 11:52:22 -05001012 if (A->n > n && A != X) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
1014 }
1015 if (X->n > A->n) {
1016 memset(X->p + A->n, 0, (X->n - A->n) * ciL);
1017 }
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
1020 if (carry != 0) {
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001021 /* Propagate the carry through the rest of X. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n);
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001023
1024 /* If we have further carry/borrow, the result is negative. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if (carry != 0) {
Gilles Peskine89b41302020-07-23 01:16:46 +02001026 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1027 goto cleanup;
1028 }
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001031 /* X should always be positive as a result of unsigned subtractions. */
1032 X->s = 1;
1033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001036}
1037
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001038/* Common function for signed addition and subtraction.
1039 * Calculate A + B * flip_B where flip_B is 1 or -1.
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041static int add_sub_mpi(mbedtls_mpi *X,
1042 const mbedtls_mpi *A, const mbedtls_mpi *B,
1043 int flip_B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001044{
Hanno Becker73d7d792018-12-11 10:35:51 +00001045 int ret, s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 MPI_VALIDATE_RET(X != NULL);
1047 MPI_VALIDATE_RET(A != NULL);
1048 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Hanno Becker73d7d792018-12-11 10:35:51 +00001050 s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (A->s * B->s * flip_B < 0) {
1052 int cmp = mbedtls_mpi_cmp_abs(A, B);
1053 if (cmp >= 0) {
1054 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001055 /* If |A| = |B|, the result is 0 and we must set the sign bit
1056 * to +1 regardless of which of A or B was negative. Otherwise,
1057 * since |A| > |B|, the sign is the sign of A. */
1058 X->s = cmp == 0 ? 1 : s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 } else {
1060 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001061 /* Since |A| < |B|, the sign is the opposite of A. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 X->s = -s;
1063 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 } else {
1065 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 X->s = s;
1067 }
1068
1069cleanup:
1070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001072}
1073
1074/*
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001075 * Signed addition: X = A + B
1076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001078{
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return add_sub_mpi(X, A, B, 1);
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001080}
1081
1082/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001083 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001086{
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return add_sub_mpi(X, A, B, -1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001088}
1089
1090/*
1091 * Signed addition: X = A + b
1092 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001094{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001095 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 MPI_VALIDATE_RET(X != NULL);
1098 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 p[0] = mpi_sint_abs(b);
1101 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001102 B.n = 1;
1103 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 return mbedtls_mpi_add_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001106}
1107
1108/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001109 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001112{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001113 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 MPI_VALIDATE_RET(X != NULL);
1116 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 p[0] = mpi_sint_abs(b);
1119 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001120 B.n = 1;
1121 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 return mbedtls_mpi_sub_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001124}
1125
Paul Bakker5121ce52009-01-03 21:22:43 +00001126/*
1127 * Baseline multiplication: X = A * B (HAC 14.12)
1128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001130{
Janos Follath24eed8d2019-11-22 13:21:35 +00001131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker1772e052022-04-13 06:51:40 +01001132 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 mbedtls_mpi TA, TB;
Hanno Beckerda763de2022-04-13 06:50:02 +01001134 int result_is_zero = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 MPI_VALIDATE_RET(X != NULL);
1136 MPI_VALIDATE_RET(A != NULL);
1137 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001139 mbedtls_mpi_init(&TA);
1140 mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (X == A) {
1143 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
1144 }
1145 if (X == B) {
1146 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
1147 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 for (i = A->n; i > 0; i--) {
1150 if (A->p[i - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001151 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 }
1153 }
1154 if (i == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001155 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 for (j = B->n; j > 0; j--) {
1159 if (B->p[j - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001160 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 }
1162 }
1163 if (j == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001164 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
1168 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +00001169
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001170 mbedtls_mpi_core_mul(X->p, A->p, i, B->p, j);
Paul Bakker5121ce52009-01-03 21:22:43 +00001171
Hanno Beckerda763de2022-04-13 06:50:02 +01001172 /* If the result is 0, we don't shortcut the operation, which reduces
1173 * but does not eliminate side channels leaking the zero-ness. We do
1174 * need to take care to set the sign bit properly since the library does
1175 * not fully support an MPI object with a value of 0 and s == -1. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (result_is_zero) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001177 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 } else {
Hanno Beckerda763de2022-04-13 06:50:02 +01001179 X->s = A->s * B->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001181
1182cleanup:
1183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
Paul Bakker5121ce52009-01-03 21:22:43 +00001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001187}
1188
1189/*
1190 * Baseline multiplication: X = A * b
1191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001193{
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 MPI_VALIDATE_RET(X != NULL);
1195 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001196
Hanno Becker35771312022-04-14 11:52:11 +01001197 size_t n = A->n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 while (n > 0 && A->p[n - 1] == 0) {
Hanno Becker35771312022-04-14 11:52:11 +01001199 --n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 }
Hanno Becker35771312022-04-14 11:52:11 +01001201
Hanno Becker74a11a32022-04-06 06:27:00 +01001202 /* The general method below doesn't work if b==0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (b == 0 || n == 0) {
1204 return mbedtls_mpi_lset(X, 0);
1205 }
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001206
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001207 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001209 /* In general, A * b requires 1 limb more than b. If
1210 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1211 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001212 * copy() will take care of the growth if needed. However, experimentally,
1213 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001214 * calls to calloc() in ECP code, presumably because it reuses the
1215 * same mpi for a while and this way the mpi is more likely to directly
Hanno Becker9137b9c2022-04-12 10:51:54 +01001216 * grow to its final size.
1217 *
1218 * Note that calculating A*b as 0 + A*b doesn't work as-is because
1219 * A,X can be the same. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
1221 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1222 mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001223
1224cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001226}
1227
1228/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001229 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1230 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001232static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
1233 mbedtls_mpi_uint u0,
1234 mbedtls_mpi_uint d,
1235 mbedtls_mpi_uint *r)
Simon Butcher15b15d12015-11-26 19:35:03 +00001236{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001237#if defined(MBEDTLS_HAVE_UDBL)
1238 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001239#else
Simon Butcher9803d072016-01-03 00:24:34 +00001240 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001242 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1243 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001244 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001245#endif
1246
Simon Butcher15b15d12015-11-26 19:35:03 +00001247 /*
1248 * Check for overflow
1249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (0 == d || u1 >= d) {
1251 if (r != NULL) {
1252 *r = ~(mbedtls_mpi_uint) 0u;
1253 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 return ~(mbedtls_mpi_uint) 0u;
Simon Butcher15b15d12015-11-26 19:35:03 +00001256 }
1257
1258#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001259 dividend = (mbedtls_t_udbl) u1 << biL;
1260 dividend |= (mbedtls_t_udbl) u0;
1261 quotient = dividend / d;
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
1263 quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
1264 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if (r != NULL) {
1267 *r = (mbedtls_mpi_uint) (dividend - (quotient * d));
1268 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001269
1270 return (mbedtls_mpi_uint) quotient;
1271#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001272
1273 /*
1274 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1275 * Vol. 2 - Seminumerical Algorithms, Knuth
1276 */
1277
1278 /*
1279 * Normalize the divisor, d, and dividend, u0, u1
1280 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 s = mbedtls_mpi_core_clz(d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001282 d = d << s;
1283
1284 u1 = u1 << s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
Simon Butcher15b15d12015-11-26 19:35:03 +00001286 u0 = u0 << s;
1287
1288 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001289 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001290
1291 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001292 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001293
1294 /*
1295 * Find the first quotient and remainder
1296 */
1297 q1 = u1 / d1;
1298 r0 = u1 - d1 * q1;
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001301 q1 -= 1;
1302 r0 += d1;
1303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 if (r0 >= radix) {
1305 break;
1306 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001307 }
1308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 rAX = (u1 * radix) + (u0_msw - q1 * d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001310 q0 = rAX / d1;
1311 r0 = rAX - q0 * d1;
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001314 q0 -= 1;
1315 r0 += d1;
1316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 if (r0 >= radix) {
1318 break;
1319 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001320 }
1321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if (r != NULL) {
1323 *r = (rAX * radix + u0_lsw - q0 * d) >> s;
1324 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001325
1326 quotient = q1 * radix + q0;
1327
1328 return quotient;
1329#endif
1330}
1331
1332/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1336 const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001337{
Janos Follath24eed8d2019-11-22 13:21:35 +00001338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001339 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001341 mbedtls_mpi_uint TP2[3];
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 MPI_VALIDATE_RET(A != NULL);
1343 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (mbedtls_mpi_cmp_int(B, 0) == 0) {
1346 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
1350 mbedtls_mpi_init(&T1);
Alexander Kd19a1932019-11-01 18:20:42 +03001351 /*
1352 * Avoid dynamic memory allocations for constant-size T2.
1353 *
1354 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1355 * so nobody increase the size of the MPI and we're safe to use an on-stack
1356 * buffer.
1357 */
Alexander K35d6d462019-10-31 14:46:45 +03001358 T2.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 T2.n = sizeof(TP2) / sizeof(*TP2);
Alexander Kd19a1932019-11-01 18:20:42 +03001360 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (mbedtls_mpi_cmp_abs(A, B) < 0) {
1363 if (Q != NULL) {
1364 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
1365 }
1366 if (R != NULL) {
1367 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
1368 }
1369 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 }
1371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
1373 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 X.s = Y.s = 1;
1375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
1377 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
1378 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 k = mbedtls_mpi_bitlen(&Y) % biL;
1381 if (k < biL - 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 k = biL - 1 - k;
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
1384 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
1385 } else {
1386 k = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
1389 n = X.n - 1;
1390 t = Y.n - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 Z.p[n - t]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 for (i = n; i > t; i--) {
1400 if (X.p[i] >= Y.p[t]) {
1401 Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
1402 } else {
1403 Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
1404 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 }
1406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
1408 T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
Alexander K35d6d462019-10-31 14:46:45 +03001409 T2.p[2] = X.p[i];
1410
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 Z.p[i - t - 1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 do {
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 Z.p[i - t - 1]--;
1414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
1416 T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 T1.p[1] = Y.p[t];
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
1419 } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
1422 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1423 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
1426 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
1427 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1428 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 Z.p[i - t - 1]--;
1430 }
1431 }
1432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (Q != NULL) {
1434 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 Q->s = A->s * B->s;
1436 }
1437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (R != NULL) {
1439 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
Paul Bakkerf02c5642012-11-13 10:25:21 +00001440 X.s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (mbedtls_mpi_cmp_int(R, 0) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 R->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 }
1447
1448cleanup:
1449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
1451 mbedtls_mpi_free(&T1);
1452 mbedtls_platform_zeroize(TP2, sizeof(TP2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001455}
1456
1457/*
1458 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
1461 const mbedtls_mpi *A,
1462 mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001463{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001464 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 p[0] = mpi_sint_abs(b);
1469 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001470 B.n = 1;
1471 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 return mbedtls_mpi_div_mpi(Q, R, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001474}
1475
1476/*
1477 * Modulo: R = A mod B
1478 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001480{
Janos Follath24eed8d2019-11-22 13:21:35 +00001481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 MPI_VALIDATE_RET(R != NULL);
1483 MPI_VALIDATE_RET(A != NULL);
1484 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (mbedtls_mpi_cmp_int(B, 0) < 0) {
1487 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1488 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 while (mbedtls_mpi_cmp_int(R, 0) < 0) {
1493 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
1494 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
1497 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
1498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001499
1500cleanup:
1501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001503}
1504
1505/*
1506 * Modulo: r = A mod b
1507 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001508int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001509{
Paul Bakker23986e52011-04-24 08:57:21 +00001510 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 mbedtls_mpi_uint x, y, z;
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 MPI_VALIDATE_RET(r != NULL);
1513 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (b == 0) {
1516 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (b < 0) {
1520 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001522
1523 /*
1524 * handle trivial cases
1525 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (b == 1 || A->n == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 *r = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 }
1530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 if (b == 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 *r = A->p[0] & 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 }
1535
1536 /*
1537 * general case
1538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 for (i = A->n, y = 0; i > 0; i--) {
Paul Bakker23986e52011-04-24 08:57:21 +00001540 x = A->p[i - 1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 z = y / b;
1543 y -= z * b;
1544
1545 x <<= biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 z = y / b;
1548 y -= z * b;
1549 }
1550
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001551 /*
1552 * If A is negative, then the current y represents a negative value.
1553 * Flipping it to the positive side.
1554 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (A->s < 0 && y != 0) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001556 y = b - y;
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001558
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 *r = y;
1560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001562}
1563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00001565{
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 *mm = mbedtls_mpi_core_montmul_init(N->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001567}
1568
Tom Cosgrove93842842022-08-05 16:59:43 +01001569/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1570 *
1571 * \param[in,out] A One of the numbers to multiply.
1572 * It must have at least as many limbs as N
1573 * (A->n >= N->n), and any limbs beyond n are ignored.
1574 * On successful completion, A contains the result of
1575 * the multiplication A * B * R^-1 mod N where
1576 * R = (2^ciL)^n.
1577 * \param[in] B One of the numbers to multiply.
1578 * It must be nonzero and must not have more limbs than N
1579 * (B->n <= N->n).
Tom Cosgrove40d22942022-08-17 06:42:44 +01001580 * \param[in] N The modulus. \p N must be odd.
Tom Cosgrove93842842022-08-05 16:59:43 +01001581 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1582 * This is -N^-1 mod 2^ciL.
1583 * \param[in,out] T A bignum for temporary storage.
1584 * It must be at least twice the limb size of N plus 1
1585 * (T->n >= 2 * N->n + 1).
1586 * Its initial content is unused and
1587 * its final content is indeterminate.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +01001588 * It does not get reallocated.
Tom Cosgrove93842842022-08-05 16:59:43 +01001589 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001590static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
1591 const mbedtls_mpi *N, mbedtls_mpi_uint mm,
1592 mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001593{
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 mbedtls_mpi_core_montmul(A->p, A->p, B->p, B->n, N->p, N->n, mm, T->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001595}
1596
1597/*
1598 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02001599 *
Tom Cosgrove93842842022-08-05 16:59:43 +01001600 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00001601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
1603 mbedtls_mpi_uint mm, mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 mbedtls_mpi_uint z = 1;
1606 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00001607
Paul Bakker8ddb6452013-02-27 14:56:33 +01001608 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 U.p = &z;
1610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 mpi_montmul(A, &U, N, mm, T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001612}
1613
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001614/**
1615 * Select an MPI from a table without leaking the index.
1616 *
1617 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1618 * reads the entire table in order to avoid leaking the value of idx to an
1619 * attacker able to observe memory access patterns.
1620 *
1621 * \param[out] R Where to write the selected MPI.
1622 * \param[in] T The table to read from.
1623 * \param[in] T_size The number of elements in the table.
1624 * \param[in] idx The index of the element to select;
1625 * this must satisfy 0 <= idx < T_size.
1626 *
1627 * \return \c 0 on success, or a negative error code.
1628 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001629static int mpi_select(mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx)
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001630{
1631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 for (size_t i = 0; i < T_size; i++) {
1634 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
1635 (unsigned char) mbedtls_ct_size_bool_eq(i,
1636 idx)));
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001637 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001638
1639cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 return ret;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001641}
1642
Paul Bakker5121ce52009-01-03 21:22:43 +00001643/*
1644 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1647 const mbedtls_mpi *E, const mbedtls_mpi *N,
1648 mbedtls_mpi *prec_RR)
Paul Bakker5121ce52009-01-03 21:22:43 +00001649{
Janos Follath24eed8d2019-11-22 13:21:35 +00001650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath74601202022-11-21 15:54:20 +00001651 size_t window_bitsize;
Paul Bakker23986e52011-04-24 08:57:21 +00001652 size_t i, j, nblimbs;
1653 size_t bufsize, nbits;
Paul Elliott1748de12023-02-13 15:35:35 +00001654 size_t exponent_bits_in_window = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 mbedtls_mpi_uint ei, mm, state;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001657 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 MPI_VALIDATE_RET(X != NULL);
1660 MPI_VALIDATE_RET(A != NULL);
1661 MPI_VALIDATE_RET(E != NULL);
1662 MPI_VALIDATE_RET(N != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1665 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1669 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1670 }
Paul Bakkerf6198c12012-05-16 08:02:29 +00001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1673 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1674 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1675 }
Chris Jones9246d042020-11-25 15:12:39 +00001676
Paul Bakkerf6198c12012-05-16 08:02:29 +00001677 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 * Init temps and window size
1679 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 mpi_montg_init(&mm, N);
1681 mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
1682 mbedtls_mpi_init(&Apos);
1683 mbedtls_mpi_init(&WW);
1684 memset(W, 0, sizeof(W));
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 i = mbedtls_mpi_bitlen(E);
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
1689 (i > 79) ? 4 : (i > 23) ? 3 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
1692 if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follath7fa11b82022-11-21 14:48:02 +00001693 window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 }
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001695#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001696
Janos Follathc8d66d52022-11-22 10:47:10 +00001697 const size_t w_table_used_size = (size_t) 1 << window_bitsize;
Janos Follath06000952022-11-22 10:18:06 +00001698
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 /*
Janos Follathbe54ca72022-11-21 16:14:54 +00001700 * This function is not constant-trace: its memory accesses depend on the
1701 * exponent value. To defend against timing attacks, callers (such as RSA
1702 * and DHM) should use exponent blinding. However this is not enough if the
1703 * adversary can find the exponent in a single trace, so this function
1704 * takes extra precautions against adversaries who can observe memory
1705 * access patterns.
Janos Follathf08b40e2022-11-11 15:56:38 +00001706 *
Janos Follathbe54ca72022-11-21 16:14:54 +00001707 * This function performs a series of multiplications by table elements and
1708 * squarings, and we want the prevent the adversary from finding out which
1709 * table element was used, and from distinguishing between multiplications
1710 * and squarings. Firstly, when multiplying by an element of the window
1711 * W[i], we do a constant-trace table lookup to obfuscate i. This leaves
1712 * squarings as having a different memory access patterns from other
1713 * multiplications. So secondly, we put the accumulator X in the table as
1714 * well, and also do a constant-trace table lookup to multiply by X.
1715 *
1716 * This way, all multiplications take the form of a lookup-and-multiply.
1717 * The number of lookup-and-multiply operations inside each iteration of
1718 * the main loop still depends on the bits of the exponent, but since the
1719 * other operations in the loop don't have an easily recognizable memory
1720 * trace, an adversary is unlikely to be able to observe the exact
1721 * patterns.
1722 *
1723 * An adversary may still be able to recover the exponent if they can
1724 * observe both memory accesses and branches. However, branch prediction
1725 * exploitation typically requires many traces of execution over the same
1726 * data, which is defeated by randomized blinding.
Janos Follath84461482022-11-21 14:31:22 +00001727 *
1728 * To achieve this, we make a copy of X and we use the table entry in each
1729 * calculation from this point on.
Janos Follath8e7d6a02022-10-04 13:27:40 +01001730 */
Janos Follathc8d66d52022-11-22 10:47:10 +00001731 const size_t x_index = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 mbedtls_mpi_init(&W[x_index]);
1733 mbedtls_mpi_copy(&W[x_index], X);
Janos Follath84461482022-11-21 14:31:22 +00001734
Paul Bakker5121ce52009-01-03 21:22:43 +00001735 j = N->n + 1;
Tom Cosgrove93842842022-08-05 16:59:43 +01001736 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
Paul Bakker5121ce52009-01-03 21:22:43 +00001737 * and mpi_montred() calls later. Here we ensure that W[1] and X are
1738 * large enough, and later we'll grow other W[i] to the same length.
1739 * They must not be shrunk midway through this function!
1740 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
1742 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], j));
1743 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
1745 /*
Paul Bakker50546922012-05-19 08:40:49 +00001746 * Compensate for negative A (and correct at the end)
1747 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 neg = (A->s == -1);
1749 if (neg) {
1750 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
Paul Bakker50546922012-05-19 08:40:49 +00001751 Apos.s = 1;
1752 A = &Apos;
1753 }
1754
1755 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001756 * If 1st call, pre-compute R^2 mod N
1757 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (prec_RR == NULL || prec_RR->p == NULL) {
1759 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&RR, 1));
1760 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&RR, N->n * 2 * biL));
1761 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&RR, &RR, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if (prec_RR != NULL) {
1764 memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1765 }
1766 } else {
1767 memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001769
1770 /*
1771 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1772 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
1774 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001775 /* This should be a no-op because W[1] is already that large before
1776 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
Tom Cosgrove93842842022-08-05 16:59:43 +01001777 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
1779 } else {
1780 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001781 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001783 /* Note that this is safe because W[1] always has at least N->n limbs
1784 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 mpi_montmul(&W[1], &RR, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
1787 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001788 * W[x_index] = R^2 * R^-1 mod N = R mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
1791 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Janos Follathc8d66d52022-11-22 10:47:10 +00001793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (window_bitsize > 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001795 /*
Janos Follath74601202022-11-21 15:54:20 +00001796 * W[i] = W[1] ^ i
1797 *
1798 * The first bit of the sliding window is always 1 and therefore we
1799 * only need to store the second half of the table.
Janos Follathc8d66d52022-11-22 10:47:10 +00001800 *
1801 * (There are two special elements in the table: W[0] for the
1802 * accumulator/result and W[1] for A in Montgomery form. Both of these
1803 * are already set at this point.)
Paul Bakker5121ce52009-01-03 21:22:43 +00001804 */
Janos Follath74601202022-11-21 15:54:20 +00001805 j = w_table_used_size / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
1808 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 for (i = 0; i < window_bitsize - 1; i++) {
1811 mpi_montmul(&W[j], &W[j], N, mm, &T);
1812 }
Paul Bakker0d7702c2013-10-29 16:18:35 +01001813
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 /*
1815 * W[i] = W[i - 1] * W[1]
1816 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 for (i = j + 1; i < w_table_used_size; i++) {
1818 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
1819 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 mpi_montmul(&W[i], &W[1], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001822 }
1823 }
1824
1825 nblimbs = E->n;
1826 bufsize = 0;
1827 nbits = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 state = 0;
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 while (1) {
1831 if (bufsize == 0) {
1832 if (nblimbs == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001835
Paul Bakker0d7702c2013-10-29 16:18:35 +01001836 nblimbs--;
1837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 bufsize = sizeof(mbedtls_mpi_uint) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 }
1840
1841 bufsize--;
1842
1843 ei = (E->p[nblimbs] >> bufsize) & 1;
1844
1845 /*
1846 * skip leading 0s
1847 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 if (ei == 0 && state == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (ei == 0 && state == 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001854 * out of window, square W[x_index]
Paul Bakker5121ce52009-01-03 21:22:43 +00001855 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1857 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 continue;
1859 }
1860
1861 /*
1862 * add ei to current window
1863 */
1864 state = 2;
1865
1866 nbits++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 exponent_bits_in_window |= (ei << (window_bitsize - nbits));
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 if (nbits == window_bitsize) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001871 * W[x_index] = W[x_index]^window_bitsize R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 for (i = 0; i < window_bitsize; i++) {
1874 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1875 x_index));
1876 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001877 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
1879 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001880 * W[x_index] = W[x_index] * W[exponent_bits_in_window] R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1883 exponent_bits_in_window));
1884 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
1886 state--;
1887 nbits = 0;
Janos Follath7fa11b82022-11-21 14:48:02 +00001888 exponent_bits_in_window = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 }
1890 }
1891
1892 /*
1893 * process the remaining bits
1894 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 for (i = 0; i < nbits; i++) {
1896 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1897 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001898
Janos Follath7fa11b82022-11-21 14:48:02 +00001899 exponent_bits_in_window <<= 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
1902 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
1903 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001904 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 }
1906
1907 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001908 * W[x_index] = A^E * R * R^-1 mod N = A^E mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
Janos Follath8e7d6a02022-10-04 13:27:40 +01001913 W[x_index].s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
Paul Bakkerf6198c12012-05-16 08:02:29 +00001915 }
1916
Janos Follath8e7d6a02022-10-04 13:27:40 +01001917 /*
1918 * Load the result in the output variable.
1919 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 mbedtls_mpi_copy(X, &W[x_index]);
Janos Follath8e7d6a02022-10-04 13:27:40 +01001921
Paul Bakker5121ce52009-01-03 21:22:43 +00001922cleanup:
1923
Janos Follathb2c2fca2022-11-21 15:05:31 +00001924 /* The first bit of the sliding window is always 1 and therefore the first
1925 * half of the table was unused. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 for (i = w_table_used_size/2; i < w_table_used_size; i++) {
1927 mbedtls_mpi_free(&W[i]);
1928 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 mbedtls_mpi_free(&W[x_index]);
1931 mbedtls_mpi_free(&W[1]);
1932 mbedtls_mpi_free(&T);
1933 mbedtls_mpi_free(&Apos);
1934 mbedtls_mpi_free(&WW);
Paul Bakker6c591fa2011-05-05 11:49:20 +00001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (prec_RR == NULL || prec_RR->p == NULL) {
1937 mbedtls_mpi_free(&RR);
1938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001941}
1942
Paul Bakker5121ce52009-01-03 21:22:43 +00001943/*
1944 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
1945 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001947{
Janos Follath24eed8d2019-11-22 13:21:35 +00001948 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001949 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03001950 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 MPI_VALIDATE_RET(G != NULL);
1953 MPI_VALIDATE_RET(A != NULL);
1954 MPI_VALIDATE_RET(B != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00001955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
1959 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 lz = mbedtls_mpi_lsb(&TA);
1962 lzt = mbedtls_mpi_lsb(&TB);
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001963
Gilles Peskine27253bc2021-06-09 13:26:43 +02001964 /* The loop below gives the correct result when A==0 but not when B==0.
1965 * So have a special case for B==0. Leverage the fact that we just
1966 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
1967 * slightly more efficient than cmp_int(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
1969 ret = mbedtls_mpi_copy(G, A);
Gilles Peskine27253bc2021-06-09 13:26:43 +02001970 goto cleanup;
1971 }
1972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 if (lzt < lz) {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001974 lz = lzt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001976
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 TA.s = TB.s = 1;
1978
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001979 /* We mostly follow the procedure described in HAC 14.54, but with some
1980 * minor differences:
1981 * - Sequences of multiplications or divisions by 2 are grouped into a
1982 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02001983 * - The procedure in HAC assumes that 0 < TB <= TA.
1984 * - The condition TB <= TA is not actually necessary for correctness.
1985 * TA and TB have symmetric roles except for the loop termination
1986 * condition, and the shifts at the beginning of the loop body
1987 * remove any significance from the ordering of TA vs TB before
1988 * the shifts.
1989 * - If TA = 0, the loop goes through 0 iterations and the result is
1990 * correctly TB.
1991 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001992 *
1993 * For the correctness proof below, decompose the original values of
1994 * A and B as
1995 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
1996 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
1997 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
1998 * and gcd(A',B') is odd or 0.
1999 *
2000 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2001 * The code maintains the following invariant:
2002 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02002003 */
2004
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002005 /* Proof that the loop terminates:
2006 * At each iteration, either the right-shift by 1 is made on a nonzero
2007 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2008 * by at least 1, or the right-shift by 1 is made on zero and then
2009 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2010 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002013 /* Divisions by 2 preserve the invariant (I). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
2015 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002017 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2018 * TA-TB is even so the division by 2 has an integer result.
2019 * Invariant (I) is preserved since any odd divisor of both TA and TB
2020 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002021 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002022 * divides TA.
2023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
2025 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
2026 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
2027 } else {
2028 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
2029 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002031 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 }
2033
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002034 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2035 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2036 * - If there was at least one loop iteration, then one of TA or TB is odd,
2037 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2038 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2039 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002040 * In this case, lz = 0 and B = TB so gcd(A,B) = B = 2^lz * TB as well.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002041 */
2042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
2044 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
2046cleanup:
2047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002051}
2052
Paul Bakker33dc46b2014-04-30 16:11:39 +02002053/*
2054 * Fill X with size bytes of random.
Gilles Peskine22cdd0c2022-10-27 20:15:13 +02002055 * The bytes returned from the RNG are used in a specific order which
2056 * is suitable for deterministic ECDSA (see the specification of
2057 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
Paul Bakker33dc46b2014-04-30 16:11:39 +02002058 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002059int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
2060 int (*f_rng)(void *, unsigned char *, size_t),
2061 void *p_rng)
Paul Bakker287781a2011-03-26 13:18:49 +00002062{
Janos Follath24eed8d2019-11-22 13:21:35 +00002063 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 const size_t limbs = CHARS_TO_LIMBS(size);
Hanno Beckerda1655a2017-10-18 14:21:44 +01002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 MPI_VALIDATE_RET(X != NULL);
2067 MPI_VALIDATE_RET(f_rng != NULL);
Paul Bakker33dc46b2014-04-30 16:11:39 +02002068
Hanno Beckerda1655a2017-10-18 14:21:44 +01002069 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
2071 if (size == 0) {
2072 return 0;
2073 }
Paul Bakker287781a2011-03-26 13:18:49 +00002074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +00002076
2077cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 return ret;
Paul Bakker287781a2011-03-26 13:18:49 +00002079}
2080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081int mbedtls_mpi_random(mbedtls_mpi *X,
2082 mbedtls_mpi_sint min,
2083 const mbedtls_mpi *N,
2084 int (*f_rng)(void *, unsigned char *, size_t),
2085 void *p_rng)
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002086{
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (min < 0) {
2088 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2089 }
2090 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
2091 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2092 }
Gilles Peskine1e918f42021-03-29 22:14:51 +02002093
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002094 /* Ensure that target MPI has exactly the same number of limbs
2095 * as the upper bound, even if the upper bound has leading zeros.
Gilles Peskine6b7ce962022-12-15 15:04:33 +01002096 * This is necessary for mbedtls_mpi_core_random. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 int ret = mbedtls_mpi_resize_clear(X, N->n);
2098 if (ret != 0) {
2099 return ret;
2100 }
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002103}
2104
Paul Bakker5121ce52009-01-03 21:22:43 +00002105/*
2106 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00002109{
Janos Follath24eed8d2019-11-22 13:21:35 +00002110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 MPI_VALIDATE_RET(X != NULL);
2113 MPI_VALIDATE_RET(A != NULL);
2114 MPI_VALIDATE_RET(N != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
2117 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2118 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
2121 mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
2122 mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 goto cleanup;
2129 }
2130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
2132 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
2133 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
2134 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
2137 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
2138 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
2139 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 do {
2142 while ((TU.p[0] & 1) == 0) {
2143 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
2146 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
2147 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 }
2149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
2151 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
2153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 while ((TV.p[0] & 1) == 0) {
2155 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
2158 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
2159 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 }
2161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
2163 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 }
2165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
2167 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
2168 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
2169 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
2170 } else {
2171 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
2172 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
2173 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
2176
2177 while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
2178 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
2182 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
2183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
2187cleanup:
2188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
2190 mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
2191 mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002194}
2195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002197
Paul Bakker5121ce52009-01-03 21:22:43 +00002198static const int small_prime[] =
2199{
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 3, 5, 7, 11, 13, 17, 19, 23,
2201 29, 31, 37, 41, 43, 47, 53, 59,
2202 61, 67, 71, 73, 79, 83, 89, 97,
2203 101, 103, 107, 109, 113, 127, 131, 137,
2204 139, 149, 151, 157, 163, 167, 173, 179,
2205 181, 191, 193, 197, 199, 211, 223, 227,
2206 229, 233, 239, 241, 251, 257, 263, 269,
2207 271, 277, 281, 283, 293, 307, 311, 313,
2208 317, 331, 337, 347, 349, 353, 359, 367,
2209 373, 379, 383, 389, 397, 401, 409, 419,
2210 421, 431, 433, 439, 443, 449, 457, 461,
2211 463, 467, 479, 487, 491, 499, 503, 509,
2212 521, 523, 541, 547, 557, 563, 569, 571,
2213 577, 587, 593, 599, 601, 607, 613, 617,
2214 619, 631, 641, 643, 647, 653, 659, 661,
2215 673, 677, 683, 691, 701, 709, 719, 727,
2216 733, 739, 743, 751, 757, 761, 769, 773,
2217 787, 797, 809, 811, 821, 823, 827, 829,
2218 839, 853, 857, 859, 863, 877, 881, 883,
2219 887, 907, 911, 919, 929, 937, 941, 947,
2220 953, 967, 971, 977, 983, 991, 997, -103
Paul Bakker5121ce52009-01-03 21:22:43 +00002221};
2222
2223/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002224 * Small divisors test (X must be positive)
2225 *
2226 * Return values:
2227 * 0: no small factor (possible prime, more tests needed)
2228 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002230 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002232static int mpi_check_small_factors(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +00002233{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002234 int ret = 0;
2235 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if ((X->p[0] & 1) == 0) {
2239 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 for (i = 0; small_prime[i] > 0; i++) {
2243 if (mbedtls_mpi_cmp_int(X, small_prime[i]) <= 0) {
2244 return 1;
2245 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, small_prime[i]));
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (r == 0) {
2250 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2251 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 }
2253
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002254cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 return ret;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002256}
2257
2258/*
2259 * Miller-Rabin pseudo-primality test (HAC 4.24)
2260 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002261static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2262 int (*f_rng)(void *, unsigned char *, size_t),
2263 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002264{
Pascal Junodb99183d2015-03-11 16:49:45 +01002265 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002266 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MPI_VALIDATE_RET(X != NULL);
2270 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2273 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2274 mbedtls_mpi_init(&RR);
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002275
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 /*
2277 * W = |X| - 1
2278 * R = W >> lsb( W )
2279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2281 s = mbedtls_mpi_lsb(&W);
2282 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2283 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 for (i = 0; i < rounds; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 /*
2287 * pick a random A, 1 < A < |X| - 1
2288 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002289 count = 0;
2290 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
Pascal Junodb99183d2015-03-11 16:49:45 +01002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 j = mbedtls_mpi_bitlen(&A);
2294 k = mbedtls_mpi_bitlen(&W);
Pascal Junodb99183d2015-03-11 16:49:45 +01002295 if (j > k) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002297 }
2298
2299 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002300 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2301 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002302 }
2303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2305 mbedtls_mpi_cmp_int(&A, 1) <= 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002306
2307 /*
2308 * A = A^R mod |X|
2309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
Paul Bakker5121ce52009-01-03 21:22:43 +00002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2313 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317 j = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002319 /*
2320 * A = A * A mod |X|
2321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2323 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
2329 j++;
2330 }
2331
2332 /*
2333 * not prime if A != |X| - 1 or A == 1
2334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2336 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002338 break;
2339 }
2340 }
2341
2342cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2344 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2345 mbedtls_mpi_free(&RR);
Paul Bakker5121ce52009-01-03 21:22:43 +00002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002348}
2349
2350/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002351 * Pseudo-primality test: small factors, then Miller-Rabin
2352 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002353int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2354 int (*f_rng)(void *, unsigned char *, size_t),
2355 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002356{
Janos Follath24eed8d2019-11-22 13:21:35 +00002357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 mbedtls_mpi XX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 MPI_VALIDATE_RET(X != NULL);
2360 MPI_VALIDATE_RET(f_rng != NULL);
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002361
2362 XX.s = 1;
2363 XX.n = X->n;
2364 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2367 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2368 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002369 }
2370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2372 return 0;
2373 }
2374
2375 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2376 if (ret == 1) {
2377 return 0;
2378 }
2379
2380 return ret;
2381 }
2382
2383 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
Janos Follathf301d232018-08-14 13:34:01 +01002384}
2385
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002386/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002388 *
Janos Follathf301d232018-08-14 13:34:01 +01002389 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2390 * be either 1024 bits or 1536 bits long, and flags must contain
2391 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002393int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2394 int (*f_rng)(void *, unsigned char *, size_t),
2395 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00002396{
Jethro Beekman66689272018-02-14 19:24:10 -08002397#ifdef MBEDTLS_HAVE_INT64
2398// ceil(2^63.5)
2399#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2400#else
2401// ceil(2^31.5)
2402#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2403#endif
2404 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002405 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002406 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 mbedtls_mpi_uint r;
2408 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 MPI_VALIDATE_RET(X != NULL);
2411 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2414 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 mbedtls_mpi_init(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 n = BITS_TO_LIMBS(nbits);
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
Janos Follathda31fa12018-09-03 14:45:23 +01002422 /*
2423 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2424 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2426 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2427 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2428 } else {
Janos Follathda31fa12018-09-03 14:45:23 +01002429 /*
2430 * 2^-100 error probability, number of rounds computed based on HAC,
2431 * fact 4.48
2432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2434 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2435 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2436 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
Janos Follathda31fa12018-09-03 14:45:23 +01002437 }
2438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 while (1) {
2440 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
Jethro Beekman66689272018-02-14 19:24:10 -08002441 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2443 continue;
2444 }
Jethro Beekman66689272018-02-14 19:24:10 -08002445
2446 k = n * biL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (k > nbits) {
2448 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2449 }
Jethro Beekman66689272018-02-14 19:24:10 -08002450 X->p[0] |= 1;
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2453 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
Jethro Beekman66689272018-02-14 19:24:10 -08002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 }
2458 } else {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002459 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002460 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002461 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2462 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002463 */
Jethro Beekman66689272018-02-14 19:24:10 -08002464
2465 X->p[0] |= 2;
2466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2468 if (r == 0) {
2469 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2470 } else if (r == 1) {
2471 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2472 }
Jethro Beekman66689272018-02-14 19:24:10 -08002473
2474 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2476 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
Jethro Beekman66689272018-02-14 19:24:10 -08002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 while (1) {
Jethro Beekman66689272018-02-14 19:24:10 -08002479 /*
2480 * First, check small factors for X and Y
2481 * before doing Miller-Rabin on any of them
2482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if ((ret = mpi_check_small_factors(X)) == 0 &&
2484 (ret = mpi_check_small_factors(&Y)) == 0 &&
2485 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2486 == 0 &&
2487 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2488 == 0) {
Jethro Beekman66689272018-02-14 19:24:10 -08002489 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 }
Jethro Beekman66689272018-02-14 19:24:10 -08002491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Jethro Beekman66689272018-02-14 19:24:10 -08002493 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 }
Jethro Beekman66689272018-02-14 19:24:10 -08002495
2496 /*
2497 * Next candidates. We want to preserve Y = (X-1) / 2 and
2498 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2499 * so up Y by 6 and X by 12.
2500 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2502 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 }
2505 }
2506
2507cleanup:
2508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 mbedtls_mpi_free(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002512}
2513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Paul Bakker23986e52011-04-24 08:57:21 +00002518#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002519
2520static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2521{
2522 { 693, 609, 21 },
2523 { 1764, 868, 28 },
2524 { 768454923, 542167814, 1 }
2525};
2526
Paul Bakker5121ce52009-01-03 21:22:43 +00002527/*
2528 * Checkup routine
2529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002530int mbedtls_mpi_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002531{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002532 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2536 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2539 "EFE021C2645FD1DC586E69184AF4A31E" \
2540 "D5F53E93B5F123FA41680867BA110131" \
2541 "944FE7952E2517337780CB0DB80E61AA" \
2542 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2545 "B2E7EFD37075B9F03FF989C7C5051C20" \
2546 "34D2A323810251127E7BF8625A4F49A5" \
2547 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2548 "5B5C25763222FEFCCFC38B832366C29E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2551 "0066A198186C18C10B2F5ED9B522752A" \
2552 "9830B69916E535C8F047518A889A43A5" \
2553 "94B6BED27A168D31D4A52F88925AA8F5"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2558 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2559 "9E857EA95A03512E2BAE7391688D264A" \
2560 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2561 "8001B72E848A38CAE1C65F78E56ABDEF" \
2562 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2563 "ECF677152EF804370C1A305CAF3B5BF1" \
2564 "30879B56C61DE584A0F53A2447A51E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 if (verbose != 0) {
2567 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2571 if (verbose != 0) {
2572 mbedtls_printf("failed\n");
2573 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002575 ret = 1;
2576 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 }
2578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (verbose != 0) {
2580 mbedtls_printf("passed\n");
2581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2586 "256567336059E52CAE22925474705F39A94"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2589 "6613F26162223DF488E9CD48CC132C7A" \
2590 "0AC93C701B001B092E4E5B9F73BCD27B" \
2591 "9EE50D0657C77F374E903CDFA4C642"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (verbose != 0) {
2594 mbedtls_printf(" MPI test #2 (div_mpi): ");
2595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2598 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2599 if (verbose != 0) {
2600 mbedtls_printf("failed\n");
2601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002603 ret = 1;
2604 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 }
2606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (verbose != 0) {
2608 mbedtls_printf("passed\n");
2609 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2614 "36E139AEA55215609D2816998ED020BB" \
2615 "BD96C37890F65171D948E9BC7CBAA4D9" \
2616 "325D24D6A3C12710F10A09FA08AB87"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (verbose != 0) {
2619 mbedtls_printf(" MPI test #3 (exp_mod): ");
2620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2623 if (verbose != 0) {
2624 mbedtls_printf("failed\n");
2625 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002627 ret = 1;
2628 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 }
2630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 if (verbose != 0) {
2632 mbedtls_printf("passed\n");
2633 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2638 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2639 "C3DBA76456363A10869622EAC2DD84EC" \
2640 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (verbose != 0) {
2643 mbedtls_printf(" MPI test #4 (inv_mod): ");
2644 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2647 if (verbose != 0) {
2648 mbedtls_printf("failed\n");
2649 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002651 ret = 1;
2652 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 }
2654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 if (verbose != 0) {
2656 mbedtls_printf("passed\n");
2657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (verbose != 0) {
2660 mbedtls_printf(" MPI test #5 (simple gcd): ");
2661 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2664 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2665 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2670 if (verbose != 0) {
2671 mbedtls_printf("failed at %d\n", i);
2672 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002673
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002674 ret = 1;
2675 goto cleanup;
2676 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002677 }
2678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (verbose != 0) {
2680 mbedtls_printf("passed\n");
2681 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002682
Paul Bakker5121ce52009-01-03 21:22:43 +00002683cleanup:
2684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (ret != 0 && verbose != 0) {
2686 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2687 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2690 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if (verbose != 0) {
2693 mbedtls_printf("\n");
2694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002697}
2698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#endif /* MBEDTLS_BIGNUM_C */