2018-02-08 18:04:43 +00:00
|
|
|
/*
|
|
|
|
* Helper functions for the RSA module
|
|
|
|
*
|
2020-09-05 10:53:20 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2024-01-30 13:09:13 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2018-02-08 18:04:43 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-12-21 11:54:05 +00:00
|
|
|
#include "common.h"
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
|
|
|
|
#include "mbedtls/rsa.h"
|
|
|
|
#include "mbedtls/bignum.h"
|
2023-09-25 03:04:06 +00:00
|
|
|
#include "rsa_alt_helpers.h"
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute RSA prime factors from public and private exponents
|
|
|
|
*
|
|
|
|
* Summary of algorithm:
|
|
|
|
* Setting F := lcm(P-1,Q-1), the idea is as follows:
|
|
|
|
*
|
|
|
|
* (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
|
|
|
|
* is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
|
|
|
|
* square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
|
|
|
|
* possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
|
|
|
|
* or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
|
|
|
|
* factors of N.
|
|
|
|
*
|
|
|
|
* (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
|
|
|
|
* construction still applies since (-)^K is the identity on the set of
|
|
|
|
* roots of 1 in Z/NZ.
|
|
|
|
*
|
|
|
|
* The public and private key primitives (-)^E and (-)^D are mutually inverse
|
|
|
|
* bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
|
|
|
|
* if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
|
|
|
|
* Splitting L = 2^t * K with K odd, we have
|
|
|
|
*
|
|
|
|
* DE - 1 = FL = (F/2) * (2^(t+1)) * K,
|
|
|
|
*
|
|
|
|
* so (F / 2) * K is among the numbers
|
|
|
|
*
|
|
|
|
* (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
|
|
|
|
*
|
|
|
|
* where ord is the order of 2 in (DE - 1).
|
|
|
|
* We can therefore iterate through these numbers apply the construction
|
|
|
|
* of (a) and (b) above to attempt to factor N.
|
|
|
|
*
|
|
|
|
*/
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
|
|
|
|
mbedtls_mpi const *E, mbedtls_mpi const *D,
|
|
|
|
mbedtls_mpi *P, mbedtls_mpi *Q)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
uint16_t attempt; /* Number of current attempt */
|
|
|
|
uint16_t iter; /* Number of squares computed in the current attempt */
|
|
|
|
|
|
|
|
uint16_t order; /* Order of 2 in DE - 1 */
|
|
|
|
|
|
|
|
mbedtls_mpi T; /* Holds largest odd divisor of DE - 1 */
|
|
|
|
mbedtls_mpi K; /* Temporary holding the current candidate */
|
|
|
|
|
|
|
|
const unsigned char primes[] = { 2,
|
2023-04-18 08:38:24 +00: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 };
|
|
|
|
|
|
|
|
const size_t num_primes = sizeof(primes) / sizeof(*primes);
|
|
|
|
|
|
|
|
if (P == NULL || Q == NULL || P->p != NULL || Q->p != NULL) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbedtls_mpi_cmp_int(N, 0) <= 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(D, 1) <= 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(E, 1) <= 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi(E, N) >= 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initializations and temporary changes
|
|
|
|
*/
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mpi_init(&K);
|
|
|
|
mbedtls_mpi_init(&T);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/* T := DE - 1 */
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, D, E));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&T, &T, 1));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if ((order = (uint16_t) mbedtls_mpi_lsb(&T)) == 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* After this operation, T holds the largest odd divisor of DE - 1. */
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&T, order));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Actual work
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Skip trying 2 if N == 1 mod 8 */
|
|
|
|
attempt = 0;
|
2023-04-18 08:38:24 +00:00
|
|
|
if (N->p[0] % 8 == 1) {
|
2018-02-08 18:04:43 +00:00
|
|
|
attempt = 1;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
for (; attempt < num_primes; ++attempt) {
|
2023-10-21 12:06:23 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&K, primes[attempt]));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/* Check if gcd(K,N) = 1 */
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P, &K, N));
|
|
|
|
if (mbedtls_mpi_cmp_int(P, 1) != 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
continue;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
|
|
|
|
* and check whether they have nontrivial GCD with N. */
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&K, &K, &T, N,
|
|
|
|
Q /* temporarily use Q for storing Montgomery
|
|
|
|
* multiplication helper values */));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
for (iter = 1; iter <= order; ++iter) {
|
2018-02-08 18:04:43 +00:00
|
|
|
/* If we reach 1 prematurely, there's no point
|
|
|
|
* in continuing to square K */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (mbedtls_mpi_cmp_int(&K, 1) == 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
break;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&K, &K, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P, &K, N));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (mbedtls_mpi_cmp_int(P, 1) == 1 &&
|
|
|
|
mbedtls_mpi_cmp_mpi(P, N) == -1) {
|
2018-02-08 18:04:43 +00:00
|
|
|
/*
|
|
|
|
* Have found a nontrivial divisor P of N.
|
|
|
|
* Set Q := N / P.
|
|
|
|
*/
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(Q, NULL, N, P));
|
2018-02-08 18:04:43 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &K));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, N));
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we get here, then either we prematurely aborted the loop because
|
|
|
|
* we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must
|
|
|
|
* be 1 if D,E,N were consistent.
|
|
|
|
* Check if that's the case and abort if not, to avoid very long,
|
|
|
|
* yet eventually failing, computations if N,D,E were not sane.
|
|
|
|
*/
|
2023-04-18 08:38:24 +00:00
|
|
|
if (mbedtls_mpi_cmp_int(&K, 1) != 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mpi_free(&K);
|
|
|
|
mbedtls_mpi_free(&T);
|
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given P, Q and the public exponent E, deduce D.
|
|
|
|
* This is essentially a modular inversion.
|
|
|
|
*/
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
|
|
|
|
mbedtls_mpi const *Q,
|
|
|
|
mbedtls_mpi const *E,
|
|
|
|
mbedtls_mpi *D)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
mbedtls_mpi K, L;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (D == NULL || mbedtls_mpi_cmp_int(D, 0) != 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(Q, 1) <= 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(E, 0) == 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mpi_init(&K);
|
|
|
|
mbedtls_mpi_init(&L);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/* Temporarily put K := P-1 and L := Q-1 */
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/* Temporarily put D := gcd(P-1, Q-1) */
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(D, &K, &L));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/* K := LCM(P-1, Q-1) */
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &L));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&K, NULL, &K, D));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/* Compute modular inverse of E in LCM(P-1, Q-1) */
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(D, E, &K));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mpi_free(&K);
|
|
|
|
mbedtls_mpi_free(&L);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 03:04:06 +00:00
|
|
|
int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
|
|
|
|
const mbedtls_mpi *D, mbedtls_mpi *DP,
|
|
|
|
mbedtls_mpi *DQ, mbedtls_mpi *QP)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2023-09-25 03:04:06 +00:00
|
|
|
mbedtls_mpi K;
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mpi_init(&K);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-09-25 03:04:06 +00:00
|
|
|
/* DP = D mod P-1 */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (DP != NULL) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
|
2023-09-25 03:04:06 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DP, D, &K));
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 03:04:06 +00:00
|
|
|
/* DQ = D mod Q-1 */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (DQ != NULL) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
|
2023-09-25 03:04:06 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DQ, D, &K));
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 03:04:06 +00:00
|
|
|
/* QP = Q^{-1} mod P */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (QP != NULL) {
|
2023-09-25 03:04:06 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(QP, Q, P));
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mpi_free(&K);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that core RSA parameters are sane.
|
|
|
|
*/
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
|
|
|
|
const mbedtls_mpi *Q, const mbedtls_mpi *D,
|
|
|
|
const mbedtls_mpi *E,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
mbedtls_mpi K, L;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mpi_init(&K);
|
|
|
|
mbedtls_mpi_init(&L);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 1: If PRNG provided, check that P and Q are prime
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_GENPRIME)
|
2019-02-16 16:19:46 +00:00
|
|
|
/*
|
|
|
|
* When generating keys, the strongest security we support aims for an error
|
|
|
|
* rate of at most 2^-100 and we are aiming for the same certainty here as
|
|
|
|
* well.
|
|
|
|
*/
|
2023-04-18 08:38:24 +00:00
|
|
|
if (f_rng != NULL && P != NULL &&
|
|
|
|
(ret = mbedtls_mpi_is_prime_ext(P, 50, f_rng, p_rng)) != 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (f_rng != NULL && Q != NULL &&
|
|
|
|
(ret = mbedtls_mpi_is_prime_ext(Q, 50, f_rng, p_rng)) != 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) f_rng);
|
|
|
|
((void) p_rng);
|
|
|
|
#endif /* MBEDTLS_GENPRIME */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 2: Check that 1 < N = P * Q
|
|
|
|
*/
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (P != NULL && Q != NULL && N != NULL) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, P, Q));
|
|
|
|
if (mbedtls_mpi_cmp_int(N, 1) <= 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi(&K, N) != 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 3: Check and 1 < D, E < N if present.
|
|
|
|
*/
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (N != NULL && D != NULL && E != NULL) {
|
|
|
|
if (mbedtls_mpi_cmp_int(D, 1) <= 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(E, 1) <= 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi(E, N) >= 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 4: Check that D, E are inverse modulo P-1 and Q-1
|
|
|
|
*/
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (P != NULL && Q != NULL && D != NULL && E != NULL) {
|
|
|
|
if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(Q, 1) <= 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute DE-1 mod P-1 */
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, P, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
|
|
|
|
if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute DE-1 mod Q-1 */
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L));
|
|
|
|
if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mpi_free(&K);
|
|
|
|
mbedtls_mpi_free(&L);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/* Wrap MPI error codes by RSA check failure error code */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 03:04:06 +00:00
|
|
|
/*
|
|
|
|
* Check that RSA CRT parameters are in accordance with core parameters.
|
|
|
|
*/
|
|
|
|
int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
|
|
|
|
const mbedtls_mpi *D, const mbedtls_mpi *DP,
|
|
|
|
const mbedtls_mpi *DQ, const mbedtls_mpi *QP)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2023-09-25 03:04:06 +00:00
|
|
|
|
|
|
|
mbedtls_mpi K, L;
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mpi_init(&K);
|
2023-09-25 03:04:06 +00:00
|
|
|
mbedtls_mpi_init(&L);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-09-25 03:04:06 +00:00
|
|
|
/* Check that DP - D == 0 mod P - 1 */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (DP != NULL) {
|
2023-09-25 03:04:06 +00:00
|
|
|
if (P == NULL) {
|
|
|
|
ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1));
|
2023-09-25 03:04:06 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DP, D));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
|
|
|
|
|
|
|
|
if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
|
|
|
|
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 03:04:06 +00:00
|
|
|
/* Check that DQ - D == 0 mod Q - 1 */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (DQ != NULL) {
|
2023-09-25 03:04:06 +00:00
|
|
|
if (Q == NULL) {
|
|
|
|
ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1));
|
2023-09-25 03:04:06 +00:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DQ, D));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K));
|
|
|
|
|
|
|
|
if (mbedtls_mpi_cmp_int(&L, 0) != 0) {
|
|
|
|
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 03:04:06 +00:00
|
|
|
/* Check that QP * Q - 1 == 0 mod P */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (QP != NULL) {
|
2023-09-25 03:04:06 +00:00
|
|
|
if (P == NULL || Q == NULL) {
|
|
|
|
ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, QP, Q));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, P));
|
|
|
|
if (mbedtls_mpi_cmp_int(&K, 0) != 0) {
|
|
|
|
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2023-09-25 03:04:06 +00:00
|
|
|
|
|
|
|
/* Wrap MPI error codes by RSA check failure error code */
|
|
|
|
if (ret != 0 &&
|
|
|
|
ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
|
|
|
|
ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) {
|
|
|
|
ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mpi_free(&K);
|
2023-09-25 03:04:06 +00:00
|
|
|
mbedtls_mpi_free(&L);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_RSA_C */
|