From d6adcb61467ee2146f89a0fdc069c47276f278db Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Tue, 11 Oct 2022 15:34:56 +0100 Subject: [PATCH] Add negative LMS import/export tests Signed-off-by: Raef Coles --- tests/suites/test_suite_lms.data | 8 +++++-- tests/suites/test_suite_lms.function | 34 +++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/tests/suites/test_suite_lms.data b/tests/suites/test_suite_lms.data index 5dd901982..5acf69caf 100644 --- a/tests/suites/test_suite_lms.data +++ b/tests/suites/test_suite_lms.data @@ -179,5 +179,9 @@ lms_verify_test:"bfff9cd687351db88a98c71fd2f9b927a0ee600130a112533b791041d30cb91 LMS import/export test # This test uses a randomly generated LMS public key. It imports the key, and # then exports it, and verifies that the exported key is identical to the -# original key. -lms_import_export_test:"00000006000000046B0927585C8547228D495361D73B970C287A2254BF8F1B170E55ACC9520A56CE5D2C711B6617718B49247D28CCC6D11D" +# original key. It also tests handling of too-small key export buffers. +lms_import_export_test:"0000000600000004d96bb26744d99ef624e32161c36d3d6efcdd0484e2b17a6dd183125be4b1af1cda931a91a3acb1151877c174f7943fd9":0 + +LMS import/export negative test #1 +# This test uses the randomly generated LMS public key +lms_import_export_test:"0000000600000004d96bb26744d99ef624e32161c36d3d6efcdd0484e2b17a6dd183125be4b1af1cda931a91a3acb1151877c174f7943fd9":0 diff --git a/tests/suites/test_suite_lms.function b/tests/suites/test_suite_lms.function index 7ba02c58a..860304e02 100644 --- a/tests/suites/test_suite_lms.function +++ b/tests/suites/test_suite_lms.function @@ -149,18 +149,40 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void lms_import_export_test ( data_t * pub_key ) +void lms_import_export_test ( data_t * pub_key, int expected_import_rc ) { mbedtls_lms_public_t ctx; - uint8_t exported_pub_key[MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10)]; + size_t exported_pub_key_buf_size = 0; + size_t exported_pub_key_size = 0; + unsigned char *exported_pub_key = NULL; mbedtls_lms_public_init(&ctx); TEST_EQUAL( mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len ), 0 ); - TEST_EQUAL( mbedtls_lms_export_public_key( &ctx, exported_pub_key, - sizeof(exported_pub_key), NULL ), 0 ); - ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10), - exported_pub_key, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) ); + if( expected_import_rc == 0 ) + { + exported_pub_key_buf_size = MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10); + ASSERT_ALLOC( exported_pub_key, exported_pub_key_buf_size ); + + TEST_EQUAL( mbedtls_lms_export_public_key( &ctx, exported_pub_key, + exported_pub_key_buf_size, + &exported_pub_key_size ), 0 ); + + TEST_EQUAL( exported_pub_key_buf_size, exported_pub_key_size ); + ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10), + exported_pub_key, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) ); + mbedtls_free(exported_pub_key); + exported_pub_key = NULL; + + /* Export into too-small buffer should fail */ + exported_pub_key_buf_size = MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) - 1; + ASSERT_ALLOC( exported_pub_key, exported_pub_key_buf_size); + TEST_EQUAL( mbedtls_lms_export_public_key( &ctx, exported_pub_key, + exported_pub_key_buf_size, NULL ), + MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); + mbedtls_free(exported_pub_key); + exported_pub_key = NULL; + } exit: mbedtls_lms_public_free( &ctx );