2 * Copyright 1999-2023 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
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include <openssl/bn.h>
17 #include <openssl/err.h>
18 #include "crypto/rsa.h"
19 #include "rsa_local.h"
22 static int rsa_validate_keypair_multiprime(const RSA
*key
, BN_GENCB
*cb
)
24 BIGNUM
*i
, *j
, *k
, *l
, *m
;
26 int ret
= 1, ex_primes
= 0, idx
;
27 RSA_PRIME_INFO
*pinfo
;
29 if (key
->p
== NULL
|| key
->q
== NULL
|| key
->n
== NULL
30 || key
->e
== NULL
|| key
->d
== NULL
) {
31 ERR_raise(ERR_LIB_RSA
, RSA_R_VALUE_MISSING
);
36 if (key
->version
== RSA_ASN1_VERSION_MULTI
) {
37 ex_primes
= sk_RSA_PRIME_INFO_num(key
->prime_infos
);
39 || (ex_primes
+ 2) > ossl_rsa_multip_cap(BN_num_bits(key
->n
))) {
40 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_MULTI_PRIME_KEY
);
50 ctx
= BN_CTX_new_ex(key
->libctx
);
51 if (i
== NULL
|| j
== NULL
|| k
== NULL
|| l
== NULL
52 || m
== NULL
|| ctx
== NULL
) {
54 ERR_raise(ERR_LIB_RSA
, ERR_R_BN_LIB
);
58 if (BN_is_one(key
->e
)) {
60 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_E_VALUE
);
62 if (!BN_is_odd(key
->e
)) {
64 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_E_VALUE
);
68 if (BN_check_prime(key
->p
, ctx
, cb
) != 1) {
70 ERR_raise(ERR_LIB_RSA
, RSA_R_P_NOT_PRIME
);
74 if (BN_check_prime(key
->q
, ctx
, cb
) != 1) {
76 ERR_raise(ERR_LIB_RSA
, RSA_R_Q_NOT_PRIME
);
80 for (idx
= 0; idx
< ex_primes
; idx
++) {
81 pinfo
= sk_RSA_PRIME_INFO_value(key
->prime_infos
, idx
);
82 if (BN_check_prime(pinfo
->r
, ctx
, cb
) != 1) {
84 ERR_raise(ERR_LIB_RSA
, RSA_R_MP_R_NOT_PRIME
);
88 /* n = p*q * r_3...r_i? */
89 if (!BN_mul(i
, key
->p
, key
->q
, ctx
)) {
93 for (idx
= 0; idx
< ex_primes
; idx
++) {
94 pinfo
= sk_RSA_PRIME_INFO_value(key
->prime_infos
, idx
);
95 if (!BN_mul(i
, i
, pinfo
->r
, ctx
)) {
100 if (BN_cmp(i
, key
->n
) != 0) {
103 ERR_raise(ERR_LIB_RSA
, RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES
);
105 ERR_raise(ERR_LIB_RSA
, RSA_R_N_DOES_NOT_EQUAL_P_Q
);
108 /* d*e = 1 mod \lambda(n)? */
109 if (!BN_sub(i
, key
->p
, BN_value_one())) {
113 if (!BN_sub(j
, key
->q
, BN_value_one())) {
118 /* now compute k = \lambda(n) = LCM(i, j, r_3 - 1...) */
119 if (!BN_mul(l
, i
, j
, ctx
)) {
123 if (!BN_gcd(m
, i
, j
, ctx
)) {
127 if (!BN_div(m
, NULL
, l
, m
, ctx
)) { /* remainder is 0 */
131 for (idx
= 0; idx
< ex_primes
; idx
++) {
132 pinfo
= sk_RSA_PRIME_INFO_value(key
->prime_infos
, idx
);
133 if (!BN_sub(k
, pinfo
->r
, BN_value_one())) {
137 if (!BN_mul(l
, m
, k
, ctx
)) {
141 if (!BN_gcd(m
, m
, k
, ctx
)) {
145 if (!BN_div(m
, NULL
, l
, m
, ctx
)) { /* remainder is 0 */
150 if (!BN_mod_mul(i
, key
->d
, key
->e
, m
, ctx
)) {
157 ERR_raise(ERR_LIB_RSA
, RSA_R_D_E_NOT_CONGRUENT_TO_1
);
160 if (key
->dmp1
!= NULL
&& key
->dmq1
!= NULL
&& key
->iqmp
!= NULL
) {
161 /* dmp1 = d mod (p-1)? */
162 if (!BN_sub(i
, key
->p
, BN_value_one())) {
166 if (!BN_mod(j
, key
->d
, i
, ctx
)) {
170 if (BN_cmp(j
, key
->dmp1
) != 0) {
172 ERR_raise(ERR_LIB_RSA
, RSA_R_DMP1_NOT_CONGRUENT_TO_D
);
175 /* dmq1 = d mod (q-1)? */
176 if (!BN_sub(i
, key
->q
, BN_value_one())) {
180 if (!BN_mod(j
, key
->d
, i
, ctx
)) {
184 if (BN_cmp(j
, key
->dmq1
) != 0) {
186 ERR_raise(ERR_LIB_RSA
, RSA_R_DMQ1_NOT_CONGRUENT_TO_D
);
189 /* iqmp = q^-1 mod p? */
190 if (!BN_mod_inverse(i
, key
->q
, key
->p
, ctx
)) {
194 if (BN_cmp(i
, key
->iqmp
) != 0) {
196 ERR_raise(ERR_LIB_RSA
, RSA_R_IQMP_NOT_INVERSE_OF_Q
);
200 for (idx
= 0; idx
< ex_primes
; idx
++) {
201 pinfo
= sk_RSA_PRIME_INFO_value(key
->prime_infos
, idx
);
202 /* d_i = d mod (r_i - 1)? */
203 if (!BN_sub(i
, pinfo
->r
, BN_value_one())) {
207 if (!BN_mod(j
, key
->d
, i
, ctx
)) {
211 if (BN_cmp(j
, pinfo
->d
) != 0) {
213 ERR_raise(ERR_LIB_RSA
, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D
);
215 /* t_i = R_i ^ -1 mod r_i ? */
216 if (!BN_mod_inverse(i
, pinfo
->pp
, pinfo
->r
, ctx
)) {
220 if (BN_cmp(i
, pinfo
->t
) != 0) {
222 ERR_raise(ERR_LIB_RSA
, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R
);
235 #endif /* FIPS_MODULE */
237 int ossl_rsa_validate_public(const RSA
*key
)
239 return ossl_rsa_sp800_56b_check_public(key
);
242 int ossl_rsa_validate_private(const RSA
*key
)
244 return ossl_rsa_sp800_56b_check_private(key
);
247 int ossl_rsa_validate_pairwise(const RSA
*key
)
250 return ossl_rsa_sp800_56b_check_keypair(key
, NULL
, -1, RSA_bits(key
));
252 return rsa_validate_keypair_multiprime(key
, NULL
) > 0;
256 int RSA_check_key(const RSA
*key
)
258 return RSA_check_key_ex(key
, NULL
);
261 int RSA_check_key_ex(const RSA
*key
, BN_GENCB
*cb
)
264 return ossl_rsa_validate_public(key
)
265 && ossl_rsa_validate_private(key
)
266 && ossl_rsa_validate_pairwise(key
);
268 return rsa_validate_keypair_multiprime(key
, cb
);
269 #endif /* FIPS_MODULE */