blob: 425e994de4204080fd7226d2a2ede63bdc6d2106 [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 Coles8ff6df52021-07-21 12:42:15 +010088
Raef Colesbe3bdd82022-10-07 12:04:24 +010089 mbedtls_lms_public_init( &ctx);
Raef Coles8ff6df52021-07-21 12:42:15 +010090
Raef Coles01c71a12022-08-31 15:55:00 +010091 mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len );
Raef Coles8ff6df52021-07-21 12:42:15 +010092
Raef Coles01c71a12022-08-31 15:55:00 +010093 TEST_ASSERT( mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ) == expected_rc );
Raef Coles8ff6df52021-07-21 12:42:15 +010094
95exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +010096 mbedtls_lms_public_free( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010097}
98/* END_CASE */
99
Raef Coles5127e852022-10-07 10:35:56 +0100100/* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100101void lms_import_export_test ( data_t * pub_key )
102{
Raef Coles01c71a12022-08-31 15:55:00 +0100103 mbedtls_lms_public_t ctx;
Raef Colese9479a02022-09-01 16:06:35 +0100104 uint8_t exported_pub_key[MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10)];
Raef Coles8ff6df52021-07-21 12:42:15 +0100105
Raef Colesbe3bdd82022-10-07 12:04:24 +0100106 mbedtls_lms_public_init(&ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100107 TEST_ASSERT( mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len ) == 0 );
108 TEST_ASSERT( mbedtls_lms_export_public_key( &ctx, exported_pub_key,
109 sizeof(exported_pub_key), NULL ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100110
Raef Colese9479a02022-09-01 16:06:35 +0100111 ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10),
112 exported_pub_key, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100113
114exit:
Raef Colesbe3bdd82022-10-07 12:04:24 +0100115 mbedtls_lms_public_free( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100116}
117/* END_CASE */
118