/src/rauc/subprojects/openssl-3.0.8/crypto/rsa/rsa_chk.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1999-2022 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 | 0 | { |
24 | 0 | BIGNUM *i, *j, *k, *l, *m; |
25 | 0 | BN_CTX *ctx; |
26 | 0 | int ret = 1, ex_primes = 0, idx; |
27 | 0 | RSA_PRIME_INFO *pinfo; |
28 | |
|
29 | 0 | if (key->p == NULL || key->q == NULL || key->n == NULL |
30 | 0 | || key->e == NULL || key->d == NULL) { |
31 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_VALUE_MISSING); |
32 | 0 | return 0; |
33 | 0 | } |
34 | | |
35 | | /* multi-prime? */ |
36 | 0 | if (key->version == RSA_ASN1_VERSION_MULTI) { |
37 | 0 | ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos); |
38 | 0 | if (ex_primes <= 0 |
39 | 0 | || (ex_primes + 2) > ossl_rsa_multip_cap(BN_num_bits(key->n))) { |
40 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MULTI_PRIME_KEY); |
41 | 0 | return 0; |
42 | 0 | } |
43 | 0 | } |
44 | | |
45 | 0 | i = BN_new(); |
46 | 0 | j = BN_new(); |
47 | 0 | k = BN_new(); |
48 | 0 | l = BN_new(); |
49 | 0 | m = BN_new(); |
50 | 0 | ctx = BN_CTX_new_ex(key->libctx); |
51 | 0 | if (i == NULL || j == NULL || k == NULL || l == NULL |
52 | 0 | || 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 | 0 | if (BN_is_one(key->e)) { |
59 | 0 | ret = 0; |
60 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE); |
61 | 0 | } |
62 | 0 | if (!BN_is_odd(key->e)) { |
63 | 0 | ret = 0; |
64 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE); |
65 | 0 | } |
66 | | |
67 | | /* p prime? */ |
68 | 0 | if (BN_check_prime(key->p, ctx, cb) != 1) { |
69 | 0 | ret = 0; |
70 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_P_NOT_PRIME); |
71 | 0 | } |
72 | | |
73 | | /* q prime? */ |
74 | 0 | if (BN_check_prime(key->q, ctx, cb) != 1) { |
75 | 0 | ret = 0; |
76 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_Q_NOT_PRIME); |
77 | 0 | } |
78 | | |
79 | | /* r_i prime? */ |
80 | 0 | for (idx = 0; idx < ex_primes; idx++) { |
81 | 0 | pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx); |
82 | 0 | if (BN_check_prime(pinfo->r, ctx, cb) != 1) { |
83 | 0 | ret = 0; |
84 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_MP_R_NOT_PRIME); |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | | /* n = p*q * r_3...r_i? */ |
89 | 0 | if (!BN_mul(i, key->p, key->q, ctx)) { |
90 | 0 | ret = -1; |
91 | 0 | goto err; |
92 | 0 | } |
93 | 0 | for (idx = 0; idx < ex_primes; idx++) { |
94 | 0 | pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx); |
95 | 0 | if (!BN_mul(i, i, pinfo->r, ctx)) { |
96 | 0 | ret = -1; |
97 | 0 | goto err; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | if (BN_cmp(i, key->n) != 0) { |
101 | 0 | ret = 0; |
102 | 0 | if (ex_primes) |
103 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES); |
104 | 0 | else |
105 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_N_DOES_NOT_EQUAL_P_Q); |
106 | 0 | } |
107 | | |
108 | | /* d*e = 1 mod \lambda(n)? */ |
109 | 0 | if (!BN_sub(i, key->p, BN_value_one())) { |
110 | 0 | ret = -1; |
111 | 0 | goto err; |
112 | 0 | } |
113 | 0 | 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 | 0 | if (!BN_mul(l, i, j, ctx)) { |
120 | 0 | ret = -1; |
121 | 0 | goto err; |
122 | 0 | } |
123 | 0 | if (!BN_gcd(m, i, j, ctx)) { |
124 | 0 | ret = -1; |
125 | 0 | goto err; |
126 | 0 | } |
127 | 0 | for (idx = 0; idx < ex_primes; idx++) { |
128 | 0 | pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx); |
129 | 0 | if (!BN_sub(k, pinfo->r, BN_value_one())) { |
130 | 0 | ret = -1; |
131 | 0 | goto err; |
132 | 0 | } |
133 | 0 | if (!BN_mul(l, l, k, ctx)) { |
134 | 0 | ret = -1; |
135 | 0 | goto err; |
136 | 0 | } |
137 | 0 | if (!BN_gcd(m, m, k, ctx)) { |
138 | 0 | ret = -1; |
139 | 0 | goto err; |
140 | 0 | } |
141 | 0 | } |
142 | 0 | if (!BN_div(k, NULL, l, m, ctx)) { /* remainder is 0 */ |
143 | 0 | ret = -1; |
144 | 0 | goto err; |
145 | 0 | } |
146 | 0 | if (!BN_mod_mul(i, key->d, key->e, k, ctx)) { |
147 | 0 | ret = -1; |
148 | 0 | goto err; |
149 | 0 | } |
150 | | |
151 | 0 | if (!BN_is_one(i)) { |
152 | 0 | ret = 0; |
153 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1); |
154 | 0 | } |
155 | |
|
156 | 0 | if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) { |
157 | | /* dmp1 = d mod (p-1)? */ |
158 | 0 | if (!BN_sub(i, key->p, BN_value_one())) { |
159 | 0 | ret = -1; |
160 | 0 | goto err; |
161 | 0 | } |
162 | 0 | if (!BN_mod(j, key->d, i, ctx)) { |
163 | 0 | ret = -1; |
164 | 0 | goto err; |
165 | 0 | } |
166 | 0 | if (BN_cmp(j, key->dmp1) != 0) { |
167 | 0 | ret = 0; |
168 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_DMP1_NOT_CONGRUENT_TO_D); |
169 | 0 | } |
170 | | |
171 | | /* dmq1 = d mod (q-1)? */ |
172 | 0 | if (!BN_sub(i, key->q, BN_value_one())) { |
173 | 0 | ret = -1; |
174 | 0 | goto err; |
175 | 0 | } |
176 | 0 | if (!BN_mod(j, key->d, i, ctx)) { |
177 | 0 | ret = -1; |
178 | 0 | goto err; |
179 | 0 | } |
180 | 0 | if (BN_cmp(j, key->dmq1) != 0) { |
181 | 0 | ret = 0; |
182 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_DMQ1_NOT_CONGRUENT_TO_D); |
183 | 0 | } |
184 | | |
185 | | /* iqmp = q^-1 mod p? */ |
186 | 0 | if (!BN_mod_inverse(i, key->q, key->p, ctx)) { |
187 | 0 | ret = -1; |
188 | 0 | goto err; |
189 | 0 | } |
190 | 0 | if (BN_cmp(i, key->iqmp) != 0) { |
191 | 0 | ret = 0; |
192 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_IQMP_NOT_INVERSE_OF_Q); |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | 0 | for (idx = 0; idx < ex_primes; idx++) { |
197 | 0 | pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx); |
198 | | /* d_i = d mod (r_i - 1)? */ |
199 | 0 | if (!BN_sub(i, pinfo->r, BN_value_one())) { |
200 | 0 | ret = -1; |
201 | 0 | goto err; |
202 | 0 | } |
203 | 0 | if (!BN_mod(j, key->d, i, ctx)) { |
204 | 0 | ret = -1; |
205 | 0 | goto err; |
206 | 0 | } |
207 | 0 | if (BN_cmp(j, pinfo->d) != 0) { |
208 | 0 | ret = 0; |
209 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D); |
210 | 0 | } |
211 | | /* t_i = R_i ^ -1 mod r_i ? */ |
212 | 0 | if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) { |
213 | 0 | ret = -1; |
214 | 0 | goto err; |
215 | 0 | } |
216 | 0 | if (BN_cmp(i, pinfo->t) != 0) { |
217 | 0 | ret = 0; |
218 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R); |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | 0 | err: |
223 | 0 | BN_free(i); |
224 | 0 | BN_free(j); |
225 | 0 | BN_free(k); |
226 | 0 | BN_free(l); |
227 | 0 | BN_free(m); |
228 | 0 | BN_CTX_free(ctx); |
229 | 0 | return ret; |
230 | 0 | } |
231 | | #endif /* FIPS_MODULE */ |
232 | | |
233 | | int ossl_rsa_validate_public(const RSA *key) |
234 | 0 | { |
235 | 0 | return ossl_rsa_sp800_56b_check_public(key); |
236 | 0 | } |
237 | | |
238 | | int ossl_rsa_validate_private(const RSA *key) |
239 | 0 | { |
240 | 0 | return ossl_rsa_sp800_56b_check_private(key); |
241 | 0 | } |
242 | | |
243 | | int ossl_rsa_validate_pairwise(const RSA *key) |
244 | 0 | { |
245 | | #ifdef FIPS_MODULE |
246 | | return ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, RSA_bits(key)); |
247 | | #else |
248 | 0 | return rsa_validate_keypair_multiprime(key, NULL) > 0; |
249 | 0 | #endif |
250 | 0 | } |
251 | | |
252 | | int RSA_check_key(const RSA *key) |
253 | 0 | { |
254 | 0 | return RSA_check_key_ex(key, NULL); |
255 | 0 | } |
256 | | |
257 | | int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) |
258 | 0 | { |
259 | | #ifdef FIPS_MODULE |
260 | | return ossl_rsa_validate_public(key) |
261 | | && ossl_rsa_validate_private(key) |
262 | | && ossl_rsa_validate_pairwise(key); |
263 | | #else |
264 | 0 | return rsa_validate_keypair_multiprime(key, cb); |
265 | 0 | #endif /* FIPS_MODULE */ |
266 | 0 | } |