diff options
author | Shane Lontis <shane.lontis@oracle.com> | 2020-09-16 14:10:23 +1000 |
---|---|---|
committer | Shane Lontis <shane.lontis@oracle.com> | 2020-09-16 14:10:23 +1000 |
commit | 4343a4187d28d719006642a610afea6e186832bf (patch) | |
tree | 1dfeffe1ad5f1b6e823163d7e1c21657c3307eb8 /providers/fips | |
parent | a268ed3acf16948c0e19ba67b2b3f89b3312a416 (diff) | |
download | openssl-new-4343a4187d28d719006642a610afea6e186832bf.tar.gz |
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)
Diffstat (limited to 'providers/fips')
-rw-r--r-- | providers/fips/self_test_data.inc | 106 | ||||
-rw-r--r-- | providers/fips/self_test_kats.c | 102 |
2 files changed, 204 insertions, 4 deletions
diff --git a/providers/fips/self_test_data.inc b/providers/fips/self_test_data.inc index 17bfc950db..8a4b6fcee0 100644 --- a/providers/fips/self_test_data.inc +++ b/providers/fips/self_test_data.inc @@ -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, 0x31, 0xad, 0x2a, + 0xa4, 0x6e, 0x7a, 0xc5, 0x93, 0xf3, 0x0f, 0xfc, + 0xf1, 0x62, 0xce, 0x82, 0x12, 0x45, 0xc9, 0x35, + 0xb0, 0x7a, 0xcd, 0x99, 0x8c, 0x91, 0x6b, 0x5a, + 0xd3, 0x46, 0xdb, 0xf9, 0x9e, 0x52, 0x49, 0xbd, + 0x1e, 0xe8, 0xda, 0xac, 0x61, 0x47, 0xc2, 0xda, + 0xfc, 0x1e, 0xfb, 0x74, 0xd7, 0xd6, 0xc1, 0x18, + 0x86, 0x3e, 0x20, 0x9c, 0x7a, 0xe1, 0x04, 0xb7, + 0x38, 0x43, 0xb1, 0x4e, 0xa0, 0xd8, 0xc1, 0x39, + 0x4d, 0xe1, 0xd3, 0xb0, 0xb3, 0xf1, 0x82, 0x87, + 0x1f, 0x74, 0xb5, 0x69, 0xfd, 0x33, 0xd6, 0x21, + 0x7c, 0x61, 0x60, 0x28, 0xca, 0x70, 0xdb, 0xa0, + 0xbb, 0xc8, 0x73, 0xa9, 0x82, 0xf8, 0x6b, 0xd8, + 0xf0, 0xc9, 0x7b, 0x20, 0xdf, 0x9d, 0xfb, 0x8c, + 0xd4, 0xa2, 0x89, 0xe1, 0x9b, 0x04, 0xad, 0xaa, + 0x11, 0x6c, 0x8f, 0xce, 0x83, 0x29, 0x56, 0x69, + 0xbb, 0x00, 0x3b, 0xef, 0xca, 0x2d, 0xcd, 0x52, + 0xc8, 0xf1, 0xb3, 0x9b, 0xb4, 0x4f, 0x6d, 0x9c, + 0x3d, 0x69, 0xcc, 0x6d, 0x1f, 0x38, 0x4d, 0xe6, + 0xbb, 0x0c, 0x87, 0xdc, 0x5f, 0xa9, 0x24, 0x93, + 0x03, 0x46, 0xa2, 0x33, 0x6c, 0xf4, 0xd8, 0x5d, + 0x68, 0xf3, 0xd3, 0xe0, 0xf2, 0x30, 0xdb, 0xf5, + 0x4f, 0x0f, 0xad, 0xc7, 0xd0, 0xaa, 0x47, 0xd9, + 0x9f, 0x85, 0x1b, 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 */ +}; diff --git a/providers/fips/self_test_kats.c b/providers/fips/self_test_kats.c index 44d7557530..50a0a69609 100644 --- a/providers/fips/self_test_kats.c +++ b/providers/fips/self_test_kats.c @@ -342,8 +342,6 @@ err: return ret; } - - static int self_test_ka(const ST_KAT_KAS *t, OSSL_SELF_TEST *st, OPENSSL_CTX *libctx) { @@ -505,6 +503,93 @@ err: } /* + * 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. * Return 0 if any test fails. @@ -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; } |