Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Raef Coles | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 2 | #include "lmots.h" |
| 3 | #include "mbedtls/lms.h" |
| 4 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 5 | #include "mbedtls/entropy.h" |
| 6 | #include "mbedtls/ctr_drbg.h" |
| 7 | |
| 8 | /* END_HEADER */ |
| 9 | |
| 10 | /* BEGIN_DEPENDENCIES |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 11 | * 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] | 12 | * END_DEPENDENCIES |
| 13 | */ |
| 14 | |
| 15 | /* BEGIN_CASE */ |
| 16 | void lmots_sign_verify_test ( data_t * msg ) |
| 17 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 18 | mbedtls_lmots_public_t pub_ctx; |
| 19 | mbedtls_lmots_private_t priv_ctx; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 20 | unsigned char sig[MBEDTLS_LMOTS_SIG_LEN]; |
| 21 | 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 Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 27 | mbedtls_lmots_init_public( &pub_ctx ); |
| 28 | mbedtls_lmots_init_private( &priv_ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 29 | |
| 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 Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 34 | 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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 40 | |
| 41 | exit: |
| 42 | mbedtls_entropy_free( &entropy_ctx ); |
| 43 | mbedtls_ctr_drbg_free( &drbg_ctx ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 44 | mbedtls_lmots_free_public( &pub_ctx ); |
| 45 | mbedtls_lmots_free_private( &priv_ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 46 | } |
| 47 | /* END_CASE */ |
| 48 | |
| 49 | /* BEGIN_CASE */ |
| 50 | void lmots_verify_test ( data_t * msg, data_t * sig, data_t * pub_key, |
| 51 | int expected_rc ) |
| 52 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 53 | mbedtls_lmots_public_t ctx; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 54 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 55 | mbedtls_lmots_init_public( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 56 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 57 | mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 58 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 59 | TEST_ASSERT(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ) == expected_rc ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 60 | |
| 61 | exit: |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 62 | mbedtls_lmots_free_public( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 63 | } |
| 64 | /* END_CASE */ |
| 65 | |
| 66 | /* BEGIN_CASE */ |
| 67 | void lmots_import_export_test ( data_t * pub_key ) |
| 68 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 69 | mbedtls_lmots_public_t ctx; |
| 70 | uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 71 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 72 | 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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 75 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 76 | TEST_ASSERT( memcmp( pub_key->x, exported_pub_key, MBEDTLS_LMOTS_PUBLIC_KEY_LEN ) == 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 77 | |
| 78 | exit: |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 79 | mbedtls_lmots_free_public( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 80 | } |
| 81 | /* END_CASE */ |
| 82 | |
| 83 | /* BEGIN_CASE */ |
| 84 | void lmots_reuse_test ( data_t * msg ) |
| 85 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 86 | mbedtls_lmots_private_t ctx; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 87 | unsigned char sig[MBEDTLS_LMOTS_SIG_LEN]; |
| 88 | mbedtls_entropy_context entropy_ctx; |
| 89 | mbedtls_ctr_drbg_context drbg_ctx; |
| 90 | uint8_t seed[16]; |
| 91 | |
| 92 | mbedtls_entropy_init( &entropy_ctx ); |
| 93 | mbedtls_ctr_drbg_init( &drbg_ctx ); |
| 94 | TEST_ASSERT( mbedtls_ctr_drbg_seed(&drbg_ctx, mbedtls_entropy_func, |
| 95 | &entropy_ctx, (uint8_t*)"", 0 ) == 0 ); |
| 96 | |
| 97 | mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ); |
| 98 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 99 | mbedtls_lmots_init_private( &ctx ); |
| 100 | TEST_ASSERT( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8, |
| 101 | (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 ); |
| 102 | TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, |
| 103 | msg->x, msg->len, sig, sizeof( sig ), NULL ) == 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 104 | |
| 105 | /* Running another sign operation should fail, since the key should now have |
| 106 | * been erased. |
| 107 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 108 | TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, |
| 109 | msg->x, msg->len, sig, sizeof( sig ), NULL ) != 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 110 | |
| 111 | exit: |
| 112 | mbedtls_entropy_free( &entropy_ctx ); |
| 113 | mbedtls_ctr_drbg_free( &drbg_ctx ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 114 | mbedtls_lmots_free_private( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 115 | } |
| 116 | /* END_CASE */ |