os/ossrv/ssl/libcrypto/src/crypto/rsa/rsa_chk.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* crypto/rsa/rsa_chk.c  -*- Mode: C; c-file-style: "eay" -*- */
sl@0
     2
/* ====================================================================
sl@0
     3
 * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
sl@0
     4
 *
sl@0
     5
 * Redistribution and use in source and binary forms, with or without
sl@0
     6
 * modification, are permitted provided that the following conditions
sl@0
     7
 * are met:
sl@0
     8
 *
sl@0
     9
 * 1. Redistributions of source code must retain the above copyright
sl@0
    10
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    11
 *
sl@0
    12
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    13
 *    notice, this list of conditions and the following disclaimer in
sl@0
    14
 *    the documentation and/or other materials provided with the
sl@0
    15
 *    distribution.
sl@0
    16
 *
sl@0
    17
 * 3. All advertising materials mentioning features or use of this
sl@0
    18
 *    software must display the following acknowledgment:
sl@0
    19
 *    "This product includes software developed by the OpenSSL Project
sl@0
    20
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
sl@0
    21
 *
sl@0
    22
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    23
 *    endorse or promote products derived from this software without
sl@0
    24
 *    prior written permission. For written permission, please contact
sl@0
    25
 *    openssl-core@OpenSSL.org.
sl@0
    26
 *
sl@0
    27
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    28
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    29
 *    permission of the OpenSSL Project.
sl@0
    30
 *
sl@0
    31
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    32
 *    acknowledgment:
sl@0
    33
 *    "This product includes software developed by the OpenSSL Project
sl@0
    34
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
sl@0
    35
 *
sl@0
    36
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    37
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    38
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    39
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    40
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    41
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    42
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    43
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    44
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    45
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    46
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    47
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    48
 * ====================================================================
sl@0
    49
 */
sl@0
    50
sl@0
    51
#include <openssl/bn.h>
sl@0
    52
#include <openssl/err.h>
sl@0
    53
#include <openssl/rsa.h>
sl@0
    54
sl@0
    55
sl@0
    56
EXPORT_C int RSA_check_key(const RSA *key)
sl@0
    57
	{
sl@0
    58
	BIGNUM *i, *j, *k, *l, *m;
sl@0
    59
	BN_CTX *ctx;
sl@0
    60
	int r;
sl@0
    61
	int ret=1;
sl@0
    62
	
sl@0
    63
	i = BN_new();
sl@0
    64
	j = BN_new();
sl@0
    65
	k = BN_new();
sl@0
    66
	l = BN_new();
sl@0
    67
	m = BN_new();
sl@0
    68
	ctx = BN_CTX_new();
sl@0
    69
	if (i == NULL || j == NULL || k == NULL || l == NULL ||
sl@0
    70
		m == NULL || ctx == NULL)
sl@0
    71
		{
sl@0
    72
		ret = -1;
sl@0
    73
		RSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE);
sl@0
    74
		goto err;
sl@0
    75
		}
sl@0
    76
	
sl@0
    77
	/* p prime? */
sl@0
    78
	r = BN_is_prime_ex(key->p, BN_prime_checks, NULL, NULL);
sl@0
    79
	if (r != 1)
sl@0
    80
		{
sl@0
    81
		ret = r;
sl@0
    82
		if (r != 0)
sl@0
    83
			goto err;
sl@0
    84
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME);
sl@0
    85
		}
sl@0
    86
	
sl@0
    87
	/* q prime? */
sl@0
    88
	r = BN_is_prime_ex(key->q, BN_prime_checks, NULL, NULL);
sl@0
    89
	if (r != 1)
sl@0
    90
		{
sl@0
    91
		ret = r;
sl@0
    92
		if (r != 0)
sl@0
    93
			goto err;
sl@0
    94
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME);
sl@0
    95
		}
sl@0
    96
	
sl@0
    97
	/* n = p*q? */
sl@0
    98
	r = BN_mul(i, key->p, key->q, ctx);
sl@0
    99
	if (!r) { ret = -1; goto err; }
sl@0
   100
	
sl@0
   101
	if (BN_cmp(i, key->n) != 0)
sl@0
   102
		{
sl@0
   103
		ret = 0;
sl@0
   104
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q);
sl@0
   105
		}
