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