sl@0: /* crypto/rsa/rsa_chk.c -*- Mode: C; c-file-style: "eay" -*- */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1999 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: sl@0: EXPORT_C int RSA_check_key(const RSA *key) sl@0: { sl@0: BIGNUM *i, *j, *k, *l, *m; sl@0: BN_CTX *ctx; sl@0: int r; sl@0: int ret=1; sl@0: sl@0: i = BN_new(); sl@0: j = BN_new(); sl@0: k = BN_new(); sl@0: l = BN_new(); sl@0: m = BN_new(); sl@0: ctx = BN_CTX_new(); sl@0: if (i == NULL || j == NULL || k == NULL || l == NULL || sl@0: m == NULL || ctx == NULL) sl@0: { sl@0: ret = -1; sl@0: RSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: /* p prime? */ sl@0: r = BN_is_prime_ex(key->p, BN_prime_checks, NULL, NULL); sl@0: if (r != 1) sl@0: { sl@0: ret = r; sl@0: if (r != 0) sl@0: goto err; sl@0: RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME); sl@0: } sl@0: sl@0: /* q prime? */ sl@0: r = BN_is_prime_ex(key->q, BN_prime_checks, NULL, NULL); sl@0: if (r != 1) sl@0: { sl@0: ret = r; sl@0: if (r != 0) sl@0: goto err; sl@0: RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME); sl@0: } sl@0: sl@0: /* n = p*q? */ sl@0: r = BN_mul(i, key->p, key->q, ctx); sl@0: if (!r) { ret = -1; goto err; } sl@0: sl@0: if (BN_cmp(i, key->n) != 0) sl@0: { sl@0: ret = 0; sl@0: RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q); sl@0: } sl@0: sl@0: /* d*e = 1 mod lcm(p-1,q-1)? */ sl@0: sl@0: r = BN_sub(i, key->p, BN_value_one()); sl@0: if (!r) { ret = -1; goto err; } sl@0: r = BN_sub(j, key->q, BN_value_one()); sl@0: if (!r) { ret = -1; goto err; } sl@0: sl@0: /* now compute k = lcm(i,j) */ sl@0: r = BN_mul(l, i, j, ctx); sl@0: if (!r) { ret = -1; goto err; } sl@0: r = BN_gcd(m, i, j, ctx); sl@0: if (!r) { ret = -1; goto err; } sl@0: r = BN_div(k, NULL, l, m, ctx); /* remainder is 0 */ sl@0: if (!r) { ret = -1; goto err; } sl@0: sl@0: r = BN_mod_mul(i, key->d, key->e, k, ctx); sl@0: if (!r) { ret = -1; goto err; } sl@0: sl@0: if (!BN_is_one(i)) sl@0: { sl@0: ret = 0; sl@0: RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1); sl@0: } sl@0: sl@0: if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) sl@0: { sl@0: /* dmp1 = d mod (p-1)? */ sl@0: r = BN_sub(i, key->p, BN_value_one()); sl@0: if (!r) { ret = -1; goto err; } sl@0: sl@0: r = BN_mod(j, key->d, i, ctx); sl@0: if (!r) { ret = -1; goto err; } sl@0: sl@0: if (BN_cmp(j, key->dmp1) != 0) sl@0: { sl@0: ret = 0; sl@0: RSAerr(RSA_F_RSA_CHECK_KEY, sl@0: RSA_R_DMP1_NOT_CONGRUENT_TO_D); sl@0: } sl@0: sl@0: /* dmq1 = d mod (q-1)? */ sl@0: r = BN_sub(i, key->q, BN_value_one()); sl@0: if (!r) { ret = -1; goto err; } sl@0: sl@0: r = BN_mod(j, key->d, i, ctx); sl@0: if (!r) { ret = -1; goto err; } sl@0: sl@0: if (BN_cmp(j, key->dmq1) != 0) sl@0: { sl@0: ret = 0; sl@0: RSAerr(RSA_F_RSA_CHECK_KEY, sl@0: RSA_R_DMQ1_NOT_CONGRUENT_TO_D); sl@0: } sl@0: sl@0: /* iqmp = q^-1 mod p? */ sl@0: if(!BN_mod_inverse(i, key->q, key->p, ctx)) sl@0: { sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: if (BN_cmp(i, key->iqmp) != 0) sl@0: { sl@0: ret = 0; sl@0: RSAerr(RSA_F_RSA_CHECK_KEY, sl@0: RSA_R_IQMP_NOT_INVERSE_OF_Q); sl@0: } sl@0: } sl@0: sl@0: err: sl@0: if (i != NULL) BN_free(i); sl@0: if (j != NULL) BN_free(j); sl@0: if (k != NULL) BN_free(k); sl@0: if (l != NULL) BN_free(l); sl@0: if (m != NULL) BN_free(m); sl@0: if (ctx != NULL) BN_CTX_free(ctx); sl@0: return (ret); sl@0: }