From 73cb6f54de0ddb65a621ed8cb4b1a3e8cd89a5b6 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 31 Jan 2022 16:35:50 +0100 Subject: [PATCH] Add tests for mbedtls_psa_hkdf_extrct The tests are based on the the test of mbedtls_hkdf_extract. Signed-off-by: Gabor Mezei --- tests/suites/test_suite_ssl.data | 31 +++++++++++++++ tests/suites/test_suite_ssl.function | 58 ++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/tests/suites/test_suite_ssl.data b/tests/suites/test_suite_ssl.data index eb1b8f44e..6f8969523 100644 --- a/tests/suites/test_suite_ssl.data +++ b/tests/suites/test_suite_ssl.data @@ -4392,6 +4392,37 @@ SSL TLS 1.3 Key schedule: Secret evolution #3 # Handshake secret to Master Secret ssl_tls13_key_evolution:MBEDTLS_MD_SHA256:"fb9fc80689b3a5d02c33243bf69a1b1b20705588a794304a6e7120155edf149a":"":"7f2882bb9b9a46265941653e9c2f19067118151e21d12e57a7b6aca1f8150c8d" +SSL TLS 1.3 Key schedule: HKDF RFC5869 Test Vector #1 Extract +depends_on:PSA_WANT_ALG_SHA_256 +psa_hkdf_extract:PSA_ALG_HMAC(PSA_ALG_SHA_256):"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b":"000102030405060708090a0b0c":"077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5" + +SSL TLS 1.3 Key schedule: HKDF RFC5869 Test Vector #2 Extract +depends_on:PSA_WANT_ALG_SHA_256 +psa_hkdf_extract:PSA_ALG_HMAC(PSA_ALG_SHA_256):"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f":"606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf":"06a6b88c5853361a06104c9ceb35b45cef760014904671014a193f40c15fc244" + +SSL TLS 1.3 Key schedule: HKDF RFC5869 Test Vector #3 Extract +depends_on:PSA_WANT_ALG_SHA_256 +psa_hkdf_extract:PSA_ALG_HMAC(PSA_ALG_SHA_256):"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b":"":"19ef24a32c717b167f33a91d6f648bdf96596776afdb6377ac434c1c293ccb04" + +SSL TLS 1.3 Key schedule: HKDF RFC5869 Test Vector #4 Extract +depends_on:PSA_WANT_ALG_SHA_1 +psa_hkdf_extract:PSA_ALG_HMAC(PSA_ALG_SHA_1):"0b0b0b0b0b0b0b0b0b0b0b":"000102030405060708090a0b0c":"9b6c18c432a7bf8f0e71c8eb88f4b30baa2ba243" + +SSL TLS 1.3 Key schedule: HKDF RFC5869 Test Vector #5 Extract +depends_on:PSA_WANT_ALG_SHA_1 +psa_hkdf_extract:PSA_ALG_HMAC(PSA_ALG_SHA_1):"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f":"606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf":"8adae09a2a307059478d309b26c4115a224cfaf6" + +SSL TLS 1.3 Key schedule: HKDF RFC5869 Test Vector #6 Extract +depends_on:PSA_WANT_ALG_SHA_1 +psa_hkdf_extract:PSA_ALG_HMAC(PSA_ALG_SHA_1):"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b":"":"da8c8a73c7fa77288ec6f5e7c297786aa0d32d01" + +SSL TLS 1.3 Key schedule: HKDF RFC5869 Test Vector #7 Extract +depends_on:PSA_WANT_ALG_SHA_1 +psa_hkdf_extract:PSA_ALG_HMAC(PSA_ALG_SHA_1):"0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c":"":"2adccada18779e7c2077ad2eb19d3f3e731385dd" + +SSL TLS 1.3 Key schedule: HKDF extract fails with hash_len of 0 +psa_hkdf_extract_ret:0:MBEDTLS_ERR_SSL_BAD_INPUT_DATA + SSL TLS 1.3 Key schedule: HKDF RFC5869 Test Vector #1 Expand depends_on:PSA_WANT_ALG_SHA_256 psa_hkdf_expand:PSA_ALG_HMAC(PSA_ALG_SHA_256):"f0f1f2f3f4f5f6f7f8f9":"077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5":"3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865" diff --git a/tests/suites/test_suite_ssl.function b/tests/suites/test_suite_ssl.function index cb66f3afc..0122d46e5 100644 --- a/tests/suites/test_suite_ssl.function +++ b/tests/suites/test_suite_ssl.function @@ -3806,6 +3806,64 @@ exit: } /* END_CASE */ +/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_SSL_PROTO_TLS1_3 */ +void psa_hkdf_extract( int alg, char *hex_ikm_string, + char *hex_salt_string, char *hex_prk_string ) +{ + unsigned char *ikm = NULL; + unsigned char *salt = NULL; + unsigned char *prk = NULL; + unsigned char *output_prk = NULL; + size_t ikm_len, salt_len, prk_len, output_prk_len; + + output_prk_len = PSA_HASH_LENGTH( alg ); + output_prk = mbedtls_calloc( 1, output_prk_len ); + + ikm = mbedtls_test_unhexify_alloc( hex_ikm_string, &ikm_len ); + salt = mbedtls_test_unhexify_alloc( hex_salt_string, &salt_len ); + prk = mbedtls_test_unhexify_alloc( hex_prk_string, &prk_len ); + + PSA_ASSERT( psa_crypto_init() ); + PSA_ASSERT( mbedtls_psa_hkdf_extract( alg, salt, salt_len, + ikm, ikm_len, output_prk ) ); + + ASSERT_COMPARE( output_prk, output_prk_len, prk, prk_len ); + +exit: + mbedtls_free(ikm); + mbedtls_free(salt); + mbedtls_free(prk); + mbedtls_free(output_prk); + + PSA_DONE( ); +} +/* END_CASE */ + +/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_SSL_PROTO_TLS1_3 */ +void psa_hkdf_extract_ret( int hash_len, int ret ) +{ + int output_ret; + unsigned char *salt = NULL; + unsigned char *ikm = NULL; + unsigned char *prk = NULL; + size_t salt_len, ikm_len; + + prk = mbedtls_calloc( PSA_MAC_MAX_SIZE, 1 ); + salt_len = hash_len; + ikm_len = 0; + + PSA_ASSERT( psa_crypto_init() ); + output_ret = mbedtls_psa_hkdf_extract( 0, salt, salt_len, + ikm, ikm_len, prk ); + TEST_ASSERT( output_ret == ret ); + +exit: + mbedtls_free(prk); + + PSA_DONE( ); +} +/* END_CASE */ + /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_SSL_PROTO_TLS1_3 */ void psa_hkdf_expand( int alg, char *hex_info_string, char *hex_prk_string, char *hex_okm_string )