blob: dd8a665d58e8c0cebedfdce22fa6e5fe9715deaa [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 Coles9c9027b2022-09-02 18:26:31 +01005#if defined(MBEDTLS_TEST_HOOKS)
Raef Coles9c9027b2022-09-02 18:26:31 +01006int check_lmots_private_key_for_leak(unsigned char * sig)
7{
8 size_t idx;
9
10 for( idx = MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(MBEDTLS_LMOTS_SHA256_N32_W8);
11 idx < MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8);
12 idx++ )
13 {
14 if( sig[idx] != 0x7E ) {
15 while(1){}
16 return 1;
17 }
18 }
19
20 return 0;
21}
22#endif /* defined(MBEDTLS_TEST_HOOKS) */
23
Raef Coles8ff6df52021-07-21 12:42:15 +010024/* END_HEADER */
25
26/* BEGIN_DEPENDENCIES
Raef Colesf5919e22022-09-02 16:05:10 +010027 * depends_on:MBEDTLS_LMS_C:MBEDTLS_LMS_PRIVATE:MBEDTLS_PSA_CRYPTO_C
Raef Coles8ff6df52021-07-21 12:42:15 +010028 * END_DEPENDENCIES
29 */
30
31/* BEGIN_CASE */
Raef Colesf5919e22022-09-02 16:05:10 +010032void lmots_sign_verify_test ( data_t *msg, data_t *key_id, int leaf_id,
33 data_t *seed )
Raef Coles8ff6df52021-07-21 12:42:15 +010034{
Raef Coles01c71a12022-08-31 15:55:00 +010035 mbedtls_lmots_public_t pub_ctx;
36 mbedtls_lmots_private_t priv_ctx;
Raef Colese9479a02022-09-01 16:06:35 +010037 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Coles8ff6df52021-07-21 12:42:15 +010038
Raef Coles01c71a12022-08-31 15:55:00 +010039 mbedtls_lmots_init_public( &pub_ctx );
40 mbedtls_lmots_init_private( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010041
Raef Coles01c71a12022-08-31 15:55:00 +010042 TEST_ASSERT( mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
Raef Colesf5919e22022-09-02 16:05:10 +010043 key_id->x, leaf_id, seed->x, seed->len ) == 0 );
Raef Coles01c71a12022-08-31 15:55:00 +010044 TEST_ASSERT( mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx) == 0 );
Raef Colesf5919e22022-09-02 16:05:10 +010045 TEST_ASSERT( mbedtls_lmots_sign(&priv_ctx, &mbedtls_test_rnd_std_rand, NULL,
Raef Coles01c71a12022-08-31 15:55:00 +010046 msg->x, msg->len, sig, sizeof(sig), NULL ) == 0 );
47 TEST_ASSERT( mbedtls_lmots_verify(&pub_ctx, msg->x, msg->len, sig, sizeof(sig)) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010048
49exit:
Raef Coles01c71a12022-08-31 15:55:00 +010050 mbedtls_lmots_free_public( &pub_ctx );
51 mbedtls_lmots_free_private( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010052}
53/* END_CASE */
54
55/* BEGIN_CASE */
Raef Coles9c9027b2022-09-02 18:26:31 +010056void lmots_sign_verify_null_msg_test ( data_t *key_id, int leaf_id, data_t *seed )
57{
58 mbedtls_lmots_public_t pub_ctx;
59 mbedtls_lmots_private_t priv_ctx;
60 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
61
62 mbedtls_lmots_init_public( &pub_ctx );
63 mbedtls_lmots_init_private( &priv_ctx );
64
65 TEST_ASSERT( mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
66 key_id->x, leaf_id, seed->x, seed->len ) == 0 );
67 TEST_ASSERT( mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx) == 0 );
68 TEST_ASSERT( mbedtls_lmots_sign(&priv_ctx, &mbedtls_test_rnd_std_rand, NULL,
69 NULL, 0, sig, sizeof(sig), NULL ) == 0 );
70 TEST_ASSERT( mbedtls_lmots_verify(&pub_ctx, NULL, 0, sig, sizeof(sig)) == 0 );
71
72exit:
73 mbedtls_lmots_free_public( &pub_ctx );
74 mbedtls_lmots_free_private( &priv_ctx );
75}
76/* END_CASE */
77
78/* BEGIN_CASE */
Raef Colesf5919e22022-09-02 16:05:10 +010079void lmots_verify_test ( data_t *msg, data_t *sig, data_t *pub_key,
Raef Coles8ff6df52021-07-21 12:42:15 +010080 int expected_rc )
81{
Raef Coles01c71a12022-08-31 15:55:00 +010082 mbedtls_lmots_public_t ctx;
Raef Coles8ff6df52021-07-21 12:42:15 +010083
Raef Coles01c71a12022-08-31 15:55:00 +010084 mbedtls_lmots_init_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010085
Raef Coles01c71a12022-08-31 15:55:00 +010086 mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len );
Raef Coles8ff6df52021-07-21 12:42:15 +010087
Raef Coles01c71a12022-08-31 15:55:00 +010088 TEST_ASSERT(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ) == expected_rc );
Raef Coles8ff6df52021-07-21 12:42:15 +010089
90exit:
Raef Coles01c71a12022-08-31 15:55:00 +010091 mbedtls_lmots_free_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010092}
93/* END_CASE */
94
95/* BEGIN_CASE */
96void lmots_import_export_test ( data_t * pub_key )
97{
Raef Coles01c71a12022-08-31 15:55:00 +010098 mbedtls_lmots_public_t ctx;
Raef Colese9479a02022-09-01 16:06:35 +010099 uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Colesf5919e22022-09-02 16:05:10 +0100100 size_t exported_pub_key_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100101
Raef Coles01c71a12022-08-31 15:55:00 +0100102 mbedtls_lmots_init_public( &ctx );
103 TEST_ASSERT( mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ) == 0 );
Raef Colesf5919e22022-09-02 16:05:10 +0100104 TEST_ASSERT( mbedtls_lmots_export_public_key( &ctx, exported_pub_key,
105 sizeof( exported_pub_key ),
106 &exported_pub_key_len ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100107
Raef Colesf5919e22022-09-02 16:05:10 +0100108 ASSERT_COMPARE( pub_key->x, pub_key->len,
109 exported_pub_key, exported_pub_key_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100110
111exit:
Raef Coles01c71a12022-08-31 15:55:00 +0100112 mbedtls_lmots_free_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100113}
114/* END_CASE */
115
116/* BEGIN_CASE */
Raef Colesf5919e22022-09-02 16:05:10 +0100117void lmots_reuse_test ( data_t *msg, data_t *key_id, int leaf_id, data_t *seed )
Raef Coles8ff6df52021-07-21 12:42:15 +0100118{
Raef Coles01c71a12022-08-31 15:55:00 +0100119 mbedtls_lmots_private_t ctx;
Raef Colese9479a02022-09-01 16:06:35 +0100120 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Coles8ff6df52021-07-21 12:42:15 +0100121
Raef Coles01c71a12022-08-31 15:55:00 +0100122 mbedtls_lmots_init_private( &ctx );
123 TEST_ASSERT( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
Raef Colesf5919e22022-09-02 16:05:10 +0100124 key_id->x, leaf_id, seed->x,
125 seed->len ) == 0 );
126 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL,
Raef Coles01c71a12022-08-31 15:55:00 +0100127 msg->x, msg->len, sig, sizeof( sig ), NULL ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100128
129 /* Running another sign operation should fail, since the key should now have
130 * been erased.
131 */
Raef Colesf5919e22022-09-02 16:05:10 +0100132 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL,
Raef Coles01c71a12022-08-31 15:55:00 +0100133 msg->x, msg->len, sig, sizeof( sig ), NULL ) != 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100134
135exit:
Raef Coles01c71a12022-08-31 15:55:00 +0100136 mbedtls_lmots_free_private( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100137}
138/* END_CASE */
Raef Coles9c9027b2022-09-02 18:26:31 +0100139
140/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
141void lmots_signature_leak_test ( data_t *msg, data_t *key_id, int leaf_id,
142 data_t *seed )
143{
144 mbedtls_lmots_private_t ctx;
145 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
146
147 mbedtls_lmots_sign_private_key_invalidated_hook = &check_lmots_private_key_for_leak;
148
149 /* Fill with recognisable pattern */
150 memset( sig, 0x7E, sizeof( sig ) );
151
152 mbedtls_lmots_init_private( &ctx );
153 TEST_ASSERT( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
154 key_id->x, leaf_id, seed->x,
155 seed->len ) == 0 );
156 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL,
157 msg->x, msg->len, sig, sizeof( sig ), NULL ) == 0 );
158
159exit:
160 mbedtls_lmots_free_private( &ctx );
161 mbedtls_lmots_sign_private_key_invalidated_hook = NULL;
162}
163/* END_CASE */