From 36b6db08fe3dbb58ba2a45a6170f21b5149dfe26 Mon Sep 17 00:00:00 2001 From: Pauli Date: Mon, 24 May 2021 14:16:44 +1000 Subject: [PATCH] test: add evp_tests for the MAC size and block size Reviewed-by: Richard Levitte Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/15427) --- test/evp_test.c | 44 ++++++++++++++++++- .../recipes/30-test_evp_data/evpmac_blake.txt | 8 ++++ .../30-test_evp_data/evpmac_common.txt | 28 ++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/test/evp_test.c b/test/evp_test.c index abb0485459..bf4777eb56 100644 --- a/test/evp_test.c +++ b/test/evp_test.c @@ -1033,6 +1033,10 @@ typedef struct mac_data_st { int xof; /* Collection of controls */ STACK_OF(OPENSSL_STRING) *controls; + /* Output size */ + int output_size; + /* Block size */ + int block_size; } MAC_DATA; static int mac_test_init(EVP_TEST *t, const char *alg) @@ -1076,6 +1080,7 @@ static int mac_test_init(EVP_TEST *t, const char *alg) mdat->mac_name = OPENSSL_strdup(alg); mdat->mac = mac; mdat->controls = sk_OPENSSL_STRING_new_null(); + mdat->output_size = mdat->block_size = -1; t->data = mdat; return 1; } @@ -1130,6 +1135,18 @@ static int mac_test_parse(EVP_TEST *t, if (strcmp(keyword, "Ctrl") == 0) return sk_OPENSSL_STRING_push(mdata->controls, OPENSSL_strdup(value)) != 0; + if (strcmp(keyword, "OutputSize") == 0) { + mdata->output_size = atoi(value); + if (mdata->output_size < 0) + return -1; + return 1; + } + if (strcmp(keyword, "BlockSize") == 0) { + mdata->block_size = atoi(value); + if (mdata->block_size < 0) + return -1; + return 1; + } return 0; } @@ -1271,8 +1288,8 @@ static int mac_test_run_mac(EVP_TEST *t) EVP_MAC_CTX *ctx = NULL; unsigned char *got = NULL; size_t got_len; - int i; - OSSL_PARAM params[21]; + int i, block_size = -1, output_size = -1; + OSSL_PARAM params[21], sizes[3], *psizes = sizes; size_t params_n = 0; size_t params_n_allocstart = 0; const OSSL_PARAM *defined_params = @@ -1364,6 +1381,29 @@ static int mac_test_run_mac(EVP_TEST *t) t->err = "MAC_INIT_ERROR"; goto err; } + if (expected->output_size >= 0) + *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE, + &output_size); + if (expected->block_size >= 0) + *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE, + &block_size); + if (psizes != sizes) { + *psizes = OSSL_PARAM_construct_end(); + if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) { + t->err = "INTERNAL_ERROR"; + goto err; + } + if (expected->output_size >= 0 + && !TEST_int_eq(output_size, expected->output_size)) { + t->err = "TEST_FAILURE"; + goto err; + } + if (expected->block_size >= 0 + && !TEST_int_eq(block_size, expected->block_size)) { + t->err = "TEST_FAILURE"; + goto err; + } + } if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) { t->err = "MAC_UPDATE_ERROR"; goto err; diff --git a/test/recipes/30-test_evp_data/evpmac_blake.txt b/test/recipes/30-test_evp_data/evpmac_blake.txt index cd9a75e82e..13cb22fff3 100644 --- a/test/recipes/30-test_evp_data/evpmac_blake.txt +++ b/test/recipes/30-test_evp_data/evpmac_blake.txt @@ -17,16 +17,20 @@ MAC = BLAKE2BMAC Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f Input = Output = 10ebb67700b1868efb4417987acf4690ae9d972fb7a590c2f02871799aaa4786b5e996e8f0f4eb981fc214b005f42d2ff4233499391653df7aefcbc13fc51568 +OutputSize = 64 +BlockSize = 128 MAC = BLAKE2BMAC Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f Input = 00 Output = 961f6dd1e4dd30f63901690c512e78e4b45e4742ed197c3c5e45c549fd25f2e4187b0bc9fe30492b16b0d0bc4ef9b0f34c7003fac09a5ef1532e69430234cebd +BlockSize = 128 MAC = BLAKE2BMAC Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f Input = 0001 Output = da2cfbe2d8409a0f38026113884f84b50156371ae304c4430173d08a99d9fb1b983164a3770706d537f49e0c916d9f32b95cc37a95b99d857436f0232c88a965 +OutputSize = 64 MAC = BLAKE2BMAC Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f @@ -77,16 +81,20 @@ MAC = BLAKE2SMAC Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f Input = Output = 48a8997da407876b3d79c0d92325ad3b89cbb754d86ab71aee047ad345fd2c49 +OutputSize = 32 +BlockSize = 64 MAC = BLAKE2SMAC Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f Input = 00 Output = 40d15fee7c328830166ac3f918650f807e7e01e177258cdc0a39b11f598066f1 +BlockSize = 64 MAC = BLAKE2SMAC Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f Input = 0001 Output = 6bb71300644cd3991b26ccd4d274acd1adeab8b1d7914546c1198bbe9fc9d803 +OutputSize = 32 MAC = BLAKE2SMAC Key = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f diff --git a/test/recipes/30-test_evp_data/evpmac_common.txt b/test/recipes/30-test_evp_data/evpmac_common.txt index 0229659807..e2fbfac414 100644 --- a/test/recipes/30-test_evp_data/evpmac_common.txt +++ b/test/recipes/30-test_evp_data/evpmac_common.txt @@ -20,6 +20,8 @@ Algorithm = MD5 Key = 0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b Input = "Hi There" Output = 9294727a3638bb1c13f48ef8158bfc9d +OutputSize = 16 +BlockSize = 64 Availablein = default MAC = HMAC @@ -27,6 +29,7 @@ Algorithm = MD5 Key = "Jefe" Input = "what do ya want for nothing?" Output = 750c783e6ab0b503eaa86e310a5db738 +OutputSize = 16 Availablein = default MAC = HMAC @@ -34,6 +37,7 @@ Algorithm = MD5 Key = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Input = DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD Output = 56be34521d144c88dbb8c733f0e8b3f6 +BlockSize = 64 Title = SHA1 @@ -44,18 +48,22 @@ Algorithm = SHA1 Input = "Sample message for keylen=blocklen" Key = 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F Output = 5FD596EE78D5553C8FF4E72D266DFD192366DA29 +OutputSize = 20 +BlockSize = 64 MAC = HMAC Algorithm = SHA1 Input = "Sample message for keylen