blob: 0f76aa6dfc114c5cc5e72adb464a645ab53d900d [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 Coles7dce69a2022-08-24 14:07:06 +010011 * depends_on:MBEDTLS_LMS_C:MBEDTLS_SHA256_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{
18 mbedtls_lmots_context ctx;
19 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN];
20 mbedtls_entropy_context entropy_ctx;
21 mbedtls_ctr_drbg_context drbg_ctx;
22 uint8_t seed[16];
23
24 mbedtls_entropy_init( &entropy_ctx );
25 mbedtls_ctr_drbg_init( &drbg_ctx );
26 mbedtls_lmots_init( &ctx );
27
28 TEST_ASSERT( mbedtls_ctr_drbg_seed( &drbg_ctx, mbedtls_entropy_func,
29 &entropy_ctx, (uint8_t*)"", 0 ) == 0 );
30 TEST_ASSERT( mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ) == 0 );
31
32 TEST_ASSERT( mbedtls_lmots_set_algorithm_type(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8) == 0 );
33 TEST_ASSERT( mbedtls_lmots_gen_privkey(&ctx, (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 );
34 TEST_ASSERT( mbedtls_lmots_gen_pubkey(&ctx) == 0 );
35 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) == 0 );
36 TEST_ASSERT( mbedtls_lmots_verify(&ctx, msg->x, msg->len, sig) == 0 );
37
38exit:
39 mbedtls_entropy_free( &entropy_ctx );
40 mbedtls_ctr_drbg_free( &drbg_ctx );
41 mbedtls_lmots_free( &ctx );
42}
43/* END_CASE */
44
45/* BEGIN_CASE */
46void lmots_verify_test ( data_t * msg, data_t * sig, data_t * pub_key,
47 int expected_rc )
48{
49 mbedtls_lmots_context ctx;
50
51 mbedtls_lmots_init( &ctx );
52
53 mbedtls_lmots_import_pubkey( &ctx, pub_key->x );
54
55 TEST_ASSERT(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x ) == expected_rc );
56
57exit:
58 mbedtls_lmots_free( &ctx );
59}
60/* END_CASE */
61
62/* BEGIN_CASE */
63void lmots_import_export_test ( data_t * pub_key )
64{
65 mbedtls_lmots_context ctx;
66 uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBKEY_LEN];
67
68 mbedtls_lmots_init( &ctx );
69 TEST_ASSERT( mbedtls_lmots_import_pubkey( &ctx, pub_key->x ) == 0 );
70 TEST_ASSERT( mbedtls_lmots_export_pubkey( &ctx, exported_pub_key ) == 0 );
71
72 TEST_ASSERT( memcmp( pub_key->x, exported_pub_key, MBEDTLS_LMOTS_PUBKEY_LEN ) == 0 );
73
74exit:
75 mbedtls_lmots_free( &ctx );
76}
77/* END_CASE */
78
79/* BEGIN_CASE */
80void lmots_reuse_test ( data_t * msg )
81{
82 mbedtls_lmots_context ctx;
83 unsigned char sig[MBEDTLS_LMOTS_SIG_LEN];
84 mbedtls_entropy_context entropy_ctx;
85 mbedtls_ctr_drbg_context drbg_ctx;
86 uint8_t seed[16];
87
88 mbedtls_entropy_init( &entropy_ctx );
89 mbedtls_ctr_drbg_init( &drbg_ctx );
90 TEST_ASSERT( mbedtls_ctr_drbg_seed(&drbg_ctx, mbedtls_entropy_func,
91 &entropy_ctx, (uint8_t*)"", 0 ) == 0 );
92
93 mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) );
94
95 mbedtls_lmots_init( &ctx );
96 TEST_ASSERT( mbedtls_lmots_set_algorithm_type( &ctx, MBEDTLS_LMOTS_SHA256_N32_W8 ) == 0 );
97 TEST_ASSERT( mbedtls_lmots_gen_privkey(&ctx, (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 );
98 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) == 0 );
99
100 /* Running another sign operation should fail, since the key should now have
101 * been erased.
102 */
103 TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) != 0 );
104
105exit:
106 mbedtls_entropy_free( &entropy_ctx );
107 mbedtls_ctr_drbg_free( &drbg_ctx );
108 mbedtls_lmots_free( &ctx );
109}
110/* END_CASE */