blob: 2a9ea541a4e13238cea06ca7f27290da388ad434 [file] [log] [blame]
Raef Coles8ff6df52021-07-21 12:42:15 +01001/* BEGIN_HEADER */
Raef Coles7dce69a2022-08-24 14:07:06 +01002#include "lmots.h"
3#include "mbedtls/lms.h"
4
Raef Coles8ff6df52021-07-21 12:42:15 +01005/* END_HEADER */
6
7/* BEGIN_DEPENDENCIES
Raef Colesf5919e22022-09-02 16:05:10 +01008 * depends_on:MBEDTLS_LMS_C:MBEDTLS_LMS_PRIVATE:MBEDTLS_PSA_CRYPTO_C
Raef Coles8ff6df52021-07-21 12:42:15 +01009 * END_DEPENDENCIES
10 */
11
12/* BEGIN_CASE */
Raef Colesf5919e22022-09-02 16:05:10 +010013void lmots_sign_verify_test ( data_t *msg, data_t *key_id, int leaf_id,
14 data_t *seed )
Raef Coles8ff6df52021-07-21 12:42:15 +010015{
Raef Coles01c71a12022-08-31 15:55:00 +010016 mbedtls_lmots_public_t pub_ctx;
17 mbedtls_lmots_private_t priv_ctx;
Raef Colese9479a02022-09-01 16:06:35 +010018 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Coles8ff6df52021-07-21 12:42:15 +010019
Raef Coles01c71a12022-08-31 15:55:00 +010020 mbedtls_lmots_init_public( &pub_ctx );
21 mbedtls_lmots_init_private( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010022
Raef Coles01c71a12022-08-31 15:55:00 +010023 TEST_ASSERT( mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
Raef Colesf5919e22022-09-02 16:05:10 +010024 key_id->x, leaf_id, seed->x, seed->len ) == 0 );
Raef Coles01c71a12022-08-31 15:55:00 +010025 TEST_ASSERT( mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx) == 0 );
Raef Colesf5919e22022-09-02 16:05:10 +010026 TEST_ASSERT( mbedtls_lmots_sign(&priv_ctx, &mbedtls_test_rnd_std_rand, NULL,
Raef Coles01c71a12022-08-31 15:55:00 +010027 msg->x, msg->len, sig, sizeof(sig), NULL ) == 0 );
28 TEST_ASSERT( mbedtls_lmots_verify(&pub_ctx, msg->x, msg->len, sig, sizeof(sig)) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010029
30exit:
Raef Coles01c71a12022-08-31 15:55:00 +010031 mbedtls_lmots_free_public( &pub_ctx );
32 mbedtls_lmots_free_private( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010033}
34/* END_CASE */
35
36/* BEGIN_CASE */
Raef Colesf5919e22022-09-02 16:05:10 +010037void lmots_verify_test ( data_t *msg, data_t *sig, data_t *pub_key,
Raef Coles8ff6df52021-07-21 12:42:15 +010038 int expected_rc )
39{
Raef Coles01c71a12022-08-31 15:55:00 +010040 mbedtls_lmots_public_t ctx;
Raef Coles8ff6df52021-07-21 12:42:15 +010041
Raef Coles01c71a12022-08-31 15:55:00 +010042 mbedtls_lmots_init_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010043
Raef Coles01c71a12022-08-31 15:55:00 +010044 mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len );
Raef Coles8ff6df52021-07-21 12:42:15 +010045
Raef Coles01c71a12022-08-31 15:55:00 +010046 TEST_ASSERT(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ) == expected_rc );
Raef Coles8ff6df52021-07-21 12:42:15 +010047
48exit:
Raef Coles01c71a12022-08-31 15:55:00 +010049 mbedtls_lmots_free_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010050}
51/* END_CASE */
52
53/* BEGIN_CASE */
54void lmots_import_export_test ( data_t * pub_key )
55{
Raef Coles01c71a12022-08-31 15:55:00 +010056 mbedtls_lmots_public_t ctx;
Raef Colese9479a02022-09-01 16:06:35 +010057 uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Colesf5919e22022-09-02 16:05:10 +010058 size_t exported_pub_key_len;
Raef Coles8ff6df52021-07-21 12:42:15 +010059
Raef Coles01c71a12022-08-31 15:55:00 +010060 mbedtls_lmots_init_public( &ctx );
61 TEST_ASSERT( mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ) == 0 );
Raef Colesf5919e22022-09-02 16:05:10 +010062 TEST_ASSERT( mbedtls_lmots_export_public_key( &ctx, exported_pub_key,
63 sizeof( exported_pub_key ),
64 &exported_pub_key_len ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010065
Raef Colesf5919e22022-09-02 16:05:10 +010066 ASSERT_COMPARE( pub_key->x, pub_key->len,
67 exported_pub_key, exported_pub_key_len );
Raef Coles8ff6df52021-07-21 12:42:15 +010068
69exit:
Raef Coles01c71a12022-08-31 15:55:00 +010070 mbedtls_lmots_free_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010071}
72/* END_CASE */
73
74/* BEGIN_CASE */
Raef Colesf5919e22022-09-02 16:05:10 +010075void lmots_reuse_test ( data_t *msg, data_t *key_id, int leaf_id, data_t *seed )
Raef Coles8ff6df52021-07-21 12:42:15 +010076{
Raef Coles01c71a12022-08-31 15:55:00 +010077 mbedtls_lmots_private_t ctx;
Raef Colese9479a02022-09-01 16:06:35 +010078 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Coles8ff6df52021-07-21 12:42:15 +010079
Raef Coles01c71a12022-08-31 15:55:00 +010080 mbedtls_lmots_init_private( &ctx );
81 TEST_ASSERT( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
Raef Colesf5919e22022-09-02 16:05:10 +010082 key_id->x, leaf_id, seed->x,
83 seed->len ) == 0 );
84 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL,
Raef Coles01c71a12022-08-31 15:55:00 +010085 msg->x, msg->len, sig, sizeof( sig ), NULL ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010086
87 /* Running another sign operation should fail, since the key should now have
88 * been erased.
89 */
Raef Colesf5919e22022-09-02 16:05:10 +010090 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL,
Raef Coles01c71a12022-08-31 15:55:00 +010091 msg->x, msg->len, sig, sizeof( sig ), NULL ) != 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010092
93exit:
Raef Coles01c71a12022-08-31 15:55:00 +010094 mbedtls_lmots_free_private( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010095}
96/* END_CASE */