blob: 22c8eb5a122fb635a79c6e86d5aa4a67624b0035 [file] [log] [blame]
Raef Coles8ff6df52021-07-21 12:42:15 +01001/* BEGIN_HEADER */
2#include "mbedtls/lms.h"
3#include "mbedtls/entropy.h"
4#include "mbedtls/ctr_drbg.h"
5
6/* END_HEADER */
7
8/* BEGIN_DEPENDENCIES
Raef Colesab4f8742022-09-01 12:24:31 +01009 * depends_on:MBEDTLS_LMS_C:MBEDTLS_LMS_PRIVATE:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_CTR_DRBG_C
Raef Coles8ff6df52021-07-21 12:42:15 +010010 * END_DEPENDENCIES
11 */
12
13/* BEGIN_CASE */
14void lms_sign_verify_test ( data_t * msg )
15{
Raef Coles01c71a12022-08-31 15:55:00 +010016 mbedtls_lms_public_t pub_ctx;
17 mbedtls_lms_private_t priv_ctx;
Raef Coles8ff6df52021-07-21 12:42:15 +010018 unsigned char sig[MBEDTLS_LMS_SIG_LEN];
19 mbedtls_entropy_context entropy_ctx;
20 mbedtls_ctr_drbg_context drbg_ctx;
21 uint8_t seed[16];
22 int rc;
23
24 mbedtls_entropy_init( &entropy_ctx );
25 mbedtls_ctr_drbg_init( &drbg_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +010026 mbedtls_lms_init_public( &pub_ctx );
27 mbedtls_lms_init_private( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010028
29 TEST_ASSERT( mbedtls_ctr_drbg_seed( &drbg_ctx, mbedtls_entropy_func,
30 &entropy_ctx, ( uint8_t* )"", 0 ) == 0 );
31 TEST_ASSERT( mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ) == 0 );
32
Raef Coles8ff6df52021-07-21 12:42:15 +010033 /* Allocation failure isn't a test failure, since it likely just means there's not enough memory to run the test */
Raef Coles01c71a12022-08-31 15:55:00 +010034 rc = mbedtls_lms_generate_private_key( &priv_ctx, MBEDTLS_LMS_SHA256_M32_H10,
35 MBEDTLS_LMOTS_SHA256_N32_W8,
36 mbedtls_ctr_drbg_random, &drbg_ctx, seed,
37 sizeof( seed ) );
Raef Coles8ff6df52021-07-21 12:42:15 +010038 TEST_ASSUME( rc != MBEDTLS_ERR_LMS_ALLOC_FAILED );
39 TEST_ASSERT( rc == 0 );
40
Raef Coles01c71a12022-08-31 15:55:00 +010041 TEST_ASSERT( mbedtls_lms_calculate_public_key( &pub_ctx, &priv_ctx ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010042
Raef Coles01c71a12022-08-31 15:55:00 +010043 TEST_ASSERT( mbedtls_lms_sign( &priv_ctx, mbedtls_ctr_drbg_random,
44 &drbg_ctx, msg->x, msg->len, sig,
45 sizeof( sig ), NULL ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010046
Raef Coles01c71a12022-08-31 15:55:00 +010047 TEST_ASSERT( mbedtls_lms_verify( &pub_ctx, msg->x, msg->len, sig,
48 sizeof( sig ) ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010049
50exit:
51 mbedtls_entropy_free( &entropy_ctx );
52 mbedtls_ctr_drbg_free( &drbg_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +010053 mbedtls_lms_free_public( &pub_ctx );
54 mbedtls_lms_free_private( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010055}
56/* END_CASE */
57
58/* BEGIN_CASE */
59void lms_verify_test ( data_t * msg, data_t * sig, data_t * pub_key,
60 int expected_rc )
61{
Raef Coles01c71a12022-08-31 15:55:00 +010062 mbedtls_lms_public_t ctx;
Raef Coles8ff6df52021-07-21 12:42:15 +010063
Raef Coles01c71a12022-08-31 15:55:00 +010064 mbedtls_lms_init_public( &ctx);
Raef Coles8ff6df52021-07-21 12:42:15 +010065
Raef Coles01c71a12022-08-31 15:55:00 +010066 mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len );
Raef Coles8ff6df52021-07-21 12:42:15 +010067
Raef Coles01c71a12022-08-31 15:55:00 +010068 TEST_ASSERT( mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ) == expected_rc );
Raef Coles8ff6df52021-07-21 12:42:15 +010069
70exit:
Raef Coles01c71a12022-08-31 15:55:00 +010071 mbedtls_lms_free_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010072}
73/* END_CASE */
74
75/* BEGIN_CASE */
76void lms_import_export_test ( data_t * pub_key )
77{
Raef Coles01c71a12022-08-31 15:55:00 +010078 mbedtls_lms_public_t ctx;
79 uint8_t exported_pub_key[MBEDTLS_LMS_PUBLIC_KEY_LEN];
Raef Coles8ff6df52021-07-21 12:42:15 +010080
Raef Coles01c71a12022-08-31 15:55:00 +010081 mbedtls_lms_init_public(&ctx);
82 TEST_ASSERT( mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len ) == 0 );
83 TEST_ASSERT( mbedtls_lms_export_public_key( &ctx, exported_pub_key,
84 sizeof(exported_pub_key), NULL ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010085
Raef Coles01c71a12022-08-31 15:55:00 +010086 ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBLIC_KEY_LEN,
87 exported_pub_key, MBEDTLS_LMS_PUBLIC_KEY_LEN );
Raef Coles8ff6df52021-07-21 12:42:15 +010088
89exit:
Raef Coles01c71a12022-08-31 15:55:00 +010090 mbedtls_lms_free_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010091}
92/* END_CASE */
93