2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Multi-precision integer library
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2015-11-26 20:35:03 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2015-11-26 20:35:03 +01:00
|
|
|
* The following sources were referenced in the design of this Multi-precision
|
|
|
|
* Integer library:
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-11-26 20:35:03 +01:00
|
|
|
* [1] Handbook of Applied Cryptography - 1997
|
|
|
|
* Menezes, van Oorschot and Vanstone
|
|
|
|
*
|
|
|
|
* [2] Multi-Precision Math
|
|
|
|
* Tom St Denis
|
|
|
|
* https://github.com/libtom/libtommath/blob/develop/tommath.pdf
|
|
|
|
*
|
|
|
|
* [3] GNU Multi-Precision Arithmetic Library
|
|
|
|
* https://gmplib.org/manual/index.html
|
|
|
|
*
|
2015-12-28 00:01:55 +01:00
|
|
|
*/
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/bignum.h"
|
2022-07-21 16:34:47 +02:00
|
|
|
#include "bignum_core.h"
|
2021-03-03 18:45:34 +01:00
|
|
|
#include "bn_mul.h"
|
2018-04-17 16:51:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-11-22 14:21:35 +01:00
|
|
|
#include "mbedtls/error.h"
|
2021-10-20 12:09:35 +02:00
|
|
|
#include "constant_time_internal.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2021-12-06 18:50:53 +01:00
|
|
|
#include <limits.h>
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MPI_VALIDATE_RET(cond) \
|
|
|
|
MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA)
|
|
|
|
#define MPI_VALIDATE(cond) \
|
|
|
|
MBEDTLS_INTERNAL_VALIDATE(cond)
|
2022-08-03 12:52:26 +02:00
|
|
|
|
2023-05-09 15:01:05 +02:00
|
|
|
/*
|
|
|
|
* Compare signed values in constant time
|
|
|
|
*/
|
|
|
|
int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *Y,
|
|
|
|
unsigned *ret)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
/* The value of any of these variables is either 0 or 1 at all times. */
|
|
|
|
unsigned cond, done, X_is_negative, Y_is_negative;
|
|
|
|
|
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(Y != NULL);
|
|
|
|
MPI_VALIDATE_RET(ret != NULL);
|
|
|
|
|
|
|
|
if (X->n != Y->n) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set sign_N to 1 if N >= 0, 0 if N < 0.
|
|
|
|
* We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
|
|
|
|
*/
|
|
|
|
X_is_negative = (X->s & 2) >> 1;
|
|
|
|
Y_is_negative = (Y->s & 2) >> 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the signs are different, then the positive operand is the bigger.
|
|
|
|
* That is if X is negative (X_is_negative == 1), then X < Y is true and it
|
|
|
|
* is false if X is positive (X_is_negative == 0).
|
|
|
|
*/
|
|
|
|
cond = (X_is_negative ^ Y_is_negative);
|
|
|
|
*ret = cond & X_is_negative;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a constant-time function. We might have the result, but we still
|
|
|
|
* need to go through the loop. Record if we have the result already.
|
|
|
|
*/
|
|
|
|
done = cond;
|
|
|
|
|
|
|
|
for (i = X->n; i > 0; i--) {
|
|
|
|
/*
|
|
|
|
* If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
|
|
|
|
* X and Y are negative.
|
|
|
|
*
|
|
|
|
* Again even if we can make a decision, we just mark the result and
|
|
|
|
* the fact that we are done and continue looping.
|
|
|
|
*/
|
|
|
|
cond = mbedtls_ct_mpi_uint_lt(Y->p[i - 1], X->p[i - 1]);
|
|
|
|
*ret |= cond & (1 - done) & X_is_negative;
|
|
|
|
done |= cond;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
|
|
|
|
* X and Y are positive.
|
|
|
|
*
|
|
|
|
* Again even if we can make a decision, we just mark the result and
|
|
|
|
* the fact that we are done and continue looping.
|
|
|
|
*/
|
|
|
|
cond = mbedtls_ct_mpi_uint_lt(X->p[i - 1], Y->p[i - 1]);
|
|
|
|
*ret |= cond & (1 - done) & (1 - X_is_negative);
|
|
|
|
done |= cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Conditionally assign X = Y, without leaking information
|
|
|
|
* about whether the assignment was made or not.
|
|
|
|
* (Leaking information about the respective sizes of X and Y is ok however.)
|
|
|
|
*/
|
|
|
|
#if defined(_MSC_VER) && defined(_M_ARM64) && (_MSC_FULL_VER < 193131103)
|
|
|
|
/*
|
|
|
|
* MSVC miscompiles this function if it's inlined prior to Visual Studio 2022 version 17.1. See:
|
|
|
|
* https://developercommunity.visualstudio.com/t/c-compiler-miscompiles-part-of-mbedtls-library-on/1646989
|
|
|
|
*/
|
|
|
|
__declspec(noinline)
|
|
|
|
#endif
|
|
|
|
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *Y,
|
|
|
|
unsigned char assign)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(Y != NULL);
|
|
|
|
|
2023-05-17 14:31:55 +02:00
|
|
|
mbedtls_ct_condition_t do_assign = mbedtls_ct_bool(assign);
|
|
|
|
|
2023-05-09 15:01:05 +02:00
|
|
|
/* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
|
|
|
|
mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask(assign);
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
|
|
|
|
|
|
|
|
X->s = (int) mbedtls_ct_uint_if(assign, Y->s, X->s);
|
|
|
|
|
2023-05-17 14:31:55 +02:00
|
|
|
mbedtls_mpi_core_cond_assign(X->p, Y->p, Y->n, do_assign);
|
2023-05-09 15:01:05 +02:00
|
|
|
|
|
|
|
for (size_t i = Y->n; i < X->n; i++) {
|
|
|
|
X->p[i] &= ~limb_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Conditionally swap X and Y, without leaking information
|
|
|
|
* about whether the swap was made or not.
|
|
|
|
* Here it is not ok to simply swap the pointers, which would lead to
|
|
|
|
* different memory access patterns when X and Y are used afterwards.
|
|
|
|
*/
|
|
|
|
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X,
|
|
|
|
mbedtls_mpi *Y,
|
|
|
|
unsigned char swap)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int s;
|
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(Y != NULL);
|
|
|
|
|
|
|
|
if (X == Y) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-17 14:31:55 +02:00
|
|
|
mbedtls_ct_condition_t do_swap = mbedtls_ct_bool(swap);
|
|
|
|
|
2023-05-09 15:01:05 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Y, X->n));
|
|
|
|
|
|
|
|
s = X->s;
|
|
|
|
X->s = (int) mbedtls_ct_uint_if(swap, Y->s, X->s);
|
|
|
|
Y->s = (int) mbedtls_ct_uint_if(swap, s, Y->s);
|
|
|
|
|
2023-05-17 14:31:55 +02:00
|
|
|
mbedtls_mpi_core_cond_swap(X->p, Y->p, X->n, do_swap);
|
2023-05-09 15:01:05 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-17 16:51:09 +02:00
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
2023-01-11 14:50:10 +01:00
|
|
|
static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n)
|
2018-04-24 15:39:07 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(v, ciL * n);
|
2018-04-17 16:51:09 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2011-05-05 13:49:20 +02:00
|
|
|
* Initialize one MPI
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_init(mbedtls_mpi *X)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE(X != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-05-05 13:49:20 +02:00
|
|
|
X->s = 1;
|
|
|
|
X->n = 0;
|
|
|
|
X->p = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-05-05 13:49:20 +02:00
|
|
|
* Unallocate one MPI
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_free(mbedtls_mpi *X)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X == NULL) {
|
2011-05-05 13:49:20 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->p != NULL) {
|
|
|
|
mbedtls_mpi_zeroize(X->p, X->n);
|
|
|
|
mbedtls_free(X->p);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-05-05 13:49:20 +02:00
|
|
|
X->s = 1;
|
|
|
|
X->n = 0;
|
|
|
|
X->p = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enlarge to the specified number of limbs
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint *p;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
|
|
|
|
return MBEDTLS_ERR_MPI_ALLOC_FAILED;
|
|
|
|
}
|
2011-05-05 12:00:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->n < nblimbs) {
|
|
|
|
if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_MPI_ALLOC_FAILED;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->p != NULL) {
|
|
|
|
memcpy(p, X->p, X->n * ciL);
|
|
|
|
mbedtls_mpi_zeroize(X->p, X->n);
|
|
|
|
mbedtls_free(X->p);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
X->n = nblimbs;
|
|
|
|
X->p = p;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2013-11-21 10:39:37 +01:00
|
|
|
/*
|
|
|
|
* Resize down as much as possible,
|
|
|
|
* while keeping at least the specified number of limbs
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
|
2013-11-21 10:39:37 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint *p;
|
2013-11-21 10:39:37 +01:00
|
|
|
size_t i;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
2018-12-11 11:35:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
|
|
|
|
return MBEDTLS_ERR_MPI_ALLOC_FAILED;
|
|
|
|
}
|
2013-11-21 10:39:37 +01:00
|
|
|
|
2020-01-20 21:17:43 +01:00
|
|
|
/* Actually resize up if there are currently fewer than nblimbs limbs. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->n <= nblimbs) {
|
|
|
|
return mbedtls_mpi_grow(X, nblimbs);
|
|
|
|
}
|
2020-01-21 13:59:51 +01:00
|
|
|
/* After this point, then X->n > nblimbs and in particular X->n > 0. */
|
2013-11-21 10:39:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = X->n - 1; i > 0; i--) {
|
|
|
|
if (X->p[i] != 0) {
|
2013-11-21 10:39:37 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-21 10:39:37 +01:00
|
|
|
i++;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i < nblimbs) {
|
2013-11-21 10:39:37 +01:00
|
|
|
i = nblimbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-11-21 10:39:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_MPI_ALLOC_FAILED;
|
|
|
|
}
|
2013-11-21 10:39:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->p != NULL) {
|
|
|
|
memcpy(p, X->p, i * ciL);
|
|
|
|
mbedtls_mpi_zeroize(X->p, X->n);
|
|
|
|
mbedtls_free(X->p);
|
2013-11-21 10:39:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
X->n = i;
|
|
|
|
X->p = p;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-11-21 10:39:37 +01:00
|
|
|
}
|
|
|
|
|
2021-06-02 22:17:52 +02:00
|
|
|
/* Resize X to have exactly n limbs and set it to 0. */
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
|
2021-06-02 22:17:52 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (limbs == 0) {
|
|
|
|
mbedtls_mpi_free(X);
|
|
|
|
return 0;
|
|
|
|
} else if (X->n == limbs) {
|
|
|
|
memset(X->p, 0, limbs * ciL);
|
2021-06-02 22:17:52 +02:00
|
|
|
X->s = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
mbedtls_mpi_free(X);
|
|
|
|
return mbedtls_mpi_grow(X, limbs);
|
2021-06-02 22:17:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2021-06-08 23:17:42 +02:00
|
|
|
* Copy the contents of Y into X.
|
|
|
|
*
|
|
|
|
* This function is not constant-time. Leading zeros in Y may be removed.
|
|
|
|
*
|
|
|
|
* Ensure that X does not shrink. This is not guaranteed by the public API,
|
|
|
|
* but some code in the bignum module relies on this property, for example
|
|
|
|
* in mbedtls_mpi_exp_mod().
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2018-03-21 16:29:03 +01:00
|
|
|
int ret = 0;
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t i;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(Y != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X == Y) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (Y->n == 0) {
|
|
|
|
if (X->n != 0) {
|
2021-06-08 23:17:42 +02:00
|
|
|
X->s = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(X->p, 0, X->n * ciL);
|
2021-06-08 23:17:42 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-08-12 17:02:59 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = Y->n - 1; i > 0; i--) {
|
|
|
|
if (Y->p[i] != 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
i++;
|
|
|
|
|
|
|
|
X->s = Y->s;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->n < i) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
|
|
|
|
} else {
|
|
|
|
memset(X->p + i, 0, (X->n - i) * ciL);
|
2018-03-21 16:29:03 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X->p, Y->p, i * ciL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Swap the contents of X and Y
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi T;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE(X != NULL);
|
|
|
|
MPI_VALIDATE(Y != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(&T, X, sizeof(mbedtls_mpi));
|
|
|
|
memcpy(X, Y, sizeof(mbedtls_mpi));
|
|
|
|
memcpy(Y, &T, sizeof(mbedtls_mpi));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
|
2022-11-15 23:25:27 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (z >= 0) {
|
|
|
|
return z;
|
|
|
|
}
|
2022-11-15 23:25:27 +01:00
|
|
|
/* Take care to handle the most negative value (-2^(biL-1)) correctly.
|
|
|
|
* A naive -z would have undefined behavior.
|
|
|
|
* Write this in a way that makes popular compilers happy (GCC, Clang,
|
|
|
|
* MSVC). */
|
2023-01-11 14:50:10 +01:00
|
|
|
return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
|
2022-11-15 23:25:27 +01:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Set value from integer
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
|
|
|
|
memset(X->p, 0, X->n * ciL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
X->p[0] = mpi_sint_abs(z);
|
|
|
|
X->s = (z < 0) ? -1 : 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-05-18 17:47:11 +02:00
|
|
|
/*
|
|
|
|
* Get a specific bit
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
|
2011-05-18 17:47:11 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
2018-12-11 11:35:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->n * biL <= pos) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (X->p[pos / biL] >> (pos % biL)) & 0x01;
|
2011-05-18 17:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a bit to a specific value of 0 or 1
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
|
2011-05-18 17:47:11 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
size_t off = pos / biL;
|
|
|
|
size_t idx = pos % biL;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (val != 0 && val != 1) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2014-05-01 13:03:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->n * biL <= pos) {
|
|
|
|
if (val == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
|
2011-05-18 17:47:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
|
2015-04-08 12:49:31 +02:00
|
|
|
X->p[off] |= (mbedtls_mpi_uint) val << idx;
|
2011-05-18 17:47:11 +02:00
|
|
|
|
|
|
|
cleanup:
|
2014-05-01 13:03:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2011-05-18 17:47:11 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2015-06-18 16:47:17 +02:00
|
|
|
* Return the number of less significant zero-bits
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t i, j, count = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_INTERNAL_VALIDATE_RET(X != NULL, 0);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < X->n; i++) {
|
|
|
|
for (j = 0; j < biL; j++, count++) {
|
|
|
|
if (((X->p[i] >> j) & 1) != 0) {
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-06-18 16:47:17 +02:00
|
|
|
* Return the number of bits
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_mpi_core_bitlen(X->p, X->n);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the total size in bytes
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return (mbedtls_mpi_bitlen(X) + 7) >> 3;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert an ASCII character to digit value
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
*d = 255;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (c >= 0x30 && c <= 0x39) {
|
|
|
|
*d = c - 0x30;
|
|
|
|
}
|
|
|
|
if (c >= 0x41 && c <= 0x46) {
|
|
|
|
*d = c - 0x37;
|
|
|
|
}
|
|
|
|
if (c >= 0x61 && c <= 0x66) {
|
|
|
|
*d = c - 0x57;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*d >= (mbedtls_mpi_uint) radix) {
|
|
|
|
return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Import from an ASCII string
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t i, j, slen, n;
|
2021-04-03 18:26:13 +02:00
|
|
|
int sign = 1;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint d;
|
|
|
|
mbedtls_mpi T;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(s != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (radix < 2 || radix > 16) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&T);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s[0] == 0) {
|
|
|
|
mbedtls_mpi_free(X);
|
|
|
|
return 0;
|
2021-06-08 18:32:34 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s[0] == '-') {
|
2021-04-03 18:26:13 +02:00
|
|
|
++s;
|
|
|
|
sign = -1;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
slen = strlen(s);
|
2010-03-16 22:09:09 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (radix == 16) {
|
2023-05-18 21:49:03 +02:00
|
|
|
if (slen > SIZE_MAX >> 2) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2015-09-28 13:48:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
n = BITS_TO_LIMBS(slen << 2);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = slen, j = 0; i > 0; i--, j++) {
|
|
|
|
MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
|
|
|
|
X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < slen; i++) {
|
|
|
|
MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
|
2021-04-03 18:26:13 +02:00
|
|
|
X->s = -1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-04-03 18:26:13 +02:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&T);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-11-20 13:07:01 +01:00
|
|
|
* Helper to write the digits high-order first.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mpi_write_hlp(mbedtls_mpi *X, int radix,
|
|
|
|
char **p, const size_t buflen)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint r;
|
2018-11-20 13:07:01 +01:00
|
|
|
size_t length = 0;
|
|
|
|
char *p_end = *p + buflen;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
|
|
|
if (length >= buflen) {
|
|
|
|
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
|
2018-11-20 13:07:01 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
|
2018-11-20 13:07:01 +01:00
|
|
|
/*
|
|
|
|
* Write the residue in the current position, as an ASCII character.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (r < 0xA) {
|
|
|
|
*(--p_end) = (char) ('0' + r);
|
|
|
|
} else {
|
|
|
|
*(--p_end) = (char) ('A' + (r - 0xA));
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2018-11-20 13:07:01 +01:00
|
|
|
length++;
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (mbedtls_mpi_cmp_int(X, 0) != 0);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memmove(*p, p_end, length);
|
2018-11-20 13:07:01 +01:00
|
|
|
*p += length;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Export into an ASCII string
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
|
|
|
|
char *buf, size_t buflen, size_t *olen)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret = 0;
|
|
|
|
size_t n;
|
2009-01-03 22:22:43 +01:00
|
|
|
char *p;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi T;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(olen != NULL);
|
|
|
|
MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (radix < 2 || radix > 16) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */
|
|
|
|
if (radix >= 4) {
|
|
|
|
n >>= 1; /* Number of 4-adic digits necessary to present
|
2019-02-04 10:45:07 +01:00
|
|
|
* `n`. If radix > 4, this might be a strict
|
|
|
|
* overapproximation of the number of
|
|
|
|
* radix-adic digits needed to present `n`. */
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (radix >= 16) {
|
|
|
|
n >>= 1; /* Number of hexadecimal digits necessary to
|
2019-02-04 10:45:07 +01:00
|
|
|
* present `n`. */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-03-06 14:43:02 +01:00
|
|
|
n += 1; /* Terminating null byte */
|
2019-02-04 10:45:07 +01:00
|
|
|
n += 1; /* Compensate for the divisions above, which round down `n`
|
|
|
|
* in case it's not even. */
|
|
|
|
n += 1; /* Potential '-'-sign. */
|
2023-01-11 14:50:10 +01:00
|
|
|
n += (n & 1); /* Make n even to have enough space for hexadecimal writing,
|
2019-02-04 10:45:07 +01:00
|
|
|
* which always uses an even number of hex-digits. */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buflen < n) {
|
2015-06-02 16:41:48 +02:00
|
|
|
*olen = n;
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-06-02 16:41:48 +02:00
|
|
|
p = buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&T);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->s == -1) {
|
2009-01-03 22:22:43 +01:00
|
|
|
*p++ = '-';
|
2019-02-01 17:41:30 +01:00
|
|
|
buflen--;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (radix == 16) {
|
2011-04-24 10:57:21 +02:00
|
|
|
int c;
|
|
|
|
size_t i, j, k;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = X->n, k = 0; i > 0; i--) {
|
|
|
|
for (j = ciL; j > 0; j--) {
|
|
|
|
c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (c == 0 && k == 0 && (i + j) != 2) {
|
2009-01-03 22:22:43 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2012-10-24 13:17:48 +02:00
|
|
|
*(p++) = "0123456789ABCDEF" [c / 16];
|
2012-10-30 08:49:19 +01:00
|
|
|
*(p++) = "0123456789ABCDEF" [c % 16];
|
2009-01-03 22:22:43 +01:00
|
|
|
k = 1;
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
|
2009-06-23 21:46:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (T.s == -1) {
|
2009-06-23 21:46:08 +02:00
|
|
|
T.s = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-06-23 21:46:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*p++ = '\0';
|
2015-06-02 16:41:48 +02:00
|
|
|
*olen = p - buf;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&T);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Read X from an opened file
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint d;
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t slen;
|
2009-01-03 22:22:43 +01:00
|
|
|
char *p;
|
2011-11-25 13:11:43 +01:00
|
|
|
/*
|
2011-11-30 17:00:20 +01:00
|
|
|
* Buffer should have space for (short) label and decimal formatted MPI,
|
|
|
|
* newline characters and '\0'
|
2011-11-25 13:11:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(fin != NULL);
|
2018-12-11 11:35:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (radix < 2 || radix > 16) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-12-11 11:35:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(s, 0, sizeof(s));
|
|
|
|
if (fgets(s, sizeof(s) - 1, fin) == NULL) {
|
|
|
|
return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
slen = strlen(s);
|
|
|
|
if (slen == sizeof(s) - 2) {
|
|
|
|
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2011-11-30 17:00:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (slen > 0 && s[slen - 1] == '\n') {
|
|
|
|
slen--; s[slen] = '\0';
|
|
|
|
}
|
|
|
|
if (slen > 0 && s[slen - 1] == '\r') {
|
|
|
|
slen--; s[slen] = '\0';
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
p = s + slen;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p-- > s) {
|
|
|
|
if (mpi_get_digit(&d, radix, *p) != 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_mpi_read_string(X, radix, p + 1);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write X into an opened file (or stdout if fout == NULL)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t n, slen, plen;
|
2011-11-25 13:11:43 +01:00
|
|
|
/*
|
2012-09-26 21:20:46 +02:00
|
|
|
* Buffer should have space for (short) label and decimal formatted MPI,
|
|
|
|
* newline characters and '\0'
|
2011-11-25 13:11:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
|
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
2018-12-11 11:35:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (radix < 2 || radix > 16) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(s, 0, sizeof(s));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p == NULL) {
|
|
|
|
p = "";
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
plen = strlen(p);
|
|
|
|
slen = strlen(s);
|
2009-01-03 22:22:43 +01:00
|
|
|
s[slen++] = '\r';
|
|
|
|
s[slen++] = '\n';
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fout != NULL) {
|
|
|
|
if (fwrite(p, 1, plen, fout) != plen ||
|
|
|
|
fwrite(s, 1, slen, fout) != slen) {
|
|
|
|
return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mbedtls_printf("%s%s", p, s);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_FS_IO */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2019-02-13 11:28:28 +01:00
|
|
|
/*
|
|
|
|
* Import X from unsigned binary data, little endian
|
2022-02-21 13:42:09 +01:00
|
|
|
*
|
|
|
|
* This function is guaranteed to return an MPI with exactly the necessary
|
|
|
|
* number of limbs (in particular, it does not skip 0s in the input).
|
2019-02-13 11:28:28 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
|
|
|
|
const unsigned char *buf, size_t buflen)
|
2019-02-13 11:28:28 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t limbs = CHARS_TO_LIMBS(buflen);
|
2019-02-13 11:28:28 +01:00
|
|
|
|
|
|
|
/* Ensure that target MPI has exactly the necessary number of limbs */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
|
2019-02-13 11:28:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
|
2019-02-13 11:28:28 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2019-02-15 17:17:45 +01:00
|
|
|
/*
|
|
|
|
* This function is also used to import keys. However, wiping the buffers
|
|
|
|
* upon failure is not necessary because failure only can happen before any
|
|
|
|
* input is copied.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2019-02-13 11:28:28 +01:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Import X from unsigned binary data, big endian
|
2022-02-21 13:42:09 +01:00
|
|
|
*
|
|
|
|
* This function is guaranteed to return an MPI with exactly the necessary
|
|
|
|
* number of limbs (in particular, it does not skip 0s in the input).
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t limbs = CHARS_TO_LIMBS(buflen);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
|
2018-12-11 11:35:51 +01:00
|
|
|
|
2017-10-17 16:17:27 +02:00
|
|
|
/* Ensure that target MPI has exactly the necessary number of limbs */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2019-02-15 17:17:45 +01:00
|
|
|
/*
|
|
|
|
* This function is also used to import keys. However, wiping the buffers
|
|
|
|
* upon failure is not necessary because failure only can happen before any
|
|
|
|
* input is copied.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 17:17:40 +01:00
|
|
|
/*
|
|
|
|
* Export X into unsigned binary data, little endian
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
|
|
|
|
unsigned char *buf, size_t buflen)
|
2019-02-19 17:17:40 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
|
2019-02-19 17:17:40 +01:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Export X into unsigned binary data, big endian
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
|
|
|
|
unsigned char *buf, size_t buflen)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Left-shift: X <<= count
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-05-02 15:02:32 +02:00
|
|
|
size_t i;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
i = mbedtls_mpi_bitlen(X) + count;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->n * biL < i) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2023-05-02 15:02:32 +02:00
|
|
|
mbedtls_mpi_core_shift_l(X->p, X->n, count);
|
2009-01-03 22:22:43 +01:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Right-shift: X >>= count
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
|
2022-09-21 15:36:16 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
if (X->n != 0) {
|
|
|
|
mbedtls_mpi_core_shift_r(X->p, X->n, count);
|
|
|
|
}
|
|
|
|
return 0;
|
2022-09-21 15:36:16 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Compare unsigned values
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t i, j;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(Y != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = X->n; i > 0; i--) {
|
|
|
|
if (X->p[i - 1] != 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (j = Y->n; j > 0; j--) {
|
|
|
|
if (Y->p[j - 1] != 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i == 0 && j == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i > j) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (j > i) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; i > 0; i--) {
|
|
|
|
if (X->p[i - 1] > Y->p[i - 1]) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (X->p[i - 1] < Y->p[i - 1]) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare signed values
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t i, j;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(Y != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = X->n; i > 0; i--) {
|
|
|
|
if (X->p[i - 1] != 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (j = Y->n; j > 0; j--) {
|
|
|
|
if (Y->p[j - 1] != 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i == 0 && j == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i > j) {
|
|
|
|
return X->s;
|
|
|
|
}
|
|
|
|
if (j > i) {
|
|
|
|
return -Y->s;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->s > 0 && Y->s < 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (Y->s > 0 && X->s < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; i > 0; i--) {
|
|
|
|
if (X->p[i - 1] > Y->p[i - 1]) {
|
|
|
|
return X->s;
|
|
|
|
}
|
|
|
|
if (X->p[i - 1] < Y->p[i - 1]) {
|
|
|
|
return -X->s;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare signed values
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi Y;
|
|
|
|
mbedtls_mpi_uint p[1];
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
*p = mpi_sint_abs(z);
|
|
|
|
Y.s = (z < 0) ? -1 : 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
Y.n = 1;
|
|
|
|
Y.p = p;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_mpi_cmp_mpi(X, &Y);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unsigned addition: X = |A| + |B| (HAC 14.7)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-08-24 15:05:26 +02:00
|
|
|
size_t j;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
|
|
|
MPI_VALIDATE_RET(B != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X == B) {
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_mpi *T = A; A = X; B = T;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X != A) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
|
|
|
|
}
|
2014-05-01 13:03:14 +02:00
|
|
|
|
2009-06-20 12:31:06 +02:00
|
|
|
/*
|
2022-08-24 15:05:26 +02:00
|
|
|
* X must always be positive as a result of unsigned additions.
|
2009-06-20 12:31:06 +02:00
|
|
|
*/
|
|
|
|
X->s = 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (j = B->n; j > 0; j--) {
|
|
|
|
if (B->p[j - 1] != 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-11-15 22:59:00 +01:00
|
|
|
/* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
|
|
|
|
* and B is 0 (of any size). */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (j == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-11-15 22:59:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-08-24 15:05:26 +02:00
|
|
|
/* j is the number of non-zero limbs of B. Add those to X. */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-08-24 15:05:26 +02:00
|
|
|
mbedtls_mpi_uint *p = X->p;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint c = mbedtls_mpi_core_add(p, p, B->p, j);
|
2022-08-24 15:05:26 +02:00
|
|
|
|
|
|
|
p += j;
|
|
|
|
|
|
|
|
/* Now propagate any carry */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (c != 0) {
|
|
|
|
if (j >= X->n) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
|
2022-08-24 15:05:26 +02:00
|
|
|
p = X->p + j;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
*p += c; c = (*p < c); j++; p++;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-06-08 22:50:35 +02:00
|
|
|
* Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t n;
|
2020-06-08 22:50:35 +02:00
|
|
|
mbedtls_mpi_uint carry;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
|
|
|
MPI_VALIDATE_RET(B != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (n = B->n; n > 0; n--) {
|
|
|
|
if (B->p[n - 1] != 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n > A->n) {
|
2021-01-27 22:30:43 +01:00
|
|
|
/* B >= (2^ciL)^n > A */
|
|
|
|
ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
|
2020-07-23 01:03:22 +02:00
|
|
|
|
|
|
|
/* Set the high limbs of X to match A. Don't touch the lower limbs
|
|
|
|
* because X might be aliased to B, and we must not overwrite the
|
|
|
|
* significant digits of B. */
|
2023-01-17 17:52:22 +01:00
|
|
|
if (A->n > n && A != X) {
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
|
|
|
|
}
|
|
|
|
if (X->n > A->n) {
|
|
|
|
memset(X->p + A->n, 0, (X->n - A->n) * ciL);
|
|
|
|
}
|
|
|
|
|
|
|
|
carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
|
|
|
|
if (carry != 0) {
|
2022-08-25 11:07:07 +02:00
|
|
|
/* Propagate the carry through the rest of X. */
|
2023-01-11 14:50:10 +01:00
|
|
|
carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n);
|
2022-08-25 11:07:07 +02:00
|
|
|
|
|
|
|
/* If we have further carry/borrow, the result is negative. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (carry != 0) {
|
2020-07-23 01:16:46 +02:00
|
|
|
ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2020-06-08 21:58:22 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-07-23 01:03:22 +02:00
|
|
|
/* X should always be positive as a result of unsigned subtractions. */
|
|
|
|
X->s = 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-07-23 01:03:22 +02:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 21:34:09 +01:00
|
|
|
/* Common function for signed addition and subtraction.
|
|
|
|
* Calculate A + B * flip_B where flip_B is 1 or -1.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int add_sub_mpi(mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *A, const mbedtls_mpi *B,
|
|
|
|
int flip_B)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2018-12-11 11:35:51 +01:00
|
|
|
int ret, s;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
|
|
|
MPI_VALIDATE_RET(B != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2018-12-11 11:35:51 +01:00
|
|
|
s = A->s;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (A->s * B->s * flip_B < 0) {
|
|
|
|
int cmp = mbedtls_mpi_cmp_abs(A, B);
|
|
|
|
if (cmp >= 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
|
2022-11-09 22:02:16 +01:00
|
|
|
/* If |A| = |B|, the result is 0 and we must set the sign bit
|
|
|
|
* to +1 regardless of which of A or B was negative. Otherwise,
|
|
|
|
* since |A| > |B|, the sign is the sign of A. */
|
|
|
|
X->s = cmp == 0 ? 1 : s;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
|
2022-11-09 22:02:16 +01:00
|
|
|
/* Since |A| < |B|, the sign is the opposite of A. */
|
2009-01-03 22:22:43 +01:00
|
|
|
X->s = -s;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
|
2009-01-03 22:22:43 +01:00
|
|
|
X->s = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 21:34:09 +01:00
|
|
|
/*
|
|
|
|
* Signed addition: X = A + B
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
2022-11-09 21:34:09 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return add_sub_mpi(X, A, B, 1);
|
2022-11-09 21:34:09 +01:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2013-10-29 10:02:51 +01:00
|
|
|
* Signed subtraction: X = A - B
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return add_sub_mpi(X, A, B, -1);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Signed addition: X = A + b
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2021-07-05 10:10:52 +02:00
|
|
|
mbedtls_mpi B;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint p[1];
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
p[0] = mpi_sint_abs(b);
|
|
|
|
B.s = (b < 0) ? -1 : 1;
|
2021-07-05 10:10:52 +02:00
|
|
|
B.n = 1;
|
|
|
|
B.p = p;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_mpi_add_mpi(X, A, &B);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-10-29 10:02:51 +01:00
|
|
|
* Signed subtraction: X = A - b
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2021-07-05 10:10:52 +02:00
|
|
|
mbedtls_mpi B;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint p[1];
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
p[0] = mpi_sint_abs(b);
|
|
|
|
B.s = (b < 0) ? -1 : 1;
|
2021-07-05 10:10:52 +02:00
|
|
|
B.n = 1;
|
|
|
|
B.p = p;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_mpi_sub_mpi(X, A, &B);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Baseline multiplication: X = A * B (HAC 14.12)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-04-13 07:51:40 +02:00
|
|
|
size_t i, j;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi TA, TB;
|
2022-04-13 07:50:02 +02:00
|
|
|
int result_is_zero = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
|
|
|
MPI_VALIDATE_RET(B != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-08-24 17:40:55 +02:00
|
|
|
mbedtls_mpi_init(&TA);
|
|
|
|
mbedtls_mpi_init(&TB);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X == A) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
|
|
|
|
}
|
|
|
|
if (X == B) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = A->n; i > 0; i--) {
|
|
|
|
if (A->p[i - 1] != 0) {
|
2022-04-13 07:50:02 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == 0) {
|
2022-04-13 07:50:02 +02:00
|
|
|
result_is_zero = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-04-13 07:50:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (j = B->n; j > 0; j--) {
|
|
|
|
if (B->p[j - 1] != 0) {
|
2022-04-13 07:50:02 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j == 0) {
|
2022-04-13 07:50:02 +02:00
|
|
|
result_is_zero = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-04-13 07:50:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-08-24 17:40:55 +02:00
|
|
|
mbedtls_mpi_core_mul(X->p, A->p, i, B->p, j);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-04-13 07:50:02 +02:00
|
|
|
/* If the result is 0, we don't shortcut the operation, which reduces
|
|
|
|
* but does not eliminate side channels leaking the zero-ness. We do
|
|
|
|
* need to take care to set the sign bit properly since the library does
|
|
|
|
* not fully support an MPI object with a value of 0 and s == -1. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (result_is_zero) {
|
2022-04-13 07:50:02 +02:00
|
|
|
X->s = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-04-13 07:50:02 +02:00
|
|
|
X->s = A->s * B->s;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Baseline multiplication: X = A * b
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-04-14 12:52:11 +02:00
|
|
|
size_t n = A->n;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (n > 0 && A->p[n - 1] == 0) {
|
2022-04-14 12:52:11 +02:00
|
|
|
--n;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-04-14 12:52:11 +02:00
|
|
|
|
2022-04-06 07:27:00 +02:00
|
|
|
/* The general method below doesn't work if b==0. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (b == 0 || n == 0) {
|
|
|
|
return mbedtls_mpi_lset(X, 0);
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-04-11 07:36:29 +02:00
|
|
|
/* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
|
2020-07-23 21:58:50 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-07-24 00:09:04 +02:00
|
|
|
/* In general, A * b requires 1 limb more than b. If
|
|
|
|
* A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
|
|
|
|
* number of limbs as A and the call to grow() is not required since
|
2021-03-10 23:44:10 +01:00
|
|
|
* copy() will take care of the growth if needed. However, experimentally,
|
|
|
|
* making the call to grow() unconditional causes slightly fewer
|
2020-07-24 00:09:04 +02:00
|
|
|
* calls to calloc() in ECP code, presumably because it reuses the
|
|
|
|
* same mpi for a while and this way the mpi is more likely to directly
|
2022-04-12 11:51:54 +02:00
|
|
|
* grow to its final size.
|
|
|
|
*
|
|
|
|
* Note that calculating A*b as 0 + A*b doesn't work as-is because
|
|
|
|
* A,X can be the same. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
|
|
|
|
mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-07-23 21:58:50 +02:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-11-26 20:35:03 +01:00
|
|
|
/*
|
2015-12-28 00:01:55 +01:00
|
|
|
* Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
|
|
|
|
* mbedtls_mpi_uint divisor, d
|
2015-11-26 20:35:03 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
|
|
|
|
mbedtls_mpi_uint u0,
|
|
|
|
mbedtls_mpi_uint d,
|
|
|
|
mbedtls_mpi_uint *r)
|
2015-11-26 20:35:03 +01:00
|
|
|
{
|
2015-12-01 10:27:00 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_UDBL)
|
|
|
|
mbedtls_t_udbl dividend, quotient;
|
2015-12-28 00:01:55 +01:00
|
|
|
#else
|
2016-01-03 01:24:34 +01:00
|
|
|
const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
|
2015-12-28 00:01:55 +01:00
|
|
|
mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
|
|
|
|
mbedtls_mpi_uint u0_msw, u0_lsw;
|
2016-01-03 01:24:34 +01:00
|
|
|
size_t s;
|
2015-12-01 10:27:00 +01:00
|
|
|
#endif
|
|
|
|
|
2015-11-26 20:35:03 +01:00
|
|
|
/*
|
|
|
|
* Check for overflow
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (0 == d || u1 >= d) {
|
|
|
|
if (r != NULL) {
|
|
|
|
*r = ~(mbedtls_mpi_uint) 0u;
|
|
|
|
}
|
2015-11-26 20:35:03 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ~(mbedtls_mpi_uint) 0u;
|
2015-11-26 20:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_UDBL)
|
|
|
|
dividend = (mbedtls_t_udbl) u1 << biL;
|
|
|
|
dividend |= (mbedtls_t_udbl) u0;
|
|
|
|
quotient = dividend / d;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
|
|
|
|
quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
|
|
|
|
}
|
2015-11-26 20:35:03 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (r != NULL) {
|
|
|
|
*r = (mbedtls_mpi_uint) (dividend - (quotient * d));
|
|
|
|
}
|
2015-11-26 20:35:03 +01:00
|
|
|
|
|
|
|
return (mbedtls_mpi_uint) quotient;
|
|
|
|
#else
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Algorithm D, Section 4.3.1 - The Art of Computer Programming
|
|
|
|
* Vol. 2 - Seminumerical Algorithms, Knuth
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normalize the divisor, d, and dividend, u0, u1
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
s = mbedtls_mpi_core_clz(d);
|
2015-11-26 20:35:03 +01:00
|
|
|
d = d << s;
|
|
|
|
|
|
|
|
u1 = u1 << s;
|
2023-01-11 14:50:10 +01:00
|
|
|
u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
|
2015-11-26 20:35:03 +01:00
|
|
|
u0 = u0 << s;
|
|
|
|
|
|
|
|
d1 = d >> biH;
|
2016-01-03 01:24:34 +01:00
|
|
|
d0 = d & uint_halfword_mask;
|
2015-11-26 20:35:03 +01:00
|
|
|
|
|
|
|
u0_msw = u0 >> biH;
|
2016-01-03 01:24:34 +01:00
|
|
|
u0_lsw = u0 & uint_halfword_mask;
|
2015-11-26 20:35:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the first quotient and remainder
|
|
|
|
*/
|
|
|
|
q1 = u1 / d1;
|
|
|
|
r0 = u1 - d1 * q1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
|
2015-11-26 20:35:03 +01:00
|
|
|
q1 -= 1;
|
|
|
|
r0 += d1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (r0 >= radix) {
|
|
|
|
break;
|
|
|
|
}
|
2015-11-26 20:35:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
rAX = (u1 * radix) + (u0_msw - q1 * d);
|
2015-11-26 20:35:03 +01:00
|
|
|
q0 = rAX / d1;
|
|
|
|
r0 = rAX - q0 * d1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
|
2015-11-26 20:35:03 +01:00
|
|
|
q0 -= 1;
|
|
|
|
r0 += d1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (r0 >= radix) {
|
|
|
|
break;
|
|
|
|
}
|
2015-11-26 20:35:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (r != NULL) {
|
|
|
|
*r = (rAX * radix + u0_lsw - q0 * d) >> s;
|
|
|
|
}
|
2015-11-26 20:35:03 +01:00
|
|
|
|
|
|
|
quotient = q1 * radix + q0;
|
|
|
|
|
|
|
|
return quotient;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
|
|
|
|
const mbedtls_mpi *B)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t i, n, t, k;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, T1, T2;
|
2019-11-01 16:20:42 +01:00
|
|
|
mbedtls_mpi_uint TP2[3];
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
|
|
|
MPI_VALIDATE_RET(B != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(B, 0) == 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
|
|
|
|
mbedtls_mpi_init(&T1);
|
2019-11-01 16:20:42 +01:00
|
|
|
/*
|
|
|
|
* Avoid dynamic memory allocations for constant-size T2.
|
|
|
|
*
|
|
|
|
* T2 is used for comparison only and the 3 limbs are assigned explicitly,
|
|
|
|
* so nobody increase the size of the MPI and we're safe to use an on-stack
|
|
|
|
* buffer.
|
|
|
|
*/
|
2019-10-31 12:46:45 +01:00
|
|
|
T2.s = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
T2.n = sizeof(TP2) / sizeof(*TP2);
|
2019-11-01 16:20:42 +01:00
|
|
|
T2.p = TP2;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_abs(A, B) < 0) {
|
|
|
|
if (Q != NULL) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
|
|
|
|
}
|
|
|
|
if (R != NULL) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
|
|
|
|
}
|
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
|
2009-01-03 22:22:43 +01:00
|
|
|
X.s = Y.s = 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
k = mbedtls_mpi_bitlen(&Y) % biL;
|
|
|
|
if (k < biL - 1) {
|
2009-01-03 22:22:43 +01:00
|
|
|
k = biL - 1 - k;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
|
|
|
|
} else {
|
|
|
|
k = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
n = X.n - 1;
|
|
|
|
t = Y.n - 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
Z.p[n - t]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = n; i > t; i--) {
|
|
|
|
if (X.p[i] >= Y.p[t]) {
|
|
|
|
Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
|
|
|
|
} else {
|
|
|
|
Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
|
|
|
|
Y.p[t], NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
|
|
|
|
T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
|
2019-10-31 12:46:45 +01:00
|
|
|
T2.p[2] = X.p[i];
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
Z.p[i - t - 1]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
2009-01-03 22:22:43 +01:00
|
|
|
Z.p[i - t - 1]--;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
|
|
|
|
T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
|
2009-01-03 22:22:43 +01:00
|
|
|
T1.p[1] = Y.p[t];
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
|
|
|
|
} while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
|
2009-01-03 22:22:43 +01:00
|
|
|
Z.p[i - t - 1]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (Q != NULL) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
|
2009-01-03 22:22:43 +01:00
|
|
|
Q->s = A->s * B->s;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (R != NULL) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
|
2012-11-13 11:25:21 +01:00
|
|
|
X.s = A->s;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(R, 0) == 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
R->s = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
|
|
|
|
mbedtls_mpi_free(&T1);
|
|
|
|
mbedtls_platform_zeroize(TP2, sizeof(TP2));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Division by int: A = Q * b + R
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
|
|
|
|
const mbedtls_mpi *A,
|
|
|
|
mbedtls_mpi_sint b)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2021-07-05 10:10:52 +02:00
|
|
|
mbedtls_mpi B;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint p[1];
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
p[0] = mpi_sint_abs(b);
|
|
|
|
B.s = (b < 0) ? -1 : 1;
|
2021-07-05 10:10:52 +02:00
|
|
|
B.n = 1;
|
|
|
|
B.p = p;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_mpi_div_mpi(Q, R, A, &B);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modulo: R = A mod B
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(R != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
|
|
|
MPI_VALIDATE_RET(B != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(B, 0) < 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
|
|
|
|
}
|
2009-06-23 21:46:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (mbedtls_mpi_cmp_int(R, 0) < 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modulo: r = A mod b
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t i;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint x, y, z;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(r != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (b == 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (b < 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* handle trivial cases
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (b == 1 || A->n == 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
*r = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (b == 2) {
|
2009-01-03 22:22:43 +01:00
|
|
|
*r = A->p[0] & 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* general case
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = A->n, y = 0; i > 0; i--) {
|
2011-04-24 10:57:21 +02:00
|
|
|
x = A->p[i - 1];
|
2023-01-11 14:50:10 +01:00
|
|
|
y = (y << biH) | (x >> biH);
|
2009-01-03 22:22:43 +01:00
|
|
|
z = y / b;
|
|
|
|
y -= z * b;
|
|
|
|
|
|
|
|
x <<= biH;
|
2023-01-11 14:50:10 +01:00
|
|
|
y = (y << biH) | (x >> biH);
|
2009-01-03 22:22:43 +01:00
|
|
|
z = y / b;
|
|
|
|
y -= z * b;
|
|
|
|
}
|
|
|
|
|
2009-06-23 21:46:08 +02:00
|
|
|
/*
|
|
|
|
* If A is negative, then the current y represents a negative value.
|
|
|
|
* Flipping it to the positive side.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (A->s < 0 && y != 0) {
|
2009-06-23 21:46:08 +02:00
|
|
|
y = b - y;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-06-23 21:46:08 +02:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
*r = y;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
*mm = mbedtls_mpi_core_montmul_init(N->p);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2022-08-05 17:59:43 +02:00
|
|
|
/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
|
|
|
|
*
|
|
|
|
* \param[in,out] A One of the numbers to multiply.
|
|
|
|
* It must have at least as many limbs as N
|
|
|
|
* (A->n >= N->n), and any limbs beyond n are ignored.
|
|
|
|
* On successful completion, A contains the result of
|
|
|
|
* the multiplication A * B * R^-1 mod N where
|
|
|
|
* R = (2^ciL)^n.
|
|
|
|
* \param[in] B One of the numbers to multiply.
|
|
|
|
* It must be nonzero and must not have more limbs than N
|
|
|
|
* (B->n <= N->n).
|
2022-08-17 07:42:44 +02:00
|
|
|
* \param[in] N The modulus. \p N must be odd.
|
2022-08-05 17:59:43 +02:00
|
|
|
* \param mm The value calculated by `mpi_montg_init(&mm, N)`.
|
|
|
|
* This is -N^-1 mod 2^ciL.
|
|
|
|
* \param[in,out] T A bignum for temporary storage.
|
|
|
|
* It must be at least twice the limb size of N plus 1
|
|
|
|
* (T->n >= 2 * N->n + 1).
|
|
|
|
* Its initial content is unused and
|
|
|
|
* its final content is indeterminate.
|
2022-08-30 15:31:49 +02:00
|
|
|
* It does not get reallocated.
|
2022-08-05 17:59:43 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
|
|
|
|
const mbedtls_mpi *N, mbedtls_mpi_uint mm,
|
|
|
|
mbedtls_mpi *T)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(A->p, A->p, B->p, B->n, N->p, N->n, mm, T->p);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Montgomery reduction: A = A * R^-1 mod N
|
2020-06-04 15:00:49 +02:00
|
|
|
*
|
2022-08-05 17:59:43 +02:00
|
|
|
* See mpi_montmul() regarding constraints and guarantees on the parameters.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
|
|
|
|
mbedtls_mpi_uint mm, mbedtls_mpi *T)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint z = 1;
|
|
|
|
mbedtls_mpi U;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-02-27 14:56:33 +01:00
|
|
|
U.n = U.s = (int) z;
|
2009-01-03 22:22:43 +01:00
|
|
|
U.p = &z;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mpi_montmul(A, &U, N, mm, T);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 11:22:20 +01:00
|
|
|
/**
|
|
|
|
* Select an MPI from a table without leaking the index.
|
|
|
|
*
|
|
|
|
* This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
|
|
|
|
* reads the entire table in order to avoid leaking the value of idx to an
|
|
|
|
* attacker able to observe memory access patterns.
|
|
|
|
*
|
|
|
|
* \param[out] R Where to write the selected MPI.
|
|
|
|
* \param[in] T The table to read from.
|
|
|
|
* \param[in] T_size The number of elements in the table.
|
|
|
|
* \param[in] idx The index of the element to select;
|
|
|
|
* this must satisfy 0 <= idx < T_size.
|
|
|
|
*
|
|
|
|
* \return \c 0 on success, or a negative error code.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mpi_select(mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx)
|
2021-03-09 11:22:20 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < T_size; i++) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
|
|
|
|
(unsigned char) mbedtls_ct_size_bool_eq(i,
|
|
|
|
idx)));
|
2021-06-03 10:42:46 +02:00
|
|
|
}
|
2021-03-09 11:22:20 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-03-09 11:22:20 +01:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
|
|
|
|
const mbedtls_mpi *E, const mbedtls_mpi *N,
|
|
|
|
mbedtls_mpi *prec_RR)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-11-21 16:54:20 +01:00
|
|
|
size_t window_bitsize;
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t i, j, nblimbs;
|
|
|
|
size_t bufsize, nbits;
|
2023-02-13 16:35:35 +01:00
|
|
|
size_t exponent_bits_in_window = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint ei, mm, state;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
|
2012-05-16 10:02:29 +02:00
|
|
|
int neg;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
|
|
|
MPI_VALIDATE_RET(E != NULL);
|
|
|
|
MPI_VALIDATE_RET(N != NULL);
|
2018-12-11 11:35:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(E, 0) < 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2012-05-16 10:02:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
|
|
|
|
mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2020-11-25 16:12:39 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Init temps and window size
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mpi_montg_init(&mm, N);
|
|
|
|
mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
|
|
|
|
mbedtls_mpi_init(&Apos);
|
|
|
|
mbedtls_mpi_init(&WW);
|
|
|
|
memset(W, 0, sizeof(W));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
i = mbedtls_mpi_bitlen(E);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
|
|
|
|
(i > 79) ? 4 : (i > 23) ? 3 : 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
|
|
|
|
if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
|
2022-11-21 15:48:02 +01:00
|
|
|
window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-12-11 21:01:44 +01:00
|
|
|
#endif
|
2011-11-25 12:52:11 +01:00
|
|
|
|
2022-11-22 11:47:10 +01:00
|
|
|
const size_t w_table_used_size = (size_t) 1 << window_bitsize;
|
2022-11-22 11:18:06 +01:00
|
|
|
|
2022-10-04 14:27:40 +02:00
|
|
|
/*
|
2022-11-21 17:14:54 +01:00
|
|
|
* This function is not constant-trace: its memory accesses depend on the
|
|
|
|
* exponent value. To defend against timing attacks, callers (such as RSA
|
|
|
|
* and DHM) should use exponent blinding. However this is not enough if the
|
|
|
|
* adversary can find the exponent in a single trace, so this function
|
|
|
|
* takes extra precautions against adversaries who can observe memory
|
|
|
|
* access patterns.
|
2022-11-11 16:56:38 +01:00
|
|
|
*
|
2022-11-21 17:14:54 +01:00
|
|
|
* This function performs a series of multiplications by table elements and
|
|
|
|
* squarings, and we want the prevent the adversary from finding out which
|
|
|
|
* table element was used, and from distinguishing between multiplications
|
|
|
|
* and squarings. Firstly, when multiplying by an element of the window
|
|
|
|
* W[i], we do a constant-trace table lookup to obfuscate i. This leaves
|
|
|
|
* squarings as having a different memory access patterns from other
|
|
|
|
* multiplications. So secondly, we put the accumulator X in the table as
|
|
|
|
* well, and also do a constant-trace table lookup to multiply by X.
|
|
|
|
*
|
|
|
|
* This way, all multiplications take the form of a lookup-and-multiply.
|
|
|
|
* The number of lookup-and-multiply operations inside each iteration of
|
|
|
|
* the main loop still depends on the bits of the exponent, but since the
|
|
|
|
* other operations in the loop don't have an easily recognizable memory
|
|
|
|
* trace, an adversary is unlikely to be able to observe the exact
|
|
|
|
* patterns.
|
|
|
|
*
|
|
|
|
* An adversary may still be able to recover the exponent if they can
|
|
|
|
* observe both memory accesses and branches. However, branch prediction
|
|
|
|
* exploitation typically requires many traces of execution over the same
|
|
|
|
* data, which is defeated by randomized blinding.
|
2022-11-21 15:31:22 +01:00
|
|
|
*
|
|
|
|
* To achieve this, we make a copy of X and we use the table entry in each
|
|
|
|
* calculation from this point on.
|
2022-10-04 14:27:40 +02:00
|
|
|
*/
|
2022-11-22 11:47:10 +01:00
|
|
|
const size_t x_index = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&W[x_index]);
|
|
|
|
mbedtls_mpi_copy(&W[x_index], X);
|
2022-11-21 15:31:22 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
j = N->n + 1;
|
2022-08-05 17:59:43 +02:00
|
|
|
/* All W[i] and X must have at least N->n limbs for the mpi_montmul()
|
2021-06-08 23:17:42 +02:00
|
|
|
* and mpi_montred() calls later. Here we ensure that W[1] and X are
|
|
|
|
* large enough, and later we'll grow other W[i] to the same length.
|
|
|
|
* They must not be shrunk midway through this function!
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], j));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2012-05-19 10:40:49 +02:00
|
|
|
/*
|
|
|
|
* Compensate for negative A (and correct at the end)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
neg = (A->s == -1);
|
|
|
|
if (neg) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
|
2012-05-19 10:40:49 +02:00
|
|
|
Apos.s = 1;
|
|
|
|
A = &Apos;
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* If 1st call, pre-compute R^2 mod N
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (prec_RR == NULL || prec_RR->p == NULL) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&RR, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&RR, N->n * 2 * biL));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&RR, &RR, N));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (prec_RR != NULL) {
|
|
|
|
memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* W[1] = A * R^2 * R^-1 mod N = A * R mod N
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
|
2021-06-08 23:17:42 +02:00
|
|
|
/* This should be a no-op because W[1] is already that large before
|
|
|
|
* mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
|
2022-08-05 17:59:43 +02:00
|
|
|
* in mpi_montmul() below, so let's make sure. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
|
|
|
|
} else {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
|
2021-06-08 23:17:42 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2021-06-08 23:17:42 +02:00
|
|
|
/* Note that this is safe because W[1] always has at least N->n limbs
|
|
|
|
* (it grew above and was preserved by mbedtls_mpi_copy()). */
|
2023-01-11 14:50:10 +01:00
|
|
|
mpi_montmul(&W[1], &RR, N, mm, &T);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
2022-10-04 14:27:40 +02:00
|
|
|
* W[x_index] = R^2 * R^-1 mod N = R mod N
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
|
|
|
|
mpi_montred(&W[x_index], N, mm, &T);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (window_bitsize > 1) {
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2022-11-21 16:54:20 +01:00
|
|
|
* W[i] = W[1] ^ i
|
|
|
|
*
|
|
|
|
* The first bit of the sliding window is always 1 and therefore we
|
|
|
|
* only need to store the second half of the table.
|
2022-11-22 11:47:10 +01:00
|
|
|
*
|
|
|
|
* (There are two special elements in the table: W[0] for the
|
|
|
|
* accumulator/result and W[1] for A in Montgomery form. Both of these
|
|
|
|
* are already set at this point.)
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2022-11-21 16:54:20 +01:00
|
|
|
j = w_table_used_size / 2;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < window_bitsize - 1; i++) {
|
|
|
|
mpi_montmul(&W[j], &W[j], N, mm, &T);
|
|
|
|
}
|
2013-10-29 16:18:35 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* W[i] = W[i - 1] * W[1]
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = j + 1; i < w_table_used_size; i++) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mpi_montmul(&W[i], &W[1], N, mm, &T);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nblimbs = E->n;
|
|
|
|
bufsize = 0;
|
|
|
|
nbits = 0;
|
|
|
|
state = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (1) {
|
|
|
|
if (bufsize == 0) {
|
|
|
|
if (nblimbs == 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-10-29 16:18:35 +01:00
|
|
|
nblimbs--;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
bufsize = sizeof(mbedtls_mpi_uint) << 3;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bufsize--;
|
|
|
|
|
|
|
|
ei = (E->p[nblimbs] >> bufsize) & 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* skip leading 0s
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ei == 0 && state == 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ei == 0 && state == 1) {
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2022-10-04 14:27:40 +02:00
|
|
|
* out of window, square W[x_index]
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
|
|
|
|
mpi_montmul(&W[x_index], &WW, N, mm, &T);
|
2009-01-03 22:22:43 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add ei to current window
|
|
|
|
*/
|
|
|
|
state = 2;
|
|
|
|
|
|
|
|
nbits++;
|
2023-01-11 14:50:10 +01:00
|
|
|
exponent_bits_in_window |= (ei << (window_bitsize - nbits));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (nbits == window_bitsize) {
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2022-11-21 15:48:02 +01:00
|
|
|
* W[x_index] = W[x_index]^window_bitsize R^-1 mod N
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < window_bitsize; i++) {
|
|
|
|
MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
|
|
|
|
x_index));
|
|
|
|
mpi_montmul(&W[x_index], &WW, N, mm, &T);
|
2022-10-04 15:00:09 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
2022-11-21 15:48:02 +01:00
|
|
|
* W[x_index] = W[x_index] * W[exponent_bits_in_window] R^-1 mod N
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
|
|
|
|
exponent_bits_in_window));
|
|
|
|
mpi_montmul(&W[x_index], &WW, N, mm, &T);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
state--;
|
|
|
|
nbits = 0;
|
2022-11-21 15:48:02 +01:00
|
|
|
exponent_bits_in_window = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process the remaining bits
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < nbits; i++) {
|
|
|
|
MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
|
|
|
|
mpi_montmul(&W[x_index], &WW, N, mm, &T);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-11-21 15:48:02 +01:00
|
|
|
exponent_bits_in_window <<= 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
|
|
|
|
mpi_montmul(&W[x_index], &WW, N, mm, &T);
|
2022-10-04 15:00:09 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-10-04 14:27:40 +02:00
|
|
|
* W[x_index] = A^E * R * R^-1 mod N = A^E mod N
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mpi_montred(&W[x_index], N, mm, &T);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
|
2022-10-04 14:27:40 +02:00
|
|
|
W[x_index].s = -1;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
|
2012-05-16 10:02:29 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 14:27:40 +02:00
|
|
|
/*
|
|
|
|
* Load the result in the output variable.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_copy(X, &W[x_index]);
|
2022-10-04 14:27:40 +02:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
cleanup:
|
|
|
|
|
2022-11-21 16:05:31 +01:00
|
|
|
/* The first bit of the sliding window is always 1 and therefore the first
|
|
|
|
* half of the table was unused. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = w_table_used_size/2; i < w_table_used_size; i++) {
|
|
|
|
mbedtls_mpi_free(&W[i]);
|
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&W[x_index]);
|
|
|
|
mbedtls_mpi_free(&W[1]);
|
|
|
|
mbedtls_mpi_free(&T);
|
|
|
|
mbedtls_mpi_free(&Apos);
|
|
|
|
mbedtls_mpi_free(&WW);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (prec_RR == NULL || prec_RR->p == NULL) {
|
|
|
|
mbedtls_mpi_free(&RR);
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Greatest common divisor: G = gcd(A, B) (HAC 14.54)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t lz, lzt;
|
2019-08-16 15:16:07 +02:00
|
|
|
mbedtls_mpi TA, TB;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(G != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
|
|
|
MPI_VALIDATE_RET(B != NULL);
|
2018-12-11 11:35:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
lz = mbedtls_mpi_lsb(&TA);
|
|
|
|
lzt = mbedtls_mpi_lsb(&TB);
|
2009-01-29 23:24:33 +01:00
|
|
|
|
2021-06-09 13:26:43 +02:00
|
|
|
/* The loop below gives the correct result when A==0 but not when B==0.
|
|
|
|
* So have a special case for B==0. Leverage the fact that we just
|
|
|
|
* calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
|
|
|
|
* slightly more efficient than cmp_int(). */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
|
|
|
|
ret = mbedtls_mpi_copy(G, A);
|
2021-06-09 13:26:43 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (lzt < lz) {
|
2009-01-29 23:24:33 +01:00
|
|
|
lz = lzt;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-29 23:24:33 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
TA.s = TB.s = 1;
|
|
|
|
|
2021-06-16 13:42:04 +02:00
|
|
|
/* We mostly follow the procedure described in HAC 14.54, but with some
|
|
|
|
* minor differences:
|
|
|
|
* - Sequences of multiplications or divisions by 2 are grouped into a
|
|
|
|
* single shift operation.
|
2021-06-21 18:58:39 +02:00
|
|
|
* - The procedure in HAC assumes that 0 < TB <= TA.
|
|
|
|
* - The condition TB <= TA is not actually necessary for correctness.
|
|
|
|
* TA and TB have symmetric roles except for the loop termination
|
|
|
|
* condition, and the shifts at the beginning of the loop body
|
|
|
|
* remove any significance from the ordering of TA vs TB before
|
|
|
|
* the shifts.
|
|
|
|
* - If TA = 0, the loop goes through 0 iterations and the result is
|
|
|
|
* correctly TB.
|
|
|
|
* - The case TB = 0 was short-circuited above.
|
2021-06-16 13:42:04 +02:00
|
|
|
*
|
|
|
|
* For the correctness proof below, decompose the original values of
|
|
|
|
* A and B as
|
|
|
|
* A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
|
|
|
|
* B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
|
|
|
|
* Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
|
|
|
|
* and gcd(A',B') is odd or 0.
|
|
|
|
*
|
|
|
|
* At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
|
|
|
|
* The code maintains the following invariant:
|
|
|
|
* gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
|
2021-06-15 22:09:39 +02:00
|
|
|
*/
|
|
|
|
|
2021-06-16 13:42:04 +02:00
|
|
|
/* Proof that the loop terminates:
|
|
|
|
* At each iteration, either the right-shift by 1 is made on a nonzero
|
|
|
|
* value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
|
|
|
|
* by at least 1, or the right-shift by 1 is made on zero and then
|
|
|
|
* TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
|
|
|
|
* since in that case TB is calculated from TB-TA with the condition TB>TA).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
|
2021-06-16 13:42:04 +02:00
|
|
|
/* Divisions by 2 preserve the invariant (I). */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2021-06-16 13:42:04 +02:00
|
|
|
/* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
|
|
|
|
* TA-TB is even so the division by 2 has an integer result.
|
|
|
|
* Invariant (I) is preserved since any odd divisor of both TA and TB
|
|
|
|
* also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
|
2021-12-21 06:14:10 +01:00
|
|
|
* also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
|
2021-06-16 13:42:04 +02:00
|
|
|
* divides TA.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
|
|
|
|
} else {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2021-06-16 13:42:04 +02:00
|
|
|
/* Note that one of TA or TB is still odd. */
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 13:42:04 +02:00
|
|
|
/* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
|
|
|
|
* At the loop exit, TA = 0, so gcd(TA,TB) = TB.
|
|
|
|
* - If there was at least one loop iteration, then one of TA or TB is odd,
|
|
|
|
* and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
|
|
|
|
* lz = min(a,b) so gcd(A,B) = 2^lz * TB.
|
|
|
|
* - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
|
2021-06-21 11:40:38 +02:00
|
|
|
* In this case, lz = 0 and B = TB so gcd(A,B) = B = 2^lz * TB as well.
|
2021-06-16 13:42:04 +02:00
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 16:11:39 +02:00
|
|
|
/*
|
|
|
|
* Fill X with size bytes of random.
|
2022-10-27 20:15:13 +02:00
|
|
|
* The bytes returned from the RNG are used in a specific order which
|
|
|
|
* is suitable for deterministic ECDSA (see the specification of
|
|
|
|
* mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
|
2014-04-30 16:11:39 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2011-03-26 14:18:49 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t limbs = CHARS_TO_LIMBS(size);
|
2017-10-18 15:21:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(f_rng != NULL);
|
2014-04-30 16:11:39 +02:00
|
|
|
|
2017-10-18 15:21:44 +02:00
|
|
|
/* Ensure that target MPI has exactly the necessary number of limbs */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
|
|
|
|
if (size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-26 14:18:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
|
2011-03-26 14:18:49 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2011-03-26 14:18:49 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_random(mbedtls_mpi *X,
|
|
|
|
mbedtls_mpi_sint min,
|
|
|
|
const mbedtls_mpi *N,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2021-03-29 22:02:55 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (min < 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
if (mbedtls_mpi_cmp_int(N, min) <= 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-03-29 22:14:51 +02:00
|
|
|
|
2021-04-01 15:57:18 +02:00
|
|
|
/* Ensure that target MPI has exactly the same number of limbs
|
|
|
|
* as the upper bound, even if the upper bound has leading zeros.
|
2022-12-15 15:04:33 +01:00
|
|
|
* This is necessary for mbedtls_mpi_core_random. */
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = mbedtls_mpi_resize_clear(X, N->n);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-03-29 22:02:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
|
2021-03-29 22:02:55 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(A != NULL);
|
|
|
|
MPI_VALIDATE_RET(N != NULL);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
|
|
|
|
mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
|
|
|
|
mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
2009-01-03 22:22:43 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
|
|
|
while ((TU.p[0] & 1) == 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while ((TV.p[0] & 1) == 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
|
|
|
|
} else {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
|
|
|
|
mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
|
|
|
|
mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_GENPRIME)
|
2012-11-02 12:02:58 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
static const int small_prime[] =
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
3, 5, 7, 11, 13, 17, 19, 23,
|
|
|
|
29, 31, 37, 41, 43, 47, 53, 59,
|
|
|
|
61, 67, 71, 73, 79, 83, 89, 97,
|
|
|
|
101, 103, 107, 109, 113, 127, 131, 137,
|
|
|
|
139, 149, 151, 157, 163, 167, 173, 179,
|
|
|
|
181, 191, 193, 197, 199, 211, 223, 227,
|
|
|
|
229, 233, 239, 241, 251, 257, 263, 269,
|
|
|
|
271, 277, 281, 283, 293, 307, 311, 313,
|
|
|
|
317, 331, 337, 347, 349, 353, 359, 367,
|
|
|
|
373, 379, 383, 389, 397, 401, 409, 419,
|
|
|
|
421, 431, 433, 439, 443, 449, 457, 461,
|
|
|
|
463, 467, 479, 487, 491, 499, 503, 509,
|
|
|
|
521, 523, 541, 547, 557, 563, 569, 571,
|
|
|
|
577, 587, 593, 599, 601, 607, 613, 617,
|
|
|
|
619, 631, 641, 643, 647, 653, 659, 661,
|
|
|
|
673, 677, 683, 691, 701, 709, 719, 727,
|
|
|
|
733, 739, 743, 751, 757, 761, 769, 773,
|
|
|
|
787, 797, 809, 811, 821, 823, 827, 829,
|
|
|
|
839, 853, 857, 859, 863, 877, 881, 883,
|
|
|
|
887, 907, 911, 919, 929, 937, 941, 947,
|
|
|
|
953, 967, 971, 977, 983, 991, 997, -103
|
2009-01-03 22:22:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2013-11-22 18:39:18 +01:00
|
|
|
* Small divisors test (X must be positive)
|
|
|
|
*
|
|
|
|
* Return values:
|
|
|
|
* 0: no small factor (possible prime, more tests needed)
|
|
|
|
* 1: certain prime
|
2015-04-08 12:49:31 +02:00
|
|
|
* MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
|
2013-11-22 18:39:18 +01:00
|
|
|
* other negative: error
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mpi_check_small_factors(const mbedtls_mpi *X)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-11-22 18:39:18 +01:00
|
|
|
int ret = 0;
|
|
|
|
size_t i;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint r;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((X->p[0] & 1) == 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; small_prime[i] > 0; i++) {
|
|
|
|
if (mbedtls_mpi_cmp_int(X, small_prime[i]) <= 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, small_prime[i]));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (r == 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2013-11-22 18:39:18 +01:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-11-22 18:39:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Miller-Rabin pseudo-primality test (HAC 4.24)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2013-11-22 18:39:18 +01:00
|
|
|
{
|
2015-03-11 16:49:45 +01:00
|
|
|
int ret, count;
|
2018-09-03 15:45:23 +02:00
|
|
|
size_t i, j, k, s;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi W, R, T, A, RR;
|
2013-11-22 18:39:18 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(f_rng != NULL);
|
2018-12-11 11:35:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
|
|
|
|
mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
|
|
|
|
mbedtls_mpi_init(&RR);
|
2013-11-22 18:39:18 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* W = |X| - 1
|
|
|
|
* R = W >> lsb( W )
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
|
|
|
|
s = mbedtls_mpi_lsb(&W);
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < rounds; i++) {
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* pick a random A, 1 < A < |X| - 1
|
|
|
|
*/
|
2015-03-11 16:49:45 +01:00
|
|
|
count = 0;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
|
2015-03-11 16:49:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
j = mbedtls_mpi_bitlen(&A);
|
|
|
|
k = mbedtls_mpi_bitlen(&W);
|
2015-03-11 16:49:45 +01:00
|
|
|
if (j > k) {
|
2023-01-11 14:50:10 +01:00
|
|
|
A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
|
2015-03-11 16:49:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count++ > 30) {
|
2019-01-17 13:30:57 +01:00
|
|
|
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
|
|
|
goto cleanup;
|
2015-03-11 16:49:45 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(&A, 1) <= 0);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A = A^R mod |X|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(&A, 1) == 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
j = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* A = A * A mod |X|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* not prime if A != |X| - 1 or A == 1
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(&A, 1) == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
|
|
|
|
mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
|
|
|
|
mbedtls_mpi_free(&RR);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2013-11-22 18:39:18 +01:00
|
|
|
/*
|
|
|
|
* Pseudo-primality test: small factors, then Miller-Rabin
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2013-11-22 18:39:18 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi XX;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(f_rng != NULL);
|
2014-10-14 20:56:02 +02:00
|
|
|
|
|
|
|
XX.s = 1;
|
|
|
|
XX.n = X->n;
|
|
|
|
XX.p = X->p;
|
2013-11-22 18:39:18 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(&XX, 1) == 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
|
|
|
}
|
2013-11-22 18:39:18 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-22 18:39:18 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mpi_check_small_factors(&XX)) != 0) {
|
|
|
|
if (ret == 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-22 18:39:18 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-11-22 18:39:18 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
|
Bignum: Improve primality test for FIPS primes
The FIPS 186-4 RSA key generation prescribes lower failure probability
in primality testing and this makes key generation slower. We enable the
caller to decide between compliance/security and performance.
This python script calculates the base two logarithm of the formulas in
HAC Fact 4.48 and was used to determine the breakpoints and number of
rounds:
def mrpkt_log_2(k, t):
if t <= k/9.0:
return 3*math.log(k,2)/2+t-math.log(t,2)/2+4-2*math.sqrt(t*k)
elif t <= k/4.0:
c1 = math.log(7.0*k/20,2)-5*t
c2 = math.log(1/7.0,2)+15*math.log(k,2)/4.0-k/2.0-2*t
c3 = math.log(12*k,2)-k/4.0-3*t
return max(c1, c2, c3)
else:
return math.log(1/7.0)+15*math.log(k,2)/4.0-k/2.0-2*t
2018-08-14 14:34:01 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Prime number generation
|
2018-02-15 04:24:10 +01:00
|
|
|
*
|
Bignum: Improve primality test for FIPS primes
The FIPS 186-4 RSA key generation prescribes lower failure probability
in primality testing and this makes key generation slower. We enable the
caller to decide between compliance/security and performance.
This python script calculates the base two logarithm of the formulas in
HAC Fact 4.48 and was used to determine the breakpoints and number of
rounds:
def mrpkt_log_2(k, t):
if t <= k/9.0:
return 3*math.log(k,2)/2+t-math.log(t,2)/2+4-2*math.sqrt(t*k)
elif t <= k/4.0:
c1 = math.log(7.0*k/20,2)-5*t
c2 = math.log(1/7.0,2)+15*math.log(k,2)/4.0-k/2.0-2*t
c3 = math.log(12*k,2)-k/4.0-3*t
return max(c1, c2, c3)
else:
return math.log(1/7.0)+15*math.log(k,2)/4.0-k/2.0-2*t
2018-08-14 14:34:01 +02:00
|
|
|
* To generate an RSA key in a way recommended by FIPS 186-4, both primes must
|
|
|
|
* be either 1024 bits or 1536 bits long, and flags must contain
|
|
|
|
* MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2018-02-15 04:24:10 +01:00
|
|
|
#ifdef MBEDTLS_HAVE_INT64
|
|
|
|
// ceil(2^63.5)
|
|
|
|
#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
|
|
|
|
#else
|
|
|
|
// ceil(2^31.5)
|
|
|
|
#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
|
|
|
|
#endif
|
|
|
|
int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t k, n;
|
2018-09-03 15:45:23 +02:00
|
|
|
int rounds;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint r;
|
|
|
|
mbedtls_mpi Y;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(f_rng != NULL);
|
2018-12-11 11:35:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&Y);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
n = BITS_TO_LIMBS(nbits);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
|
2018-09-03 15:45:23 +02:00
|
|
|
/*
|
|
|
|
* 2^-80 error probability, number of rounds chosen per HAC, table 4.4
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
|
|
|
|
(nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
|
|
|
|
(nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
|
|
|
|
} else {
|
2018-09-03 15:45:23 +02:00
|
|
|
/*
|
|
|
|
* 2^-100 error probability, number of rounds computed based on HAC,
|
|
|
|
* fact 4.48
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
|
|
|
|
(nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
|
|
|
|
(nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
|
|
|
|
(nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
|
2018-09-03 15:45:23 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (1) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
|
2018-02-15 04:24:10 +01:00
|
|
|
/* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2018-02-15 04:24:10 +01:00
|
|
|
k = n * biL;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (k > nbits) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
|
|
|
|
}
|
2018-02-15 04:24:10 +01:00
|
|
|
X->p[0] |= 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
|
|
|
|
ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
|
2018-02-15 04:24:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
|
2009-01-03 22:22:43 +01:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-02-15 04:24:10 +01:00
|
|
|
/*
|
2022-07-28 06:50:56 +02:00
|
|
|
* A necessary condition for Y and X = 2Y + 1 to be prime
|
2018-02-15 04:24:10 +01:00
|
|
|
* is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
|
|
|
|
* Make sure it is satisfied, while keeping X = 3 mod 4
|
|
|
|
*/
|
2015-03-11 16:49:45 +01:00
|
|
|
|
2018-02-15 04:24:10 +01:00
|
|
|
X->p[0] |= 2;
|
2015-03-11 16:49:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
|
|
|
|
if (r == 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
|
|
|
|
} else if (r == 1) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
|
|
|
|
}
|
2013-11-22 17:54:59 +01:00
|
|
|
|
2018-02-15 04:24:10 +01:00
|
|
|
/* Set Y = (X-1) / 2, which is X / 2 because X is odd */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (1) {
|
2018-02-15 04:24:10 +01:00
|
|
|
/*
|
|
|
|
* First, check small factors for X and Y
|
|
|
|
* before doing Miller-Rabin on any of them
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mpi_check_small_factors(X)) == 0 &&
|
|
|
|
(ret = mpi_check_small_factors(&Y)) == 0 &&
|
|
|
|
(ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
|
|
|
|
== 0 &&
|
|
|
|
(ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
|
|
|
|
== 0) {
|
2018-02-15 04:24:10 +01:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-02-15 04:24:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
|
2018-02-15 04:24:10 +01:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-02-15 04:24:10 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Next candidates. We want to preserve Y = (X-1) / 2 and
|
|
|
|
* Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
|
|
|
|
* so up Y by 6 and X by 12.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&Y);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_GENPRIME */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
#define GCD_PAIR_COUNT 3
|
2009-01-29 23:24:33 +01:00
|
|
|
|
|
|
|
static const int gcd_pairs[GCD_PAIR_COUNT][3] =
|
|
|
|
{
|
|
|
|
{ 693, 609, 21 },
|
|
|
|
{ 1764, 868, 28 },
|
|
|
|
{ 768454923, 542167814, 1 }
|
|
|
|
};
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_self_test(int verbose)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2009-01-29 23:24:33 +01:00
|
|
|
int ret, i;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi A, E, N, X, Y, U, V;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
|
|
|
|
mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
|
|
|
|
"EFE021C2645FD1DC586E69184AF4A31E" \
|
|
|
|
"D5F53E93B5F123FA41680867BA110131" \
|
|
|
|
"944FE7952E2517337780CB0DB80E61AA" \
|
|
|
|
"E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
|
|
|
|
"B2E7EFD37075B9F03FF989C7C5051C20" \
|
|
|
|
"34D2A323810251127E7BF8625A4F49A5" \
|
|
|
|
"F3E27F4DA8BD59C47D6DAABA4C8127BD" \
|
|
|
|
"5B5C25763222FEFCCFC38B832366C29E"));
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
|
|
|
|
"0066A198186C18C10B2F5ED9B522752A" \
|
|
|
|
"9830B69916E535C8F047518A889A43A5" \
|
|
|
|
"94B6BED27A168D31D4A52F88925AA8F5"));
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
|
|
|
|
"602AB7ECA597A3D6B56FF9829A5E8B85" \
|
|
|
|
"9E857EA95A03512E2BAE7391688D264A" \
|
|
|
|
"A5663B0341DB9CCFD2C4C5F421FEC814" \
|
|
|
|
"8001B72E848A38CAE1C65F78E56ABDEF" \
|
|
|
|
"E12D3C039B8A02D6BE593F0BBBDA56F1" \
|
|
|
|
"ECF677152EF804370C1A305CAF3B5BF1" \
|
|
|
|
"30879B56C61DE584A0F53A2447A51E"));
|
|
|
|
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" MPI test #1 (mul_mpi): ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("failed\n");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-01-20 10:03:15 +01:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
|
|
|
|
"256567336059E52CAE22925474705F39A94"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
|
|
|
|
"6613F26162223DF488E9CD48CC132C7A" \
|
|
|
|
"0AC93C701B001B092E4E5B9F73BCD27B" \
|
|
|
|
"9EE50D0657C77F374E903CDFA4C642"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" MPI test #2 (div_mpi): ");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("failed\n");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-01-20 10:03:15 +01:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
|
|
|
|
"36E139AEA55215609D2816998ED020BB" \
|
|
|
|
"BD96C37890F65171D948E9BC7CBAA4D9" \
|
|
|
|
"325D24D6A3C12710F10A09FA08AB87"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" MPI test #3 (exp_mod): ");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("failed\n");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-01-20 10:03:15 +01:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
|
|
|
|
"003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
|
|
|
|
"C3DBA76456363A10869622EAC2DD84EC" \
|
|
|
|
"C5B8A74DAC4D09E03B5E0BE779F2DF61"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" MPI test #4 (inv_mod): ");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("failed\n");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-01-20 10:03:15 +01:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" MPI test #5 (simple gcd): ");
|
|
|
|
}
|
2009-01-29 23:24:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < GCD_PAIR_COUNT; i++) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
|
2009-01-29 23:24:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
|
2009-01-29 23:24:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("failed at %d\n", i);
|
|
|
|
}
|
2009-01-29 23:24:33 +01:00
|
|
|
|
2014-01-20 10:03:15 +01:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2009-01-29 23:24:33 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n");
|
|
|
|
}
|
2009-01-29 23:24:33 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0 && verbose != 0) {
|
|
|
|
mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
|
|
|
|
mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("\n");
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|