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