/src/openssl31/crypto/rsa/rsa_chk.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /* |
11 | | * RSA low level APIs are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <openssl/bn.h> |
17 | | #include <openssl/err.h> |
18 | | #include "crypto/rsa.h" |
19 | | #include "rsa_local.h" |
20 | | |
21 | | #ifndef FIPS_MODULE |
22 | | static int rsa_validate_keypair_multiprime(const RSA *key, BN_GENCB *cb) |
23 | 1.02k | { |
24 | 1.02k | BIGNUM *i, *j, *k, *l, *m; |
25 | 1.02k | BN_CTX *ctx; |
26 | 1.02k | int ret = 1, ex_primes = 0, idx; |
27 | 1.02k | RSA_PRIME_INFO *pinfo; |
28 | | |
29 | 1.02k | if (key->p == NULL || key->q == NULL || key->n == NULL |
30 | 1.02k | || key->e == NULL || key->d == NULL) { |
31 | 324 | ERR_raise(ERR_LIB_RSA, RSA_R_VALUE_MISSING); |
32 | 324 | return 0; |
33 | 324 | } |
34 | | |
35 | | /* multi-prime? */ |
36 | 699 | if (key->version == RSA_ASN1_VERSION_MULTI) { |
37 | 98 | ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos); |
38 | 98 | if (ex_primes <= 0 |
39 | 98 | || (ex_primes + 2) > ossl_rsa_multip_cap(BN_num_bits(key->n))) { |
40 | 61 | ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MULTI_PRIME_KEY); |
41 | 61 | return 0; |
42 | 61 | } |
43 | 98 | } |
44 | | |
45 | 638 | i = BN_new(); |
46 | 638 | j = BN_new(); |
47 | 638 | k = BN_new(); |
48 | 638 | l = BN_new(); |
49 | 638 | m = BN_new(); |
50 | 638 | ctx = BN_CTX_new_ex(key->libctx); |
51 | 638 | if (i == NULL || j == NULL || k == NULL || l == NULL |
52 | 638 | || m == NULL || ctx == NULL) { |
53 | 0 | ret = -1; |
54 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); |
55 | 0 | goto err; |
56 | 0 | } |
57 | | |
58 | 638 | if (BN_is_one(key->e)) { |
59 | 5 | ret = 0; |
60 | 5 | ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE); |
61 | 5 | } |
62 | 638 | if (!BN_is_odd(key->e)) { |
63 | 498 | ret = 0; |
64 | 498 | ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE); |
65 | 498 | } |
66 | | |
67 | | /* p prime? */ |
68 | 638 | if (BN_check_prime(key->p, ctx, cb) != 1) { |
69 | 529 | ret = 0; |
70 | 529 | ERR_raise(ERR_LIB_RSA, RSA_R_P_NOT_PRIME); |
71 | 529 | } |
72 | | |
73 | | /* q prime? */ |
74 | 638 | if (BN_check_prime(key->q, ctx, cb) != 1) { |
75 | 526 | ret = 0; |
76 | 526 | ERR_raise(ERR_LIB_RSA, RSA_R_Q_NOT_PRIME); |
77 | 526 | } |
78 | | |
79 | | /* r_i prime? */ |
80 | 680 | for (idx = 0; idx < ex_primes; idx++) { |
81 | 42 | pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx); |
82 | 42 | if (BN_check_prime(pinfo->r, ctx, cb) != 1) { |
83 | 30 | ret = 0; |
84 | 30 | ERR_raise(ERR_LIB_RSA, RSA_R_MP_R_NOT_PRIME); |
85 | 30 | } |
86 | 42 | } |
87 | | |
88 | | /* n = p*q * r_3...r_i? */ |
89 | 638 | if (!BN_mul(i, key->p, key->q, ctx)) { |
90 | 0 | ret = -1; |
91 | 0 | goto err; |
92 | 0 | } |
93 | 680 | for (idx = 0; idx < ex_primes; idx++) { |
94 | 42 | pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx); |
95 | 42 | if (!BN_mul(i, i, pinfo->r, ctx)) { |
96 | 0 | ret = -1; |
97 | 0 | goto err; |
98 | 0 | } |
99 | 42 | } |
100 | 638 | if (BN_cmp(i, key->n) != 0) { |
101 | 474 | ret = 0; |
102 | 474 | if (ex_primes) |
103 | 474 | ERR_raise(ERR_LIB_RSA, RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES); |
104 | 437 | else |
105 | 474 | ERR_raise(ERR_LIB_RSA, RSA_R_N_DOES_NOT_EQUAL_P_Q); |
106 | 474 | } |
107 | | |
108 | | /* d*e = 1 mod \lambda(n)? */ |
109 | 638 | if (!BN_sub(i, key->p, BN_value_one())) { |
110 | 0 | ret = -1; |
111 | 0 | goto err; |
112 | 0 | } |
113 | 638 | if (!BN_sub(j, key->q, BN_value_one())) { |
114 | 0 | ret = -1; |
115 | 0 | goto err; |
116 | 0 | } |
117 | | |
118 | | /* now compute k = \lambda(n) = LCM(i, j, r_3 - 1...) */ |
119 | 638 | if (!BN_mul(l, i, j, ctx)) { |
120 | 0 | ret = -1; |
121 | 0 | goto err; |
122 | 0 | } |
123 | 638 | if (!BN_gcd(m, i, j, ctx)) { |
124 | 0 | ret = -1; |
125 | 0 | goto err; |
126 | 0 | } |
127 | 638 | if (!BN_div(m, NULL, l, m, ctx)) { /* remainder is 0 */ |
128 | 2 | ret = -1; |
129 | 2 | goto err; |
130 | 2 | } |
131 | 675 | for (idx = 0; idx < ex_primes; idx++) { |
132 | 41 | pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx); |
133 | 41 | if (!BN_sub(k, pinfo->r, BN_value_one())) { |
134 | 0 | ret = -1; |
135 | 0 | goto err; |
136 | 0 | } |
137 | 41 | if (!BN_mul(l, m, k, ctx)) { |
138 | 0 | ret = -1; |
139 | 0 | goto err; |
140 | 0 | } |
141 | 41 | if (!BN_gcd(m, m, k, ctx)) { |
142 | 0 | ret = -1; |
143 | 0 | goto err; |
144 | 0 | } |
145 | 41 | if (!BN_div(m, NULL, l, m, ctx)) { /* remainder is 0 */ |
146 | 2 | ret = -1; |
147 | 2 | goto err; |
148 | 2 | } |
149 | 41 | } |
150 | 634 | if (!BN_mod_mul(i, key->d, key->e, m, ctx)) { |
151 | 14 | ret = -1; |
152 | 14 | goto err; |
153 | 14 | } |
154 | | |
155 | 620 | if (!BN_is_one(i)) { |
156 | 617 | ret = 0; |
157 | 617 | ERR_raise(ERR_LIB_RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1); |
158 | 617 | } |
159 | | |
160 | 620 | if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) { |
161 | | /* dmp1 = d mod (p-1)? */ |
162 | 620 | if (!BN_sub(i, key->p, BN_value_one())) { |
163 | 0 | ret = -1; |
164 | 0 | goto err; |
165 | 0 | } |
166 | 620 | if (!BN_mod(j, key->d, i, ctx)) { |
167 | 0 | ret = -1; |
168 | 0 | goto err; |
169 | 0 | } |
170 | 620 | if (BN_cmp(j, key->dmp1) != 0) { |
171 | 350 | ret = 0; |
172 | 350 | ERR_raise(ERR_LIB_RSA, RSA_R_DMP1_NOT_CONGRUENT_TO_D); |
173 | 350 | } |
174 | | |
175 | | /* dmq1 = d mod (q-1)? */ |
176 | 620 | if (!BN_sub(i, key->q, BN_value_one())) { |
177 | 0 | ret = -1; |
178 | 0 | goto err; |
179 | 0 | } |
180 | 620 | if (!BN_mod(j, key->d, i, ctx)) { |
181 | 0 | ret = -1; |
182 | 0 | goto err; |
183 | 0 | } |
184 | 620 | if (BN_cmp(j, key->dmq1) != 0) { |
185 | 380 | ret = 0; |
186 | 380 | ERR_raise(ERR_LIB_RSA, RSA_R_DMQ1_NOT_CONGRUENT_TO_D); |
187 | 380 | } |
188 | | |
189 | | /* iqmp = q^-1 mod p? */ |
190 | 620 | if (!BN_mod_inverse(i, key->q, key->p, ctx)) { |
191 | 366 | ret = -1; |
192 | 366 | goto err; |
193 | 366 | } |
194 | 254 | if (BN_cmp(i, key->iqmp) != 0) { |
195 | 252 | ret = 0; |
196 | 252 | ERR_raise(ERR_LIB_RSA, RSA_R_IQMP_NOT_INVERSE_OF_Q); |
197 | 252 | } |
198 | 254 | } |
199 | | |
200 | 270 | for (idx = 0; idx < ex_primes; idx++) { |
201 | 23 | pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx); |
202 | | /* d_i = d mod (r_i - 1)? */ |
203 | 23 | if (!BN_sub(i, pinfo->r, BN_value_one())) { |
204 | 0 | ret = -1; |
205 | 0 | goto err; |
206 | 0 | } |
207 | 23 | if (!BN_mod(j, key->d, i, ctx)) { |
208 | 0 | ret = -1; |
209 | 0 | goto err; |
210 | 0 | } |
211 | 23 | if (BN_cmp(j, pinfo->d) != 0) { |
212 | 21 | ret = 0; |
213 | 21 | ERR_raise(ERR_LIB_RSA, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D); |
214 | 21 | } |
215 | | /* t_i = R_i ^ -1 mod r_i ? */ |
216 | 23 | if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) { |
217 | 7 | ret = -1; |
218 | 7 | goto err; |
219 | 7 | } |
220 | 16 | if (BN_cmp(i, pinfo->t) != 0) { |
221 | 15 | ret = 0; |
222 | 15 | ERR_raise(ERR_LIB_RSA, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R); |
223 | 15 | } |
224 | 16 | } |
225 | | |
226 | 638 | err: |
227 | 638 | BN_free(i); |
228 | 638 | BN_free(j); |
229 | 638 | BN_free(k); |
230 | 638 | BN_free(l); |
231 | 638 | BN_free(m); |
232 | 638 | BN_CTX_free(ctx); |
233 | 638 | return ret; |
234 | 254 | } |
235 | | #endif /* FIPS_MODULE */ |
236 | | |
237 | | int ossl_rsa_validate_public(const RSA *key) |
238 | 1.33k | { |
239 | 1.33k | return ossl_rsa_sp800_56b_check_public(key); |
240 | 1.33k | } |
241 | | |
242 | | int ossl_rsa_validate_private(const RSA *key) |
243 | 1.02k | { |
244 | 1.02k | return ossl_rsa_sp800_56b_check_private(key); |
245 | 1.02k | } |
246 | | |
247 | | int ossl_rsa_validate_pairwise(const RSA *key) |
248 | 1.02k | { |
249 | | #ifdef FIPS_MODULE |
250 | | return ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, RSA_bits(key)); |
251 | | #else |
252 | 1.02k | return rsa_validate_keypair_multiprime(key, NULL) > 0; |
253 | 1.02k | #endif |
254 | 1.02k | } |
255 | | |
256 | | int RSA_check_key(const RSA *key) |
257 | 0 | { |
258 | 0 | return RSA_check_key_ex(key, NULL); |
259 | 0 | } |
260 | | |
261 | | int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) |
262 | 0 | { |
263 | | #ifdef FIPS_MODULE |
264 | | return ossl_rsa_validate_public(key) |
265 | | && ossl_rsa_validate_private(key) |
266 | | && ossl_rsa_validate_pairwise(key); |
267 | | #else |
268 | 0 | return rsa_validate_keypair_multiprime(key, cb); |
269 | 0 | #endif /* FIPS_MODULE */ |
270 | 0 | } |