2021-07-21 13:42:15 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/lms.h"
|
|
|
|
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2022-10-13 10:44:27 +02:00
|
|
|
* depends_on:MBEDTLS_LMS_C
|
2021-07-21 13:42:15 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2022-10-07 11:35:56 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void lms_sign_verify_test(data_t *msg, data_t *seed)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_public_t pub_ctx;
|
|
|
|
mbedtls_lms_private_t priv_ctx;
|
2022-09-01 17:06:35 +02:00
|
|
|
unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8)];
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_lms_public_init(&pub_ctx);
|
|
|
|
mbedtls_lms_private_init(&priv_ctx);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-02 17:05:10 +02:00
|
|
|
/* Allocation failure isn't a test failure, since it likely just means
|
|
|
|
* there's not enough memory to run the test.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_generate_private_key(&priv_ctx, MBEDTLS_LMS_SHA256_M32_H10,
|
|
|
|
MBEDTLS_LMOTS_SHA256_N32_W8,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
seed->x, seed->len), 0);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_calculate_public_key(&pub_ctx, &priv_ctx), 0);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_sign(&priv_ctx, mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
msg->x, msg->len, sig, sizeof(sig),
|
|
|
|
NULL), 0);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_verify(&pub_ctx, msg->x, msg->len, sig,
|
|
|
|
sizeof(sig)), 0);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_lms_public_free(&pub_ctx);
|
|
|
|
mbedtls_lms_private_free(&priv_ctx);
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-10-07 11:35:56 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void lms_sign_verify_null_msg_test(data_t *seed)
|
2022-09-02 19:26:31 +02:00
|
|
|
{
|
|
|
|
mbedtls_lms_public_t pub_ctx;
|
|
|
|
mbedtls_lms_private_t priv_ctx;
|
|
|
|
unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8)];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_lms_public_init(&pub_ctx);
|
|
|
|
mbedtls_lms_private_init(&priv_ctx);
|
2022-09-02 19:26:31 +02:00
|
|
|
|
|
|
|
/* Allocation failure isn't a test failure, since it likely just means
|
|
|
|
* there's not enough memory to run the test.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_generate_private_key(&priv_ctx, MBEDTLS_LMS_SHA256_M32_H10,
|
|
|
|
MBEDTLS_LMOTS_SHA256_N32_W8,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
seed->x, seed->len), 0);
|
2022-09-02 19:26:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_calculate_public_key(&pub_ctx, &priv_ctx), 0);
|
2022-09-02 19:26:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_sign(&priv_ctx, mbedtls_test_rnd_std_rand, NULL,
|
|
|
|
NULL, 0, sig, sizeof(sig),
|
|
|
|
NULL), 0);
|
2022-09-02 19:26:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_verify(&pub_ctx, NULL, 0, sig,
|
|
|
|
sizeof(sig)), 0);
|
2022-09-02 19:26:31 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_lms_public_free(&pub_ctx);
|
|
|
|
mbedtls_lms_private_free(&priv_ctx);
|
2022-09-02 19:26:31 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-07-21 13:42:15 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void lms_verify_test(data_t *msg, data_t *sig, data_t *pub_key,
|
|
|
|
int expected_rc)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_public_t ctx;
|
2022-10-10 18:35:26 +02:00
|
|
|
unsigned int size;
|
|
|
|
unsigned char *tmp_sig = NULL;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_lms_public_init(&ctx);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_import_public_key(&ctx, pub_key->x, pub_key->len), 0);
|
2022-10-10 18:35:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_verify(&ctx, msg->x, msg->len, sig->x, sig->len), expected_rc);
|
2022-10-10 18:35:26 +02:00
|
|
|
|
|
|
|
/* Test negative cases if the input data is valid */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_rc == 0) {
|
|
|
|
if (msg->len >= 1) {
|
2022-10-11 13:48:18 +02:00
|
|
|
/* Altering first message byte must cause verification failure */
|
|
|
|
msg->x[0] ^= 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_verify(&ctx, msg->x, msg->len, sig->x, sig->len),
|
2022-10-11 13:48:18 +02:00
|
|
|
MBEDTLS_ERR_LMS_VERIFY_FAILED);
|
|
|
|
msg->x[0] ^= 1;
|
|
|
|
|
|
|
|
/* Altering last message byte must cause verification failure */
|
|
|
|
msg->x[msg->len - 1] ^= 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_verify(&ctx, msg->x, msg->len, sig->x, sig->len),
|
2022-10-11 13:48:18 +02:00
|
|
|
MBEDTLS_ERR_LMS_VERIFY_FAILED);
|
|
|
|
msg->x[msg->len - 1] ^= 1;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig->len >= 1) {
|
2022-10-11 13:48:18 +02:00
|
|
|
/* Altering first signature byte must cause verification failure */
|
|
|
|
sig->x[0] ^= 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_verify(&ctx, msg->x, msg->len, sig->x, sig->len),
|
2022-10-11 13:48:18 +02:00
|
|
|
MBEDTLS_ERR_LMS_VERIFY_FAILED);
|
|
|
|
sig->x[0] ^= 1;
|
|
|
|
|
|
|
|
/* Altering last signature byte must cause verification failure */
|
|
|
|
sig->x[sig->len - 1] ^= 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_verify(&ctx, msg->x, msg->len, sig->x, sig->len),
|
2022-10-11 13:48:18 +02:00
|
|
|
MBEDTLS_ERR_LMS_VERIFY_FAILED);
|
|
|
|
sig->x[sig->len - 1] ^= 1;
|
|
|
|
}
|
2022-10-10 18:35:26 +02:00
|
|
|
|
|
|
|
/* Signatures of all sizes must not verify, whether shorter or longer */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size = 0; size < sig->len; size++) {
|
|
|
|
if (size == sig->len) {
|
2022-10-10 18:35:26 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-10-10 18:35:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(tmp_sig, size);
|
|
|
|
if (tmp_sig != NULL) {
|
|
|
|
memcpy(tmp_sig, sig->x, MIN(size, sig->len));
|
|
|
|
}
|
2022-10-10 18:35:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_verify(&ctx, msg->x, msg->len, tmp_sig, size),
|
2022-10-10 18:35:26 +02:00
|
|
|
MBEDTLS_ERR_LMS_VERIFY_FAILED);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(tmp_sig);
|
2022-10-10 18:35:26 +02:00
|
|
|
tmp_sig = NULL;
|
|
|
|
}
|
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(tmp_sig);
|
|
|
|
mbedtls_lms_public_free(&ctx);
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-10-07 17:07:33 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void lms_import_export_test(data_t *pub_key, int expected_import_rc)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_public_t ctx;
|
2022-10-11 16:34:56 +02:00
|
|
|
size_t exported_pub_key_buf_size = 0;
|
|
|
|
size_t exported_pub_key_size = 0;
|
|
|
|
unsigned char *exported_pub_key = NULL;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-10-07 13:04:24 +02:00
|
|
|
mbedtls_lms_public_init(&ctx);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_import_public_key(&ctx, pub_key->x, pub_key->len),
|
|
|
|
expected_import_rc);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_import_rc == 0) {
|
2022-10-11 16:34:56 +02:00
|
|
|
exported_pub_key_buf_size = MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10);
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(exported_pub_key, exported_pub_key_buf_size);
|
2022-10-11 16:34:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_lms_export_public_key(&ctx, exported_pub_key,
|
|
|
|
exported_pub_key_buf_size,
|
|
|
|
&exported_pub_key_size), 0);
|
2022-10-11 16:34:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(exported_pub_key_size,
|
|
|
|
MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10));
|
|
|
|
ASSERT_COMPARE(pub_key->x, pub_key->len,
|
|
|
|
exported_pub_key, exported_pub_key_size);
|
2022-10-11 16:34:56 +02:00
|
|
|
mbedtls_free(exported_pub_key);
|
|
|
|
exported_pub_key = NULL;
|
|
|
|
|
|
|
|
/* Export into too-small buffer should fail */
|
|
|
|
exported_pub_key_buf_size = MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) - 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(exported_pub_key, exported_pub_key_buf_size);
|
|
|
|
TEST_EQUAL(mbedtls_lms_export_public_key(&ctx, exported_pub_key,
|
|
|
|
exported_pub_key_buf_size, NULL),
|
|
|
|
MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL);
|
2022-10-11 16:34:56 +02:00
|
|
|
mbedtls_free(exported_pub_key);
|
|
|
|
exported_pub_key = NULL;
|
2022-10-13 10:41:39 +02:00
|
|
|
|
|
|
|
/* Export into too-large buffer should succeed */
|
|
|
|
exported_pub_key_buf_size = MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(exported_pub_key, exported_pub_key_buf_size);
|
|
|
|
TEST_EQUAL(mbedtls_lms_export_public_key(&ctx, exported_pub_key,
|
|
|
|
exported_pub_key_buf_size,
|
|
|
|
&exported_pub_key_size),
|
|
|
|
0);
|
|
|
|
ASSERT_COMPARE(pub_key->x, pub_key->len,
|
|
|
|
exported_pub_key, exported_pub_key_size);
|
2022-10-13 10:41:39 +02:00
|
|
|
mbedtls_free(exported_pub_key);
|
|
|
|
exported_pub_key = NULL;
|
2022-10-11 16:34:56 +02:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(exported_pub_key);
|
|
|
|
mbedtls_lms_public_free(&ctx);
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|