rsa_chk.c 5.0 KB
Newer Older
B
typo  
Bodo Möller 已提交
1
/* crypto/rsa/rsa_chk.c  -*- Mode: C; c-file-style: "eay" -*- */
B
Bodo Möller 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/* ====================================================================
 * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    openssl-core@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */

#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/rsa.h>


int RSA_check_key(RSA *key)
	{
	BIGNUM *i, *j, *k, *l, *m;
	BN_CTX *ctx;
B
Bodo Möller 已提交
60
	int r;
B
Bodo Möller 已提交
61 62 63 64 65 66 67 68 69 70 71
	int ret=1;
	
	i = BN_new();
	j = BN_new();
	k = BN_new();
	l = BN_new();
	m = BN_new();
	ctx = BN_CTX_new();
	if (i == NULL || j == NULL || k == NULL || l == NULL ||
		m == NULL || ctx == NULL)
		{
B
Bodo Möller 已提交
72
		ret = -1;
B
Bodo Möller 已提交
73 74 75 76 77
		RSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE);
		goto err;
		}
	
	/* p prime? */
B
Bodo Möller 已提交
78 79
	r = BN_is_prime(key->p, BN_prime_checks, NULL, NULL, NULL);
	if (r != 1)
B
Bodo Möller 已提交
80
		{
B
Bodo Möller 已提交
81 82
		ret = r;
		if (r != 0)
B
Bodo Möller 已提交
83 84 85 86 87
			goto err;
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME);
		}
	
	/* q prime? */
B
Bodo Möller 已提交
88 89
	r = BN_is_prime(key->q, BN_prime_checks, NULL, NULL, NULL);
	if (r != 1)
B
Bodo Möller 已提交
90
		{
B
Bodo Möller 已提交
91 92
		ret = r;
		if (r != 0)
B
Bodo Möller 已提交
93 94 95 96 97
			goto err;
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME);
		}
	
	/* n = p*q? */
B
Bodo Möller 已提交
98 99 100
	r = BN_mul(i, key->p, key->q, ctx);
	if (!r) { ret = -1; goto err; }
	
B
Bodo Möller 已提交
101 102 103
	if (BN_cmp(i, key->n) != 0)
		{
		ret = 0;
B
Bodo Möller 已提交
104
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q);
B
Bodo Möller 已提交
105 106 107
		}
	
	/* dmp1 = d mod (p-1)? */
B
Bodo Möller 已提交
108 109 110 111 112 113
	r = BN_sub(i, key->p, BN_value_one());
	if (!r) { ret = -1; goto err; }

	r = BN_mod(j, key->d, i, ctx);
	if (!r) { ret = -1; goto err; }

B
Bodo Möller 已提交
114 115 116 117 118 119 120
	if (BN_cmp(j, key->dmp1) != 0)
		{
		ret = 0;
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_DMP1_NOT_CONGRUENT_TO_D);
		}
	
	/* dmq1 = d mod (q-1)? */    
B
Bodo Möller 已提交
121 122 123 124 125 126
	r = BN_sub(i, key->q, BN_value_one());
	if (!r) { ret = -1; goto err; }
	
	r = BN_mod(j, key->d, i, ctx);
	if (!r) { ret = -1; goto err; }

B
Bodo Möller 已提交
127 128 129 130 131 132 133
	if (BN_cmp(j, key->dmq1) != 0)
		{
		ret = 0;
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
		}
	
	/* iqmp = q^-1 mod p? */
B
Bodo Möller 已提交
134 135 136
	r = BN_mod_inverse(i, key->q, key->p, ctx);
	if (!r) { ret = -1; goto err; }

B
Bodo Möller 已提交
137 138 139 140 141 142 143
	if (BN_cmp(i, key->iqmp) != 0)
		{
		ret = 0;
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_IQMP_NOT_INVERSE_OF_Q);
		}
	
	/* d*e = 1  mod lcm(p-1,q-1)? */
B
Bodo Möller 已提交
144 145 146 147 148 149

	r = BN_sub(i, key->p, BN_value_one());
	if (!r) { ret = -1; goto err; }
	r = BN_sub(j, key->q, BN_value_one());
	if (!r) { ret = -1; goto err; }

B
Bodo Möller 已提交
150
	/* now compute k = lcm(i,j) */
B
Bodo Möller 已提交
151 152 153 154 155 156 157 158 159 160
	r = BN_mul(l, i, j, ctx);
	if (!r) { ret = -1; goto err; }
	r = BN_gcd(m, i, j, ctx);
	if (!r) { ret = -1; goto err; }
	r = BN_div(k, NULL, l, m, ctx); /* remainder is 0 */
	if (!r) { ret = -1; goto err; }

	r = BN_mod_mul(i, key->d, key->e, k, ctx);
	if (!r) { ret = -1; goto err; }

B
Bodo Möller 已提交
161 162 163
	if (!BN_is_one(i))
		{
		ret = 0;
B
Bodo Möller 已提交
164
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1);
B
Bodo Möller 已提交
165 166 167 168 169 170 171 172 173 174 175
		}
	
 err:
	if (i != NULL) BN_free(i);
	if (j != NULL) BN_free(j);
	if (k != NULL) BN_free(k);
	if (l != NULL) BN_free(l);
	if (m != NULL) BN_free(m);
	if (ctx != NULL) BN_CTX_free(ctx);
	return (ret);
	}