From 66edf6a833baa592758becceea2f9a4047ad5085 Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Wed, 12 Oct 2022 09:36:58 +0100 Subject: [PATCH] Use hsslms data for LMOTS import/export test Also, test that export fails when the buffer is too small. Signed-off-by: Raef Coles --- tests/suites/test_suite_lmots.data | 8 +++--- tests/suites/test_suite_lmots.function | 39 ++++++++++++++++++++------ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/tests/suites/test_suite_lmots.data b/tests/suites/test_suite_lmots.data index 7ae80e92f..8fdce0cd1 100644 --- a/tests/suites/test_suite_lmots.data +++ b/tests/suites/test_suite_lmots.data @@ -104,10 +104,10 @@ LMOTS negative test (invalid type) #2 lmots_verify_test:"0000000000000000000000000000000000000000":"0000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"0000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":MBEDTLS_ERR_LMS_BAD_INPUT_DATA LMOTS key import / export test -# This test uses a randomly generated LMOTS public key. It imports the key, and -# then exports it, and verifies that the exported key is identical to the -# original key. -lmots_import_export_test:"000000048440BBD3F47A167DD2D0E446DBCEA774000000138686E25BC07C69B43A2D3B7165DFF85C134177C876EA47D96FEF069BC96A981A" +# This test uses the valid public key for hsslms interop test 1, imports it, and +# then exports it. It also checks if the export correctly fails when the export +# buffer is too small. +lmots_import_export_test:"0000000447cc5b29dd0cecd01c382434a6d1686400000001f337dde97685d008a4440b59550277390018d3f1d485fa4b8c91796032de494b":0 LMOTS key reuse test # This test uses a fixed message, and then generates a private key, signs the diff --git a/tests/suites/test_suite_lmots.function b/tests/suites/test_suite_lmots.function index 27a74b6d3..367e55aaa 100644 --- a/tests/suites/test_suite_lmots.function +++ b/tests/suites/test_suite_lmots.function @@ -146,20 +146,41 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void lmots_import_export_test ( data_t * pub_key ) +void lmots_import_export_test ( data_t * pub_key, int expected_import_rc ) { mbedtls_lmots_public_t ctx; - uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)]; - size_t exported_pub_key_len; + unsigned char *exported_pub_key = NULL; + size_t exported_pub_key_buf_size; + size_t exported_pub_key_size; mbedtls_lmots_public_init( &ctx ); - TEST_EQUAL( mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ), 0 ); - TEST_EQUAL( mbedtls_lmots_export_public_key( &ctx, exported_pub_key, - sizeof( exported_pub_key ), - &exported_pub_key_len ), 0 ); + TEST_EQUAL( mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ), + expected_import_rc ); - ASSERT_COMPARE( pub_key->x, pub_key->len, - exported_pub_key, exported_pub_key_len ); + if( expected_import_rc == 0 ) + { + exported_pub_key_buf_size = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8); + ASSERT_ALLOC( exported_pub_key, exported_pub_key_buf_size ); + + TEST_EQUAL( mbedtls_lmots_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_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8), + exported_pub_key, MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8) ); + mbedtls_free(exported_pub_key); + exported_pub_key = NULL; + + /* Export into too-small buffer should fail */ + exported_pub_key_buf_size = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8) - 1; + ASSERT_ALLOC( exported_pub_key, exported_pub_key_buf_size); + TEST_EQUAL( mbedtls_lmots_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_lmots_public_free( &ctx );