sl@0
   106
	
sl@0
   107
	/* d*e = 1  mod lcm(p-1,q-1)? */
sl@0
   108
sl@0
   109
	r = BN_sub(i, key->p, BN_value_one());
sl@0
   110
	if (!r) { ret = -1; goto err; }
sl@0
   111
	r = BN_sub(j, key->q, BN_value_one());
sl@0
   112
	if (!r) { ret = -1; goto err; }
sl@0
   113
sl@0
   114
	/* now compute k = lcm(i,j) */
sl@0
   115
	r = BN_mul(l, i, j, ctx);
sl@0
   116
	if (!r) { ret = -1; goto err; }
sl@0
   117
	r = BN_gcd(m, i, j, ctx);
sl@0
   118
	if (!r) { ret = -1; goto err; }
sl@0
   119
	r = BN_div(k, NULL, l, m, ctx); /* remainder is 0 */
sl@0
   120
	if (!r) { ret = -1; goto err; }
sl@0
   121
sl@0
   122
	r = BN_mod_mul(i, key->d, key->e, k, ctx);
sl@0
   123
	if (!r) { ret = -1; goto err; }
sl@0
   124
sl@0
   125
	if (!BN_is_one(i))
sl@0
   126
		{
sl@0
   127
		ret = 0;
sl@0
   128
		RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1);
sl@0
   129
		}
sl@0
   130
	
sl@0
   131
	if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL)
sl@0
   132
		{
sl@0
   133
		/* dmp1 = d mod (p-1)? */
sl@0
   134
		r = BN_sub(i, key->p, BN_value_one());
sl@0
   135
		if (!r) { ret = -1; goto err; }
sl@0
   136
sl@0
   137
		r = BN_mod(j, key->d, i, ctx);
sl@0
   138
		if (!r) { ret = -1; goto err; }
sl@0
   139
sl@0
   140
		if (BN_cmp(j, key->dmp1) != 0)
sl@0
   141
			{
sl@0
   142
			ret = 0;
sl@0
   143
			RSAerr(RSA_F_RSA_CHECK_KEY,
sl@0
   144
				RSA_R_DMP1_NOT_CONGRUENT_TO_D);
sl@0
   145
			}
sl@0
   146
	
sl@0
   147
		/* dmq1 = d mod (q-1)? */    
sl@0
   148
		r = BN_sub(i, key->q, BN_value_one());
sl@0
   149
		if (!r) { ret = -1; goto err; }
sl@0
   150
	
sl@0
   151
		r = BN_mod(j, key->d, i, ctx);
sl@0
   152
		if (!r) { ret = -1; goto err; }
sl@0
   153
sl@0
   154
		if (BN_cmp(j, key->dmq1) != 0)
sl@0
   155
			{
sl@0
   156
			ret = 0;
sl@0
   157
			RSAerr(RSA_F_RSA_CHECK_KEY,
sl@0
   158
				RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
sl@0
   159
			}
sl@0
   160
	
sl@0
   161
		/* iqmp = q^-1 mod p? */
sl@0
   162
		if(!BN_mod_inverse(i, key->q, key->p, ctx))
sl@0
   163
			{
sl@0
   164
			ret = -1;
sl@0
   165
			goto err;
sl@0
   166
			}
sl@0
   167
sl@0
   168
		if (BN_cmp(i, key->iqmp) != 0)
sl@0
   169
			{
sl@0
   170
			ret = 0;
sl@0
   171
			RSAerr(RSA_F_RSA_CHECK_KEY,
sl@0
   172
				RSA_R_IQMP_NOT_INVERSE_OF_Q);
sl@0
   173
			}
sl@0
   174
		}
sl@0
   175
sl@0
   176
 err:
sl@0
   177
	if (i != NULL) BN_free(i);
sl@0
   178
	if (j != NULL) BN_free(j);
sl@0
   179
	if (k != NULL) BN_free(k);
sl@0
   180
	if (l != NULL) BN_free(l);
sl@0
   181
	if (m != NULL) BN_free(m);
sl@0
   182
	if (ctx != NULL) BN_CTX_free(ctx);
sl@0
   183
	return (ret);
sl@0
   184
	}