blob: ce69803f4e99957238814cb2f75590af72163567 [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 Coles8ff6df52021-07-21 12:42:15 +01005#include "mbedtls/entropy.h"
6#include "mbedtls/ctr_drbg.h"
7
8/* END_HEADER */
9
10/* BEGIN_DEPENDENCIES
Raef Colesab4f8742022-09-01 12:24:31 +010011 * depends_on:MBEDTLS_LMS_C:MBEDTLS_LMS_PRIVATE:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_CTR_DRBG_C
Raef Coles8ff6df52021-07-21 12:42:15 +010012 * END_DEPENDENCIES
13 */
14
15/* BEGIN_CASE */
16void lmots_sign_verify_test ( data_t * msg )
17{
Raef Coles01c71a12022-08-31 15:55:00 +010018 mbedtls_lmots_public_t pub_ctx;
19 mbedtls_lmots_private_t priv_ctx;
Raef Colese9479a02022-09-01 16:06:35 +010020 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Coles8ff6df52021-07-21 12:42:15 +010021 mbedtls_entropy_context entropy_ctx;
22 mbedtls_ctr_drbg_context drbg_ctx;
23 uint8_t seed[16];
24
25 mbedtls_entropy_init( &entropy_ctx );
26 mbedtls_ctr_drbg_init( &drbg_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +010027 mbedtls_lmots_init_public( &pub_ctx );
28 mbedtls_lmots_init_private( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010029
30 TEST_ASSERT( mbedtls_ctr_drbg_seed( &drbg_ctx, mbedtls_entropy_func,
31 &entropy_ctx, (uint8_t*)"", 0 ) == 0 );
32 TEST_ASSERT( mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ) == 0 );
33
Raef Coles01c71a12022-08-31 15:55:00 +010034 TEST_ASSERT( mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
35 (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 );
36 TEST_ASSERT( mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx) == 0 );
37 TEST_ASSERT( mbedtls_lmots_sign(&priv_ctx, mbedtls_ctr_drbg_random, &drbg_ctx,
38 msg->x, msg->len, sig, sizeof(sig), NULL ) == 0 );
39 TEST_ASSERT( mbedtls_lmots_verify(&pub_ctx, msg->x, msg->len, sig, sizeof(sig)) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010040
41exit:
42 mbedtls_entropy_free( &entropy_ctx );
43 mbedtls_ctr_drbg_free( &drbg_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +010044 mbedtls_lmots_free_public( &pub_ctx );
45 mbedtls_lmots_free_private( &priv_ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010046}
47/* END_CASE */
48
49/* BEGIN_CASE */
50void lmots_verify_test ( data_t * msg, data_t * sig, data_t * pub_key,
51 int expected_rc )
52{
Raef Coles01c71a12022-08-31 15:55:00 +010053 mbedtls_lmots_public_t ctx;
Raef Coles8ff6df52021-07-21 12:42:15 +010054
Raef Coles01c71a12022-08-31 15:55:00 +010055 mbedtls_lmots_init_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010056
Raef Coles01c71a12022-08-31 15:55:00 +010057 mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len );
Raef Coles8ff6df52021-07-21 12:42:15 +010058
Raef Coles01c71a12022-08-31 15:55:00 +010059 TEST_ASSERT(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ) == expected_rc );
Raef Coles8ff6df52021-07-21 12:42:15 +010060
61exit:
Raef Coles01c71a12022-08-31 15:55:00 +010062 mbedtls_lmots_free_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010063}
64/* END_CASE */
65
66/* BEGIN_CASE */
67void lmots_import_export_test ( data_t * pub_key )
68{
Raef Coles01c71a12022-08-31 15:55:00 +010069 mbedtls_lmots_public_t ctx;
Raef Colese9479a02022-09-01 16:06:35 +010070 uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Coles8ff6df52021-07-21 12:42:15 +010071
Raef Coles01c71a12022-08-31 15:55:00 +010072 mbedtls_lmots_init_public( &ctx );
73 TEST_ASSERT( mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ) == 0 );
74 TEST_ASSERT( mbedtls_lmots_export_public_key( &ctx, exported_pub_key, sizeof( exported_pub_key ), NULL ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010075
Raef Colese9479a02022-09-01 16:06:35 +010076 TEST_ASSERT( memcmp( pub_key->x, exported_pub_key,
77 MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8) ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +010078
79exit:
Raef Coles01c71a12022-08-31 15:55:00 +010080 mbedtls_lmots_free_public( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +010081}
82/* END_CASE */
83
84/* BEGIN_CASE */
85void lmots_reuse_test ( data_t * msg )
86{
Raef Coles01c71a12022-08-31 15:55:00 +010087 mbedtls_lmots_private_t ctx;
Raef Colese9479a02022-09-01 16:06:35 +010088 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
Raef Coles8ff6df52021-07-21 12:42:15 +010089 mbedtls_entropy_context entropy_ctx;
90 mbedtls_ctr_drbg_context drbg_ctx;
91 uint8_t seed[16];
92
93 mbedtls_entropy_init( &entropy_ctx );
94 mbedtls_ctr_drbg_init( &drbg_ctx );
95 TEST_ASSERT( mbedtls_ctr_drbg_seed(&drbg_ctx, mbedtls_entropy_func,
96 &entropy_ctx, (uint8_t*)"", 0 ) == 0 );
97
98 mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) );
99
Raef Coles01c71a12022-08-31 15:55:00 +0100100 mbedtls_lmots_init_private( &ctx );
101 TEST_ASSERT( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8,
102 (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 );
103 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx,
104 msg->x, msg->len, sig, sizeof( sig ), NULL ) == 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100105
106 /* Running another sign operation should fail, since the key should now have
107 * been erased.
108 */
Raef Coles01c71a12022-08-31 15:55:00 +0100109 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx,
110 msg->x, msg->len, sig, sizeof( sig ), NULL ) != 0 );
Raef Coles8ff6df52021-07-21 12:42:15 +0100111
112exit:
113 mbedtls_entropy_free( &entropy_ctx );
114 mbedtls_ctr_drbg_free( &drbg_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +0100115 mbedtls_lmots_free_private( &ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100116}
117/* END_CASE */