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 | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame] | 11 | * depends_on:MBEDTLS_LMS_C:MBEDTLS_LMS_PRIVATE: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 | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 20 | unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 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; |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 70 | uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)]; |
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 | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 76 | TEST_ASSERT( memcmp( pub_key->x, exported_pub_key, |
| 77 | MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8) ) == 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 78 | |
| 79 | exit: |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 80 | mbedtls_lmots_free_public( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 81 | } |
| 82 | /* END_CASE */ |
| 83 | |
| 84 | /* BEGIN_CASE */ |
| 85 | void lmots_reuse_test ( data_t * msg ) |
| 86 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 87 | mbedtls_lmots_private_t ctx; |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 88 | unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 89 | mbedtls_entropy_context entropy_ctx; |
| 90 | mbedtls_ctr_drbg_context drbg_ctx; |
| 91 | uint8_t seed[16]; |
| 92 | |
| 93 | mbedtls_entropy_init( &entropy_ctx ); |
| 94 | mbedtls_ctr_drbg_init( &drbg_ctx ); |
| 95 | TEST_ASSERT( mbedtls_ctr_drbg_seed(&drbg_ctx, mbedtls_entropy_func, |
| 96 | &entropy_ctx, (uint8_t*)"", 0 ) == 0 ); |
| 97 | |
| 98 | mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ); |
| 99 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 100 | mbedtls_lmots_init_private( &ctx ); |
| 101 | TEST_ASSERT( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8, |
| 102 | (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 ); |
| 103 | TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, |
| 104 | msg->x, msg->len, sig, sizeof( sig ), NULL ) == 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 105 | |
| 106 | /* Running another sign operation should fail, since the key should now have |
| 107 | * been erased. |
| 108 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 109 | TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, |
| 110 | msg->x, msg->len, sig, sizeof( sig ), NULL ) != 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 111 | |
| 112 | exit: |
| 113 | mbedtls_entropy_free( &entropy_ctx ); |
| 114 | mbedtls_ctr_drbg_free( &drbg_ctx ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 115 | mbedtls_lmots_free_private( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 116 | } |
| 117 | /* END_CASE */ |