2018-07-05 07:28:51 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2018-07-05 07:28:51 +08:00
|
|
|
* Copyright (c) 2018-2019, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
*
|
2020-04-27 12:59:50 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2018-07-05 07:28:51 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/bn.h>
|
2020-06-17 09:33:16 +08:00
|
|
|
#include <openssl/core.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/bn.h"
|
2020-03-07 05:47:58 +08:00
|
|
|
#include "crypto/security_bits.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "rsa_local.h"
|
2018-07-05 07:28:51 +08:00
|
|
|
|
|
|
|
#define RSA_FIPS1864_MIN_KEYGEN_KEYSIZE 2048
|
|
|
|
#define RSA_FIPS1864_MIN_KEYGEN_STRENGTH 112
|
|
|
|
#define RSA_FIPS1864_MAX_KEYGEN_STRENGTH 256
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate probable primes 'p' & 'q'. See FIPS 186-4 Section B.3.6
|
|
|
|
* "Generation of Probable Primes with Conditions Based on Auxiliary Probable
|
|
|
|
* Primes".
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* rsa Object used to store primes p & q.
|
2020-06-17 09:33:16 +08:00
|
|
|
* test Object used for CAVS testing only.that contains..
|
|
|
|
* p1, p2 The returned auxiliary primes for p.
|
|
|
|
* If NULL they are not returned.
|
|
|
|
* Xpout An optionally returned random number used during generation of p.
|
|
|
|
* Xp An optional passed in value (that is random number used during
|
|
|
|
* generation of p).
|
|
|
|
* Xp1, Xp2 Optionally passed in randomly generated numbers from which
|
|
|
|
* auxiliary primes p1 & p2 are calculated. If NULL these values
|
|
|
|
* are generated internally.
|
|
|
|
* q1, q2 The returned auxiliary primes for q.
|
|
|
|
* If NULL they are not returned.
|
|
|
|
* Xqout An optionally returned random number used during generation of q.
|
|
|
|
* Xq An optional passed in value (that is random number used during
|
|
|
|
* generation of q).
|
|
|
|
* Xq1, Xq2 Optionally passed in randomly generated numbers from which
|
|
|
|
* auxiliary primes q1 & q2 are calculated. If NULL these values
|
|
|
|
* are generated internally.
|
2018-07-05 07:28:51 +08:00
|
|
|
* nbits The key size in bits (The size of the modulus n).
|
|
|
|
* e The public exponent.
|
|
|
|
* ctx A BN_CTX object.
|
|
|
|
* cb An optional BIGNUM callback.
|
|
|
|
* Returns: 1 if successful, or 0 otherwise.
|
|
|
|
* Notes:
|
|
|
|
* p1, p2, q1, q2, Xpout, Xqout are returned if they are not NULL.
|
|
|
|
* Xp, Xp1, Xp2, Xq, Xq1, Xq2 are optionally passed in.
|
|
|
|
* (Required for CAVS testing).
|
|
|
|
*/
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
int ossl_rsa_fips186_4_gen_prob_primes(RSA *rsa, RSA_ACVP_TEST *test,
|
|
|
|
int nbits, const BIGNUM *e, BN_CTX *ctx,
|
|
|
|
BN_GENCB *cb)
|
2018-07-05 07:28:51 +08:00
|
|
|
{
|
|
|
|
int ret = 0, ok;
|
2020-06-17 09:33:16 +08:00
|
|
|
/* Temp allocated BIGNUMS */
|
2018-07-05 07:28:51 +08:00
|
|
|
BIGNUM *Xpo = NULL, *Xqo = NULL, *tmp = NULL;
|
2020-06-17 09:33:16 +08:00
|
|
|
/* Intermediate BIGNUMS that can be returned for testing */
|
|
|
|
BIGNUM *p1 = NULL, *p2 = NULL;
|
|
|
|
BIGNUM *q1 = NULL, *q2 = NULL;
|
|
|
|
/* Intermediate BIGNUMS that can be input for testing */
|
|
|
|
BIGNUM *Xpout = NULL, *Xqout = NULL;
|
|
|
|
BIGNUM *Xp = NULL, *Xp1 = NULL, *Xp2 = NULL;
|
|
|
|
BIGNUM *Xq = NULL, *Xq1 = NULL, *Xq2 = NULL;
|
|
|
|
|
|
|
|
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
|
|
|
|
if (test != NULL) {
|
|
|
|
Xp1 = test->Xp1;
|
|
|
|
Xp2 = test->Xp2;
|
|
|
|
Xq1 = test->Xq1;
|
|
|
|
Xq2 = test->Xq2;
|
|
|
|
Xp = test->Xp;
|
|
|
|
Xq = test->Xq;
|
|
|
|
p1 = test->p1;
|
|
|
|
p2 = test->p2;
|
|
|
|
q1 = test->q1;
|
|
|
|
q2 = test->q2;
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-05 07:28:51 +08:00
|
|
|
|
|
|
|
/* (Step 1) Check key length
|
|
|
|
* NOTE: SP800-131A Rev1 Disallows key lengths of < 2048 bits for RSA
|
|
|
|
* Signature Generation and Key Agree/Transport.
|
|
|
|
*/
|
|
|
|
if (nbits < RSA_FIPS1864_MIN_KEYGEN_KEYSIZE) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
|
2018-07-05 07:28:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
if (!ossl_rsa_check_public_exponent(e)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
|
2019-03-25 07:52:28 +08:00
|
|
|
return 0;
|
2018-07-05 07:28:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (Step 3) Determine strength and check rand generator strength is ok -
|
|
|
|
* this step is redundant because the generator always returns a higher
|
|
|
|
* strength than is required.
|
|
|
|
*/
|
|
|
|
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
tmp = BN_CTX_get(ctx);
|
|
|
|
Xpo = (Xpout != NULL) ? Xpout : BN_CTX_get(ctx);
|
|
|
|
Xqo = (Xqout != NULL) ? Xqout : BN_CTX_get(ctx);
|
|
|
|
if (tmp == NULL || Xpo == NULL || Xqo == NULL)
|
|
|
|
goto err;
|
2020-06-17 01:12:13 +08:00
|
|
|
BN_set_flags(Xpo, BN_FLG_CONSTTIME);
|
|
|
|
BN_set_flags(Xqo, BN_FLG_CONSTTIME);
|
2018-07-05 07:28:51 +08:00
|
|
|
|
|
|
|
if (rsa->p == NULL)
|
|
|
|
rsa->p = BN_secure_new();
|
|
|
|
if (rsa->q == NULL)
|
|
|
|
rsa->q = BN_secure_new();
|
|
|
|
if (rsa->p == NULL || rsa->q == NULL)
|
|
|
|
goto err;
|
2020-06-17 01:12:13 +08:00
|
|
|
BN_set_flags(rsa->p, BN_FLG_CONSTTIME);
|
|
|
|
BN_set_flags(rsa->q, BN_FLG_CONSTTIME);
|
2018-07-05 07:28:51 +08:00
|
|
|
|
|
|
|
/* (Step 4) Generate p, Xp */
|
2021-02-19 17:15:41 +08:00
|
|
|
if (!ossl_bn_rsa_fips186_4_gen_prob_primes(rsa->p, Xpo, p1, p2, Xp, Xp1, Xp2,
|
|
|
|
nbits, e, ctx, cb))
|
2018-07-05 07:28:51 +08:00
|
|
|
goto err;
|
|
|
|
for(;;) {
|
|
|
|
/* (Step 5) Generate q, Xq*/
|
2021-02-19 17:15:41 +08:00
|
|
|
if (!ossl_bn_rsa_fips186_4_gen_prob_primes(rsa->q, Xqo, q1, q2, Xq, Xq1,
|
|
|
|
Xq2, nbits, e, ctx, cb))
|
2018-07-05 07:28:51 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* (Step 6) |Xp - Xq| > 2^(nbitlen/2 - 100) */
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
ok = ossl_rsa_check_pminusq_diff(tmp, Xpo, Xqo, nbits);
|
2018-07-05 07:28:51 +08:00
|
|
|
if (ok < 0)
|
|
|
|
goto err;
|
|
|
|
if (ok == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* (Step 6) |p - q| > 2^(nbitlen/2 - 100) */
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
ok = ossl_rsa_check_pminusq_diff(tmp, rsa->p, rsa->q, nbits);
|
2018-07-05 07:28:51 +08:00
|
|
|
if (ok < 0)
|
|
|
|
goto err;
|
|
|
|
if (ok == 0)
|
|
|
|
continue;
|
|
|
|
break; /* successfully finished */
|
|
|
|
}
|
2019-10-16 03:31:45 +08:00
|
|
|
rsa->dirty_cnt++;
|
2018-07-05 07:28:51 +08:00
|
|
|
ret = 1;
|
|
|
|
err:
|
|
|
|
/* Zeroize any internally generated values that are not returned */
|
|
|
|
if (Xpo != Xpout)
|
|
|
|
BN_clear(Xpo);
|
|
|
|
if (Xqo != Xqout)
|
|
|
|
BN_clear(Xqo);
|
|
|
|
BN_clear(tmp);
|
|
|
|
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validates the RSA key size based on the target strength.
|
|
|
|
* See SP800-56Br1 6.3.1.1 (Steps 1a-1b)
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* nbits The key size in bits.
|
|
|
|
* strength The target strength in bits. -1 means the target
|
|
|
|
* strength is unknown.
|
|
|
|
* Returns: 1 if the key size matches the target strength, or 0 otherwise.
|
|
|
|
*/
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
int ossl_rsa_sp800_56b_validate_strength(int nbits, int strength)
|
2018-07-05 07:28:51 +08:00
|
|
|
{
|
2021-02-25 07:08:54 +08:00
|
|
|
int s = (int)ossl_ifc_ffc_compute_security_bits(nbits);
|
|
|
|
|
2020-06-10 06:59:56 +08:00
|
|
|
#ifdef FIPS_MODULE
|
2018-07-05 07:28:51 +08:00
|
|
|
if (s < RSA_FIPS1864_MIN_KEYGEN_STRENGTH
|
|
|
|
|| s > RSA_FIPS1864_MAX_KEYGEN_STRENGTH) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MODULUS);
|
2018-07-05 07:28:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-10 06:59:56 +08:00
|
|
|
#endif
|
2018-07-05 07:28:51 +08:00
|
|
|
if (strength != -1 && s != strength) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_STRENGTH);
|
2018-07-05 07:28:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Using p & q, calculate other required parameters such as n, d.
|
|
|
|
* as well as the CRT parameters dP, dQ, qInv.
|
|
|
|
*
|
|
|
|
* See SP800-56Br1
|
|
|
|
* 6.3.1.1 rsakpg1 - basic (Steps 3-4)
|
|
|
|
* 6.3.1.3 rsakpg1 - crt (Step 5)
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* rsa An rsa object.
|
|
|
|
* nbits The key size.
|
|
|
|
* e The public exponent.
|
|
|
|
* ctx A BN_CTX object.
|
|
|
|
* Notes:
|
|
|
|
* There is a small chance that the generated d will be too small.
|
|
|
|
* Returns: -1 = error,
|
|
|
|
* 0 = d is too small,
|
|
|
|
* 1 = success.
|
|
|
|
*/
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
int ossl_rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
|
|
|
|
const BIGNUM *e, BN_CTX *ctx)
|
2018-07-05 07:28:51 +08:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
BIGNUM *p1, *q1, *lcm, *p1q1, *gcd;
|
|
|
|
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
p1 = BN_CTX_get(ctx);
|
|
|
|
q1 = BN_CTX_get(ctx);
|
|
|
|
lcm = BN_CTX_get(ctx);
|
|
|
|
p1q1 = BN_CTX_get(ctx);
|
|
|
|
gcd = BN_CTX_get(ctx);
|
|
|
|
if (gcd == NULL)
|
|
|
|
goto err;
|
|
|
|
|
2020-06-17 01:12:13 +08:00
|
|
|
BN_set_flags(p1, BN_FLG_CONSTTIME);
|
|
|
|
BN_set_flags(q1, BN_FLG_CONSTTIME);
|
|
|
|
BN_set_flags(lcm, BN_FLG_CONSTTIME);
|
|
|
|
BN_set_flags(p1q1, BN_FLG_CONSTTIME);
|
|
|
|
BN_set_flags(gcd, BN_FLG_CONSTTIME);
|
|
|
|
|
2018-07-05 07:28:51 +08:00
|
|
|
/* LCM((p-1, q-1)) */
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
if (ossl_rsa_get_lcm(ctx, rsa->p, rsa->q, lcm, gcd, p1, q1, p1q1) != 1)
|
2018-07-05 07:28:51 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* copy e */
|
|
|
|
BN_free(rsa->e);
|
|
|
|
rsa->e = BN_dup(e);
|
|
|
|
if (rsa->e == NULL)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
BN_clear_free(rsa->d);
|
|
|
|
/* (Step 3) d = (e^-1) mod (LCM(p-1, q-1)) */
|
|
|
|
rsa->d = BN_secure_new();
|
2020-06-17 01:12:13 +08:00
|
|
|
if (rsa->d == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_set_flags(rsa->d, BN_FLG_CONSTTIME);
|
|
|
|
if (BN_mod_inverse(rsa->d, e, lcm, ctx) == NULL)
|
2018-07-05 07:28:51 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* (Step 3) return an error if d is too small */
|
|
|
|
if (BN_num_bits(rsa->d) <= (nbits >> 1)) {
|
|
|
|
ret = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (Step 4) n = pq */
|
|
|
|
if (rsa->n == NULL)
|
|
|
|
rsa->n = BN_new();
|
|
|
|
if (rsa->n == NULL || !BN_mul(rsa->n, rsa->p, rsa->q, ctx))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* (Step 5a) dP = d mod (p-1) */
|
|
|
|
if (rsa->dmp1 == NULL)
|
2020-06-17 01:12:13 +08:00
|
|
|
rsa->dmp1 = BN_secure_new();
|
|
|
|
if (rsa->dmp1 == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_set_flags(rsa->dmp1, BN_FLG_CONSTTIME);
|
|
|
|
if (!BN_mod(rsa->dmp1, rsa->d, p1, ctx))
|
2018-07-05 07:28:51 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* (Step 5b) dQ = d mod (q-1) */
|
|
|
|
if (rsa->dmq1 == NULL)
|
|
|
|
rsa->dmq1 = BN_secure_new();
|
2020-06-17 01:12:13 +08:00
|
|
|
if (rsa->dmq1 == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_set_flags(rsa->dmq1, BN_FLG_CONSTTIME);
|
|
|
|
if (!BN_mod(rsa->dmq1, rsa->d, q1, ctx))
|
2018-07-05 07:28:51 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* (Step 5c) qInv = (inverse of q) mod p */
|
|
|
|
BN_free(rsa->iqmp);
|
|
|
|
rsa->iqmp = BN_secure_new();
|
2020-06-17 01:12:13 +08:00
|
|
|
if (rsa->iqmp == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_set_flags(rsa->iqmp, BN_FLG_CONSTTIME);
|
|
|
|
if (BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx) == NULL)
|
2018-07-05 07:28:51 +08:00
|
|
|
goto err;
|
|
|
|
|
2019-10-16 03:31:45 +08:00
|
|
|
rsa->dirty_cnt++;
|
2018-07-05 07:28:51 +08:00
|
|
|
ret = 1;
|
|
|
|
err:
|
|
|
|
if (ret != 1) {
|
|
|
|
BN_free(rsa->e);
|
|
|
|
rsa->e = NULL;
|
|
|
|
BN_free(rsa->d);
|
|
|
|
rsa->d = NULL;
|
|
|
|
BN_free(rsa->n);
|
|
|
|
rsa->n = NULL;
|
|
|
|
BN_free(rsa->iqmp);
|
|
|
|
rsa->iqmp = NULL;
|
|
|
|
BN_free(rsa->dmq1);
|
|
|
|
rsa->dmq1 = NULL;
|
|
|
|
BN_free(rsa->dmp1);
|
|
|
|
rsa->dmp1 = NULL;
|
|
|
|
}
|
|
|
|
BN_clear(p1);
|
|
|
|
BN_clear(q1);
|
|
|
|
BN_clear(lcm);
|
|
|
|
BN_clear(p1q1);
|
|
|
|
BN_clear(gcd);
|
|
|
|
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a SP800-56B RSA key.
|
|
|
|
*
|
|
|
|
* See SP800-56Br1 6.3.1 "RSA Key-Pair Generation with a Fixed Public Exponent"
|
|
|
|
* 6.3.1.1 rsakpg1 - basic
|
|
|
|
* 6.3.1.3 rsakpg1 - crt
|
|
|
|
*
|
|
|
|
* See also FIPS 186-4 Section B.3.6
|
|
|
|
* "Generation of Probable Primes with Conditions Based on Auxiliary
|
|
|
|
* Probable Primes."
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* rsa The rsa object.
|
|
|
|
* nbits The intended key size in bits.
|
|
|
|
* efixed The public exponent. If NULL a default of 65537 is used.
|
|
|
|
* cb An optional BIGNUM callback.
|
|
|
|
* Returns: 1 if successfully generated otherwise it returns 0.
|
|
|
|
*/
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
int ossl_rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed,
|
|
|
|
BN_GENCB *cb)
|
2018-07-05 07:28:51 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int ok;
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
BIGNUM *e = NULL;
|
2020-06-17 09:33:16 +08:00
|
|
|
RSA_ACVP_TEST *info = NULL;
|
|
|
|
|
|
|
|
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
|
|
|
|
info = rsa->acvp_test;
|
|
|
|
#endif
|
2018-07-05 07:28:51 +08:00
|
|
|
|
|
|
|
/* (Steps 1a-1b) : Currently ignores the strength check */
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
if (!ossl_rsa_sp800_56b_validate_strength(nbits, -1))
|
2018-07-05 07:28:51 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-01-17 22:47:18 +08:00
|
|
|
ctx = BN_CTX_new_ex(rsa->libctx);
|
2018-07-05 07:28:51 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Set default if e is not passed in */
|
|
|
|
if (efixed == NULL) {
|
|
|
|
e = BN_new();
|
|
|
|
if (e == NULL || !BN_set_word(e, 65537))
|
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
e = (BIGNUM *)efixed;
|
|
|
|
}
|
2020-06-17 09:33:16 +08:00
|
|
|
/* (Step 1c) fixed exponent is checked later .*/
|
2018-07-05 07:28:51 +08:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/* (Step 2) Generate prime factors */
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
if (!ossl_rsa_fips186_4_gen_prob_primes(rsa, info, nbits, e, ctx, cb))
|
2018-07-05 07:28:51 +08:00
|
|
|
goto err;
|
|
|
|
/* (Steps 3-5) Compute params d, n, dP, dQ, qInv */
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
ok = ossl_rsa_sp800_56b_derive_params_from_pq(rsa, nbits, e, ctx);
|
2018-07-05 07:28:51 +08:00
|
|
|
if (ok < 0)
|
|
|
|
goto err;
|
|
|
|
if (ok > 0)
|
|
|
|
break;
|
|
|
|
/* Gets here if computed d is too small - so try again */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (Step 6) Do pairwise test - optional validity test has been omitted */
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
ret = ossl_rsa_sp800_56b_pairwise_test(rsa, ctx);
|
2018-07-05 07:28:51 +08:00
|
|
|
err:
|
|
|
|
if (efixed == NULL)
|
|
|
|
BN_free(e);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See SP800-56Br1 6.3.1.3 (Step 6) Perform a pair-wise consistency test by
|
|
|
|
* verifying that: k = (k^e)^d mod n for some integer k where 1 < k < n-1.
|
|
|
|
*
|
|
|
|
* Returns 1 if the RSA key passes the pairwise test or 0 it it fails.
|
|
|
|
*/
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
int ossl_rsa_sp800_56b_pairwise_test(RSA *rsa, BN_CTX *ctx)
|
2018-07-05 07:28:51 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
BIGNUM *k, *tmp;
|
|
|
|
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
tmp = BN_CTX_get(ctx);
|
|
|
|
k = BN_CTX_get(ctx);
|
|
|
|
if (k == NULL)
|
|
|
|
goto err;
|
2020-06-17 01:12:13 +08:00
|
|
|
BN_set_flags(k, BN_FLG_CONSTTIME);
|
2018-07-05 07:28:51 +08:00
|
|
|
|
|
|
|
ret = (BN_set_word(k, 2)
|
|
|
|
&& BN_mod_exp(tmp, k, rsa->e, rsa->n, ctx)
|
|
|
|
&& BN_mod_exp(tmp, tmp, rsa->d, rsa->n, ctx)
|
|
|
|
&& BN_cmp(k, tmp) == 0);
|
|
|
|
if (ret == 0)
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_PAIRWISE_TEST_FAILURE);
|
2018-07-05 07:28:51 +08:00
|
|
|
err:
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|