blob: b19d074fa709b1e294e10dfe8db104e6cbc3e8e2 [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
9 * depends_on:MBEDTLS_LMS_C:MBEDTLS_SHA256_C:MBEDTLS_CTR_DRBG_C
10 * END_DEPENDENCIES
11 */
12
13/* BEGIN_CASE */
14void lms_sign_verify_test ( data_t * msg )
15{
16 mbedtls_lms_context ctx;
17 unsigned char sig[MBEDTLS_LMS_SIG_LEN];
18 mbedtls_entropy_context entropy_ctx;
19 mbedtls_ctr_drbg_context drbg_ctx;
20 uint8_t seed[16];
21 int rc;
22
23 mbedtls_entropy_init( &entropy_ctx );
24 mbedtls_ctr_drbg_init( &drbg_ctx );
25 mbedtls_lms_init( &ctx );
26
27 TEST_ASSERT( mbedtls_ctr_drbg_seed( &drbg_ctx, mbedtls_entropy_func,
28 &entropy_ctx, ( uint8_t* )"", 0 ) == 0 );
29 TEST_ASSERT( mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ) == 0 );
30
31 TEST_ASSERT( mbedtls_lms_set_algorithm_type( &ctx, MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8 ) == 0 );
32
33 /* Allocation failure isn't a test failure, since it likely just means there's not enough memory to run the test */
34 rc = mbedtls_lms_gen_privkey( &ctx, mbedtls_ctr_drbg_random, &drbg_ctx, seed, sizeof( seed ) );
35 TEST_ASSUME( rc != MBEDTLS_ERR_LMS_ALLOC_FAILED );
36 TEST_ASSERT( rc == 0 );
37
38 TEST_ASSERT( mbedtls_lms_gen_pubkey( &ctx) == 0 );
39
40 TEST_ASSERT( mbedtls_lms_sign( &ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) == 0 );
41
42 TEST_ASSERT( mbedtls_lms_verify( &ctx, msg->x, msg->len, sig) == 0 );
43
44exit:
45 mbedtls_entropy_free( &entropy_ctx );
46 mbedtls_ctr_drbg_free( &drbg_ctx );
47 mbedtls_lms_free( &ctx );
48}
49/* END_CASE */
50
51/* BEGIN_CASE */
52void lms_verify_test ( data_t * msg, data_t * sig, data_t * pub_key,
53 int expected_rc )
54{
55 mbedtls_lms_context ctx;
56
57 mbedtls_lms_init( &ctx);
58
59 mbedtls_lms_import_pubkey( &ctx, pub_key->x );
60
61 TEST_ASSERT( mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x ) == expected_rc );
62
63exit:
64 mbedtls_lms_free( &ctx );
65}
66/* END_CASE */
67
68/* BEGIN_CASE */
69void lms_import_export_test ( data_t * pub_key )
70{
71 mbedtls_lms_context ctx;
72 uint8_t exported_pub_key[MBEDTLS_LMS_PUBKEY_LEN];
73
74 mbedtls_lms_init(&ctx);
75 TEST_ASSERT( mbedtls_lms_import_pubkey( &ctx, pub_key->x ) == 0 );
76 TEST_ASSERT( mbedtls_lms_export_pubkey( &ctx, exported_pub_key) == 0 );
77
78 ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBKEY_LEN,
79 exported_pub_key, MBEDTLS_LMS_PUBKEY_LEN );
80
81exit:
82 mbedtls_lms_free( &ctx );
83}
84/* END_CASE */
85