blob: 36effaf8dace5e73904b801d115e54618480645f [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
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050057/* Implementation that should never be optimized out by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +010058static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n)
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050059{
Gilles Peskine449bd832023-01-11 14:50:10 +010060 mbedtls_platform_zeroize(v, ciL * n);
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050061}
62
Paul Bakker5121ce52009-01-03 21:22:43 +000063/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000064 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000065 */
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_mpi_init(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +000067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 MPI_VALIDATE(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Paul Bakker6c591fa2011-05-05 11:49:20 +000070 X->s = 1;
71 X->n = 0;
72 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000073}
74
75/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000076 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000077 */
Gilles Peskine449bd832023-01-11 14:50:10 +010078void mbedtls_mpi_free(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +000079{
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if (X == NULL) {
Paul Bakker6c591fa2011-05-05 11:49:20 +000081 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010082 }
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 if (X->p != NULL) {
85 mbedtls_mpi_zeroize(X->p, X->n);
86 mbedtls_free(X->p);
Paul Bakker5121ce52009-01-03 21:22:43 +000087 }
88
Paul Bakker6c591fa2011-05-05 11:49:20 +000089 X->s = 1;
90 X->n = 0;
91 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000092}
93
94/*
95 * Enlarge to the specified number of limbs
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Paul Bakker5121ce52009-01-03 21:22:43 +000098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_mpi_uint *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
103 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
104 }
Paul Bakkerf9688572011-05-05 10:00:45 +0000105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (X->n < nblimbs) {
107 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
108 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
109 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (X->p != NULL) {
112 memcpy(p, X->p, X->n * ciL);
113 mbedtls_mpi_zeroize(X->p, X->n);
114 mbedtls_free(X->p);
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 }
116
117 X->n = nblimbs;
118 X->p = p;
119 }
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000122}
123
124/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100125 * Resize down as much as possible,
126 * while keeping at least the specified number of limbs
127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100131 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
135 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
136 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100137
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100138 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (X->n <= nblimbs) {
140 return mbedtls_mpi_grow(X, nblimbs);
141 }
Gilles Peskine322752b2020-01-21 13:59:51 +0100142 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 for (i = X->n - 1; i > 0; i--) {
145 if (X->p[i] != 0) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100146 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 }
148 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100149 i++;
150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (i < nblimbs) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100152 i = nblimbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
156 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
157 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 if (X->p != NULL) {
160 memcpy(p, X->p, i * ciL);
161 mbedtls_mpi_zeroize(X->p, X->n);
162 mbedtls_free(X->p);
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100163 }
164
165 X->n = i;
166 X->p = p;
167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return 0;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100169}
170
Gilles Peskineed32b572021-06-02 22:17:52 +0200171/* Resize X to have exactly n limbs and set it to 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
Gilles Peskineed32b572021-06-02 22:17:52 +0200173{
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (limbs == 0) {
175 mbedtls_mpi_free(X);
176 return 0;
177 } else if (X->n == limbs) {
178 memset(X->p, 0, limbs * ciL);
Gilles Peskineed32b572021-06-02 22:17:52 +0200179 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return 0;
181 } else {
182 mbedtls_mpi_free(X);
183 return mbedtls_mpi_grow(X, limbs);
Gilles Peskineed32b572021-06-02 22:17:52 +0200184 }
185}
186
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100187/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200188 * Copy the contents of Y into X.
189 *
190 * This function is not constant-time. Leading zeros in Y may be removed.
191 *
192 * Ensure that X does not shrink. This is not guaranteed by the public API,
193 * but some code in the bignum module relies on this property, for example
194 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000197{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100198 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000199 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 MPI_VALIDATE_RET(X != NULL);
201 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (X == Y) {
204 return 0;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200205 }
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (Y->n == 0) {
208 if (X->n != 0) {
209 X->s = 1;
210 memset(X->p, 0, X->n * ciL);
211 }
212 return 0;
213 }
214
215 for (i = Y->n - 1; i > 0; i--) {
216 if (Y->p[i] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 }
219 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 i++;
221
222 X->s = Y->s;
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (X->n < i) {
225 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
226 } else {
227 memset(X->p + i, 0, (X->n - i) * ciL);
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100228 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 memcpy(X->p, Y->p, i * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232cleanup:
233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000235}
236
237/*
238 * Swap the contents of X and Y
239 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 MPI_VALIDATE(X != NULL);
244 MPI_VALIDATE(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 memcpy(&T, X, sizeof(mbedtls_mpi));
247 memcpy(X, Y, sizeof(mbedtls_mpi));
248 memcpy(Y, &T, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +0000249}
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100252{
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (z >= 0) {
254 return z;
255 }
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100256 /* Take care to handle the most negative value (-2^(biL-1)) correctly.
257 * A naive -z would have undefined behavior.
258 * Write this in a way that makes popular compilers happy (GCC, Clang,
259 * MSVC). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100261}
262
Paul Bakker5121ce52009-01-03 21:22:43 +0000263/*
264 * Set value from integer
265 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000267{
Janos Follath24eed8d2019-11-22 13:21:35 +0000268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
272 memset(X->p, 0, X->n * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 X->p[0] = mpi_sint_abs(z);
275 X->s = (z < 0) ? -1 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
277cleanup:
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000280}
281
282/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000283 * Get a specific bit
284 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000286{
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (X->n * biL <= pos) {
290 return 0;
291 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return (X->p[pos / biL] >> (pos % biL)) & 0x01;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000294}
295
296/*
297 * Set a bit to a specific value of 0 or 1
298 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000300{
301 int ret = 0;
302 size_t off = pos / biL;
303 size_t idx = pos % biL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 MPI_VALIDATE_RET(X != NULL);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (val != 0 && val != 1) {
307 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000308 }
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (X->n * biL <= pos) {
311 if (val == 0) {
312 return 0;
313 }
314
315 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
316 }
317
318 X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000320
321cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return ret;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000324}
325
326/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200327 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100329size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000330{
Paul Bakker23986e52011-04-24 08:57:21 +0000331 size_t i, j, count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 MBEDTLS_INTERNAL_VALIDATE_RET(X != NULL, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 for (i = 0; i < X->n; i++) {
335 for (j = 0; j < biL; j++, count++) {
336 if (((X->p[i] >> j) & 1) != 0) {
337 return count;
338 }
339 }
340 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000343}
344
345/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200346 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000349{
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return mbedtls_mpi_core_bitlen(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000351}
352
353/*
354 * Return the total size in bytes
355 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000357{
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return (mbedtls_mpi_bitlen(X) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000359}
360
361/*
362 * Convert an ASCII character to digit value
363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
Paul Bakker5121ce52009-01-03 21:22:43 +0000365{
366 *d = 255;
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (c >= 0x30 && c <= 0x39) {
369 *d = c - 0x30;
370 }
371 if (c >= 0x41 && c <= 0x46) {
372 *d = c - 0x37;
373 }
374 if (c >= 0x61 && c <= 0x66) {
375 *d = c - 0x57;
376 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (*d >= (mbedtls_mpi_uint) radix) {
379 return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
380 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000383}
384
385/*
386 * Import from an ASCII string
387 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100388int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Paul Bakker5121ce52009-01-03 21:22:43 +0000389{
Janos Follath24eed8d2019-11-22 13:21:35 +0000390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000391 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200392 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_mpi_uint d;
394 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 MPI_VALIDATE_RET(X != NULL);
396 MPI_VALIDATE_RET(s != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (radix < 2 || radix > 16) {
399 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine7cba8592021-06-08 18:32:34 +0200400 }
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 mbedtls_mpi_init(&T);
403
404 if (s[0] == 0) {
405 mbedtls_mpi_free(X);
406 return 0;
407 }
408
409 if (s[0] == '-') {
Gilles Peskine80f56732021-04-03 18:26:13 +0200410 ++s;
411 sign = -1;
412 }
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 slen = strlen(s);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (radix == 16) {
Dave Rodgman68ef1d62023-05-18 20:49:03 +0100417 if (slen > SIZE_MAX >> 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 n = BITS_TO_LIMBS(slen << 2);
422
423 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
424 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
425
426 for (i = slen, j = 0; i > 0; i--, j++) {
427 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
428 X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
429 }
430 } else {
431 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
432
433 for (i = 0; i < slen; i++) {
434 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
435 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
436 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 }
438 }
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
Gilles Peskine80f56732021-04-03 18:26:13 +0200441 X->s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 }
Gilles Peskine80f56732021-04-03 18:26:13 +0200443
Paul Bakker5121ce52009-01-03 21:22:43 +0000444cleanup:
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000449}
450
451/*
Ron Eldora16fa292018-11-20 14:07:01 +0200452 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454static int mpi_write_hlp(mbedtls_mpi *X, int radix,
455 char **p, const size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000456{
Janos Follath24eed8d2019-11-22 13:21:35 +0000457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200459 size_t length = 0;
460 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 do {
463 if (length >= buflen) {
464 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Ron Eldora16fa292018-11-20 14:07:01 +0200465 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
468 MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
Ron Eldora16fa292018-11-20 14:07:01 +0200469 /*
470 * Write the residue in the current position, as an ASCII character.
471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (r < 0xA) {
473 *(--p_end) = (char) ('0' + r);
474 } else {
475 *(--p_end) = (char) ('A' + (r - 0xA));
476 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
Ron Eldora16fa292018-11-20 14:07:01 +0200478 length++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 } while (mbedtls_mpi_cmp_int(X, 0) != 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 memmove(*p, p_end, length);
Ron Eldora16fa292018-11-20 14:07:01 +0200482 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484cleanup:
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000487}
488
489/*
490 * Export into an ASCII string
491 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
493 char *buf, size_t buflen, size_t *olen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000494{
Paul Bakker23986e52011-04-24 08:57:21 +0000495 int ret = 0;
496 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 MPI_VALIDATE_RET(X != NULL);
500 MPI_VALIDATE_RET(olen != NULL);
501 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (radix < 2 || radix > 16) {
504 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
505 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */
508 if (radix >= 4) {
509 n >>= 1; /* Number of 4-adic digits necessary to present
Hanno Becker23cfea02019-02-04 09:45:07 +0000510 * `n`. If radix > 4, this might be a strict
511 * overapproximation of the number of
512 * radix-adic digits needed to present `n`. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 }
514 if (radix >= 16) {
515 n >>= 1; /* Number of hexadecimal digits necessary to
Hanno Becker23cfea02019-02-04 09:45:07 +0000516 * present `n`. */
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 }
Janos Follath80470622019-03-06 13:43:02 +0000519 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000520 n += 1; /* Compensate for the divisions above, which round down `n`
521 * in case it's not even. */
522 n += 1; /* Potential '-'-sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 n += (n & 1); /* Make n even to have enough space for hexadecimal writing,
Hanno Becker23cfea02019-02-04 09:45:07 +0000524 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (buflen < n) {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100527 *olen = n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 }
530
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100531 p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (X->s == -1) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000536 buflen--;
537 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (radix == 16) {
Paul Bakker23986e52011-04-24 08:57:21 +0000540 int c;
541 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 for (i = X->n, k = 0; i > 0; i--) {
544 for (j = ciL; j > 0; j--) {
545 c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (c == 0 && k == 0 && (i + j) != 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000551 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000552 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 k = 1;
554 }
555 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 } else {
557 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (T.s == -1) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000560 T.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 }
565
566 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100567 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569cleanup:
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000574}
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000577/*
578 * Read X from an opened file
579 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100580int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Paul Bakker5121ce52009-01-03 21:22:43 +0000581{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000583 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000585 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000586 * Buffer should have space for (short) label and decimal formatted MPI,
587 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000588 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 MPI_VALIDATE_RET(X != NULL);
592 MPI_VALIDATE_RET(fin != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (radix < 2 || radix > 16) {
595 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
596 }
Hanno Becker73d7d792018-12-11 10:35:51 +0000597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 memset(s, 0, sizeof(s));
599 if (fgets(s, sizeof(s) - 1, fin) == NULL) {
600 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
601 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 slen = strlen(s);
604 if (slen == sizeof(s) - 2) {
605 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
606 }
Paul Bakkercb37aa52011-11-30 16:00:20 +0000607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (slen > 0 && s[slen - 1] == '\n') {
609 slen--; s[slen] = '\0';
610 }
611 if (slen > 0 && s[slen - 1] == '\r') {
612 slen--; s[slen] = '\0';
613 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
615 p = s + slen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 while (p-- > s) {
617 if (mpi_get_digit(&d, radix, *p) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 }
620 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return mbedtls_mpi_read_string(X, radix, p + 1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000623}
624
625/*
626 * Write X into an opened file (or stdout if fout == NULL)
627 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Paul Bakker5121ce52009-01-03 21:22:43 +0000629{
Janos Follath24eed8d2019-11-22 13:21:35 +0000630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000631 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000632 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000633 * Buffer should have space for (short) label and decimal formatted MPI,
634 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000635 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
637 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (radix < 2 || radix > 16) {
640 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
641 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 memset(s, 0, sizeof(s));
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (p == NULL) {
648 p = "";
649 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 plen = strlen(p);
652 slen = strlen(s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 s[slen++] = '\r';
654 s[slen++] = '\n';
655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (fout != NULL) {
657 if (fwrite(p, 1, plen, fout) != plen ||
658 fwrite(s, 1, slen, fout) != slen) {
659 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
660 }
661 } else {
662 mbedtls_printf("%s%s", p, s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000664
665cleanup:
666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671/*
Janos Follatha778a942019-02-13 10:28:28 +0000672 * Import X from unsigned binary data, little endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100673 *
674 * This function is guaranteed to return an MPI with exactly the necessary
675 * number of limbs (in particular, it does not skip 0s in the input).
Janos Follatha778a942019-02-13 10:28:28 +0000676 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100677int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
678 const unsigned char *buf, size_t buflen)
Janos Follatha778a942019-02-13 10:28:28 +0000679{
Janos Follath24eed8d2019-11-22 13:21:35 +0000680 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 const size_t limbs = CHARS_TO_LIMBS(buflen);
Janos Follatha778a942019-02-13 10:28:28 +0000682
683 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Janos Follatha778a942019-02-13 10:28:28 +0000685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
Janos Follatha778a942019-02-13 10:28:28 +0000687
688cleanup:
689
Janos Follath171a7ef2019-02-15 16:17:45 +0000690 /*
691 * This function is also used to import keys. However, wiping the buffers
692 * upon failure is not necessary because failure only can happen before any
693 * input is copied.
694 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return ret;
Janos Follatha778a942019-02-13 10:28:28 +0000696}
697
698/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 * Import X from unsigned binary data, big endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100700 *
701 * This function is guaranteed to return an MPI with exactly the necessary
702 * number of limbs (in particular, it does not skip 0s in the input).
Paul Bakker5121ce52009-01-03 21:22:43 +0000703 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000705{
Janos Follath24eed8d2019-11-22 13:21:35 +0000706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 const size_t limbs = CHARS_TO_LIMBS(buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 MPI_VALIDATE_RET(X != NULL);
710 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000711
Hanno Becker073c1992017-10-17 15:17:27 +0100712 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
717cleanup:
718
Janos Follath171a7ef2019-02-15 16:17:45 +0000719 /*
720 * This function is also used to import keys. However, wiping the buffers
721 * upon failure is not necessary because failure only can happen before any
722 * input is copied.
723 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000725}
726
727/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000728 * Export X into unsigned binary data, little endian
729 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
731 unsigned char *buf, size_t buflen)
Janos Follathe344d0f2019-02-19 16:17:40 +0000732{
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
Janos Follathe344d0f2019-02-19 16:17:40 +0000734}
735
736/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 * Export X into unsigned binary data, big endian
738 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
740 unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000741{
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000743}
744
745/*
746 * Left-shift: X <<= count
747 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000749{
Janos Follath24eed8d2019-11-22 13:21:35 +0000750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Minos Galanakis0144b352023-05-02 14:02:32 +0100751 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 i = mbedtls_mpi_bitlen(X) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if (X->n * biL < i) {
757 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
758 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
760 ret = 0;
761
Minos Galanakis0144b352023-05-02 14:02:32 +0100762 mbedtls_mpi_core_shift_l(X->p, X->n, count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000763cleanup:
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000766}
767
768/*
769 * Right-shift: X >>= count
770 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000772{
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 MPI_VALIDATE_RET(X != NULL);
774 if (X->n != 0) {
775 mbedtls_mpi_core_shift_r(X->p, X->n, count);
776 }
777 return 0;
Gilles Peskine66414202022-09-21 15:36:16 +0200778}
779
Paul Bakker5121ce52009-01-03 21:22:43 +0000780/*
781 * Compare unsigned values
782 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100783int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000784{
Paul Bakker23986e52011-04-24 08:57:21 +0000785 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 MPI_VALIDATE_RET(X != NULL);
787 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 for (i = X->n; i > 0; i--) {
790 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000791 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000793 }
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 for (j = Y->n; j > 0; j--) {
796 if (Y->p[j - 1] != 0) {
797 break;
798 }
799 }
800
801 if (i == 0 && j == 0) {
802 return 0;
803 }
804
805 if (i > j) {
806 return 1;
807 }
808 if (j > i) {
809 return -1;
810 }
811
812 for (; i > 0; i--) {
813 if (X->p[i - 1] > Y->p[i - 1]) {
814 return 1;
815 }
816 if (X->p[i - 1] < Y->p[i - 1]) {
817 return -1;
818 }
819 }
820
821 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000822}
823
824/*
825 * Compare signed values
826 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000828{
Paul Bakker23986e52011-04-24 08:57:21 +0000829 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 MPI_VALIDATE_RET(X != NULL);
831 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 for (i = X->n; i > 0; i--) {
834 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 }
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 for (j = Y->n; j > 0; j--) {
840 if (Y->p[j - 1] != 0) {
841 break;
842 }
843 }
844
845 if (i == 0 && j == 0) {
846 return 0;
847 }
848
849 if (i > j) {
850 return X->s;
851 }
852 if (j > i) {
853 return -Y->s;
854 }
855
856 if (X->s > 0 && Y->s < 0) {
857 return 1;
858 }
859 if (Y->s > 0 && X->s < 0) {
860 return -1;
861 }
862
863 for (; i > 0; i--) {
864 if (X->p[i - 1] > Y->p[i - 1]) {
865 return X->s;
866 }
867 if (X->p[i - 1] < Y->p[i - 1]) {
868 return -X->s;
869 }
870 }
871
872 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000873}
874
Janos Follathee6abce2019-09-05 14:47:19 +0100875/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 * Compare signed values
877 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_mpi Y;
881 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 *p = mpi_sint_abs(z);
885 Y.s = (z < 0) ? -1 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000886 Y.n = 1;
887 Y.p = p;
888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 return mbedtls_mpi_cmp_mpi(X, &Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000890}
891
892/*
893 * Unsigned addition: X = |A| + |B| (HAC 14.7)
894 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +0000896{
Janos Follath24eed8d2019-11-22 13:21:35 +0000897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100898 size_t j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 MPI_VALIDATE_RET(X != NULL);
900 MPI_VALIDATE_RET(A != NULL);
901 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (X == B) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 }
906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (X != A) {
908 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
909 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200910
Paul Bakkerf7ca7b92009-06-20 10:31:06 +0000911 /*
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100912 * X must always be positive as a result of unsigned additions.
Paul Bakkerf7ca7b92009-06-20 10:31:06 +0000913 */
914 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 for (j = B->n; j > 0; j--) {
917 if (B->p[j - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000918 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 }
920 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
Gilles Peskinedb14a9d2022-11-15 22:59:00 +0100922 /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
923 * and B is 0 (of any size). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (j == 0) {
925 return 0;
926 }
Gilles Peskinedb14a9d2022-11-15 22:59:00 +0100927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100930 /* j is the number of non-zero limbs of B. Add those to X. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100932 mbedtls_mpi_uint *p = X->p;
933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 mbedtls_mpi_uint c = mbedtls_mpi_core_add(p, p, B->p, j);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100935
936 p += j;
937
938 /* Now propagate any carry */
Paul Bakker5121ce52009-01-03 21:22:43 +0000939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 while (c != 0) {
941 if (j >= X->n) {
942 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100943 p = X->p + j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000944 }
945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 *p += c; c = (*p < c); j++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 }
948
949cleanup:
950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000952}
953
Paul Bakker5121ce52009-01-03 21:22:43 +0000954/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +0200955 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +0000958{
Janos Follath24eed8d2019-11-22 13:21:35 +0000959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000960 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +0200961 mbedtls_mpi_uint carry;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 MPI_VALIDATE_RET(X != NULL);
963 MPI_VALIDATE_RET(A != NULL);
964 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 for (n = B->n; n > 0; n--) {
967 if (B->p[n - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 }
970 }
971 if (n > A->n) {
Gilles Peskinec8a91772021-01-27 22:30:43 +0100972 /* B >= (2^ciL)^n > A */
973 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
974 goto cleanup;
975 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
Gilles Peskine1acf7cb2020-07-23 01:03:22 +0200978
979 /* Set the high limbs of X to match A. Don't touch the lower limbs
980 * because X might be aliased to B, and we must not overwrite the
981 * significant digits of B. */
Aaron M. Uckoaf67d2c2023-01-17 11:52:22 -0500982 if (A->n > n && A != X) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
984 }
985 if (X->n > A->n) {
986 memset(X->p + A->n, 0, (X->n - A->n) * ciL);
987 }
Gilles Peskine1acf7cb2020-07-23 01:03:22 +0200988
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
990 if (carry != 0) {
Tom Cosgrove452c99c2022-08-25 10:07:07 +0100991 /* Propagate the carry through the rest of X. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n);
Tom Cosgrove452c99c2022-08-25 10:07:07 +0100993
994 /* If we have further carry/borrow, the result is negative. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 if (carry != 0) {
Gilles Peskine89b41302020-07-23 01:16:46 +0200996 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
997 goto cleanup;
998 }
Gilles Peskinec097e9e2020-06-08 21:58:22 +0200999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001001 /* X should always be positive as a result of unsigned subtractions. */
1002 X->s = 1;
1003
Paul Bakker5121ce52009-01-03 21:22:43 +00001004cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001006}
1007
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001008/* Common function for signed addition and subtraction.
1009 * Calculate A + B * flip_B where flip_B is 1 or -1.
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011static int add_sub_mpi(mbedtls_mpi *X,
1012 const mbedtls_mpi *A, const mbedtls_mpi *B,
1013 int flip_B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001014{
Hanno Becker73d7d792018-12-11 10:35:51 +00001015 int ret, s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 MPI_VALIDATE_RET(X != NULL);
1017 MPI_VALIDATE_RET(A != NULL);
1018 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
Hanno Becker73d7d792018-12-11 10:35:51 +00001020 s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if (A->s * B->s * flip_B < 0) {
1022 int cmp = mbedtls_mpi_cmp_abs(A, B);
1023 if (cmp >= 0) {
1024 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001025 /* If |A| = |B|, the result is 0 and we must set the sign bit
1026 * to +1 regardless of which of A or B was negative. Otherwise,
1027 * since |A| > |B|, the sign is the sign of A. */
1028 X->s = cmp == 0 ? 1 : s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 } else {
1030 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001031 /* Since |A| < |B|, the sign is the opposite of A. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 X->s = -s;
1033 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 } else {
1035 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 X->s = s;
1037 }
1038
1039cleanup:
1040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001042}
1043
1044/*
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001045 * Signed addition: X = A + B
1046 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001048{
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 return add_sub_mpi(X, A, B, 1);
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001050}
1051
1052/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001053 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001056{
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 return add_sub_mpi(X, A, B, -1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001058}
1059
1060/*
1061 * Signed addition: X = A + b
1062 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001064{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001065 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 MPI_VALIDATE_RET(X != NULL);
1068 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 p[0] = mpi_sint_abs(b);
1071 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001072 B.n = 1;
1073 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return mbedtls_mpi_add_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001076}
1077
1078/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001079 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001082{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001083 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 MPI_VALIDATE_RET(X != NULL);
1086 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 p[0] = mpi_sint_abs(b);
1089 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001090 B.n = 1;
1091 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 return mbedtls_mpi_sub_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001094}
1095
Paul Bakker5121ce52009-01-03 21:22:43 +00001096/*
1097 * Baseline multiplication: X = A * B (HAC 14.12)
1098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001100{
Janos Follath24eed8d2019-11-22 13:21:35 +00001101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker1772e052022-04-13 06:51:40 +01001102 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_mpi TA, TB;
Hanno Beckerda763de2022-04-13 06:50:02 +01001104 int result_is_zero = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 MPI_VALIDATE_RET(X != NULL);
1106 MPI_VALIDATE_RET(A != NULL);
1107 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001109 mbedtls_mpi_init(&TA);
1110 mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (X == A) {
1113 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
1114 }
1115 if (X == B) {
1116 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
1117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 for (i = A->n; i > 0; i--) {
1120 if (A->p[i - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001121 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 }
1123 }
1124 if (i == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001125 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 for (j = B->n; j > 0; j--) {
1129 if (B->p[j - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001130 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 }
1132 }
1133 if (j == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001134 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
1138 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001140 mbedtls_mpi_core_mul(X->p, A->p, i, B->p, j);
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Hanno Beckerda763de2022-04-13 06:50:02 +01001142 /* If the result is 0, we don't shortcut the operation, which reduces
1143 * but does not eliminate side channels leaking the zero-ness. We do
1144 * need to take care to set the sign bit properly since the library does
1145 * not fully support an MPI object with a value of 0 and s == -1. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (result_is_zero) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001147 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 } else {
Hanno Beckerda763de2022-04-13 06:50:02 +01001149 X->s = A->s * B->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
1152cleanup:
1153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001157}
1158
1159/*
1160 * Baseline multiplication: X = A * b
1161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001162int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001163{
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 MPI_VALIDATE_RET(X != NULL);
1165 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
Hanno Becker35771312022-04-14 11:52:11 +01001167 size_t n = A->n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 while (n > 0 && A->p[n - 1] == 0) {
Hanno Becker35771312022-04-14 11:52:11 +01001169 --n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 }
Hanno Becker35771312022-04-14 11:52:11 +01001171
Hanno Becker74a11a32022-04-06 06:27:00 +01001172 /* The general method below doesn't work if b==0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (b == 0 || n == 0) {
1174 return mbedtls_mpi_lset(X, 0);
1175 }
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001176
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001177 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001179 /* In general, A * b requires 1 limb more than b. If
1180 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1181 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001182 * copy() will take care of the growth if needed. However, experimentally,
1183 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001184 * calls to calloc() in ECP code, presumably because it reuses the
1185 * same mpi for a while and this way the mpi is more likely to directly
Hanno Becker9137b9c2022-04-12 10:51:54 +01001186 * grow to its final size.
1187 *
1188 * Note that calculating A*b as 0 + A*b doesn't work as-is because
1189 * A,X can be the same. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
1191 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1192 mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001193
1194cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001196}
1197
1198/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001199 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1200 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
1203 mbedtls_mpi_uint u0,
1204 mbedtls_mpi_uint d,
1205 mbedtls_mpi_uint *r)
Simon Butcher15b15d12015-11-26 19:35:03 +00001206{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001207#if defined(MBEDTLS_HAVE_UDBL)
1208 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001209#else
Simon Butcher9803d072016-01-03 00:24:34 +00001210 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001212 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1213 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001214 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001215#endif
1216
Simon Butcher15b15d12015-11-26 19:35:03 +00001217 /*
1218 * Check for overflow
1219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (0 == d || u1 >= d) {
1221 if (r != NULL) {
1222 *r = ~(mbedtls_mpi_uint) 0u;
1223 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 return ~(mbedtls_mpi_uint) 0u;
Simon Butcher15b15d12015-11-26 19:35:03 +00001226 }
1227
1228#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001229 dividend = (mbedtls_t_udbl) u1 << biL;
1230 dividend |= (mbedtls_t_udbl) u0;
1231 quotient = dividend / d;
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
1233 quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
1234 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (r != NULL) {
1237 *r = (mbedtls_mpi_uint) (dividend - (quotient * d));
1238 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001239
1240 return (mbedtls_mpi_uint) quotient;
1241#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001242
1243 /*
1244 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1245 * Vol. 2 - Seminumerical Algorithms, Knuth
1246 */
1247
1248 /*
1249 * Normalize the divisor, d, and dividend, u0, u1
1250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 s = mbedtls_mpi_core_clz(d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001252 d = d << s;
1253
1254 u1 = u1 << s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
Simon Butcher15b15d12015-11-26 19:35:03 +00001256 u0 = u0 << s;
1257
1258 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001259 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001260
1261 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001262 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001263
1264 /*
1265 * Find the first quotient and remainder
1266 */
1267 q1 = u1 / d1;
1268 r0 = u1 - d1 * q1;
1269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001271 q1 -= 1;
1272 r0 += d1;
1273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if (r0 >= radix) {
1275 break;
1276 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001277 }
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 rAX = (u1 * radix) + (u0_msw - q1 * d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001280 q0 = rAX / d1;
1281 r0 = rAX - q0 * d1;
1282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001284 q0 -= 1;
1285 r0 += d1;
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if (r0 >= radix) {
1288 break;
1289 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001290 }
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (r != NULL) {
1293 *r = (rAX * radix + u0_lsw - q0 * d) >> s;
1294 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001295
1296 quotient = q1 * radix + q0;
1297
1298 return quotient;
1299#endif
1300}
1301
1302/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001305int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1306 const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001307{
Janos Follath24eed8d2019-11-22 13:21:35 +00001308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001309 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001311 mbedtls_mpi_uint TP2[3];
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 MPI_VALIDATE_RET(A != NULL);
1313 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (mbedtls_mpi_cmp_int(B, 0) == 0) {
1316 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1317 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
1320 mbedtls_mpi_init(&T1);
Alexander Kd19a1932019-11-01 18:20:42 +03001321 /*
1322 * Avoid dynamic memory allocations for constant-size T2.
1323 *
1324 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1325 * so nobody increase the size of the MPI and we're safe to use an on-stack
1326 * buffer.
1327 */
Alexander K35d6d462019-10-31 14:46:45 +03001328 T2.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 T2.n = sizeof(TP2) / sizeof(*TP2);
Alexander Kd19a1932019-11-01 18:20:42 +03001330 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (mbedtls_mpi_cmp_abs(A, B) < 0) {
1333 if (Q != NULL) {
1334 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
1335 }
1336 if (R != NULL) {
1337 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
1338 }
1339 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
1343 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 X.s = Y.s = 1;
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
1347 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
1348 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 k = mbedtls_mpi_bitlen(&Y) % biL;
1351 if (k < biL - 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 k = biL - 1 - k;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
1354 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
1355 } else {
1356 k = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
1359 n = X.n - 1;
1360 t = Y.n - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 Z.p[n - t]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 for (i = n; i > t; i--) {
1370 if (X.p[i] >= Y.p[t]) {
1371 Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
1372 } else {
1373 Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
1374 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 }
1376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
1378 T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
Alexander K35d6d462019-10-31 14:46:45 +03001379 T2.p[2] = X.p[i];
1380
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 Z.p[i - t - 1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 do {
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 Z.p[i - t - 1]--;
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
1386 T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 T1.p[1] = Y.p[t];
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
1389 } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
1392 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1393 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
1396 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
1397 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1398 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 Z.p[i - t - 1]--;
1400 }
1401 }
1402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (Q != NULL) {
1404 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 Q->s = A->s * B->s;
1406 }
1407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (R != NULL) {
1409 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
Paul Bakkerf02c5642012-11-13 10:25:21 +00001410 X.s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (mbedtls_mpi_cmp_int(R, 0) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 R->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 }
1417
1418cleanup:
1419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
1421 mbedtls_mpi_free(&T1);
1422 mbedtls_platform_zeroize(TP2, sizeof(TP2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001425}
1426
1427/*
1428 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
1431 const mbedtls_mpi *A,
1432 mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001433{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001434 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 p[0] = mpi_sint_abs(b);
1439 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001440 B.n = 1;
1441 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 return mbedtls_mpi_div_mpi(Q, R, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001444}
1445
1446/*
1447 * Modulo: R = A mod B
1448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001449int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001450{
Janos Follath24eed8d2019-11-22 13:21:35 +00001451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 MPI_VALIDATE_RET(R != NULL);
1453 MPI_VALIDATE_RET(A != NULL);
1454 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (mbedtls_mpi_cmp_int(B, 0) < 0) {
1457 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1458 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 while (mbedtls_mpi_cmp_int(R, 0) < 0) {
1463 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
1464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
1467 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
1468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
1470cleanup:
1471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001473}
1474
1475/*
1476 * Modulo: r = A mod b
1477 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001478int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001479{
Paul Bakker23986e52011-04-24 08:57:21 +00001480 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_mpi_uint x, y, z;
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 MPI_VALIDATE_RET(r != NULL);
1483 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (b == 0) {
1486 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (b < 0) {
1490 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1491 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001492
1493 /*
1494 * handle trivial cases
1495 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (b == 1 || A->n == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 *r = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 }
1500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (b == 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 *r = A->p[0] & 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 }
1505
1506 /*
1507 * general case
1508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 for (i = A->n, y = 0; i > 0; i--) {
Paul Bakker23986e52011-04-24 08:57:21 +00001510 x = A->p[i - 1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 z = y / b;
1513 y -= z * b;
1514
1515 x <<= biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001517 z = y / b;
1518 y -= z * b;
1519 }
1520
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001521 /*
1522 * If A is negative, then the current y represents a negative value.
1523 * Flipping it to the positive side.
1524 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (A->s < 0 && y != 0) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001526 y = b - y;
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001528
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 *r = y;
1530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001532}
1533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00001535{
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 *mm = mbedtls_mpi_core_montmul_init(N->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001537}
1538
Tom Cosgrove93842842022-08-05 16:59:43 +01001539/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1540 *
1541 * \param[in,out] A One of the numbers to multiply.
1542 * It must have at least as many limbs as N
1543 * (A->n >= N->n), and any limbs beyond n are ignored.
1544 * On successful completion, A contains the result of
1545 * the multiplication A * B * R^-1 mod N where
1546 * R = (2^ciL)^n.
1547 * \param[in] B One of the numbers to multiply.
1548 * It must be nonzero and must not have more limbs than N
1549 * (B->n <= N->n).
Tom Cosgrove40d22942022-08-17 06:42:44 +01001550 * \param[in] N The modulus. \p N must be odd.
Tom Cosgrove93842842022-08-05 16:59:43 +01001551 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1552 * This is -N^-1 mod 2^ciL.
1553 * \param[in,out] T A bignum for temporary storage.
1554 * It must be at least twice the limb size of N plus 1
1555 * (T->n >= 2 * N->n + 1).
1556 * Its initial content is unused and
1557 * its final content is indeterminate.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +01001558 * It does not get reallocated.
Tom Cosgrove93842842022-08-05 16:59:43 +01001559 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001560static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
1561 const mbedtls_mpi *N, mbedtls_mpi_uint mm,
1562 mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001563{
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 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 +00001565}
1566
1567/*
1568 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02001569 *
Tom Cosgrove93842842022-08-05 16:59:43 +01001570 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001572static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
1573 mbedtls_mpi_uint mm, mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001574{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 mbedtls_mpi_uint z = 1;
1576 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00001577
Paul Bakker8ddb6452013-02-27 14:56:33 +01001578 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 U.p = &z;
1580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 mpi_montmul(A, &U, N, mm, T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001582}
1583
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001584/**
1585 * Select an MPI from a table without leaking the index.
1586 *
1587 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1588 * reads the entire table in order to avoid leaking the value of idx to an
1589 * attacker able to observe memory access patterns.
1590 *
1591 * \param[out] R Where to write the selected MPI.
1592 * \param[in] T The table to read from.
1593 * \param[in] T_size The number of elements in the table.
1594 * \param[in] idx The index of the element to select;
1595 * this must satisfy 0 <= idx < T_size.
1596 *
1597 * \return \c 0 on success, or a negative error code.
1598 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001599static 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 +01001600{
1601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 for (size_t i = 0; i < T_size; i++) {
1604 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
1605 (unsigned char) mbedtls_ct_size_bool_eq(i,
1606 idx)));
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001607 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001608
1609cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 return ret;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001611}
1612
Paul Bakker5121ce52009-01-03 21:22:43 +00001613/*
1614 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1617 const mbedtls_mpi *E, const mbedtls_mpi *N,
1618 mbedtls_mpi *prec_RR)
Paul Bakker5121ce52009-01-03 21:22:43 +00001619{
Janos Follath24eed8d2019-11-22 13:21:35 +00001620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath74601202022-11-21 15:54:20 +00001621 size_t window_bitsize;
Paul Bakker23986e52011-04-24 08:57:21 +00001622 size_t i, j, nblimbs;
1623 size_t bufsize, nbits;
Paul Elliott1748de12023-02-13 15:35:35 +00001624 size_t exponent_bits_in_window = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 mbedtls_mpi_uint ei, mm, state;
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001627 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 MPI_VALIDATE_RET(X != NULL);
1630 MPI_VALIDATE_RET(A != NULL);
1631 MPI_VALIDATE_RET(E != NULL);
1632 MPI_VALIDATE_RET(N != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1635 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1636 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1639 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1640 }
Paul Bakkerf6198c12012-05-16 08:02:29 +00001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1643 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1644 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1645 }
Chris Jones9246d042020-11-25 15:12:39 +00001646
Paul Bakkerf6198c12012-05-16 08:02:29 +00001647 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001648 * Init temps and window size
1649 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 mpi_montg_init(&mm, N);
1651 mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
1652 mbedtls_mpi_init(&Apos);
1653 mbedtls_mpi_init(&WW);
1654 memset(W, 0, sizeof(W));
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 i = mbedtls_mpi_bitlen(E);
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
1659 (i > 79) ? 4 : (i > 23) ? 3 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
1662 if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follath7fa11b82022-11-21 14:48:02 +00001663 window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 }
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001665#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001666
Janos Follathc8d66d52022-11-22 10:47:10 +00001667 const size_t w_table_used_size = (size_t) 1 << window_bitsize;
Janos Follath06000952022-11-22 10:18:06 +00001668
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 /*
Janos Follathbe54ca72022-11-21 16:14:54 +00001670 * This function is not constant-trace: its memory accesses depend on the
1671 * exponent value. To defend against timing attacks, callers (such as RSA
1672 * and DHM) should use exponent blinding. However this is not enough if the
1673 * adversary can find the exponent in a single trace, so this function
1674 * takes extra precautions against adversaries who can observe memory
1675 * access patterns.
Janos Follathf08b40e2022-11-11 15:56:38 +00001676 *
Janos Follathbe54ca72022-11-21 16:14:54 +00001677 * This function performs a series of multiplications by table elements and
1678 * squarings, and we want the prevent the adversary from finding out which
1679 * table element was used, and from distinguishing between multiplications
1680 * and squarings. Firstly, when multiplying by an element of the window
1681 * W[i], we do a constant-trace table lookup to obfuscate i. This leaves
1682 * squarings as having a different memory access patterns from other
1683 * multiplications. So secondly, we put the accumulator X in the table as
1684 * well, and also do a constant-trace table lookup to multiply by X.
1685 *
1686 * This way, all multiplications take the form of a lookup-and-multiply.
1687 * The number of lookup-and-multiply operations inside each iteration of
1688 * the main loop still depends on the bits of the exponent, but since the
1689 * other operations in the loop don't have an easily recognizable memory
1690 * trace, an adversary is unlikely to be able to observe the exact
1691 * patterns.
1692 *
1693 * An adversary may still be able to recover the exponent if they can
1694 * observe both memory accesses and branches. However, branch prediction
1695 * exploitation typically requires many traces of execution over the same
1696 * data, which is defeated by randomized blinding.
Janos Follath84461482022-11-21 14:31:22 +00001697 *
1698 * To achieve this, we make a copy of X and we use the table entry in each
1699 * calculation from this point on.
Janos Follath8e7d6a02022-10-04 13:27:40 +01001700 */
Janos Follathc8d66d52022-11-22 10:47:10 +00001701 const size_t x_index = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 mbedtls_mpi_init(&W[x_index]);
1703 mbedtls_mpi_copy(&W[x_index], X);
Janos Follath84461482022-11-21 14:31:22 +00001704
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 j = N->n + 1;
Tom Cosgrove93842842022-08-05 16:59:43 +01001706 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 * and mpi_montred() calls later. Here we ensure that W[1] and X are
1708 * large enough, and later we'll grow other W[i] to the same length.
1709 * They must not be shrunk midway through this function!
1710 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
1712 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], j));
1713 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
1715 /*
Paul Bakker50546922012-05-19 08:40:49 +00001716 * Compensate for negative A (and correct at the end)
1717 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 neg = (A->s == -1);
1719 if (neg) {
1720 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
Paul Bakker50546922012-05-19 08:40:49 +00001721 Apos.s = 1;
1722 A = &Apos;
1723 }
1724
1725 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001726 * If 1st call, pre-compute R^2 mod N
1727 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 if (prec_RR == NULL || prec_RR->p == NULL) {
1729 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&RR, 1));
1730 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&RR, N->n * 2 * biL));
1731 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&RR, &RR, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if (prec_RR != NULL) {
1734 memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1735 }
1736 } else {
1737 memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
1740 /*
1741 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1742 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
1744 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001745 /* This should be a no-op because W[1] is already that large before
1746 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
Tom Cosgrove93842842022-08-05 16:59:43 +01001747 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
1749 } else {
1750 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001751 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001752
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001753 /* Note that this is safe because W[1] always has at least N->n limbs
1754 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 mpi_montmul(&W[1], &RR, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001756
1757 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001758 * W[x_index] = R^2 * R^-1 mod N = R mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
1761 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
Janos Follathc8d66d52022-11-22 10:47:10 +00001763
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 if (window_bitsize > 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 /*
Janos Follath74601202022-11-21 15:54:20 +00001766 * W[i] = W[1] ^ i
1767 *
1768 * The first bit of the sliding window is always 1 and therefore we
1769 * only need to store the second half of the table.
Janos Follathc8d66d52022-11-22 10:47:10 +00001770 *
1771 * (There are two special elements in the table: W[0] for the
1772 * accumulator/result and W[1] for A in Montgomery form. Both of these
1773 * are already set at this point.)
Paul Bakker5121ce52009-01-03 21:22:43 +00001774 */
Janos Follath74601202022-11-21 15:54:20 +00001775 j = w_table_used_size / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
1778 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 for (i = 0; i < window_bitsize - 1; i++) {
1781 mpi_montmul(&W[j], &W[j], N, mm, &T);
1782 }
Paul Bakker0d7702c2013-10-29 16:18:35 +01001783
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 /*
1785 * W[i] = W[i - 1] * W[1]
1786 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 for (i = j + 1; i < w_table_used_size; i++) {
1788 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
1789 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 mpi_montmul(&W[i], &W[1], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001792 }
1793 }
1794
1795 nblimbs = E->n;
1796 bufsize = 0;
1797 nbits = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001798 state = 0;
1799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 while (1) {
1801 if (bufsize == 0) {
1802 if (nblimbs == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001803 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001805
Paul Bakker0d7702c2013-10-29 16:18:35 +01001806 nblimbs--;
1807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 bufsize = sizeof(mbedtls_mpi_uint) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 }
1810
1811 bufsize--;
1812
1813 ei = (E->p[nblimbs] >> bufsize) & 1;
1814
1815 /*
1816 * skip leading 0s
1817 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 if (ei == 0 && state == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001819 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 if (ei == 0 && state == 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001824 * out of window, square W[x_index]
Paul Bakker5121ce52009-01-03 21:22:43 +00001825 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1827 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 continue;
1829 }
1830
1831 /*
1832 * add ei to current window
1833 */
1834 state = 2;
1835
1836 nbits++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 exponent_bits_in_window |= (ei << (window_bitsize - nbits));
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if (nbits == window_bitsize) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001840 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001841 * W[x_index] = W[x_index]^window_bitsize R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 for (i = 0; i < window_bitsize; i++) {
1844 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1845 x_index));
1846 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001847 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
1849 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001850 * W[x_index] = W[x_index] * W[exponent_bits_in_window] R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001851 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1853 exponent_bits_in_window));
1854 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
1856 state--;
1857 nbits = 0;
Janos Follath7fa11b82022-11-21 14:48:02 +00001858 exponent_bits_in_window = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 }
1860 }
1861
1862 /*
1863 * process the remaining bits
1864 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 for (i = 0; i < nbits; i++) {
1866 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1867 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
Janos Follath7fa11b82022-11-21 14:48:02 +00001869 exponent_bits_in_window <<= 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
1872 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
1873 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 }
1876
1877 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001878 * W[x_index] = A^E * R * R^-1 mod N = A^E mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
Janos Follath8e7d6a02022-10-04 13:27:40 +01001883 W[x_index].s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
Paul Bakkerf6198c12012-05-16 08:02:29 +00001885 }
1886
Janos Follath8e7d6a02022-10-04 13:27:40 +01001887 /*
1888 * Load the result in the output variable.
1889 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 mbedtls_mpi_copy(X, &W[x_index]);
Janos Follath8e7d6a02022-10-04 13:27:40 +01001891
Paul Bakker5121ce52009-01-03 21:22:43 +00001892cleanup:
1893
Janos Follathb2c2fca2022-11-21 15:05:31 +00001894 /* The first bit of the sliding window is always 1 and therefore the first
1895 * half of the table was unused. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 for (i = w_table_used_size/2; i < w_table_used_size; i++) {
1897 mbedtls_mpi_free(&W[i]);
1898 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 mbedtls_mpi_free(&W[x_index]);
1901 mbedtls_mpi_free(&W[1]);
1902 mbedtls_mpi_free(&T);
1903 mbedtls_mpi_free(&Apos);
1904 mbedtls_mpi_free(&WW);
Paul Bakker6c591fa2011-05-05 11:49:20 +00001905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 if (prec_RR == NULL || prec_RR->p == NULL) {
1907 mbedtls_mpi_free(&RR);
1908 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001911}
1912
Paul Bakker5121ce52009-01-03 21:22:43 +00001913/*
1914 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
1915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001917{
Janos Follath24eed8d2019-11-22 13:21:35 +00001918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001919 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03001920 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00001921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 MPI_VALIDATE_RET(G != NULL);
1923 MPI_VALIDATE_RET(A != NULL);
1924 MPI_VALIDATE_RET(B != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
1929 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 lz = mbedtls_mpi_lsb(&TA);
1932 lzt = mbedtls_mpi_lsb(&TB);
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001933
Gilles Peskine27253bc2021-06-09 13:26:43 +02001934 /* The loop below gives the correct result when A==0 but not when B==0.
1935 * So have a special case for B==0. Leverage the fact that we just
1936 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
1937 * slightly more efficient than cmp_int(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
1939 ret = mbedtls_mpi_copy(G, A);
Gilles Peskine27253bc2021-06-09 13:26:43 +02001940 goto cleanup;
1941 }
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if (lzt < lz) {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001944 lz = lzt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001946
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 TA.s = TB.s = 1;
1948
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001949 /* We mostly follow the procedure described in HAC 14.54, but with some
1950 * minor differences:
1951 * - Sequences of multiplications or divisions by 2 are grouped into a
1952 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02001953 * - The procedure in HAC assumes that 0 < TB <= TA.
1954 * - The condition TB <= TA is not actually necessary for correctness.
1955 * TA and TB have symmetric roles except for the loop termination
1956 * condition, and the shifts at the beginning of the loop body
1957 * remove any significance from the ordering of TA vs TB before
1958 * the shifts.
1959 * - If TA = 0, the loop goes through 0 iterations and the result is
1960 * correctly TB.
1961 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001962 *
1963 * For the correctness proof below, decompose the original values of
1964 * A and B as
1965 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
1966 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
1967 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
1968 * and gcd(A',B') is odd or 0.
1969 *
1970 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
1971 * The code maintains the following invariant:
1972 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02001973 */
1974
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001975 /* Proof that the loop terminates:
1976 * At each iteration, either the right-shift by 1 is made on a nonzero
1977 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
1978 * by at least 1, or the right-shift by 1 is made on zero and then
1979 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
1980 * since in that case TB is calculated from TB-TA with the condition TB>TA).
1981 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001983 /* Divisions by 2 preserve the invariant (I). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
1985 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001987 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
1988 * TA-TB is even so the division by 2 has an integer result.
1989 * Invariant (I) is preserved since any odd divisor of both TA and TB
1990 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001991 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001992 * divides TA.
1993 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
1995 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
1996 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
1997 } else {
1998 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
1999 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002001 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 }
2003
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002004 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2005 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2006 * - If there was at least one loop iteration, then one of TA or TB is odd,
2007 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2008 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2009 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002010 * 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 +02002011 */
2012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
2014 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
Paul Bakker5121ce52009-01-03 21:22:43 +00002015
2016cleanup:
2017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002021}
2022
Paul Bakker33dc46b2014-04-30 16:11:39 +02002023/*
2024 * Fill X with size bytes of random.
Gilles Peskine22cdd0c2022-10-27 20:15:13 +02002025 * The bytes returned from the RNG are used in a specific order which
2026 * is suitable for deterministic ECDSA (see the specification of
2027 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
Paul Bakker33dc46b2014-04-30 16:11:39 +02002028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002029int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
2030 int (*f_rng)(void *, unsigned char *, size_t),
2031 void *p_rng)
Paul Bakker287781a2011-03-26 13:18:49 +00002032{
Janos Follath24eed8d2019-11-22 13:21:35 +00002033 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 const size_t limbs = CHARS_TO_LIMBS(size);
Hanno Beckerda1655a2017-10-18 14:21:44 +01002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 MPI_VALIDATE_RET(X != NULL);
2037 MPI_VALIDATE_RET(f_rng != NULL);
Paul Bakker33dc46b2014-04-30 16:11:39 +02002038
Hanno Beckerda1655a2017-10-18 14:21:44 +01002039 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
2041 if (size == 0) {
2042 return 0;
2043 }
Paul Bakker287781a2011-03-26 13:18:49 +00002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +00002046
2047cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return ret;
Paul Bakker287781a2011-03-26 13:18:49 +00002049}
2050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051int mbedtls_mpi_random(mbedtls_mpi *X,
2052 mbedtls_mpi_sint min,
2053 const mbedtls_mpi *N,
2054 int (*f_rng)(void *, unsigned char *, size_t),
2055 void *p_rng)
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002056{
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if (min < 0) {
2058 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2059 }
2060 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
2061 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2062 }
Gilles Peskine1e918f42021-03-29 22:14:51 +02002063
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002064 /* Ensure that target MPI has exactly the same number of limbs
2065 * as the upper bound, even if the upper bound has leading zeros.
Gilles Peskine6b7ce962022-12-15 15:04:33 +01002066 * This is necessary for mbedtls_mpi_core_random. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 int ret = mbedtls_mpi_resize_clear(X, N->n);
2068 if (ret != 0) {
2069 return ret;
2070 }
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002073}
2074
Paul Bakker5121ce52009-01-03 21:22:43 +00002075/*
2076 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2077 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002078int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00002079{
Janos Follath24eed8d2019-11-22 13:21:35 +00002080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 MPI_VALIDATE_RET(X != NULL);
2083 MPI_VALIDATE_RET(A != NULL);
2084 MPI_VALIDATE_RET(N != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00002085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
2087 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2088 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
2091 mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
2092 mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 goto cleanup;
2099 }
2100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
2102 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
2103 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
2104 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
2107 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
2108 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
2109 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 do {
2112 while ((TU.p[0] & 1) == 0) {
2113 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
2116 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
2117 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 }
2119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
2121 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 }
2123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 while ((TV.p[0] & 1) == 0) {
2125 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
2128 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
2129 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 }
2131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
2133 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 }
2135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
2137 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
2138 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
2139 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
2140 } else {
2141 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
2142 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
2143 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
2146
2147 while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
2148 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
2152 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
2153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
2157cleanup:
2158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
2160 mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
2161 mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002164}
2165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002167
Paul Bakker5121ce52009-01-03 21:22:43 +00002168static const int small_prime[] =
2169{
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 3, 5, 7, 11, 13, 17, 19, 23,
2171 29, 31, 37, 41, 43, 47, 53, 59,
2172 61, 67, 71, 73, 79, 83, 89, 97,
2173 101, 103, 107, 109, 113, 127, 131, 137,
2174 139, 149, 151, 157, 163, 167, 173, 179,
2175 181, 191, 193, 197, 199, 211, 223, 227,
2176 229, 233, 239, 241, 251, 257, 263, 269,
2177 271, 277, 281, 283, 293, 307, 311, 313,
2178 317, 331, 337, 347, 349, 353, 359, 367,
2179 373, 379, 383, 389, 397, 401, 409, 419,
2180 421, 431, 433, 439, 443, 449, 457, 461,
2181 463, 467, 479, 487, 491, 499, 503, 509,
2182 521, 523, 541, 547, 557, 563, 569, 571,
2183 577, 587, 593, 599, 601, 607, 613, 617,
2184 619, 631, 641, 643, 647, 653, 659, 661,
2185 673, 677, 683, 691, 701, 709, 719, 727,
2186 733, 739, 743, 751, 757, 761, 769, 773,
2187 787, 797, 809, 811, 821, 823, 827, 829,
2188 839, 853, 857, 859, 863, 877, 881, 883,
2189 887, 907, 911, 919, 929, 937, 941, 947,
2190 953, 967, 971, 977, 983, 991, 997, -103
Paul Bakker5121ce52009-01-03 21:22:43 +00002191};
2192
2193/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002194 * Small divisors test (X must be positive)
2195 *
2196 * Return values:
2197 * 0: no small factor (possible prime, more tests needed)
2198 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002200 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202static int mpi_check_small_factors(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +00002203{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002204 int ret = 0;
2205 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if ((X->p[0] & 1) == 0) {
2209 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 for (i = 0; small_prime[i] > 0; i++) {
2213 if (mbedtls_mpi_cmp_int(X, small_prime[i]) <= 0) {
2214 return 1;
2215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, small_prime[i]));
Paul Bakker5121ce52009-01-03 21:22:43 +00002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (r == 0) {
2220 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2221 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 }
2223
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002224cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 return ret;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002226}
2227
2228/*
2229 * Miller-Rabin pseudo-primality test (HAC 4.24)
2230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002231static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2232 int (*f_rng)(void *, unsigned char *, size_t),
2233 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002234{
Pascal Junodb99183d2015-03-11 16:49:45 +01002235 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002236 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 MPI_VALIDATE_RET(X != NULL);
2240 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2243 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2244 mbedtls_mpi_init(&RR);
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002245
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 /*
2247 * W = |X| - 1
2248 * R = W >> lsb( W )
2249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2251 s = mbedtls_mpi_lsb(&W);
2252 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2253 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 for (i = 0; i < rounds; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 /*
2257 * pick a random A, 1 < A < |X| - 1
2258 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002259 count = 0;
2260 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
Pascal Junodb99183d2015-03-11 16:49:45 +01002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 j = mbedtls_mpi_bitlen(&A);
2264 k = mbedtls_mpi_bitlen(&W);
Pascal Junodb99183d2015-03-11 16:49:45 +01002265 if (j > k) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002267 }
2268
2269 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002270 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2271 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002272 }
2273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2275 mbedtls_mpi_cmp_int(&A, 1) <= 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
2277 /*
2278 * A = A^R mod |X|
2279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2283 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002286
2287 j = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 /*
2290 * A = A * A mod |X|
2291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2293 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
2299 j++;
2300 }
2301
2302 /*
2303 * not prime if A != |X| - 1 or A == 1
2304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2306 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 break;
2309 }
2310 }
2311
2312cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2314 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2315 mbedtls_mpi_free(&RR);
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002318}
2319
2320/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002321 * Pseudo-primality test: small factors, then Miller-Rabin
2322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002323int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2324 int (*f_rng)(void *, unsigned char *, size_t),
2325 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002326{
Janos Follath24eed8d2019-11-22 13:21:35 +00002327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 mbedtls_mpi XX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 MPI_VALIDATE_RET(X != NULL);
2330 MPI_VALIDATE_RET(f_rng != NULL);
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002331
2332 XX.s = 1;
2333 XX.n = X->n;
2334 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2337 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2338 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002339 }
2340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2342 return 0;
2343 }
2344
2345 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2346 if (ret == 1) {
2347 return 0;
2348 }
2349
2350 return ret;
2351 }
2352
2353 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
Janos Follathf301d232018-08-14 13:34:01 +01002354}
2355
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002356/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002358 *
Janos Follathf301d232018-08-14 13:34:01 +01002359 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2360 * be either 1024 bits or 1536 bits long, and flags must contain
2361 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002363int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2364 int (*f_rng)(void *, unsigned char *, size_t),
2365 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00002366{
Jethro Beekman66689272018-02-14 19:24:10 -08002367#ifdef MBEDTLS_HAVE_INT64
2368// ceil(2^63.5)
2369#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2370#else
2371// ceil(2^31.5)
2372#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2373#endif
2374 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002375 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002376 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 mbedtls_mpi_uint r;
2378 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MPI_VALIDATE_RET(X != NULL);
2381 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2384 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 mbedtls_mpi_init(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 n = BITS_TO_LIMBS(nbits);
Paul Bakker5121ce52009-01-03 21:22:43 +00002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
Janos Follathda31fa12018-09-03 14:45:23 +01002392 /*
2393 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2396 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2397 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2398 } else {
Janos Follathda31fa12018-09-03 14:45:23 +01002399 /*
2400 * 2^-100 error probability, number of rounds computed based on HAC,
2401 * fact 4.48
2402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2404 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2405 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2406 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
Janos Follathda31fa12018-09-03 14:45:23 +01002407 }
2408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 while (1) {
2410 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
Jethro Beekman66689272018-02-14 19:24:10 -08002411 /* 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 +01002412 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2413 continue;
2414 }
Jethro Beekman66689272018-02-14 19:24:10 -08002415
2416 k = n * biL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (k > nbits) {
2418 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2419 }
Jethro Beekman66689272018-02-14 19:24:10 -08002420 X->p[0] |= 1;
2421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2423 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
Jethro Beekman66689272018-02-14 19:24:10 -08002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 }
2428 } else {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002429 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002430 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002431 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2432 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002433 */
Jethro Beekman66689272018-02-14 19:24:10 -08002434
2435 X->p[0] |= 2;
2436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2438 if (r == 0) {
2439 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2440 } else if (r == 1) {
2441 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2442 }
Jethro Beekman66689272018-02-14 19:24:10 -08002443
2444 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2446 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
Jethro Beekman66689272018-02-14 19:24:10 -08002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 while (1) {
Jethro Beekman66689272018-02-14 19:24:10 -08002449 /*
2450 * First, check small factors for X and Y
2451 * before doing Miller-Rabin on any of them
2452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 if ((ret = mpi_check_small_factors(X)) == 0 &&
2454 (ret = mpi_check_small_factors(&Y)) == 0 &&
2455 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2456 == 0 &&
2457 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2458 == 0) {
Jethro Beekman66689272018-02-14 19:24:10 -08002459 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 }
Jethro Beekman66689272018-02-14 19:24:10 -08002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Jethro Beekman66689272018-02-14 19:24:10 -08002463 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 }
Jethro Beekman66689272018-02-14 19:24:10 -08002465
2466 /*
2467 * Next candidates. We want to preserve Y = (X-1) / 2 and
2468 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2469 * so up Y by 6 and X by 12.
2470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2472 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 }
2475 }
2476
2477cleanup:
2478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 mbedtls_mpi_free(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002482}
2483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
Paul Bakker23986e52011-04-24 08:57:21 +00002488#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002489
2490static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2491{
2492 { 693, 609, 21 },
2493 { 1764, 868, 28 },
2494 { 768454923, 542167814, 1 }
2495};
2496
Paul Bakker5121ce52009-01-03 21:22:43 +00002497/*
2498 * Checkup routine
2499 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002500int mbedtls_mpi_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002501{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002502 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2506 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2509 "EFE021C2645FD1DC586E69184AF4A31E" \
2510 "D5F53E93B5F123FA41680867BA110131" \
2511 "944FE7952E2517337780CB0DB80E61AA" \
2512 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2515 "B2E7EFD37075B9F03FF989C7C5051C20" \
2516 "34D2A323810251127E7BF8625A4F49A5" \
2517 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2518 "5B5C25763222FEFCCFC38B832366C29E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2521 "0066A198186C18C10B2F5ED9B522752A" \
2522 "9830B69916E535C8F047518A889A43A5" \
2523 "94B6BED27A168D31D4A52F88925AA8F5"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2528 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2529 "9E857EA95A03512E2BAE7391688D264A" \
2530 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2531 "8001B72E848A38CAE1C65F78E56ABDEF" \
2532 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2533 "ECF677152EF804370C1A305CAF3B5BF1" \
2534 "30879B56C61DE584A0F53A2447A51E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (verbose != 0) {
2537 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2541 if (verbose != 0) {
2542 mbedtls_printf("failed\n");
2543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002545 ret = 1;
2546 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002547 }
2548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 if (verbose != 0) {
2550 mbedtls_printf("passed\n");
2551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2556 "256567336059E52CAE22925474705F39A94"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2559 "6613F26162223DF488E9CD48CC132C7A" \
2560 "0AC93C701B001B092E4E5B9F73BCD27B" \
2561 "9EE50D0657C77F374E903CDFA4C642"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (verbose != 0) {
2564 mbedtls_printf(" MPI test #2 (div_mpi): ");
2565 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2568 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2569 if (verbose != 0) {
2570 mbedtls_printf("failed\n");
2571 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002573 ret = 1;
2574 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
2576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (verbose != 0) {
2578 mbedtls_printf("passed\n");
2579 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2584 "36E139AEA55215609D2816998ED020BB" \
2585 "BD96C37890F65171D948E9BC7CBAA4D9" \
2586 "325D24D6A3C12710F10A09FA08AB87"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (verbose != 0) {
2589 mbedtls_printf(" MPI test #3 (exp_mod): ");
2590 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2593 if (verbose != 0) {
2594 mbedtls_printf("failed\n");
2595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002597 ret = 1;
2598 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
2600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 if (verbose != 0) {
2602 mbedtls_printf("passed\n");
2603 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2608 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2609 "C3DBA76456363A10869622EAC2DD84EC" \
2610 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (verbose != 0) {
2613 mbedtls_printf(" MPI test #4 (inv_mod): ");
2614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2617 if (verbose != 0) {
2618 mbedtls_printf("failed\n");
2619 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002621 ret = 1;
2622 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 }
2624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (verbose != 0) {
2626 mbedtls_printf("passed\n");
2627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (verbose != 0) {
2630 mbedtls_printf(" MPI test #5 (simple gcd): ");
2631 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2634 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2635 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2640 if (verbose != 0) {
2641 mbedtls_printf("failed at %d\n", i);
2642 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002643
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002644 ret = 1;
2645 goto cleanup;
2646 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002647 }
2648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if (verbose != 0) {
2650 mbedtls_printf("passed\n");
2651 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002652
Paul Bakker5121ce52009-01-03 21:22:43 +00002653cleanup:
2654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 if (ret != 0 && verbose != 0) {
2656 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2660 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if (verbose != 0) {
2663 mbedtls_printf("\n");
2664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002667}
2668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#endif /* MBEDTLS_BIGNUM_C */