blob: 27a74b6d361c14eb4af3e3597f16514c0061cc1d [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 ) {
Raef Coles9c9027b2022-09-02 18:26:31 +010015 return 1;
16 }
17 }
18
19 return 0;
20}
21#endif /* defined(MBEDTLS_TEST_HOOKS) */
22
Raef Coles8ff6df52021-07-21 12:42:15 +010023/* END_HEADER */
24
25/* BEGIN_DEPENDENCIES
Raef Coles5127e852022-10-07 10:35:56 +010026 * depends_on:MBEDTLS_LMS_C
Raef Coles8ff6df52021-07-21 12:42:15 +010027 * END_DEPENDENCIES
28 */
29
Raef Coles5127e852022-10-07 10:35:56 +010030/* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */
Raef Colesf5919e22022-09-02 16:05:10 +010031void lmots_sign_verify_test ( data_t *msg, data_t *key_id, int leaf_id,
32 data_t *seed )
Raef Coles8ff6df52021-07-21 12:42:15 +010033{
Raef Coles01c71a12022-08-31 15:55:00 +010034 mbedtls_lmots_public_t pub_ctx;
35 mbedtls_lmots_private_t priv_ctx;
Raef Colese9479a02022-09-01 16:06:35 +010036 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Coles8ff6df52021-07-21 12:42:15 +010037
Raef Colesbe3bdd82022-10-07 12:04:24 +010038 mbedtls_lmots_public_init( &pub_ctx );
39 mbedtls_lmots_private_init( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010040
Raef Coles810612e2022-10-11 13:16:53 +010041 TEST_EQUAL( mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
42 key_id->x, leaf_id, seed->x, seed->len ), 0 );
43 TEST_EQUAL( mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx), 0 );
44 TEST_EQUAL( mbedtls_lmots_sign(&priv_ctx, &mbedtls_test_rnd_std_rand, NULL,
45 msg->x, msg->len, sig, sizeof(sig), NULL ), 0 );
46 TEST_EQUAL( mbedtls_lmots_verify(&pub_ctx, msg->x, msg->len, sig, sizeof(sig)), 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010047
48exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +010049 mbedtls_lmots_public_free( &pub_ctx );
50 mbedtls_lmots_private_free( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010051}
52/* END_CASE */
53
Raef Coles5127e852022-10-07 10:35:56 +010054/* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */
Raef Coles9c9027b2022-09-02 18:26:31 +010055void lmots_sign_verify_null_msg_test ( data_t *key_id, int leaf_id, data_t *seed )
56{
57 mbedtls_lmots_public_t pub_ctx;
58 mbedtls_lmots_private_t priv_ctx;
59 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
60
Raef Colesbe3bdd82022-10-07 12:04:24 +010061 mbedtls_lmots_public_init( &pub_ctx );
62 mbedtls_lmots_private_init( &priv_ctx );
Raef Coles9c9027b2022-09-02 18:26:31 +010063
Raef Coles810612e2022-10-11 13:16:53 +010064 TEST_EQUAL( mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
65 key_id->x, leaf_id, seed->x, seed->len ), 0 );
66 TEST_EQUAL( mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx), 0 );
67 TEST_EQUAL( mbedtls_lmots_sign(&priv_ctx, &mbedtls_test_rnd_std_rand, NULL,
68 NULL, 0, sig, sizeof(sig), NULL ), 0 );
69 TEST_EQUAL( mbedtls_lmots_verify(&pub_ctx, NULL, 0, sig, sizeof(sig)), 0 );
Raef Coles9c9027b2022-09-02 18:26:31 +010070
71exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +010072 mbedtls_lmots_public_free( &pub_ctx );
73 mbedtls_lmots_private_free( &priv_ctx );
Raef Coles9c9027b2022-09-02 18:26:31 +010074}
75/* END_CASE */
76
77/* BEGIN_CASE */
Raef Colesf5919e22022-09-02 16:05:10 +010078void lmots_verify_test ( data_t *msg, data_t *sig, data_t *pub_key,
Raef Coles8b55ba62022-10-12 09:28:26 +010079 int expected_rc )
Raef Coles8ff6df52021-07-21 12:42:15 +010080{
Raef Coles01c71a12022-08-31 15:55:00 +010081 mbedtls_lmots_public_t ctx;
Raef Coles0dc604e2022-10-10 17:35:26 +010082 unsigned int size;
83 unsigned char *tmp_sig = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +010084
Raef Colesbe3bdd82022-10-07 12:04:24 +010085 mbedtls_lmots_public_init( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010086
Raef Coles0dc604e2022-10-10 17:35:26 +010087 TEST_EQUAL(mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ), 0);
Raef Coles8ff6df52021-07-21 12:42:15 +010088
Raef Coles0dc604e2022-10-10 17:35:26 +010089 TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ), expected_rc);
90
91 /* Test negative cases if the input data is valid */
92 if( expected_rc == 0 )
93 {
Raef Coles8b55ba62022-10-12 09:28:26 +010094 if( msg->len >= 1 )
95 {
96 /* Altering first message byte must cause verification failure */
97 msg->x[0] ^= 1;
98 TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ),
99 MBEDTLS_ERR_LMS_VERIFY_FAILED);
100 msg->x[0] ^= 1;
Raef Coles0dc604e2022-10-10 17:35:26 +0100101
Raef Coles8b55ba62022-10-12 09:28:26 +0100102 /* Altering last message byte must cause verification failure */
103 msg->x[msg->len - 1] ^= 1;
104 TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ),
105 MBEDTLS_ERR_LMS_VERIFY_FAILED);
106 msg->x[msg->len - 1] ^= 1;
107 }
Raef Coles0dc604e2022-10-10 17:35:26 +0100108
109 /* Altering first signature byte must cause verification failure */
110 sig->x[0] ^= 1;
111 TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ),
112 MBEDTLS_ERR_LMS_VERIFY_FAILED);
113 sig->x[0] ^= 1;
114
115 /* Altering first signature byte must cause verification failure */
116 sig->x[0] ^= 1;
117 TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ),
118 MBEDTLS_ERR_LMS_VERIFY_FAILED);
119 sig->x[0] ^= 1;
120
121 /* Altering last signature byte must cause verification failure */
122 sig->x[sig->len - 1] ^= 1;
123 TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ),
124 MBEDTLS_ERR_LMS_VERIFY_FAILED);
125 sig->x[sig->len - 1] ^= 1;
126
127 /* Signatures of all sizes must not verify, whether shorter or longer */
128 for( size = 0; size < sig->len; size++ ) {
129 if( size == sig->len )
130 continue;
131
132 ASSERT_ALLOC( tmp_sig, size );
133 if( tmp_sig != NULL )
134 memcpy( tmp_sig, sig->x, MIN(size, sig->len) );
135
136 TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, tmp_sig, size ),
137 MBEDTLS_ERR_LMS_VERIFY_FAILED);
138 mbedtls_free( tmp_sig );
139 tmp_sig = NULL;
140 }
141 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100142
143exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +0100144 mbedtls_lmots_public_free( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100145}
146/* END_CASE */
147
Raef Coles370cc432022-10-07 16:07:33 +0100148/* BEGIN_CASE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100149void lmots_import_export_test ( data_t * pub_key )
150{
Raef Coles01c71a12022-08-31 15:55:00 +0100151 mbedtls_lmots_public_t ctx;
Raef Colese9479a02022-09-01 16:06:35 +0100152 uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Colesf5919e22022-09-02 16:05:10 +0100153 size_t exported_pub_key_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100154
Raef Colesbe3bdd82022-10-07 12:04:24 +0100155 mbedtls_lmots_public_init( &ctx );
Raef Coles810612e2022-10-11 13:16:53 +0100156 TEST_EQUAL( mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ), 0 );
157 TEST_EQUAL( mbedtls_lmots_export_public_key( &ctx, exported_pub_key,
158 sizeof( exported_pub_key ),
159 &exported_pub_key_len ), 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100160
Raef Colesf5919e22022-09-02 16:05:10 +0100161 ASSERT_COMPARE( pub_key->x, pub_key->len,
162 exported_pub_key, exported_pub_key_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100163
164exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +0100165 mbedtls_lmots_public_free( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100166}
167/* END_CASE */
168
Raef Coles5127e852022-10-07 10:35:56 +0100169/* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */
Raef Colesf5919e22022-09-02 16:05:10 +0100170void lmots_reuse_test ( data_t *msg, data_t *key_id, int leaf_id, data_t *seed )
Raef Coles8ff6df52021-07-21 12:42:15 +0100171{
Raef Coles01c71a12022-08-31 15:55:00 +0100172 mbedtls_lmots_private_t ctx;
Raef Colese9479a02022-09-01 16:06:35 +0100173 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Coles8ff6df52021-07-21 12:42:15 +0100174
Raef Colesbe3bdd82022-10-07 12:04:24 +0100175 mbedtls_lmots_private_init( &ctx );
Raef Coles810612e2022-10-11 13:16:53 +0100176 TEST_EQUAL( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
177 key_id->x, leaf_id, seed->x,
178 seed->len ), 0 );
179 TEST_EQUAL( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL,
180 msg->x, msg->len, sig, sizeof( sig ), NULL ), 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100181
182 /* Running another sign operation should fail, since the key should now have
183 * been erased.
184 */
Raef Coles810612e2022-10-11 13:16:53 +0100185 TEST_EQUAL( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL,
186 msg->x, msg->len, sig, sizeof( sig ), NULL ), MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100187
188exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +0100189 mbedtls_lmots_private_free( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100190}
191/* END_CASE */
Raef Coles9c9027b2022-09-02 18:26:31 +0100192
193/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
194void lmots_signature_leak_test ( data_t *msg, data_t *key_id, int leaf_id,
195 data_t *seed )
196{
197 mbedtls_lmots_private_t ctx;
198 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
199
200 mbedtls_lmots_sign_private_key_invalidated_hook = &check_lmots_private_key_for_leak;
201
202 /* Fill with recognisable pattern */
203 memset( sig, 0x7E, sizeof( sig ) );
204
Raef Colesbe3bdd82022-10-07 12:04:24 +0100205 mbedtls_lmots_private_init( &ctx );
Raef Coles810612e2022-10-11 13:16:53 +0100206 TEST_EQUAL( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
207 key_id->x, leaf_id, seed->x,
208 seed->len ), 0 );
209 TEST_EQUAL( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL,
210 msg->x, msg->len, sig, sizeof( sig ), NULL ), 0 );
Raef Coles9c9027b2022-09-02 18:26:31 +0100211
212exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +0100213 mbedtls_lmots_private_free( &ctx );
Raef Coles9c9027b2022-09-02 18:26:31 +0100214 mbedtls_lmots_sign_private_key_invalidated_hook = NULL;
215}
216/* END_CASE */