blob: 4492daaae107bc750a49fed863ebc5693ac85910 [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#include "mbedtls/entropy.h"
6#include "mbedtls/ctr_drbg.h"
7
8/* END_HEADER */
9
10/* BEGIN_DEPENDENCIES
Raef Colesc8f96042022-08-25 13:49:54 +010011 * depends_on:MBEDTLS_LMS_C:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_CTR_DRBG_C
Raef Coles8ff6df52021-07-21 12:42:15 +010012 * END_DEPENDENCIES
13 */
14
15/* BEGIN_CASE */
16void lmots_sign_verify_test ( data_t * msg )
17{
Raef Coles01c71a12022-08-31 15:55:00 +010018 mbedtls_lmots_public_t pub_ctx;
19 mbedtls_lmots_private_t priv_ctx;
Raef Coles8ff6df52021-07-21 12:42:15 +010020 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN];
21 mbedtls_entropy_context entropy_ctx;
22 mbedtls_ctr_drbg_context drbg_ctx;
23 uint8_t seed[16];
24
25 mbedtls_entropy_init( &entropy_ctx );
26 mbedtls_ctr_drbg_init( &drbg_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +010027 mbedtls_lmots_init_public( &pub_ctx );
28 mbedtls_lmots_init_private( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010029
30 TEST_ASSERT( mbedtls_ctr_drbg_seed( &drbg_ctx, mbedtls_entropy_func,
31 &entropy_ctx, (uint8_t*)"", 0 ) == 0 );
32 TEST_ASSERT( mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ) == 0 );
33
Raef Coles01c71a12022-08-31 15:55:00 +010034 TEST_ASSERT( mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
35 (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 );
36 TEST_ASSERT( mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx) == 0 );
37 TEST_ASSERT( mbedtls_lmots_sign(&priv_ctx, mbedtls_ctr_drbg_random, &drbg_ctx,
38 msg->x, msg->len, sig, sizeof(sig), NULL ) == 0 );
39 TEST_ASSERT( mbedtls_lmots_verify(&pub_ctx, msg->x, msg->len, sig, sizeof(sig)) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010040
41exit:
42 mbedtls_entropy_free( &entropy_ctx );
43 mbedtls_ctr_drbg_free( &drbg_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +010044 mbedtls_lmots_free_public( &pub_ctx );
45 mbedtls_lmots_free_private( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010046}
47/* END_CASE */
48
49/* BEGIN_CASE */
50void lmots_verify_test ( data_t * msg, data_t * sig, data_t * pub_key,
51 int expected_rc )
52{
Raef Coles01c71a12022-08-31 15:55:00 +010053 mbedtls_lmots_public_t ctx;
Raef Coles8ff6df52021-07-21 12:42:15 +010054
Raef Coles01c71a12022-08-31 15:55:00 +010055 mbedtls_lmots_init_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010056
Raef Coles01c71a12022-08-31 15:55:00 +010057 mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len );
Raef Coles8ff6df52021-07-21 12:42:15 +010058
Raef Coles01c71a12022-08-31 15:55:00 +010059 TEST_ASSERT(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ) == expected_rc );
Raef Coles8ff6df52021-07-21 12:42:15 +010060
61exit:
Raef Coles01c71a12022-08-31 15:55:00 +010062 mbedtls_lmots_free_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010063}
64/* END_CASE */
65
66/* BEGIN_CASE */
67void lmots_import_export_test ( data_t * pub_key )
68{
Raef Coles01c71a12022-08-31 15:55:00 +010069 mbedtls_lmots_public_t ctx;
70 uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN];
Raef Coles8ff6df52021-07-21 12:42:15 +010071
Raef Coles01c71a12022-08-31 15:55:00 +010072 mbedtls_lmots_init_public( &ctx );
73 TEST_ASSERT( mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ) == 0 );
74 TEST_ASSERT( mbedtls_lmots_export_public_key( &ctx, exported_pub_key, sizeof( exported_pub_key ), NULL ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010075
Raef Coles01c71a12022-08-31 15:55:00 +010076 TEST_ASSERT( memcmp( pub_key->x, exported_pub_key, MBEDTLS_LMOTS_PUBLIC_KEY_LEN ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010077
78exit:
Raef Coles01c71a12022-08-31 15:55:00 +010079 mbedtls_lmots_free_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010080}
81/* END_CASE */
82
83/* BEGIN_CASE */
84void lmots_reuse_test ( data_t * msg )
85{
Raef Coles01c71a12022-08-31 15:55:00 +010086 mbedtls_lmots_private_t ctx;
Raef Coles8ff6df52021-07-21 12:42:15 +010087 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN];
88 mbedtls_entropy_context entropy_ctx;
89 mbedtls_ctr_drbg_context drbg_ctx;
90 uint8_t seed[16];
91
92 mbedtls_entropy_init( &entropy_ctx );
93 mbedtls_ctr_drbg_init( &drbg_ctx );
94 TEST_ASSERT( mbedtls_ctr_drbg_seed(&drbg_ctx, mbedtls_entropy_func,
95 &entropy_ctx, (uint8_t*)"", 0 ) == 0 );
96
97 mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) );
98
Raef Coles01c71a12022-08-31 15:55:00 +010099 mbedtls_lmots_init_private( &ctx );
100 TEST_ASSERT( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
101 (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 );
102 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx,
103 msg->x, msg->len, sig, sizeof( sig ), NULL ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100104
105 /* Running another sign operation should fail, since the key should now have
106 * been erased.
107 */
Raef Coles01c71a12022-08-31 15:55:00 +0100108 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx,
109 msg->x, msg->len, sig, sizeof( sig ), NULL ) != 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100110
111exit:
112 mbedtls_entropy_free( &entropy_ctx );
113 mbedtls_ctr_drbg_free( &drbg_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +0100114 mbedtls_lmots_free_private( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100115}
116/* END_CASE */