2021-07-21 13:42:15 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/lms.h"
|
|
|
|
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2022-09-02 17:05:10 +02:00
|
|
|
* depends_on:MBEDTLS_LMS_C:MBEDTLS_LMS_PRIVATE:MBEDTLS_PSA_CRYPTO_C
|
2021-07-21 13:42:15 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2022-09-02 17:05:10 +02:00
|
|
|
void lms_sign_verify_test ( data_t *msg, data_t *seed )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_public_t pub_ctx;
|
|
|
|
mbedtls_lms_private_t priv_ctx;
|
2022-09-01 17:06:35 +02:00
|
|
|
unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8)];
|
2021-07-21 13:42:15 +02:00
|
|
|
int rc;
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_init_public( &pub_ctx );
|
|
|
|
mbedtls_lms_init_private( &priv_ctx );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-02 17:05:10 +02:00
|
|
|
/* Allocation failure isn't a test failure, since it likely just means
|
|
|
|
* there's not enough memory to run the test.
|
|
|
|
*/
|
2022-08-31 16:55:00 +02:00
|
|
|
rc = mbedtls_lms_generate_private_key( &priv_ctx, MBEDTLS_LMS_SHA256_M32_H10,
|
|
|
|
MBEDTLS_LMOTS_SHA256_N32_W8,
|
2022-09-02 17:05:10 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
seed->x, seed->len );
|
2021-07-21 13:42:15 +02:00
|
|
|
TEST_ASSUME( rc != MBEDTLS_ERR_LMS_ALLOC_FAILED );
|
|
|
|
TEST_ASSERT( rc == 0 );
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
TEST_ASSERT( mbedtls_lms_calculate_public_key( &pub_ctx, &priv_ctx ) == 0 );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-02 17:05:10 +02:00
|
|
|
TEST_ASSERT( mbedtls_lms_sign( &priv_ctx, mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
msg->x, msg->len, sig, sizeof( sig ),
|
|
|
|
NULL ) == 0 );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
TEST_ASSERT( mbedtls_lms_verify( &pub_ctx, msg->x, msg->len, sig,
|
|
|
|
sizeof( sig ) ) == 0 );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
exit:
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_free_public( &pub_ctx );
|
|
|
|
mbedtls_lms_free_private( &priv_ctx );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-09-02 19:26:31 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void lms_sign_verify_null_msg_test( data_t *seed )
|
|
|
|
{
|
|
|
|
mbedtls_lms_public_t pub_ctx;
|
|
|
|
mbedtls_lms_private_t priv_ctx;
|
|
|
|
unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8)];
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
mbedtls_lms_init_public( &pub_ctx );
|
|
|
|
mbedtls_lms_init_private( &priv_ctx );
|
|
|
|
|
|
|
|
/* Allocation failure isn't a test failure, since it likely just means
|
|
|
|
* there's not enough memory to run the test.
|
|
|
|
*/
|
|
|
|
rc = mbedtls_lms_generate_private_key( &priv_ctx, MBEDTLS_LMS_SHA256_M32_H10,
|
|
|
|
MBEDTLS_LMOTS_SHA256_N32_W8,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
seed->x, seed->len );
|
|
|
|
TEST_ASSUME( rc != MBEDTLS_ERR_LMS_ALLOC_FAILED );
|
|
|
|
TEST_ASSERT( rc == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_lms_calculate_public_key( &pub_ctx, &priv_ctx ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_lms_sign( &priv_ctx, mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
NULL, 0, sig, sizeof( sig ),
|
|
|
|
NULL ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_lms_verify( &pub_ctx, NULL, 0, sig,
|
|
|
|
sizeof( sig ) ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_lms_free_public( &pub_ctx );
|
|
|
|
mbedtls_lms_free_private( &priv_ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-07-21 13:42:15 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void lms_verify_test ( data_t * msg, data_t * sig, data_t * pub_key,
|
|
|
|
int expected_rc )
|
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_public_t ctx;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_init_public( &ctx);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
TEST_ASSERT( mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ) == expected_rc );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
exit:
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_free_public( &ctx );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void lms_import_export_test ( data_t * pub_key )
|
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_public_t ctx;
|
2022-09-01 17:06:35 +02:00
|
|
|
uint8_t exported_pub_key[MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10)];
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_init_public(&ctx);
|
|
|
|
TEST_ASSERT( mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_lms_export_public_key( &ctx, exported_pub_key,
|
|
|
|
sizeof(exported_pub_key), NULL ) == 0 );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
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) );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
exit:
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_free_public( &ctx );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|