Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "mbedtls/lmots.h" |
| 3 | #include "mbedtls/entropy.h" |
| 4 | #include "mbedtls/ctr_drbg.h" |
| 5 | |
| 6 | /* END_HEADER */ |
| 7 | |
| 8 | /* BEGIN_DEPENDENCIES |
| 9 | * depends_on:MBEDTLS_LMOTS_C:MBEDTLS_SHA256_C:MBEDTLS_CTR_DRBG_C |
| 10 | * END_DEPENDENCIES |
| 11 | */ |
| 12 | |
| 13 | /* BEGIN_CASE */ |
| 14 | void lmots_sign_verify_test ( data_t * msg ) |
| 15 | { |
| 16 | mbedtls_lmots_context ctx; |
| 17 | unsigned char sig[MBEDTLS_LMOTS_SIG_LEN]; |
| 18 | mbedtls_entropy_context entropy_ctx; |
| 19 | mbedtls_ctr_drbg_context drbg_ctx; |
| 20 | uint8_t seed[16]; |
| 21 | |
| 22 | mbedtls_entropy_init( &entropy_ctx ); |
| 23 | mbedtls_ctr_drbg_init( &drbg_ctx ); |
| 24 | mbedtls_lmots_init( &ctx ); |
| 25 | |
| 26 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &drbg_ctx, mbedtls_entropy_func, |
| 27 | &entropy_ctx, (uint8_t*)"", 0 ) == 0 ); |
| 28 | TEST_ASSERT( mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ) == 0 ); |
| 29 | |
| 30 | TEST_ASSERT( mbedtls_lmots_set_algorithm_type(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8) == 0 ); |
| 31 | TEST_ASSERT( mbedtls_lmots_gen_privkey(&ctx, (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 ); |
| 32 | TEST_ASSERT( mbedtls_lmots_gen_pubkey(&ctx) == 0 ); |
| 33 | TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) == 0 ); |
| 34 | TEST_ASSERT( mbedtls_lmots_verify(&ctx, msg->x, msg->len, sig) == 0 ); |
| 35 | |
| 36 | exit: |
| 37 | mbedtls_entropy_free( &entropy_ctx ); |
| 38 | mbedtls_ctr_drbg_free( &drbg_ctx ); |
| 39 | mbedtls_lmots_free( &ctx ); |
| 40 | } |
| 41 | /* END_CASE */ |
| 42 | |
| 43 | /* BEGIN_CASE */ |
| 44 | void lmots_verify_test ( data_t * msg, data_t * sig, data_t * pub_key, |
| 45 | int expected_rc ) |
| 46 | { |
| 47 | mbedtls_lmots_context ctx; |
| 48 | |
| 49 | mbedtls_lmots_init( &ctx ); |
| 50 | |
| 51 | mbedtls_lmots_import_pubkey( &ctx, pub_key->x ); |
| 52 | |
| 53 | TEST_ASSERT(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x ) == expected_rc ); |
| 54 | |
| 55 | exit: |
| 56 | mbedtls_lmots_free( &ctx ); |
| 57 | } |
| 58 | /* END_CASE */ |
| 59 | |
| 60 | /* BEGIN_CASE */ |
| 61 | void lmots_import_export_test ( data_t * pub_key ) |
| 62 | { |
| 63 | mbedtls_lmots_context ctx; |
| 64 | uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBKEY_LEN]; |
| 65 | |
| 66 | mbedtls_lmots_init( &ctx ); |
| 67 | TEST_ASSERT( mbedtls_lmots_import_pubkey( &ctx, pub_key->x ) == 0 ); |
| 68 | TEST_ASSERT( mbedtls_lmots_export_pubkey( &ctx, exported_pub_key ) == 0 ); |
| 69 | |
| 70 | TEST_ASSERT( memcmp( pub_key->x, exported_pub_key, MBEDTLS_LMOTS_PUBKEY_LEN ) == 0 ); |
| 71 | |
| 72 | exit: |
| 73 | mbedtls_lmots_free( &ctx ); |
| 74 | } |
| 75 | /* END_CASE */ |
| 76 | |
| 77 | /* BEGIN_CASE */ |
| 78 | void lmots_reuse_test ( data_t * msg ) |
| 79 | { |
| 80 | mbedtls_lmots_context ctx; |
| 81 | unsigned char sig[MBEDTLS_LMOTS_SIG_LEN]; |
| 82 | mbedtls_entropy_context entropy_ctx; |
| 83 | mbedtls_ctr_drbg_context drbg_ctx; |
| 84 | uint8_t seed[16]; |
| 85 | |
| 86 | mbedtls_entropy_init( &entropy_ctx ); |
| 87 | mbedtls_ctr_drbg_init( &drbg_ctx ); |
| 88 | TEST_ASSERT( mbedtls_ctr_drbg_seed(&drbg_ctx, mbedtls_entropy_func, |
| 89 | &entropy_ctx, (uint8_t*)"", 0 ) == 0 ); |
| 90 | |
| 91 | mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ); |
| 92 | |
| 93 | mbedtls_lmots_init( &ctx ); |
| 94 | TEST_ASSERT( mbedtls_lmots_set_algorithm_type( &ctx, MBEDTLS_LMOTS_SHA256_N32_W8 ) == 0 ); |
| 95 | TEST_ASSERT( mbedtls_lmots_gen_privkey(&ctx, (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 ); |
| 96 | TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) == 0 ); |
| 97 | |
| 98 | /* Running another sign operation should fail, since the key should now have |
| 99 | * been erased. |
| 100 | */ |
| 101 | TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) != 0 ); |
| 102 | |
| 103 | exit: |
| 104 | mbedtls_entropy_free( &entropy_ctx ); |
| 105 | mbedtls_ctr_drbg_free( &drbg_ctx ); |
| 106 | mbedtls_lmots_free( &ctx ); |
| 107 | } |
| 108 | /* END_CASE */ |