Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Helper functions for the RSA module |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 6 | * |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 9 | #include "common.h" |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 10 | |
| 11 | #if defined(MBEDTLS_RSA_C) |
| 12 | |
| 13 | #include "mbedtls/rsa.h" |
| 14 | #include "mbedtls/bignum.h" |
Manuel Pégourié-Gonnard | 7dcfd73 | 2025-07-10 09:57:29 +0200 | [diff] [blame] | 15 | #include "bignum_internal.h" |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 16 | #include "rsa_alt_helpers.h" |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Compute RSA prime factors from public and private exponents |
| 20 | * |
| 21 | * Summary of algorithm: |
| 22 | * Setting F := lcm(P-1,Q-1), the idea is as follows: |
| 23 | * |
| 24 | * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2) |
| 25 | * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the |
| 26 | * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four |
| 27 | * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1) |
| 28 | * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime |
| 29 | * factors of N. |
| 30 | * |
| 31 | * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same |
| 32 | * construction still applies since (-)^K is the identity on the set of |
| 33 | * roots of 1 in Z/NZ. |
| 34 | * |
| 35 | * The public and private key primitives (-)^E and (-)^D are mutually inverse |
| 36 | * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e. |
| 37 | * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L. |
| 38 | * Splitting L = 2^t * K with K odd, we have |
| 39 | * |
| 40 | * DE - 1 = FL = (F/2) * (2^(t+1)) * K, |
| 41 | * |
| 42 | * so (F / 2) * K is among the numbers |
| 43 | * |
| 44 | * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord |
| 45 | * |
| 46 | * where ord is the order of 2 in (DE - 1). |
| 47 | * We can therefore iterate through these numbers apply the construction |
| 48 | * of (a) and (b) above to attempt to factor N. |
| 49 | * |
| 50 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N, |
| 52 | mbedtls_mpi const *E, mbedtls_mpi const *D, |
| 53 | mbedtls_mpi *P, mbedtls_mpi *Q) |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 54 | { |
| 55 | int ret = 0; |
| 56 | |
| 57 | uint16_t attempt; /* Number of current attempt */ |
| 58 | uint16_t iter; /* Number of squares computed in the current attempt */ |
| 59 | |
| 60 | uint16_t order; /* Order of 2 in DE - 1 */ |
| 61 | |
| 62 | mbedtls_mpi T; /* Holds largest odd divisor of DE - 1 */ |
| 63 | mbedtls_mpi K; /* Temporary holding the current candidate */ |
| 64 | |
Hanno Becker | 4055a3a | 2017-10-17 09:15:26 +0100 | [diff] [blame] | 65 | const unsigned char primes[] = { 2, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | 3, 5, 7, 11, 13, 17, 19, 23, |
| 67 | 29, 31, 37, 41, 43, 47, 53, 59, |
| 68 | 61, 67, 71, 73, 79, 83, 89, 97, |
| 69 | 101, 103, 107, 109, 113, 127, 131, 137, |
| 70 | 139, 149, 151, 157, 163, 167, 173, 179, |
| 71 | 181, 191, 193, 197, 199, 211, 223, 227, |
| 72 | 229, 233, 239, 241, 251 }; |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 73 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | const size_t num_primes = sizeof(primes) / sizeof(*primes); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 75 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | if (P == NULL || Q == NULL || P->p != NULL || Q->p != NULL) { |
| 77 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 78 | } |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 79 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | if (mbedtls_mpi_cmp_int(N, 0) <= 0 || |
| 81 | mbedtls_mpi_cmp_int(D, 1) <= 0 || |
| 82 | mbedtls_mpi_cmp_mpi(D, N) >= 0 || |
| 83 | mbedtls_mpi_cmp_int(E, 1) <= 0 || |
| 84 | mbedtls_mpi_cmp_mpi(E, N) >= 0) { |
| 85 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Initializations and temporary changes |
| 90 | */ |
| 91 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | mbedtls_mpi_init(&K); |
| 93 | mbedtls_mpi_init(&T); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 94 | |
| 95 | /* T := DE - 1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, D, E)); |
| 97 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&T, &T, 1)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 98 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | if ((order = (uint16_t) mbedtls_mpi_lsb(&T)) == 0) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 100 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 101 | goto cleanup; |
| 102 | } |
| 103 | |
| 104 | /* After this operation, T holds the largest odd divisor of DE - 1. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&T, order)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * Actual work |
| 109 | */ |
| 110 | |
| 111 | /* Skip trying 2 if N == 1 mod 8 */ |
| 112 | attempt = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | if (N->p[0] % 8 == 1) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 114 | attempt = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | } |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | for (; attempt < num_primes; ++attempt) { |
Chien Wong | e2caf41 | 2023-08-01 21:38:46 +0800 | [diff] [blame] | 118 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&K, primes[attempt])); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 119 | |
| 120 | /* Check if gcd(K,N) = 1 */ |
Manuel Pégourié-Gonnard | 7dcfd73 | 2025-07-10 09:57:29 +0200 | [diff] [blame] | 121 | MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(P, NULL, &K, N)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 122 | if (mbedtls_mpi_cmp_int(P, 1) != 0) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 123 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | } |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 125 | |
| 126 | /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ... |
| 127 | * and check whether they have nontrivial GCD with N. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&K, &K, &T, N, |
| 129 | Q /* temporarily use Q for storing Montgomery |
| 130 | * multiplication helper values */)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | for (iter = 1; iter <= order; ++iter) { |
Hanno Becker | 5d42b53 | 2017-10-11 15:58:00 +0100 | [diff] [blame] | 133 | /* If we reach 1 prematurely, there's no point |
| 134 | * in continuing to square K */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | if (mbedtls_mpi_cmp_int(&K, 1) == 0) { |
Hanno Becker | 5d42b53 | 2017-10-11 15:58:00 +0100 | [diff] [blame] | 136 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | } |
Hanno Becker | 5d42b53 | 2017-10-11 15:58:00 +0100 | [diff] [blame] | 138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&K, &K, 1)); |
Manuel Pégourié-Gonnard | 7dcfd73 | 2025-07-10 09:57:29 +0200 | [diff] [blame] | 140 | MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(P, NULL, &K, N)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | if (mbedtls_mpi_cmp_int(P, 1) == 1 && |
| 143 | mbedtls_mpi_cmp_mpi(P, N) == -1) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 144 | /* |
| 145 | * Have found a nontrivial divisor P of N. |
| 146 | * Set Q := N / P. |
| 147 | */ |
| 148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(Q, NULL, N, P)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 150 | goto cleanup; |
| 151 | } |
| 152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1)); |
| 154 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &K)); |
| 155 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, N)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 156 | } |
Hanno Becker | 14a00c0 | 2017-10-11 12:58:23 +0100 | [diff] [blame] | 157 | |
Hanno Becker | 5d42b53 | 2017-10-11 15:58:00 +0100 | [diff] [blame] | 158 | /* |
| 159 | * If we get here, then either we prematurely aborted the loop because |
| 160 | * we reached 1, or K holds primes[attempt]^(DE - 1) mod N, which must |
| 161 | * be 1 if D,E,N were consistent. |
| 162 | * Check if that's the case and abort if not, to avoid very long, |
| 163 | * yet eventually failing, computations if N,D,E were not sane. |
| 164 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 165 | if (mbedtls_mpi_cmp_int(&K, 1) != 0) { |
Hanno Becker | 14a00c0 | 2017-10-11 12:58:23 +0100 | [diff] [blame] | 166 | break; |
| 167 | } |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 171 | |
| 172 | cleanup: |
| 173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | mbedtls_mpi_free(&K); |
| 175 | mbedtls_mpi_free(&T); |
| 176 | return ret; |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Given P, Q and the public exponent E, deduce D. |
| 181 | * This is essentially a modular inversion. |
| 182 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P, |
| 184 | mbedtls_mpi const *Q, |
| 185 | mbedtls_mpi const *E, |
| 186 | mbedtls_mpi *D) |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 187 | { |
| 188 | int ret = 0; |
| 189 | mbedtls_mpi K, L; |
| 190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | if (D == NULL || mbedtls_mpi_cmp_int(D, 0) != 0) { |
| 192 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | if (mbedtls_mpi_cmp_int(P, 1) <= 0 || |
| 196 | mbedtls_mpi_cmp_int(Q, 1) <= 0 || |
| 197 | mbedtls_mpi_cmp_int(E, 0) == 0) { |
| 198 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 199 | } |
| 200 | |
Manuel Pégourié-Gonnard | 9e1c532 | 2025-08-13 14:14:19 +0200 | [diff] [blame] | 201 | if (mbedtls_mpi_get_bit(E, 0) != 1) { |
| 202 | return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
| 203 | } |
| 204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | mbedtls_mpi_init(&K); |
| 206 | mbedtls_mpi_init(&L); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 207 | |
| 208 | /* Temporarily put K := P-1 and L := Q-1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1)); |
| 210 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 211 | |
| 212 | /* Temporarily put D := gcd(P-1, Q-1) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 213 | MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(D, &K, &L)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 214 | |
| 215 | /* K := LCM(P-1, Q-1) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, &K, &L)); |
| 217 | MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&K, NULL, &K, D)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 218 | |
Manuel Pégourié-Gonnard | a4bf680 | 2025-07-10 10:48:23 +0200 | [diff] [blame] | 219 | /* Compute modular inverse of E mod LCM(P-1, Q-1) |
| 220 | * This is FIPS 186-4 §B.3.1 criterion 3(b). |
| 221 | * This will return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if E is not coprime to |
| 222 | * (P-1)(Q-1), also validating FIPS 186-4 §B.3.1 criterion 2(a). */ |
Manuel Pégourié-Gonnard | 9e1c532 | 2025-08-13 14:14:19 +0200 | [diff] [blame] | 223 | MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod_even_in_range(D, E, &K)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 224 | |
| 225 | cleanup: |
| 226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | mbedtls_mpi_free(&K); |
| 228 | mbedtls_mpi_free(&L); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | return ret; |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 234 | const mbedtls_mpi *D, mbedtls_mpi *DP, |
| 235 | mbedtls_mpi *DQ, mbedtls_mpi *QP) |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 236 | { |
| 237 | int ret = 0; |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 238 | mbedtls_mpi K; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | mbedtls_mpi_init(&K); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 240 | |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 241 | /* DP = D mod P-1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | if (DP != NULL) { |
| 243 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1)); |
| 244 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DP, D, &K)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 247 | /* DQ = D mod Q-1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | if (DQ != NULL) { |
| 249 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1)); |
| 250 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(DQ, D, &K)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 251 | } |
| 252 | |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 253 | /* QP = Q^{-1} mod P */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | if (QP != NULL) { |
Manuel Pégourié-Gonnard | 630148e | 2025-08-13 13:57:35 +0200 | [diff] [blame] | 255 | MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod_odd(QP, Q, P)); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | mbedtls_mpi_free(&K); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | return ret; |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Check that core RSA parameters are sane. |
| 266 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P, |
| 268 | const mbedtls_mpi *Q, const mbedtls_mpi *D, |
| 269 | const mbedtls_mpi *E, |
| 270 | int (*f_rng)(void *, unsigned char *, size_t), |
| 271 | void *p_rng) |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 272 | { |
| 273 | int ret = 0; |
| 274 | mbedtls_mpi K, L; |
| 275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | mbedtls_mpi_init(&K); |
| 277 | mbedtls_mpi_init(&L); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * Step 1: If PRNG provided, check that P and Q are prime |
| 281 | */ |
| 282 | |
| 283 | #if defined(MBEDTLS_GENPRIME) |
Janos Follath | a0b67c2 | 2018-09-18 14:48:23 +0100 | [diff] [blame] | 284 | /* |
| 285 | * When generating keys, the strongest security we support aims for an error |
| 286 | * rate of at most 2^-100 and we are aiming for the same certainty here as |
| 287 | * well. |
| 288 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | if (f_rng != NULL && P != NULL && |
| 290 | (ret = mbedtls_mpi_is_prime_ext(P, 50, f_rng, p_rng)) != 0) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 291 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 292 | goto cleanup; |
| 293 | } |
| 294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | if (f_rng != NULL && Q != NULL && |
| 296 | (ret = mbedtls_mpi_is_prime_ext(Q, 50, f_rng, p_rng)) != 0) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 297 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 298 | goto cleanup; |
| 299 | } |
| 300 | #else |
| 301 | ((void) f_rng); |
| 302 | ((void) p_rng); |
| 303 | #endif /* MBEDTLS_GENPRIME */ |
| 304 | |
| 305 | /* |
Hanno Becker | f8c028a | 2017-10-17 09:20:57 +0100 | [diff] [blame] | 306 | * Step 2: Check that 1 < N = P * Q |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 307 | */ |
| 308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | if (P != NULL && Q != NULL && N != NULL) { |
| 310 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, P, Q)); |
| 311 | if (mbedtls_mpi_cmp_int(N, 1) <= 0 || |
| 312 | mbedtls_mpi_cmp_mpi(&K, N) != 0) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 313 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 314 | goto cleanup; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Step 3: Check and 1 < D, E < N if present. |
| 320 | */ |
| 321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | if (N != NULL && D != NULL && E != NULL) { |
| 323 | if (mbedtls_mpi_cmp_int(D, 1) <= 0 || |
| 324 | mbedtls_mpi_cmp_int(E, 1) <= 0 || |
| 325 | mbedtls_mpi_cmp_mpi(D, N) >= 0 || |
| 326 | mbedtls_mpi_cmp_mpi(E, N) >= 0) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 327 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 328 | goto cleanup; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Step 4: Check that D, E are inverse modulo P-1 and Q-1 |
| 334 | */ |
| 335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | if (P != NULL && Q != NULL && D != NULL && E != NULL) { |
| 337 | if (mbedtls_mpi_cmp_int(P, 1) <= 0 || |
| 338 | mbedtls_mpi_cmp_int(Q, 1) <= 0) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 339 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 340 | goto cleanup; |
| 341 | } |
| 342 | |
| 343 | /* Compute DE-1 mod P-1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 344 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E)); |
| 345 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1)); |
| 346 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, P, 1)); |
| 347 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L)); |
| 348 | if (mbedtls_mpi_cmp_int(&K, 0) != 0) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 349 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 350 | goto cleanup; |
| 351 | } |
| 352 | |
| 353 | /* Compute DE-1 mod Q-1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, D, E)); |
| 355 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1)); |
| 356 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&L, Q, 1)); |
| 357 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, &L)); |
| 358 | if (mbedtls_mpi_cmp_int(&K, 0) != 0) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 359 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 360 | goto cleanup; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | cleanup: |
| 365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | mbedtls_mpi_free(&K); |
| 367 | mbedtls_mpi_free(&L); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 368 | |
| 369 | /* Wrap MPI error codes by RSA check failure error code */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | if (ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED) { |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 371 | ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 372 | } |
| 373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | return ret; |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 377 | /* |
| 378 | * Check that RSA CRT parameters are in accordance with core parameters. |
| 379 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 381 | const mbedtls_mpi *D, const mbedtls_mpi *DP, |
| 382 | const mbedtls_mpi *DQ, const mbedtls_mpi *QP) |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 383 | { |
| 384 | int ret = 0; |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 385 | |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 386 | mbedtls_mpi K, L; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | mbedtls_mpi_init(&K); |
| 388 | mbedtls_mpi_init(&L); |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 389 | |
| 390 | /* Check that DP - D == 0 mod P - 1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | if (DP != NULL) { |
| 392 | if (P == NULL) { |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 393 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 394 | goto cleanup; |
| 395 | } |
| 396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, P, 1)); |
| 398 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DP, D)); |
| 399 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K)); |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | if (mbedtls_mpi_cmp_int(&L, 0) != 0) { |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 402 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 403 | goto cleanup; |
| 404 | } |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 407 | /* Check that DQ - D == 0 mod Q - 1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 408 | if (DQ != NULL) { |
| 409 | if (Q == NULL) { |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 410 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 411 | goto cleanup; |
| 412 | } |
| 413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 414 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, Q, 1)); |
| 415 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&L, DQ, D)); |
| 416 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&L, &L, &K)); |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | if (mbedtls_mpi_cmp_int(&L, 0) != 0) { |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 419 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 420 | goto cleanup; |
| 421 | } |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 424 | /* Check that QP * Q - 1 == 0 mod P */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | if (QP != NULL) { |
| 426 | if (P == NULL || Q == NULL) { |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 427 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 428 | goto cleanup; |
| 429 | } |
| 430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, QP, Q)); |
| 432 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&K, &K, 1)); |
| 433 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&K, &K, P)); |
| 434 | if (mbedtls_mpi_cmp_int(&K, 0) != 0) { |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 435 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 436 | goto cleanup; |
| 437 | } |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | cleanup: |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 441 | |
| 442 | /* Wrap MPI error codes by RSA check failure error code */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | if (ret != 0 && |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 444 | ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) { |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 446 | ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 447 | } |
| 448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | mbedtls_mpi_free(&K); |
| 450 | mbedtls_mpi_free(&L); |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | return ret; |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | #endif /* MBEDTLS_RSA_C */ |