From 17b73561d5af222163e058f6b51d78216b99b696 Mon Sep 17 00:00:00 2001 From: Pauli Date: Fri, 21 Feb 2025 11:36:56 +1100 Subject: [PATCH] fips: refactor ML-KEM tests so that key generation is separate Encapsulation and decapsulation remain as their own CAST. Reviewed-by: Tim Hudson Reviewed-by: Viktor Dukhovni (Merged from https://github.com/openssl/openssl/pull/26859) --- providers/fips/self_test_data.inc | 182 ++++++++---------------------- 1 file changed, 46 insertions(+), 136 deletions(-) diff --git a/providers/fips/self_test_data.inc b/providers/fips/self_test_data.inc index b35b4422aff..c219c47d123 100644 --- a/providers/fips/self_test_data.inc +++ b/providers/fips/self_test_data.inc @@ -158,16 +158,11 @@ typedef struct st_kat_keygen_st { typedef struct st_kat_kem_st { const char *desc; const char *algorithm; + const ST_KAT_PARAM *key; const unsigned char *cipher_text; size_t cipher_text_len; - const unsigned char *private_key; - size_t private_key_len; - const unsigned char *public_key; - size_t public_key_len; const unsigned char *entropy; size_t entropy_len; - const unsigned char *seed; - size_t seed_len; const unsigned char *secret; size_t secret_len; const unsigned char *reject_secret; @@ -3076,29 +3071,7 @@ static const ST_KAT_PARAM ml_dsa_keygen_params[] = { }; #endif -#if !defined(OPENSSL_NO_ML_DSA) || !defined(OPENSSL_NO_SLH_DSA) -static const ST_KAT_ASYM_KEYGEN st_kat_asym_keygen_tests[] = { -# if !defined(OPENSSL_NO_ML_DSA) - { - OSSL_SELF_TEST_DESC_KEYGEN_ML_DSA, - "ML-DSA-65", - ml_dsa_keygen_params, - ml_dsa_key - }, -# endif -# if !defined(OPENSSL_NO_SLH_DSA) - { - OSSL_SELF_TEST_DESC_KEYGEN_SLH_DSA, - "SLH-DSA-SHA2-128f", - slh_dsa_sha2_128f_keygen_init_params, - slh_dsa_128f_keygen_expected_params - }, -# endif -}; -#endif /* !OPENSSL_NO_ML_DSA || !OPENSSL_NO_SLH_DSA */ - /*- KEM SELF TEST DATA */ - /* * Test case generated via the OpenSSL commands: * @@ -3119,7 +3092,6 @@ static const ST_KAT_ASYM_KEYGEN st_kat_asym_keygen_tests[] = { * (cat z.dat; head -c 768 /dev/zero) | * openssl dgst -shake256 -xoflen 32 -binary > expected.dat */ - #ifndef OPENSSL_NO_ML_KEM static const unsigned char ml_kem_512_cipher_text[] = { 0x6b, 0xc5, 0x04, 0x00, 0x27, 0x7a, 0xbb, 0x7e, @@ -3427,109 +3399,6 @@ static const unsigned char ml_kem_512_private_key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -static const unsigned char ml_kem_512_public_key[] = { - 0x61, 0xb5, 0x0f, 0x5f, 0xf3, 0x80, 0x25, 0xa9, - 0x13, 0x9c, 0x0a, 0xc0, 0x21, 0x9b, 0xae, 0xc8, - 0xa8, 0xb7, 0x1c, 0xc5, 0x50, 0xfd, 0x06, 0x29, - 0x56, 0x17, 0xcf, 0x0c, 0x84, 0x54, 0xaf, 0xfc, - 0x8e, 0xa0, 0x26, 0x1b, 0xe8, 0xbb, 0x2f, 0xd4, - 0x77, 0x3c, 0xdd, 0xf2, 0x3e, 0x2c, 0x55, 0x47, - 0x27, 0xf9, 0x64, 0x60, 0x2c, 0x9e, 0x67, 0xb5, - 0x6e, 0x6e, 0xfa, 0xae, 0xca, 0x1b, 0x95, 0x08, - 0x67, 0xc4, 0xc5, 0x5b, 0xb1, 0xd7, 0x50, 0x34, - 0x27, 0x65, 0x0b, 0xeb, 0xda, 0x23, 0x55, 0xb9, - 0x24, 0x30, 0x62, 0x19, 0xad, 0x07, 0x3b, 0x98, - 0x78, 0x2a, 0xec, 0x54, 0xa8, 0xc7, 0x04, 0xcf, - 0x31, 0x13, 0xc7, 0x8b, 0x17, 0x4b, 0xa8, 0x97, - 0x66, 0x6c, 0xb0, 0x1b, 0x08, 0xb7, 0x05, 0x6d, - 0x5c, 0x6f, 0x17, 0x87, 0x0f, 0x72, 0xd4, 0x5a, - 0x9e, 0x04, 0xb0, 0xc0, 0x01, 0x7c, 0x0f, 0x96, - 0xad, 0x2c, 0x52, 0x5d, 0x98, 0xc0, 0x49, 0x83, - 0x6a, 0x59, 0x19, 0xb3, 0x66, 0xef, 0x48, 0x97, - 0x95, 0x14, 0x5f, 0x56, 0x64, 0x68, 0x93, 0x03, - 0x31, 0x3f, 0x4a, 0xa5, 0x7c, 0x55, 0x7f, 0x3c, - 0xcc, 0x3b, 0x3f, 0x59, 0xbc, 0x6f, 0xb3, 0xcb, - 0xdb, 0x78, 0x50, 0x2f, 0x1b, 0x33, 0x28, 0x4c, - 0x65, 0x80, 0x55, 0x89, 0xca, 0xbb, 0x4b, 0x5e, - 0x14, 0x24, 0xe3, 0x46, 0x7b, 0x68, 0x62, 0x1d, - 0x83, 0x60, 0x58, 0x02, 0xc0, 0x82, 0x7d, 0x45, - 0x5f, 0x73, 0x84, 0xa3, 0x58, 0xd7, 0xbb, 0xdb, - 0x16, 0xa3, 0x71, 0x63, 0x9c, 0x7c, 0x45, 0x55, - 0x9b, 0x79, 0x1c, 0x53, 0x9c, 0x99, 0xc9, 0xd7, - 0xc1, 0x47, 0x70, 0xc6, 0x76, 0xd6, 0x76, 0xef, - 0x07, 0x2b, 0xff, 0x19, 0x8e, 0x8e, 0x28, 0x89, - 0x7e, 0xa2, 0xaa, 0x12, 0x9c, 0x8b, 0xba, 0x77, - 0x46, 0xdc, 0xb6, 0xcd, 0xc0, 0x9b, 0x04, 0x47, - 0x30, 0xb4, 0xd9, 0xb8, 0x95, 0x96, 0x11, 0x6b, - 0xb5, 0xf8, 0x6a, 0xd9, 0x25, 0xb9, 0xcb, 0x2b, - 0x2c, 0x16, 0x08, 0x31, 0x47, 0x78, 0x90, 0xe4, - 0x53, 0x7e, 0x36, 0xa1, 0x53, 0x4e, 0xc5, 0x73, - 0x2f, 0xc0, 0xac, 0x0b, 0x40, 0x40, 0x5a, 0x91, - 0x25, 0xe7, 0x01, 0x7e, 0x6a, 0x41, 0x7d, 0xff, - 0x1a, 0x8f, 0x01, 0xd3, 0x36, 0x0e, 0xa8, 0x7a, - 0x03, 0x80, 0xa3, 0x30, 0xcb, 0x04, 0x69, 0x59, - 0x71, 0x9b, 0x43, 0x96, 0x1d, 0xa0, 0xb1, 0x6d, - 0x96, 0xcf, 0x61, 0x9c, 0x30, 0xbf, 0xba, 0x0d, - 0x5a, 0x19, 0x88, 0x42, 0x00, 0x90, 0x23, 0x18, - 0xbf, 0xdd, 0x68, 0xae, 0x80, 0x3b, 0x96, 0x28, - 0x5a, 0x5f, 0xb9, 0x98, 0x2b, 0x37, 0x65, 0x21, - 0x6c, 0x51, 0xc2, 0xce, 0x76, 0xc6, 0x3c, 0xd4, - 0x27, 0xbb, 0xe5, 0x9c, 0x31, 0xd4, 0x62, 0x7c, - 0x77, 0xa5, 0xc7, 0x88, 0x55, 0x90, 0x70, 0x15, - 0x13, 0xcc, 0x0c, 0x9a, 0x20, 0x15, 0xb4, 0xf2, - 0x8e, 0xf0, 0x33, 0x32, 0xcf, 0xc5, 0x4f, 0x23, - 0x56, 0xa3, 0xef, 0xf4, 0x93, 0x82, 0xf4, 0x1b, - 0x55, 0xfa, 0x5f, 0x12, 0x79, 0xb2, 0xa1, 0x88, - 0xb8, 0x88, 0xb6, 0x87, 0x66, 0xc5, 0x9e, 0x44, - 0xd2, 0xb2, 0x14, 0x74, 0x9b, 0x14, 0x25, 0x93, - 0xf0, 0x87, 0x30, 0x12, 0x5b, 0x20, 0x2e, 0x06, - 0xcb, 0xd1, 0x24, 0x0f, 0x5b, 0x25, 0x9f, 0x12, - 0x9a, 0x1b, 0x0c, 0xc8, 0xa8, 0x05, 0x40, 0xbb, - 0xb7, 0x79, 0x4e, 0x72, 0x49, 0x4d, 0x43, 0x92, - 0x73, 0xf4, 0x54, 0x42, 0x86, 0xc9, 0x90, 0x26, - 0xb0, 0x4f, 0x0c, 0xca, 0x2f, 0xf0, 0x4b, 0x48, - 0x27, 0x59, 0xba, 0x6b, 0xb7, 0x67, 0xa3, 0x93, - 0x1e, 0x7c, 0x17, 0x57, 0xa6, 0x47, 0xb0, 0xf3, - 0x78, 0x58, 0xf1, 0xe7, 0xc9, 0xbe, 0x8c, 0x4b, - 0x4e, 0xe2, 0xcd, 0xe2, 0x81, 0x25, 0xa8, 0x94, - 0x08, 0x9d, 0x70, 0xa1, 0x1e, 0x24, 0x28, 0xa4, - 0x1b, 0x2f, 0x4a, 0xf5, 0x9b, 0xc9, 0x67, 0x5a, - 0x60, 0x48, 0x3b, 0xa3, 0x86, 0x94, 0x83, 0x06, - 0x40, 0x27, 0x80, 0x79, 0xd1, 0x28, 0x03, 0x6b, - 0x23, 0x5c, 0x5f, 0xc6, 0xa8, 0xfe, 0x5a, 0xab, - 0x38, 0x9a, 0x36, 0x11, 0xf1, 0x92, 0x53, 0x30, - 0xc3, 0x0c, 0x8b, 0xba, 0xa2, 0x2a, 0x2a, 0x60, - 0x47, 0x67, 0x61, 0x72, 0x48, 0x18, 0x45, 0x37, - 0x64, 0x76, 0x2f, 0x0a, 0x35, 0x17, 0xf6, 0xf8, - 0xbc, 0x09, 0xa8, 0x41, 0x2e, 0xab, 0xa9, 0xd4, - 0x66, 0x65, 0xf5, 0x71, 0x5a, 0x7c, 0xf1, 0x0b, - 0x41, 0x25, 0x9b, 0xd8, 0xd4, 0x2b, 0xb4, 0x80, - 0x48, 0xb9, 0xbb, 0x6c, 0xd3, 0x0b, 0xcc, 0x64, - 0x7b, 0xb9, 0x46, 0x07, 0xac, 0xb8, 0xa1, 0x33, - 0xac, 0x69, 0x17, 0xa3, 0xc9, 0xbd, 0xce, 0x84, - 0xbb, 0xb2, 0x84, 0x2f, 0x60, 0xab, 0x6a, 0xa7, - 0xd2, 0x94, 0x5b, 0x6b, 0xa0, 0xcb, 0x82, 0x6f, - 0x9b, 0x97, 0xb3, 0x95, 0xe2, 0x86, 0x25, 0x77, - 0x4b, 0x6d, 0x39, 0x81, 0xb5, 0x83, 0xb6, 0x68, - 0x92, 0x73, 0x03, 0xc9, 0x3f, 0x55, 0x78, 0x5f, - 0x21, 0x93, 0xce, 0x69, 0x72, 0x18, 0x6e, 0xf7, - 0x58, 0xdf, 0x10, 0x1b, 0x31, 0x47, 0x6f, 0x7e, - 0x28, 0x1c, 0x42, 0x0a, 0x5b, 0xbe, 0x06, 0x61, - 0x4f, 0x2b, 0x06, 0xc2, 0x9c, 0x41, 0xce, 0x4b, - 0x94, 0x9e, 0xb1, 0x00, 0x0f, 0xc4, 0xb6, 0x8c, - 0x09, 0x54, 0x38, 0x7c, 0x43, 0x8b, 0x6b, 0x08, - 0xb0, 0x1c, 0x00, 0x78, 0x79, 0xae, 0xda, 0x00, - 0x9f, 0x47, 0xc6, 0x6f, 0x12, 0x05, 0xab, 0x43, - 0x15, 0x88, 0x10, 0x75, 0xbb, 0x3a, 0xdc, 0x59, - 0x98, 0xd9, 0x1d, 0x59, 0xb9, 0x7d, 0x64, 0x02, - 0xbc, 0x36, 0xc5, 0x3a, 0x27, 0xa2, 0xb0, 0x88, - 0x81, 0x20, 0x33, 0xbc, 0x5a, 0x79, 0xe6, 0x87, - 0x2f, 0x72, 0x3a, 0x7e, 0x0e, 0xa5, 0x3c, 0x8e, - 0xdc, 0x4e, 0x73, 0x4d, 0xae, 0x85, 0xe5, 0x04, - 0x18, 0x84, 0x50, 0x24, 0x85, 0xa1, 0xae, 0x53, - 0x29, 0x16, 0x22, 0xe9, 0xd4, 0xa5, 0xba, 0xd4 -}; - static const unsigned char ml_kem_512_secret[] = { 0x4a, 0xd5, 0x3a, 0x06, 0xb2, 0x9f, 0x12, 0x56, 0x84, 0x21, 0xa5, 0x52, 0xc0, 0x81, 0x95, 0xb5, @@ -3552,17 +3421,58 @@ static const unsigned char ml_kem_512_seed[ML_KEM_SEED_BYTES] = { 0 }; +static const ST_KAT_PARAM ml_kem_keygen_params[] = { + ST_KAT_PARAM_OCTET(OSSL_PKEY_PARAM_ML_KEM_SEED, ml_kem_512_seed), + ST_KAT_PARAM_END() +}; + +static const ST_KAT_PARAM ml_kem_key[] = { + ST_KAT_PARAM_OCTET(OSSL_PKEY_PARAM_PRIV_KEY, ml_kem_512_private_key), + ST_KAT_PARAM_END() +}; + static const ST_KAT_KEM st_kat_kem_tests[] = { { OSSL_SELF_TEST_DESC_KEM, "ML-KEM-512", + ml_kem_key, ITM(ml_kem_512_cipher_text), - ITM(ml_kem_512_private_key), - ITM(ml_kem_512_public_key), ITM(ml_kem_512_entropy), - ITM(ml_kem_512_seed), ITM(ml_kem_512_secret), ml_kem_512_reject_secret /* No length because same as _secret's */ }, }; -#endif +#endif /* OPENSSL_NO_ML_KEM */ + +#if !defined(OPENSSL_NO_ML_KEM) || !defined(OPENSSL_NO_ML_DSA) || !defined(OPENSSL_NO_SLH_DSA) +static const ST_KAT_ASYM_KEYGEN st_kat_asym_keygen_tests[] = { +# if !defined(OPENSSL_NO_ML_KEM) + /* + * FIPS 140-3 IG 10.3.A resolution 14 mandates a CAST for ML-KEM + * key generation. + */ + { + OSSL_SELF_TEST_DESC_KEYGEN_ML_KEM, + "ML-KEM-512", + ml_kem_keygen_params, + ml_kem_key + }, +# endif +# if !defined(OPENSSL_NO_ML_DSA) + { + OSSL_SELF_TEST_DESC_KEYGEN_ML_DSA, + "ML-DSA-65", + ml_dsa_keygen_params, + ml_dsa_key + }, +# endif +# if !defined(OPENSSL_NO_SLH_DSA) + { + OSSL_SELF_TEST_DESC_KEYGEN_SLH_DSA, + "SLH-DSA-SHA2-128f", + slh_dsa_sha2_128f_keygen_init_params, + slh_dsa_128f_keygen_expected_params + }, +# endif +}; +#endif /* !OPENSSL_NO_ML_DSA || !OPENSSL_NO_SLH_DSA */ -- 2.47.2