Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "mbedtls/lms.h" |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 3 | |
| 4 | /* END_HEADER */ |
| 5 | |
| 6 | /* BEGIN_DEPENDENCIES |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 7 | * depends_on:MBEDTLS_LMS_C |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 8 | * END_DEPENDENCIES |
| 9 | */ |
| 10 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 11 | /* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */ |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 12 | void lms_sign_verify_test ( data_t *msg, data_t *seed ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 13 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 14 | mbedtls_lms_public_t pub_ctx; |
| 15 | mbedtls_lms_private_t priv_ctx; |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 16 | unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8)]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 17 | int rc; |
| 18 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame^] | 19 | mbedtls_lms_public_init( &pub_ctx ); |
| 20 | mbedtls_lms_private_init( &priv_ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 21 | |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 22 | /* Allocation failure isn't a test failure, since it likely just means |
| 23 | * there's not enough memory to run the test. |
| 24 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 25 | rc = mbedtls_lms_generate_private_key( &priv_ctx, MBEDTLS_LMS_SHA256_M32_H10, |
| 26 | MBEDTLS_LMOTS_SHA256_N32_W8, |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 27 | mbedtls_test_rnd_std_rand, NULL, |
| 28 | seed->x, seed->len ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 29 | TEST_ASSUME( rc != MBEDTLS_ERR_LMS_ALLOC_FAILED ); |
| 30 | TEST_ASSERT( rc == 0 ); |
| 31 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 32 | TEST_ASSERT( mbedtls_lms_calculate_public_key( &pub_ctx, &priv_ctx ) == 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 33 | |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 34 | 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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 37 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 38 | TEST_ASSERT( mbedtls_lms_verify( &pub_ctx, msg->x, msg->len, sig, |
| 39 | sizeof( sig ) ) == 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 40 | |
| 41 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame^] | 42 | mbedtls_lms_public_free( &pub_ctx ); |
| 43 | mbedtls_lms_private_free( &priv_ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 44 | } |
| 45 | /* END_CASE */ |
| 46 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 47 | /* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */ |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 48 | void 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 Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame^] | 55 | mbedtls_lms_public_init( &pub_ctx ); |
| 56 | mbedtls_lms_private_init( &priv_ctx ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 57 | |
| 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 | |
| 77 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame^] | 78 | mbedtls_lms_public_free( &pub_ctx ); |
| 79 | mbedtls_lms_private_free( &priv_ctx ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 80 | } |
| 81 | /* END_CASE */ |
| 82 | |
| 83 | /* BEGIN_CASE */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 84 | void lms_verify_test ( data_t * msg, data_t * sig, data_t * pub_key, |
| 85 | int expected_rc ) |
| 86 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 87 | mbedtls_lms_public_t ctx; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 88 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame^] | 89 | mbedtls_lms_public_init( &ctx); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 90 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 91 | mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 92 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 93 | TEST_ASSERT( mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ) == expected_rc ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 94 | |
| 95 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame^] | 96 | mbedtls_lms_public_free( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 97 | } |
| 98 | /* END_CASE */ |
| 99 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 100 | /* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 101 | void lms_import_export_test ( data_t * pub_key ) |
| 102 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 103 | mbedtls_lms_public_t ctx; |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 104 | uint8_t exported_pub_key[MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10)]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 105 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame^] | 106 | mbedtls_lms_public_init(&ctx); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 107 | 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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 110 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 111 | 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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 113 | |
| 114 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame^] | 115 | mbedtls_lms_public_free( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 116 | } |
| 117 | /* END_CASE */ |
| 118 | |