Add self tests for rsa encryption

SP800-56br2 requires seperate KAT's (fips self tests) to be tested for both encryption and decryption
using the RSA primitive (i.e. no padding). This is specified in FIPS140-2 IG D.9

A copy of the methods EVP_PKEY_encrypt_init(), EVP_PKEY_encrypt(), EVP_PKEY_decrypt_init(), EVP_PKEY_decrypt()
are now in the fips module.

Removed the #ifdef FIPS_MODULE in evp_pkey_ctx_free_old_ops().

Added corruption test

Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/12835)
This commit is contained in:
Shane Lontis 2020-09-16 14:10:23 +10:00
parent a268ed3acf
commit 4343a4187d
8 changed files with 230 additions and 12 deletions

View File

@ -2,7 +2,7 @@ LIBS=../../libcrypto
$COMMON=digest.c evp_enc.c evp_lib.c evp_fetch.c cmeth_lib.c evp_utils.c \
mac_lib.c mac_meth.c keymgmt_meth.c keymgmt_lib.c kdf_lib.c kdf_meth.c \
m_sigver.c pmeth_lib.c signature.c p_lib.c pmeth_gn.c exchange.c \
pmeth_check.c evp_rand.c
pmeth_check.c evp_rand.c asymcipher.c
SOURCE[../../libcrypto]=$COMMON\
encode.c evp_key.c evp_cnf.c \
@ -13,7 +13,6 @@ SOURCE[../../libcrypto]=$COMMON\
bio_md.c bio_b64.c bio_enc.c evp_err.c e_null.c \
c_allc.c c_alld.c bio_ok.c \
evp_pkey.c evp_pbe.c p5_crpt.c p5_crpt2.c pbe_scrypt.c \
pmeth_fn.c\
e_aes_cbc_hmac_sha1.c e_aes_cbc_hmac_sha256.c e_rc4_hmac_md5.c \
e_chacha20_poly1305.c \
legacy_sha.c

View File

@ -396,10 +396,7 @@ void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
EVP_KEYEXCH_free(ctx->op.kex.exchange);
ctx->op.kex.exchprovctx = NULL;
ctx->op.kex.exchange = NULL;
}
/* TODO(3.0): add dependancies and uncomment this when available for fips mode */
#ifndef FIPS_MODULE
else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
} else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
@ -409,7 +406,6 @@ void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
}
#endif
}
void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)

View File

@ -184,6 +184,10 @@ file after successfully running the self tests during installation.
Known answer test for a symmetric cipher.
=item "KAT_AsymmetricCipher" (B<OSSL_SELF_TEST_TYPE_KAT_ASYM_CIPHER>)
Known answer test for a asymmetric cipher.
=item "KAT_Digest" (B<OSSL_SELF_TEST_TYPE_KAT_DIGEST>)
Known answer test for a digest.
@ -237,6 +241,12 @@ The FIPS module passes the following descriptions(s) to OSSL_SELF_TEST_onbegin()
Key generation tests used with the "Pairwise_Consistency_Test" type.
=item "RSA_Encrypt" (B<OSSL_SELF_TEST_DESC_ASYM_RSA_ENC>)
=item "RSA_Decrypt" (B<OSSL_SELF_TEST_DESC_ASYM_RSA_DEC>)
"KAT_AsymmetricCipher" uses this to indicate an encrypt or decrypt KAT.
=item "AES_GCM" (B<OSSL_SELF_TEST_DESC_CIPHER_AES_GCM>)
=item "TDES" (B<OSSL_SELF_TEST_DESC_CIPHER_TDES>)

View File

