2 * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/bn.h>
11 #include <openssl/err.h>
14 int RSA_check_key(const RSA *key)
16 return RSA_check_key_ex(key, NULL);
20 * NOTE: Key validation requires separate checks to be able to be accessed
21 * individually. These should be visible from the PKEY API..
22 * See rsa_sp800_56b_check_public, rsa_sp800_56b_check_private and
23 * rsa_sp800_56b_check_keypair.
25 int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)
28 if (!(rsa_sp800_56b_check_public(key)
29 && rsa_sp800_56b_check_private(key)
30 && rsa_sp800_56b_check_keypair(key, NULL, -1, RSA_bits(key))
34 BIGNUM *i, *j, *k, *l, *m;
36 int ret = 1, ex_primes = 0, idx;
37 RSA_PRIME_INFO *pinfo;
39 if (key->p == NULL || key->q == NULL || key->n == NULL
40 || key->e == NULL || key->d == NULL) {
41 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);
46 if (key->version == RSA_ASN1_VERSION_MULTI) {
47 ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos);
49 || (ex_primes + 2) > rsa_multip_cap(BN_num_bits(key->n))) {
50 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_INVALID_MULTI_PRIME_KEY);
61 if (i == NULL || j == NULL || k == NULL || l == NULL
62 || m == NULL || ctx == NULL) {
64 RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);
68 if (BN_is_one(key->e)) {
70 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);
72 if (!BN_is_odd(key->e)) {
74 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);
78 if (BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb) != 1) {
80 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);
84 if (BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb) != 1) {
86 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);
90 for (idx = 0; idx < ex_primes; idx++) {
91 pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
92 if (BN_is_prime_ex(pinfo->r, BN_prime_checks, NULL, cb) != 1) {
94 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_R_NOT_PRIME);
98 /* n = p*q * r_3...r_i? */
99 if (!BN_mul(i, key->p, key->q, ctx)) {
103 for (idx = 0; idx < ex_primes; idx++) {
104 pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
105 if (!BN_mul(i, i, pinfo->r, ctx)) {
110 if (BN_cmp(i, key->n) != 0) {
113 RSAerr(RSA_F_RSA_CHECK_KEY_EX,
114 RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES);
116 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);
119 /* d*e = 1 mod \lambda(n)? */
120 if (!BN_sub(i, key->p, BN_value_one())) {
124 if (!BN_sub(j, key->q, BN_value_one())) {
129 /* now compute k = \lambda(n) = LCM(i, j, r_3 - 1...) */
130 if (!BN_mul(l, i, j, ctx)) {
134 if (!BN_gcd(m, i, j, ctx)) {
138 for (idx = 0; idx < ex_primes; idx++) {
139 pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
140 if (!BN_sub(k, pinfo->r, BN_value_one())) {
144 if (!BN_mul(l, l, k, ctx)) {
148 if (!BN_gcd(m, m, k, ctx)) {
153 if (!BN_div(k, NULL, l, m, ctx)) { /* remainder is 0 */
157 if (!BN_mod_mul(i, key->d, key->e, k, ctx)) {
164 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);
167 if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {
168 /* dmp1 = d mod (p-1)? */
169 if (!BN_sub(i, key->p, BN_value_one())) {
173 if (!BN_mod(j, key->d, i, ctx)) {
177 if (BN_cmp(j, key->dmp1) != 0) {
179 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);
182 /* dmq1 = d mod (q-1)? */
183 if (!BN_sub(i, key->q, BN_value_one())) {
187 if (!BN_mod(j, key->d, i, ctx)) {
191 if (BN_cmp(j, key->dmq1) != 0) {
193 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
196 /* iqmp = q^-1 mod p? */
197 if (!BN_mod_inverse(i, key->q, key->p, ctx)) {
201 if (BN_cmp(i, key->iqmp) != 0) {
203 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);
207 for (idx = 0; idx < ex_primes; idx++) {
208 pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
209 /* d_i = d mod (r_i - 1)? */
210 if (!BN_sub(i, pinfo->r, BN_value_one())) {
214 if (!BN_mod(j, key->d, i, ctx)) {
218 if (BN_cmp(j, pinfo->d) != 0) {
220 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D);
222 /* t_i = R_i ^ -1 mod r_i ? */
223 if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) {
227 if (BN_cmp(i, pinfo->t) != 0) {
229 RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R);
241 #endif /* FIPS_MODE */