1 /* ====================================================================
2 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@OpenSSL.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
50 #include <openssl/bn.h>
51 #include <openssl/err.h>
52 #include <openssl/rsa.h>
54 int RSA_check_key(const RSA *key)
56 return RSA_check_key_ex(key, NULL);
59 int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)
61 BIGNUM *i, *j, *k, *l, *m;
66 if (!key->p || !key->q || !key->n || !key->e || !key->d) {
67 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);
77 if (i == NULL || j == NULL || k == NULL || l == NULL ||
78 m == NULL || ctx == NULL) {
80 RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);
85 r = BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb);
90 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);
94 r = BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb);
99 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);
103 r = BN_mul(i, key->p, key->q, ctx);
109 if (BN_cmp(i, key->n) != 0) {
111 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);
114 /* d*e = 1 mod lcm(p-1,q-1)? */
116 r = BN_sub(i, key->p, BN_value_one());
121 r = BN_sub(j, key->q, BN_value_one());
127 /* now compute k = lcm(i,j) */
128 r = BN_mul(l, i, j, ctx);
133 r = BN_gcd(m, i, j, ctx);
138 r = BN_div(k, NULL, l, m, ctx); /* remainder is 0 */
144 r = BN_mod_mul(i, key->d, key->e, k, ctx);
152 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);
155 if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {
156 /* dmp1 = d mod (p-1)? */
157 r = BN_sub(i, key->p, BN_value_one());
163 r = BN_mod(j, key->d, i, ctx);
169 if (BN_cmp(j, key->dmp1) != 0) {
171 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);
174 /* dmq1 = d mod (q-1)? */
175 r = BN_sub(i, key->q, BN_value_one());
181 r = BN_mod(j, key->d, i, ctx);
187 if (BN_cmp(j, key->dmq1) != 0) {
189 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
192 /* iqmp = q^-1 mod p? */
193 if (!BN_mod_inverse(i, key->q, key->p, ctx)) {
198 if (BN_cmp(i, key->iqmp) != 0) {
200 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);