@ -30,6 +30,7 @@ extern "C" {
# define OSSL_SELF_TEST_TYPE_CRNG "Continuous_RNG_Test"
# define OSSL_SELF_TEST_TYPE_PCT "Pairwise_Consistency_Test"
# define OSSL_SELF_TEST_TYPE_KAT_CIPHER "KAT_Cipher"
# define OSSL_SELF_TEST_TYPE_KAT_ASYM_CIPHER "KAT_AsymmetricCipher"
# define OSSL_SELF_TEST_TYPE_KAT_DIGEST "KAT_Digest"
# define OSSL_SELF_TEST_TYPE_KAT_SIGNATURE "KAT_Signature"
# define OSSL_SELF_TEST_TYPE_KAT_KDF "KAT_KDF"
@ -44,6 +45,8 @@ extern "C" {
# define OSSL_SELF_TEST_DESC_PCT_DSA "DSA"
# define OSSL_SELF_TEST_DESC_CIPHER_AES_GCM "AES_GCM"
# define OSSL_SELF_TEST_DESC_CIPHER_TDES "TDES"
# define OSSL_SELF_TEST_DESC_ASYM_RSA_ENC "RSA_Encrypt"
# define OSSL_SELF_TEST_DESC_ASYM_RSA_DEC "RSA_Decrypt"
# define OSSL_SELF_TEST_DESC_MD_SHA1 "SHA1"
# define OSSL_SELF_TEST_DESC_MD_SHA2 "SHA2"
# define OSSL_SELF_TEST_DESC_MD_SHA3 "SHA3"

View File

@ -101,6 +101,18 @@ typedef struct st_kat_sign_st {
size_t sig_expected_len;
} ST_KAT_SIGN;
typedef struct st_kat_asym_cipher_st {
const char *desc;
const char *algorithm;
int encrypt;
const ST_KAT_PARAM *key;
const ST_KAT_PARAM *postinit;
const unsigned char *in;
size_t in_len;
const unsigned char *expected;
size_t expected_len;
} ST_KAT_ASYM_CIPHER;
/*- DIGEST TEST DATA */
static const unsigned char sha1_pt[] = "abc";
static const unsigned char sha1_digest[] = {
@ -943,7 +955,7 @@ static const unsigned char rsa_qInv[] = {
0xF8, 0xF3, 0x95, 0xFE, 0x31, 0x25, 0x50, 0x12
};
static const ST_KAT_PARAM rsa_key[] = {
static const ST_KAT_PARAM rsa_crt_key[] = {
ST_KAT_PARAM_BIGNUM(OSSL_PKEY_PARAM_RSA_N, rsa_n),
ST_KAT_PARAM_BIGNUM(OSSL_PKEY_PARAM_RSA_E, rsa_e),
ST_KAT_PARAM_BIGNUM(OSSL_PKEY_PARAM_RSA_D, rsa_d),
@ -955,6 +967,25 @@ static const ST_KAT_PARAM rsa_key[] = {
ST_KAT_PARAM_END()
};
static const ST_KAT_PARAM rsa_pub_key[] = {
ST_KAT_PARAM_BIGNUM(OSSL_PKEY_PARAM_RSA_N, rsa_n),
ST_KAT_PARAM_BIGNUM(OSSL_PKEY_PARAM_RSA_E, rsa_e),
ST_KAT_PARAM_END()
};
static const ST_KAT_PARAM rsa_priv_key[] = {
ST_KAT_PARAM_BIGNUM(OSSL_PKEY_PARAM_RSA_N, rsa_n),
ST_KAT_PARAM_BIGNUM(OSSL_PKEY_PARAM_RSA_E, rsa_e),
ST_KAT_PARAM_BIGNUM(OSSL_PKEY_PARAM_RSA_D, rsa_d),
ST_KAT_PARAM_END()
};
static const ST_KAT_PARAM rsa_enc_params[] = {
ST_KAT_PARAM_UTF8STRING(OSSL_ASYM_CIPHER_PARAM_PAD_MODE,
OSSL_PKEY_RSA_PAD_MODE_NONE),
ST_KAT_PARAM_END()
};
static const unsigned char rsa_expected_sig[256] = {
0xad, 0xbe, 0x2a, 0xaf, 0x16, 0x85, 0xc5, 0x00,
0x91, 0x3e, 0xd0, 0x49, 0xfb, 0x3a, 0x81, 0xb9,
@ -990,6 +1021,45 @@ static const unsigned char rsa_expected_sig[256] = {
0x2c, 0x68, 0xf0, 0x37, 0xa9, 0xd2, 0x56, 0xd6
};
static const unsigned char rsa_asym_plaintext_encrypt[256] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
};
static const unsigned char rsa_asym_expected_encrypt[256] = {
0x54, 0xac, 0x23, 0x96, 0x1d, 0x82, 0x5d, 0x8b,
0x8f, 0x36, 0x33, 0xd0, 0xf4, 0x02, 0xa2, 0x61,
0xb1, 0x13, 0xd4, 0x4a, 0x46, 0x06, 0x37, 0x3c,
0xbf, 0x40, 0x05, 0x3c, 0xc6, 0x3b, 0x64, 0xdc,
0x22, 0x22, 0xaf, 0x36, 0x79, 0x62, 0x45, 0xf0,
0x97, 0x82, 0x22, 0x44, 0x86, 0x4a, 0x7c, 0xfa,
0xac, 0x03, 0x21, 0x84, 0x3f,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, 0x2e, 0x6c, 0x3c, 0x57, 0x04,
0x29, 0xf4, 0xf5, 0x66, 0x7d, 0x93, 0x4a, 0xaa,
0x05, 0x52, 0x55, 0xc1, 0xc6, 0x06, 0x90, 0xab,
};
#endif /* OPENSSL_NO_RSA */
#ifndef OPENSSL_NO_EC
@ -1153,7 +1223,7 @@ static const ST_KAT_SIGN st_kat_sign_tests[] = {
OSSL_SELF_TEST_DESC_SIGN_RSA,
"RSA",
"SHA-256",
rsa_key,
rsa_crt_key,
ITM(rsa_expected_sig)
},
#endif /* OPENSSL_NO_RSA */
@ -1182,3 +1252,35 @@ static const ST_KAT_SIGN st_kat_sign_tests[] = {
},
#endif /* OPENSSL_NO_DSA */
};
static const ST_KAT_ASYM_CIPHER st_kat_asym_cipher_tests[] = {
#ifndef OPENSSL_NO_RSA
{
OSSL_SELF_TEST_DESC_ASYM_RSA_ENC,
"RSA",
1,
rsa_pub_key,
rsa_enc_params,
ITM(rsa_asym_plaintext_encrypt),
ITM(rsa_asym_expected_encrypt),
},
{
OSSL_SELF_TEST_DESC_ASYM_RSA_DEC,
"RSA",
0,
rsa_priv_key,
rsa_enc_params,
ITM(rsa_asym_expected_encrypt),
ITM(rsa_asym_plaintext_encrypt),
},
{
OSSL_SELF_TEST_DESC_ASYM_RSA_DEC,
"RSA",
0,
rsa_crt_key,
rsa_enc_params,
ITM(rsa_asym_expected_encrypt),
ITM(rsa_asym_plaintext_encrypt),
},
#endif /* OPENSSL_NO_RSA */
};

View File

@ -342,8 +342,6 @@ err:
return ret;
}
static int self_test_ka(const ST_KAT_KAS *t,
OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
{
@ -504,6 +502,93 @@ err:
return ret;
}
/*
* Test an encrypt or decrypt KAT..
*
* FIPS 140-2 IG D.9 states that separate KAT tests are needed for encrypt
* and decrypt..
*/
static int self_test_asym_cipher(const ST_KAT_ASYM_CIPHER *t, OSSL_SELF_TEST *st,
OPENSSL_CTX *libctx)
{
int ret = 0;
OSSL_PARAM *keyparams = NULL, *initparams = NULL;
OSSL_PARAM_BLD *keybld = NULL, *initbld = NULL;
EVP_PKEY_CTX *encctx = NULL, *keyctx = NULL;
EVP_PKEY *key = NULL;
BN_CTX *bnctx = NULL;
unsigned char out[256];
size_t outlen = sizeof(out);
OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_ASYM_CIPHER, t->desc);
bnctx = BN_CTX_new_ex(libctx);
if (bnctx == NULL)
goto err;
/* Load a public or private key from data */
keybld = OSSL_PARAM_BLD_new();
if (keybld == NULL
|| !add_params(keybld, t->key, bnctx))
goto err;
keyparams = OSSL_PARAM_BLD_to_param(keybld);
keyctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, NULL);
if (keyctx == NULL || keyparams == NULL)
goto err;
if (EVP_PKEY_key_fromdata_init(keyctx) <= 0
|| EVP_PKEY_fromdata(keyctx, &key, keyparams) <= 0)
goto err;
/* Create a EVP_PKEY_CTX to use for the encrypt or decrypt operation */
encctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL);
if (encctx == NULL
|| (t->encrypt && EVP_PKEY_encrypt_init(encctx) <= 0)
|| (!t->encrypt && EVP_PKEY_decrypt_init(encctx) <= 0))
goto err;
/* Add any additional parameters such as padding */
if (t->postinit != NULL) {
initbld = OSSL_PARAM_BLD_new();
if (initbld == NULL)
goto err;
if (!add_params(initbld, t->postinit, bnctx))
goto err;
initparams = OSSL_PARAM_BLD_to_param(initbld);
if (initparams == NULL)
goto err;
if (EVP_PKEY_CTX_set_params(encctx, initparams) <= 0)
goto err;
}
if (t->encrypt) {
if (EVP_PKEY_encrypt(encctx, out, &outlen,
t->in, t->in_len) <= 0)
goto err;
} else {
if (EVP_PKEY_decrypt(encctx, out, &outlen,
t->in, t->in_len) <= 0)
goto err;
}
/* Check the KAT */
OSSL_SELF_TEST_oncorrupt_byte(st, out);
if (outlen != t->expected_len
|| memcmp(out, t->expected, t->expected_len) != 0)
goto err;
ret = 1;
err:
BN_CTX_free(bnctx);
EVP_PKEY_free(key);
EVP_PKEY_CTX_free(encctx);
EVP_PKEY_CTX_free(keyctx);
OSSL_PARAM_BLD_free_params(keyparams);
OSSL_PARAM_BLD_free(keybld);
OSSL_PARAM_BLD_free_params(initparams);
OSSL_PARAM_BLD_free(initbld);
OSSL_SELF_TEST_onend(st, ret);
return ret;
}
/*
* Test a data driven list of KAT's for digest algorithms.
* All tests are run regardless of if they fail or not.
@ -531,6 +616,17 @@ static int self_test_ciphers(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
return ret;
}
static int self_test_asym_ciphers(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
{
int i, ret = 1;
for (i = 0; i < (int)OSSL_NELEM(st_kat_asym_cipher_tests); ++i) {
if (!self_test_asym_cipher(&st_kat_asym_cipher_tests[i], st, libctx))
ret = 0;
}
return ret;
}
static int self_test_kdfs(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
{
int i, ret = 1;
@ -596,6 +692,8 @@ int SELF_TEST_kats(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
ret = 0;
if (!self_test_kas(st, libctx))
ret = 0;
if (!self_test_asym_ciphers(st, libctx))
ret = 0;
return ret;
}

View File

@ -24,7 +24,7 @@ use platform;
plan skip_all => "Test only supported in a fips build" if disabled("fips");
plan tests => 23;
plan tests => 24;
my $infile = bldtop_file('providers', platform->dso('fips'));
my $fipskey = $ENV{FIPSKEY} // '00';
@ -225,6 +225,16 @@ SKIP: {
"fipsinstall fails when the signature result is corrupted");
}
# corrupt an Asymmetric cipher test
SKIP: {
skip "Skipping Asymmetric RSA corruption test because of no rsa in this build", 1
if disabled("rsa");
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
'-corrupt_desc', 'RSA_Encrypt',
'-corrupt_type', 'KAT_AsymmetricCipher'])),
"fipsinstall fails when the asymmetric cipher result is corrupted");
}
$ENV{OPENSSL_CONF_INCLUDE} = ".";
ok(replace_parent_line_file('fips.cnf', 'fips_parent.cnf')