156083Skris/* crypto/rsa/rsa_chk.c  -*- Mode: C; c-file-style: "eay" -*- */
256083Skris/* ====================================================================
356083Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
456083Skris *
556083Skris * Redistribution and use in source and binary forms, with or without
656083Skris * modification, are permitted provided that the following conditions
756083Skris * are met:
856083Skris *
956083Skris * 1. Redistributions of source code must retain the above copyright
10296341Sdelphij *    notice, this list of conditions and the following disclaimer.
1156083Skris *
1256083Skris * 2. Redistributions in binary form must reproduce the above copyright
1356083Skris *    notice, this list of conditions and the following disclaimer in
1456083Skris *    the documentation and/or other materials provided with the
1556083Skris *    distribution.
1656083Skris *
1756083Skris * 3. All advertising materials mentioning features or use of this
1856083Skris *    software must display the following acknowledgment:
1956083Skris *    "This product includes software developed by the OpenSSL Project
2056083Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2156083Skris *
2256083Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2356083Skris *    endorse or promote products derived from this software without
2456083Skris *    prior written permission. For written permission, please contact
2556083Skris *    openssl-core@OpenSSL.org.
2656083Skris *
2756083Skris * 5. Products derived from this software may not be called "OpenSSL"
2856083Skris *    nor may "OpenSSL" appear in their names without prior written
2956083Skris *    permission of the OpenSSL Project.
3056083Skris *
3156083Skris * 6. Redistributions of any form whatsoever must retain the following
3256083Skris *    acknowledgment:
3356083Skris *    "This product includes software developed by the OpenSSL Project
3456083Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3556083Skris *
3656083Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
3756083Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3856083Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3956083Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4056083Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4156083Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4256083Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4356083Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4456083Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4556083Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4656083Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4756083Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
4856083Skris * ====================================================================
4956083Skris */
5056083Skris
5156083Skris#include <openssl/bn.h>
5256083Skris#include <openssl/err.h>
5356083Skris#include <openssl/rsa.h>
5456083Skris
55109998Smarkmint RSA_check_key(const RSA *key)
56296341Sdelphij{
57296341Sdelphij    BIGNUM *i, *j, *k, *l, *m;
58296341Sdelphij    BN_CTX *ctx;
59296341Sdelphij    int r;
60296341Sdelphij    int ret = 1;
61264331Sjkim
62296341Sdelphij    if (!key->p || !key->q || !key->n || !key->e || !key->d) {
63296341Sdelphij        RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_VALUE_MISSING);
64296341Sdelphij        return 0;
65296341Sdelphij    }
6656083Skris
67296341Sdelphij    i = BN_new();
68296341Sdelphij    j = BN_new();
69296341Sdelphij    k = BN_new();
70296341Sdelphij    l = BN_new();
71296341Sdelphij    m = BN_new();
72296341Sdelphij    ctx = BN_CTX_new();
73296341Sdelphij    if (i == NULL || j == NULL || k == NULL || l == NULL ||
74296341Sdelphij        m == NULL || ctx == NULL) {
75296341Sdelphij        ret = -1;
76296341Sdelphij        RSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE);
77296341Sdelphij        goto err;
78296341Sdelphij    }
7956083Skris
80296341Sdelphij    /* p prime? */
81296341Sdelphij    r = BN_is_prime_ex(key->p, BN_prime_checks, NULL, NULL);
82296341Sdelphij    if (r != 1) {
83296341Sdelphij        ret = r;
84296341Sdelphij        if (r != 0)
85296341Sdelphij            goto err;
86296341Sdelphij        RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME);
87296341Sdelphij    }
8856083Skris
89296341Sdelphij    /* q prime? */
90296341Sdelphij    r = BN_is_prime_ex(key->q, BN_prime_checks, NULL, NULL);
91296341Sdelphij    if (r != 1) {
92296341Sdelphij        ret = r;
93296341Sdelphij        if (r != 0)
94296341Sdelphij            goto err;
95296341Sdelphij        RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME);
96296341Sdelphij    }
9756083Skris
98296341Sdelphij    /* n = p*q? */
99296341Sdelphij    r = BN_mul(i, key->p, key->q, ctx);
100296341Sdelphij    if (!r) {
101296341Sdelphij        ret = -1;
102296341Sdelphij        goto err;
103296341Sdelphij    }
10456083Skris
105296341Sdelphij    if (BN_cmp(i, key->n) != 0) {
106296341Sdelphij        ret = 0;
107296341Sdelphij        RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q);
108296341Sdelphij    }
10956083Skris
110296341Sdelphij    /* d*e = 1  mod lcm(p-1,q-1)? */
11156083Skris
112296341Sdelphij    r = BN_sub(i, key->p, BN_value_one());
113296341Sdelphij    if (!r) {
114296341Sdelphij        ret = -1;
115296341Sdelphij        goto err;
116296341Sdelphij    }
117296341Sdelphij    r = BN_sub(j, key->q, BN_value_one());
118296341Sdelphij    if (!r) {
119296341Sdelphij        ret = -1;
120296341Sdelphij        goto err;
121296341Sdelphij    }
12256083Skris
123296341Sdelphij    /* now compute k = lcm(i,j) */
124296341Sdelphij    r = BN_mul(l, i, j, ctx);
125296341Sdelphij    if (!r) {
126296341Sdelphij        ret = -1;
127296341Sdelphij        goto err;
128296341Sdelphij    }
129296341Sdelphij    r = BN_gcd(m, i, j, ctx);
130296341Sdelphij    if (!r) {
131296341Sdelphij        ret = -1;
132296341Sdelphij        goto err;
133296341Sdelphij    }
134296341Sdelphij    r = BN_div(k, NULL, l, m, ctx); /* remainder is 0 */
135296341Sdelphij    if (!r) {
136296341Sdelphij        ret = -1;
137296341Sdelphij        goto err;
138296341Sdelphij    }
13956083Skris
140296341Sdelphij    r = BN_mod_mul(i, key->d, key->e, k, ctx);
141296341Sdelphij    if (!r) {
142296341Sdelphij        ret = -1;
143296341Sdelphij        goto err;
144296341Sdelphij    }
145296341Sdelphij
146296341Sdelphij    if (!BN_is_one(i)) {
147296341Sdelphij        ret = 0;
148296341Sdelphij        RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1);
149296341Sdelphij    }
150296341Sdelphij
151296341Sdelphij    if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {
152296341Sdelphij        /* dmp1 = d mod (p-1)? */
153296341Sdelphij        r = BN_sub(i, key->p, BN_value_one());
154296341Sdelphij        if (!r) {
155296341Sdelphij            ret = -1;
156296341Sdelphij            goto err;
157296341Sdelphij        }
158296341Sdelphij
159296341Sdelphij        r = BN_mod(j, key->d, i, ctx);
160296341Sdelphij        if (!r) {
161296341Sdelphij            ret = -1;
162296341Sdelphij            goto err;
163296341Sdelphij        }
164296341Sdelphij
165296341Sdelphij        if (BN_cmp(j, key->dmp1) != 0) {
166296341Sdelphij            ret = 0;
167296341Sdelphij            RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_DMP1_NOT_CONGRUENT_TO_D);
168296341Sdelphij        }
169296341Sdelphij
170296341Sdelphij        /* dmq1 = d mod (q-1)? */
171296341Sdelphij        r = BN_sub(i, key->q, BN_value_one());
172296341Sdelphij        if (!r) {
173296341Sdelphij            ret = -1;
174296341Sdelphij            goto err;
175296341Sdelphij        }
176296341Sdelphij
177296341Sdelphij        r = BN_mod(j, key->d, i, ctx);
178296341Sdelphij        if (!r) {
179296341Sdelphij            ret = -1;
180296341Sdelphij            goto err;
181296341Sdelphij        }
182296341Sdelphij
183296341Sdelphij        if (BN_cmp(j, key->dmq1) != 0) {
184296341Sdelphij            ret = 0;
185296341Sdelphij            RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
186296341Sdelphij        }
187296341Sdelphij
188296341Sdelphij        /* iqmp = q^-1 mod p? */
189296341Sdelphij        if (!BN_mod_inverse(i, key->q, key->p, ctx)) {
190296341Sdelphij            ret = -1;
191296341Sdelphij            goto err;
192296341Sdelphij        }
193296341Sdelphij
194296341Sdelphij        if (BN_cmp(i, key->iqmp) != 0) {
195296341Sdelphij            ret = 0;
196296341Sdelphij            RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_IQMP_NOT_INVERSE_OF_Q);
197296341Sdelphij        }
198296341Sdelphij    }
199296341Sdelphij
20056083Skris err:
201296341Sdelphij    if (i != NULL)
202296341Sdelphij        BN_free(i);
203296341Sdelphij    if (j != NULL)
204296341Sdelphij        BN_free(j);
205296341Sdelphij    if (k != NULL)
206296341Sdelphij        BN_free(k);
207296341Sdelphij    if (l != NULL)
208296341Sdelphij        BN_free(l);
209296341Sdelphij    if (m != NULL)
210296341Sdelphij        BN_free(m);
211296341Sdelphij    if (ctx != NULL)
212296341Sdelphij        BN_CTX_free(ctx);
213296341Sdelphij    return (ret);
214296341Sdelphij}
215