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 | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 5 | #if defined(MBEDTLS_TEST_HOOKS) |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 6 | int check_lmots_private_key_for_leak(unsigned char * sig) |
| 7 | { |
| 8 | size_t idx; |
| 9 | |
| 10 | for( idx = MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(MBEDTLS_LMOTS_SHA256_N32_W8); |
| 11 | idx < MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8); |
| 12 | idx++ ) |
| 13 | { |
| 14 | if( sig[idx] != 0x7E ) { |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 15 | return 1; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | return 0; |
| 20 | } |
| 21 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 22 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 23 | /* END_HEADER */ |
| 24 | |
| 25 | /* BEGIN_DEPENDENCIES |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 26 | * depends_on:MBEDTLS_LMS_C |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 27 | * END_DEPENDENCIES |
| 28 | */ |
| 29 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 30 | /* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */ |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 31 | void lmots_sign_verify_test ( data_t *msg, data_t *key_id, int leaf_id, |
| 32 | data_t *seed ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 33 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 34 | mbedtls_lmots_public_t pub_ctx; |
| 35 | mbedtls_lmots_private_t priv_ctx; |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 36 | unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 37 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 38 | mbedtls_lmots_public_init( &pub_ctx ); |
| 39 | mbedtls_lmots_private_init( &priv_ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 40 | |
Raef Coles | 810612e | 2022-10-11 13:16:53 +0100 | [diff] [blame] | 41 | TEST_EQUAL( mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8, |
| 42 | key_id->x, leaf_id, seed->x, seed->len ), 0 ); |
| 43 | TEST_EQUAL( mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx), 0 ); |
| 44 | TEST_EQUAL( mbedtls_lmots_sign(&priv_ctx, &mbedtls_test_rnd_std_rand, NULL, |
| 45 | msg->x, msg->len, sig, sizeof(sig), NULL ), 0 ); |
| 46 | TEST_EQUAL( 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] | 47 | |
| 48 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 49 | mbedtls_lmots_public_free( &pub_ctx ); |
| 50 | mbedtls_lmots_private_free( &priv_ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 51 | } |
| 52 | /* END_CASE */ |
| 53 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 54 | /* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */ |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 55 | void lmots_sign_verify_null_msg_test ( data_t *key_id, int leaf_id, data_t *seed ) |
| 56 | { |
| 57 | mbedtls_lmots_public_t pub_ctx; |
| 58 | mbedtls_lmots_private_t priv_ctx; |
| 59 | unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)]; |
| 60 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 61 | mbedtls_lmots_public_init( &pub_ctx ); |
| 62 | mbedtls_lmots_private_init( &priv_ctx ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 63 | |
Raef Coles | 810612e | 2022-10-11 13:16:53 +0100 | [diff] [blame] | 64 | TEST_EQUAL( mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8, |
| 65 | key_id->x, leaf_id, seed->x, seed->len ), 0 ); |
| 66 | TEST_EQUAL( mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx), 0 ); |
| 67 | TEST_EQUAL( mbedtls_lmots_sign(&priv_ctx, &mbedtls_test_rnd_std_rand, NULL, |
| 68 | NULL, 0, sig, sizeof(sig), NULL ), 0 ); |
| 69 | TEST_EQUAL( mbedtls_lmots_verify(&pub_ctx, NULL, 0, sig, sizeof(sig)), 0 ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 70 | |
| 71 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 72 | mbedtls_lmots_public_free( &pub_ctx ); |
| 73 | mbedtls_lmots_private_free( &priv_ctx ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 74 | } |
| 75 | /* END_CASE */ |
| 76 | |
| 77 | /* BEGIN_CASE */ |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 78 | void lmots_verify_test ( data_t *msg, data_t *sig, data_t *pub_key, |
Raef Coles | 8b55ba6 | 2022-10-12 09:28:26 +0100 | [diff] [blame] | 79 | int expected_rc ) |
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_lmots_public_t ctx; |
Raef Coles | 0dc604e | 2022-10-10 17:35:26 +0100 | [diff] [blame] | 82 | unsigned int size; |
| 83 | unsigned char *tmp_sig = NULL; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 84 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 85 | mbedtls_lmots_public_init( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 86 | |
Raef Coles | 0dc604e | 2022-10-10 17:35:26 +0100 | [diff] [blame] | 87 | TEST_EQUAL(mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ), 0); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 88 | |
Raef Coles | 0dc604e | 2022-10-10 17:35:26 +0100 | [diff] [blame] | 89 | TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ), expected_rc); |
| 90 | |
| 91 | /* Test negative cases if the input data is valid */ |
| 92 | if( expected_rc == 0 ) |
| 93 | { |
Raef Coles | 8b55ba6 | 2022-10-12 09:28:26 +0100 | [diff] [blame] | 94 | if( msg->len >= 1 ) |
| 95 | { |
| 96 | /* Altering first message byte must cause verification failure */ |
| 97 | msg->x[0] ^= 1; |
| 98 | TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ), |
| 99 | MBEDTLS_ERR_LMS_VERIFY_FAILED); |
| 100 | msg->x[0] ^= 1; |
Raef Coles | 0dc604e | 2022-10-10 17:35:26 +0100 | [diff] [blame] | 101 | |
Raef Coles | 8b55ba6 | 2022-10-12 09:28:26 +0100 | [diff] [blame] | 102 | /* Altering last message byte must cause verification failure */ |
| 103 | msg->x[msg->len - 1] ^= 1; |
| 104 | TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ), |
| 105 | MBEDTLS_ERR_LMS_VERIFY_FAILED); |
| 106 | msg->x[msg->len - 1] ^= 1; |
| 107 | } |
Raef Coles | 0dc604e | 2022-10-10 17:35:26 +0100 | [diff] [blame] | 108 | |
| 109 | /* Altering first signature byte must cause verification failure */ |
| 110 | sig->x[0] ^= 1; |
| 111 | TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ), |
| 112 | MBEDTLS_ERR_LMS_VERIFY_FAILED); |
| 113 | sig->x[0] ^= 1; |
| 114 | |
| 115 | /* Altering first signature byte must cause verification failure */ |
| 116 | sig->x[0] ^= 1; |
| 117 | TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ), |
| 118 | MBEDTLS_ERR_LMS_VERIFY_FAILED); |
| 119 | sig->x[0] ^= 1; |
| 120 | |
| 121 | /* Altering last signature byte must cause verification failure */ |
| 122 | sig->x[sig->len - 1] ^= 1; |
| 123 | TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x, sig->len ), |
| 124 | MBEDTLS_ERR_LMS_VERIFY_FAILED); |
| 125 | sig->x[sig->len - 1] ^= 1; |
| 126 | |
| 127 | /* Signatures of all sizes must not verify, whether shorter or longer */ |
| 128 | for( size = 0; size < sig->len; size++ ) { |
| 129 | if( size == sig->len ) |
| 130 | continue; |
| 131 | |
| 132 | ASSERT_ALLOC( tmp_sig, size ); |
| 133 | if( tmp_sig != NULL ) |
| 134 | memcpy( tmp_sig, sig->x, MIN(size, sig->len) ); |
| 135 | |
| 136 | TEST_EQUAL(mbedtls_lmots_verify( &ctx, msg->x, msg->len, tmp_sig, size ), |
| 137 | MBEDTLS_ERR_LMS_VERIFY_FAILED); |
| 138 | mbedtls_free( tmp_sig ); |
| 139 | tmp_sig = NULL; |
| 140 | } |
| 141 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 142 | |
| 143 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 144 | mbedtls_lmots_public_free( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 145 | } |
| 146 | /* END_CASE */ |
| 147 | |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 148 | /* BEGIN_CASE */ |
Raef Coles | 66edf6a | 2022-10-12 09:36:58 +0100 | [diff] [blame] | 149 | void lmots_import_export_test ( data_t * pub_key, int expected_import_rc ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 150 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 151 | mbedtls_lmots_public_t ctx; |
Raef Coles | 66edf6a | 2022-10-12 09:36:58 +0100 | [diff] [blame] | 152 | unsigned char *exported_pub_key = NULL; |
| 153 | size_t exported_pub_key_buf_size; |
| 154 | size_t exported_pub_key_size; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 155 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 156 | mbedtls_lmots_public_init( &ctx ); |
Raef Coles | 66edf6a | 2022-10-12 09:36:58 +0100 | [diff] [blame] | 157 | TEST_EQUAL( mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ), |
| 158 | expected_import_rc ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 159 | |
Raef Coles | 66edf6a | 2022-10-12 09:36:58 +0100 | [diff] [blame] | 160 | if( expected_import_rc == 0 ) |
| 161 | { |
| 162 | exported_pub_key_buf_size = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8); |
| 163 | ASSERT_ALLOC( exported_pub_key, exported_pub_key_buf_size ); |
| 164 | |
| 165 | TEST_EQUAL( mbedtls_lmots_export_public_key( &ctx, exported_pub_key, |
| 166 | exported_pub_key_buf_size, |
| 167 | &exported_pub_key_size ), 0 ); |
| 168 | |
| 169 | TEST_EQUAL( exported_pub_key_buf_size, exported_pub_key_size ); |
| 170 | ASSERT_COMPARE( pub_key->x, MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8), |
| 171 | exported_pub_key, MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8) ); |
| 172 | mbedtls_free(exported_pub_key); |
| 173 | exported_pub_key = NULL; |
| 174 | |
| 175 | /* Export into too-small buffer should fail */ |
| 176 | exported_pub_key_buf_size = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8) - 1; |
| 177 | ASSERT_ALLOC( exported_pub_key, exported_pub_key_buf_size); |
| 178 | TEST_EQUAL( mbedtls_lmots_export_public_key( &ctx, exported_pub_key, |
| 179 | exported_pub_key_buf_size, NULL ), |
| 180 | MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 181 | mbedtls_free(exported_pub_key); |
| 182 | exported_pub_key = NULL; |
| 183 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 184 | |
| 185 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 186 | mbedtls_lmots_public_free( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 187 | } |
| 188 | /* END_CASE */ |
| 189 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 190 | /* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */ |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 191 | void lmots_reuse_test ( data_t *msg, data_t *key_id, int leaf_id, data_t *seed ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 192 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 193 | mbedtls_lmots_private_t ctx; |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 194 | unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 195 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 196 | mbedtls_lmots_private_init( &ctx ); |
Raef Coles | 810612e | 2022-10-11 13:16:53 +0100 | [diff] [blame] | 197 | TEST_EQUAL( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8, |
| 198 | key_id->x, leaf_id, seed->x, |
| 199 | seed->len ), 0 ); |
| 200 | TEST_EQUAL( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL, |
| 201 | msg->x, msg->len, sig, sizeof( sig ), NULL ), 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 202 | |
| 203 | /* Running another sign operation should fail, since the key should now have |
| 204 | * been erased. |
| 205 | */ |
Raef Coles | 810612e | 2022-10-11 13:16:53 +0100 | [diff] [blame] | 206 | TEST_EQUAL( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL, |
| 207 | msg->x, msg->len, sig, sizeof( sig ), NULL ), MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 208 | |
| 209 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 210 | mbedtls_lmots_private_free( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 211 | } |
| 212 | /* END_CASE */ |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 213 | |
| 214 | /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ |
| 215 | void lmots_signature_leak_test ( data_t *msg, data_t *key_id, int leaf_id, |
| 216 | data_t *seed ) |
| 217 | { |
| 218 | mbedtls_lmots_private_t ctx; |
| 219 | unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)]; |
| 220 | |
| 221 | mbedtls_lmots_sign_private_key_invalidated_hook = &check_lmots_private_key_for_leak; |
| 222 | |
| 223 | /* Fill with recognisable pattern */ |
| 224 | memset( sig, 0x7E, sizeof( sig ) ); |
| 225 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 226 | mbedtls_lmots_private_init( &ctx ); |
Raef Coles | 810612e | 2022-10-11 13:16:53 +0100 | [diff] [blame] | 227 | TEST_EQUAL( mbedtls_lmots_generate_private_key(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8, |
| 228 | key_id->x, leaf_id, seed->x, |
| 229 | seed->len ), 0 ); |
| 230 | TEST_EQUAL( mbedtls_lmots_sign(&ctx, mbedtls_test_rnd_std_rand, NULL, |
| 231 | msg->x, msg->len, sig, sizeof( sig ), NULL ), 0 ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 232 | |
| 233 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 234 | mbedtls_lmots_private_free( &ctx ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 235 | mbedtls_lmots_sign_private_key_invalidated_hook = NULL; |
| 236 | } |
| 237 | /* END_CASE */ |