blob: 860304e02550245ad92c209b30258b49f7e7a0ce [file] [log] [blame]
Raef Coles8ff6df52021-07-21 12:42:15 +01001/* BEGIN_HEADER */
2#include "mbedtls/lms.h"
Raef Coles8ff6df52021-07-21 12:42:15 +01003
4/* END_HEADER */
5
6/* BEGIN_DEPENDENCIES
Raef Coles5127e852022-10-07 10:35:56 +01007 * depends_on:MBEDTLS_LMS_C
Raef Coles8ff6df52021-07-21 12:42:15 +01008 * END_DEPENDENCIES
9 */
10
Raef Coles5127e852022-10-07 10:35:56 +010011/* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */
Raef Colesf5919e22022-09-02 16:05:10 +010012void lms_sign_verify_test ( data_t *msg, data_t *seed )
Raef Coles8ff6df52021-07-21 12:42:15 +010013{
Raef Coles01c71a12022-08-31 15:55:00 +010014 mbedtls_lms_public_t pub_ctx;
15 mbedtls_lms_private_t priv_ctx;
Raef Colese9479a02022-09-01 16:06:35 +010016 unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Coles8ff6df52021-07-21 12:42:15 +010017 int rc;
18
Raef Colesbe3bdd82022-10-07 12:04:24 +010019 mbedtls_lms_public_init( &pub_ctx );
20 mbedtls_lms_private_init( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010021
Raef Colesf5919e22022-09-02 16:05:10 +010022 /* Allocation failure isn't a test failure, since it likely just means
23 * there's not enough memory to run the test.
24 */
Raef Coles01c71a12022-08-31 15:55:00 +010025 rc = mbedtls_lms_generate_private_key( &priv_ctx, MBEDTLS_LMS_SHA256_M32_H10,
26 MBEDTLS_LMOTS_SHA256_N32_W8,
Raef Colesf5919e22022-09-02 16:05:10 +010027 mbedtls_test_rnd_std_rand, NULL,
28 seed->x, seed->len );
Raef Coles8ff6df52021-07-21 12:42:15 +010029 TEST_ASSUME( rc != MBEDTLS_ERR_LMS_ALLOC_FAILED );
30 TEST_ASSERT( rc == 0 );
31
Raef Coles01c71a12022-08-31 15:55:00 +010032 TEST_ASSERT( mbedtls_lms_calculate_public_key( &pub_ctx, &priv_ctx ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010033
Raef Colesf5919e22022-09-02 16:05:10 +010034 TEST_ASSERT( mbedtls_lms_sign( &priv_ctx, mbedtls_test_rnd_std_rand, NULL,
35 msg->x, msg->len, sig, sizeof( sig ),
36 NULL ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010037
Raef Coles01c71a12022-08-31 15:55:00 +010038 TEST_ASSERT( mbedtls_lms_verify( &pub_ctx, msg->x, msg->len, sig,
39 sizeof( sig ) ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010040
41exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +010042 mbedtls_lms_public_free( &pub_ctx );
43 mbedtls_lms_private_free( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010044}
45/* END_CASE */
46
Raef Coles5127e852022-10-07 10:35:56 +010047/* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */
Raef Coles9c9027b2022-09-02 18:26:31 +010048void lms_sign_verify_null_msg_test( data_t *seed )
49{
50 mbedtls_lms_public_t pub_ctx;
51 mbedtls_lms_private_t priv_ctx;
52 unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8)];
53 int rc;
54
Raef Colesbe3bdd82022-10-07 12:04:24 +010055 mbedtls_lms_public_init( &pub_ctx );
56 mbedtls_lms_private_init( &priv_ctx );
Raef Coles9c9027b2022-09-02 18:26:31 +010057
58 /* Allocation failure isn't a test failure, since it likely just means
59 * there's not enough memory to run the test.
60 */
61 rc = mbedtls_lms_generate_private_key( &priv_ctx, MBEDTLS_LMS_SHA256_M32_H10,
62 MBEDTLS_LMOTS_SHA256_N32_W8,
63 mbedtls_test_rnd_std_rand, NULL,
64 seed->x, seed->len );
65 TEST_ASSUME( rc != MBEDTLS_ERR_LMS_ALLOC_FAILED );
66 TEST_ASSERT( rc == 0 );
67
68 TEST_ASSERT( mbedtls_lms_calculate_public_key( &pub_ctx, &priv_ctx ) == 0 );
69
70 TEST_ASSERT( mbedtls_lms_sign( &priv_ctx, mbedtls_test_rnd_std_rand, NULL,
71 NULL, 0, sig, sizeof( sig ),
72 NULL ) == 0 );
73
74 TEST_ASSERT( mbedtls_lms_verify( &pub_ctx, NULL, 0, sig,
75 sizeof( sig ) ) == 0 );
76
77exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +010078 mbedtls_lms_public_free( &pub_ctx );
79 mbedtls_lms_private_free( &priv_ctx );
Raef Coles9c9027b2022-09-02 18:26:31 +010080}
81/* END_CASE */
82
83/* BEGIN_CASE */
Raef Coles8ff6df52021-07-21 12:42:15 +010084void lms_verify_test ( data_t * msg, data_t * sig, data_t * pub_key,
85 int expected_rc )
86{
Raef Coles01c71a12022-08-31 15:55:00 +010087 mbedtls_lms_public_t ctx;
Raef Coles0dc604e2022-10-10 17:35:26 +010088 unsigned int size;
89 unsigned char *tmp_sig = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +010090
Raef Colesbe3bdd82022-10-07 12:04:24 +010091 mbedtls_lms_public_init( &ctx);
Raef Coles8ff6df52021-07-21 12:42:15 +010092
Raef Coles0dc604e2022-10-10 17:35:26 +010093 TEST_EQUAL(mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len ), 0);
Raef Coles8ff6df52021-07-21 12:42:15 +010094
Raef Coles0dc604e2022-10-10 17:35:26 +010095 TEST_EQUAL(mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ), expected_rc);
96
97 /* Test negative cases if the input data is valid */
98 if( expected_rc == 0 )
99 {
Raef Coles90e13fc2022-10-11 12:48:18 +0100100 if( msg->len >= 1 )
101 {
102 /* Altering first message byte must cause verification failure */
103 msg->x[0] ^= 1;
104 TEST_EQUAL(mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ),
105 MBEDTLS_ERR_LMS_VERIFY_FAILED);
106 msg->x[0] ^= 1;
Raef Coles0dc604e2022-10-10 17:35:26 +0100107
Raef Coles90e13fc2022-10-11 12:48:18 +0100108 /* Altering last message byte must cause verification failure */
109 msg->x[msg->len - 1] ^= 1;
110 TEST_EQUAL(mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ),
111 MBEDTLS_ERR_LMS_VERIFY_FAILED);
112 msg->x[msg->len - 1] ^= 1;
113 }
Raef Coles0dc604e2022-10-10 17:35:26 +0100114
Raef Coles90e13fc2022-10-11 12:48:18 +0100115 if( sig->len >= 1 )
116 {
117 /* Altering first signature byte must cause verification failure */
118 sig->x[0] ^= 1;
119 TEST_EQUAL(mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ),
120 MBEDTLS_ERR_LMS_VERIFY_FAILED);
121 sig->x[0] ^= 1;
Raef Coles0dc604e2022-10-10 17:35:26 +0100122
Raef Coles90e13fc2022-10-11 12:48:18 +0100123 /* Altering last signature byte must cause verification failure */
124 sig->x[sig->len - 1] ^= 1;
125 TEST_EQUAL(mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ),
126 MBEDTLS_ERR_LMS_VERIFY_FAILED);
127 sig->x[sig->len - 1] ^= 1;
128 }
Raef Coles0dc604e2022-10-10 17:35:26 +0100129
130 /* Signatures of all sizes must not verify, whether shorter or longer */
131 for( size = 0; size < sig->len; size++ ) {
132 if( size == sig->len )
133 continue;
134
135 ASSERT_ALLOC( tmp_sig, size );
136 if( tmp_sig != NULL )
137 memcpy( tmp_sig, sig->x, MIN(size, sig->len) );
138
139 TEST_EQUAL(mbedtls_lms_verify( &ctx, msg->x, msg->len, tmp_sig, size ),
140 MBEDTLS_ERR_LMS_VERIFY_FAILED);
141 mbedtls_free( tmp_sig );
142 tmp_sig = NULL;
143 }
144 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100145
146exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +0100147 mbedtls_lms_public_free( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100148}
149/* END_CASE */
150
Raef Coles370cc432022-10-07 16:07:33 +0100151/* BEGIN_CASE */
Raef Colesd6adcb62022-10-11 15:34:56 +0100152void lms_import_export_test ( data_t * pub_key, int expected_import_rc )
Raef Coles8ff6df52021-07-21 12:42:15 +0100153{
Raef Coles01c71a12022-08-31 15:55:00 +0100154 mbedtls_lms_public_t ctx;
Raef Colesd6adcb62022-10-11 15:34:56 +0100155 size_t exported_pub_key_buf_size = 0;
156 size_t exported_pub_key_size = 0;
157 unsigned char *exported_pub_key = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +0100158
Raef Colesbe3bdd82022-10-07 12:04:24 +0100159 mbedtls_lms_public_init(&ctx);
Raef Coles810612e2022-10-11 13:16:53 +0100160 TEST_EQUAL( mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len ), 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100161
Raef Colesd6adcb62022-10-11 15:34:56 +0100162 if( expected_import_rc == 0 )
163 {
164 exported_pub_key_buf_size = MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10);
165 ASSERT_ALLOC( exported_pub_key, exported_pub_key_buf_size );
166
167 TEST_EQUAL( mbedtls_lms_export_public_key( &ctx, exported_pub_key,
168 exported_pub_key_buf_size,
169 &exported_pub_key_size ), 0 );
170
171 TEST_EQUAL( exported_pub_key_buf_size, exported_pub_key_size );
172 ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10),
173 exported_pub_key, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) );
174 mbedtls_free(exported_pub_key);
175 exported_pub_key = NULL;
176
177 /* Export into too-small buffer should fail */
178 exported_pub_key_buf_size = MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) - 1;
179 ASSERT_ALLOC( exported_pub_key, exported_pub_key_buf_size);
180 TEST_EQUAL( mbedtls_lms_export_public_key( &ctx, exported_pub_key,
181 exported_pub_key_buf_size, NULL ),
182 MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
183 mbedtls_free(exported_pub_key);
184 exported_pub_key = NULL;
185 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100186
187exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +0100188 mbedtls_lms_public_free( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100189}
190/* END_CASE */
191