2021-07-21 13:42:15 +02:00
|
|
|
/* BEGIN_HEADER */
|
2022-08-24 15:07:06 +02:00
|
|
|
#include "lmots.h"
|
|
|
|
#include "mbedtls/lms.h"
|
|
|
|
|
2021-07-21 13:42:15 +02:00
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2022-09-01 13:24:31 +02:00
|
|
|
* depends_on:MBEDTLS_LMS_C:MBEDTLS_LMS_PRIVATE:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_CTR_DRBG_C
|
2021-07-21 13:42:15 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void lmots_sign_verify_test ( data_t * msg )
|
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_public_t pub_ctx;
|
|
|
|
mbedtls_lmots_private_t priv_ctx;
|
2021-07-21 13:42:15 +02:00
|
|
|
unsigned char sig[MBEDTLS_LMOTS_SIG_LEN];
|
|
|
|
mbedtls_entropy_context entropy_ctx;
|
|
|
|
mbedtls_ctr_drbg_context drbg_ctx;
|
|
|
|
uint8_t seed[16];
|
|
|
|
|
|
|
|
mbedtls_entropy_init( &entropy_ctx );
|
|
|
|
mbedtls_ctr_drbg_init( &drbg_ctx );
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_init_public( &pub_ctx );
|
|
|
|
mbedtls_lmots_init_private( &priv_ctx );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_seed( &drbg_ctx, mbedtls_entropy_func,
|
|
|
|
&entropy_ctx, (uint8_t*)"", 0 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ) == 0 );
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
TEST_ASSERT( mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
|
|
|
|
(uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_lmots_sign(&priv_ctx, mbedtls_ctr_drbg_random, &drbg_ctx,
|
|
|
|
msg->x, msg->len, sig, sizeof(sig), NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_lmots_verify(&pub_ctx, msg->x, msg->len, sig, sizeof(sig)) == 0 );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_entropy_free( &entropy_ctx );
|
|
|
|
mbedtls_ctr_drbg_free( &drbg_ctx );
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_free_public( &pub_ctx );
|
|
|
|
mbedtls_lmots_free_private( &priv_ctx );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void lmots_verify_test ( data_t * msg, data_t * sig, data_t * pub_key,
|
|
|
|
int expected_rc )
|
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_public_t ctx;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_init_public( &ctx );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_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_lmots_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_lmots_free_public( &ctx );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void lmots_import_export_test ( data_t * pub_key )
|
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_public_t ctx;
|
|
|
|
uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN];
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_init_public( &ctx );
|
|
|
|
TEST_ASSERT( mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_lmots_export_public_key( &ctx, exported_pub_key, sizeof( exported_pub_key ), NULL ) == 0 );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
TEST_ASSERT( memcmp( pub_key->x, exported_pub_key, MBEDTLS_LMOTS_PUBLIC_KEY_LEN ) == 0 );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
exit:
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_free_public( &ctx );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void lmots_reuse_test ( data_t * msg )
|
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_private_t ctx;
|
2021-07-21 13:42:15 +02:00
|
|
|
unsigned char sig[MBEDTLS_LMOTS_SIG_LEN];
|
|
|
|
mbedtls_entropy_context entropy_ctx;
|
|
|
|
mbedtls_ctr_drbg_context drbg_ctx;
|
|
|
|
uint8_t seed[16];
|
|
|
|
|
|
|
|
mbedtls_entropy_init( &entropy_ctx );
|
|
|
|
mbedtls_ctr_drbg_init( &drbg_ctx );
|
|
|
|
TEST_ASSERT( mbedtls_ctr_drbg_seed(&drbg_ctx, mbedtls_entropy_func,
|
|
|
|
&entropy_ctx, (uint8_t*)"", 0 ) == 0 );
|
|
|
|
|
|
|
|
mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) );
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_init_private( &ctx );
|
|
|
|
TEST_ASSERT( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
|
|
|
|
(uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx,
|
|
|
|
msg->x, msg->len, sig, sizeof( sig ), NULL ) == 0 );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
/* Running another sign operation should fail, since the key should now have
|
|
|
|
* been erased.
|
|
|
|
*/
|
2022-08-31 16:55:00 +02:00
|
|
|
TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx,
|
|
|
|
msg->x, msg->len, sig, sizeof( sig ), NULL ) != 0 );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_entropy_free( &entropy_ctx );
|
|
|
|
mbedtls_ctr_drbg_free( &drbg_ctx );
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_free_private( &ctx );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|