Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 1 | /* 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 Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 9 | * depends_on:MBEDTLS_LMS_C:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_CTR_DRBG_C |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 10 | * END_DEPENDENCIES |
| 11 | */ |
| 12 | |
| 13 | /* BEGIN_CASE */ |
| 14 | void lms_sign_verify_test ( data_t * msg ) |
| 15 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 16 | mbedtls_lms_public_t pub_ctx; |
| 17 | mbedtls_lms_private_t priv_ctx; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 18 | 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 Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 26 | mbedtls_lms_init_public( &pub_ctx ); |
| 27 | mbedtls_lms_init_private( &priv_ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 28 | |
| 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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 33 | /* Allocation failure isn't a test failure, since it likely just means there's not enough memory to run the test */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 34 | 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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 38 | TEST_ASSUME( rc != MBEDTLS_ERR_LMS_ALLOC_FAILED ); |
| 39 | TEST_ASSERT( rc == 0 ); |
| 40 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 41 | TEST_ASSERT( mbedtls_lms_calculate_public_key( &pub_ctx, &priv_ctx ) == 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 42 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 43 | 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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 46 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 47 | TEST_ASSERT( mbedtls_lms_verify( &pub_ctx, msg->x, msg->len, sig, |
| 48 | sizeof( sig ) ) == 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 49 | |
| 50 | exit: |
| 51 | mbedtls_entropy_free( &entropy_ctx ); |
| 52 | mbedtls_ctr_drbg_free( &drbg_ctx ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 53 | mbedtls_lms_free_public( &pub_ctx ); |
| 54 | mbedtls_lms_free_private( &priv_ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 55 | } |
| 56 | /* END_CASE */ |
| 57 | |
| 58 | /* BEGIN_CASE */ |
| 59 | void lms_verify_test ( data_t * msg, data_t * sig, data_t * pub_key, |
| 60 | int expected_rc ) |
| 61 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 62 | mbedtls_lms_public_t ctx; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 63 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 64 | mbedtls_lms_init_public( &ctx); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 65 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 66 | mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 67 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 68 | 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] | 69 | |
| 70 | exit: |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 71 | mbedtls_lms_free_public( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 72 | } |
| 73 | /* END_CASE */ |
| 74 | |
| 75 | /* BEGIN_CASE */ |
| 76 | void lms_import_export_test ( data_t * pub_key ) |
| 77 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 78 | mbedtls_lms_public_t ctx; |
| 79 | uint8_t exported_pub_key[MBEDTLS_LMS_PUBLIC_KEY_LEN]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 80 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 81 | 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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 85 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 86 | ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBLIC_KEY_LEN, |
| 87 | exported_pub_key, MBEDTLS_LMS_PUBLIC_KEY_LEN ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 88 | |
| 89 | exit: |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 90 | mbedtls_lms_free_public( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 91 | } |
| 92 | /* END_CASE */ |
| 93 | |