Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "mbedtls/lms.h" |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 3 | |
| 4 | /* END_HEADER */ |
| 5 | |
| 6 | /* BEGIN_DEPENDENCIES |
Raef Coles | 7656339 | 2022-10-12 15:50:57 +0100 | [diff] [blame] | 7 | * depends_on:MBEDTLS_LMS_C:PSA_WANT_ALG_SHA_256 |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 8 | * END_DEPENDENCIES |
| 9 | */ |
| 10 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 11 | /* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */ |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 12 | void lms_sign_verify_test ( data_t *msg, data_t *seed ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 13 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 14 | mbedtls_lms_public_t pub_ctx; |
| 15 | mbedtls_lms_private_t priv_ctx; |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 16 | unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8)]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 17 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 18 | mbedtls_lms_public_init( &pub_ctx ); |
| 19 | mbedtls_lms_private_init( &priv_ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 20 | |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 21 | /* Allocation failure isn't a test failure, since it likely just means |
| 22 | * there's not enough memory to run the test. |
| 23 | */ |
Raef Coles | d137c86 | 2022-10-12 15:55:25 +0100 | [diff] [blame] | 24 | TEST_EQUAL( mbedtls_lms_generate_private_key( &priv_ctx, MBEDTLS_LMS_SHA256_M32_H10, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 25 | MBEDTLS_LMOTS_SHA256_N32_W8, |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 26 | mbedtls_test_rnd_std_rand, NULL, |
Raef Coles | d137c86 | 2022-10-12 15:55:25 +0100 | [diff] [blame] | 27 | seed->x, seed->len ), 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 28 | |
Raef Coles | d0c7012 | 2022-10-12 14:19:52 +0100 | [diff] [blame] | 29 | TEST_EQUAL( mbedtls_lms_calculate_public_key( &pub_ctx, &priv_ctx ), 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 30 | |
Raef Coles | d0c7012 | 2022-10-12 14:19:52 +0100 | [diff] [blame] | 31 | TEST_EQUAL( mbedtls_lms_sign( &priv_ctx, mbedtls_test_rnd_std_rand, NULL, |
Raef Coles | f5919e2 | 2022-09-02 16:05:10 +0100 | [diff] [blame] | 32 | msg->x, msg->len, sig, sizeof( sig ), |
Raef Coles | d0c7012 | 2022-10-12 14:19:52 +0100 | [diff] [blame] | 33 | NULL ), 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 34 | |
Raef Coles | d0c7012 | 2022-10-12 14:19:52 +0100 | [diff] [blame] | 35 | TEST_EQUAL( mbedtls_lms_verify( &pub_ctx, msg->x, msg->len, sig, |
| 36 | sizeof( sig ) ), 0 ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 37 | |
| 38 | exit: |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 39 | mbedtls_lms_public_free( &pub_ctx ); |
| 40 | mbedtls_lms_private_free( &priv_ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 41 | } |
| 42 | /* END_CASE */ |
| 43 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 44 | /* BEGIN_CASE depends_on:MBEDTLS_LMS_PRIVATE */ |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 45 | void lms_sign_verify_null_msg_test( data_t *seed ) |
| 46 | { |
| 47 | mbedtls_lms_public_t pub_ctx; |
| 48 | mbedtls_lms_private_t priv_ctx; |
| 49 | unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8)]; |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 50 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 51 | mbedtls_lms_public_init( &pub_ctx ); |
| 52 | mbedtls_lms_private_init( &priv_ctx ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 53 | |
| 54 | /* Allocation failure isn't a test failure, since it likely just means |
| 55 | * there's not enough memory to run the test. |
| 56 | */ |
Raef Coles | d137c86 | 2022-10-12 15:55:25 +0100 | [diff] [blame] | 57 | TEST_EQUAL( mbedtls_lms_generate_private_key( &priv_ctx, MBEDTLS_LMS_SHA256_M32_H10, |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 58 | MBEDTLS_LMOTS_SHA256_N32_W8, |
| 59 | mbedtls_test_rnd_std_rand, NULL, |
Raef Coles | d137c86 | 2022-10-12 15:55:25 +0100 | [diff] [blame] | 60 | seed->x, seed->len ), 0 ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 61 | |
Raef Coles | d0c7012 | 2022-10-12 14:19:52 +0100 | [diff] [blame] | 62 | TEST_EQUAL( mbedtls_lms_calculate_public_key( &pub_ctx, &priv_ctx ), 0 ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 63 | |
Raef Coles | d0c7012 | 2022-10-12 14:19:52 +0100 | [diff] [blame] | 64 | TEST_EQUAL( mbedtls_lms_sign( &priv_ctx, mbedtls_test_rnd_std_rand, NULL, |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 65 | NULL, 0, sig, sizeof( sig ), |
Raef Coles | d0c7012 | 2022-10-12 14:19:52 +0100 | [diff] [blame] | 66 | NULL ), 0 ); |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 67 | |
Raef Coles | d0c7012 | 2022-10-12 14:19:52 +0100 | [diff] [blame] | 68 | TEST_EQUAL( mbedtls_lms_verify( &pub_ctx, NULL, 0, sig, |
| 69 | 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_lms_public_free( &pub_ctx ); |
| 73 | mbedtls_lms_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 | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 78 | void lms_verify_test ( data_t * msg, data_t * sig, data_t * pub_key, |
| 79 | int expected_rc ) |
| 80 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 81 | mbedtls_lms_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_lms_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_lms_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_lms_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 | 90e13fc | 2022-10-11 12:48:18 +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_lms_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 | 90e13fc | 2022-10-11 12:48:18 +0100 | [diff] [blame] | 102 | /* Altering last message byte must cause verification failure */ |
| 103 | msg->x[msg->len - 1] ^= 1; |
| 104 | TEST_EQUAL(mbedtls_lms_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 | |
Raef Coles | 90e13fc | 2022-10-11 12:48:18 +0100 | [diff] [blame] | 109 | if( sig->len >= 1 ) |
| 110 | { |
| 111 | /* Altering first signature byte must cause verification failure */ |
| 112 | sig->x[0] ^= 1; |
| 113 | TEST_EQUAL(mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ), |
| 114 | MBEDTLS_ERR_LMS_VERIFY_FAILED); |
| 115 | sig->x[0] ^= 1; |
Raef Coles | 0dc604e | 2022-10-10 17:35:26 +0100 | [diff] [blame] | 116 | |
Raef Coles | 90e13fc | 2022-10-11 12:48:18 +0100 | [diff] [blame] | 117 | /* Altering last signature byte must cause verification failure */ |
| 118 | sig->x[sig->len - 1] ^= 1; |
| 119 | TEST_EQUAL(mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x, sig->len ), |
| 120 | MBEDTLS_ERR_LMS_VERIFY_FAILED); |
| 121 | sig->x[sig->len - 1] ^= 1; |
| 122 | } |
Raef Coles | 0dc604e | 2022-10-10 17:35:26 +0100 | [diff] [blame] | 123 | |
| 124 | /* Signatures of all sizes must not verify, whether shorter or longer */ |
| 125 | for( size = 0; size < sig->len; size++ ) { |
| 126 | if( size == sig->len ) |
| 127 | continue; |
| 128 | |
| 129 | ASSERT_ALLOC( tmp_sig, size ); |
| 130 | if( tmp_sig != NULL ) |
| 131 | memcpy( tmp_sig, sig->x, MIN(size, sig->len) ); |
| 132 | |
| 133 | TEST_EQUAL(mbedtls_lms_verify( &ctx, msg->x, msg->len, tmp_sig, size ), |
| 134 | MBEDTLS_ERR_LMS_VERIFY_FAILED); |
| 135 | mbedtls_free( tmp_sig ); |
| 136 | tmp_sig = NULL; |
| 137 | } |
| 138 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 139 | |
| 140 | exit: |
Raef Coles | 20d2e06 | 2022-10-13 09:28:18 +0100 | [diff] [blame] | 141 | if( tmp_sig != NULL ) |
| 142 | mbedtls_free( tmp_sig ); |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 143 | mbedtls_lms_public_free( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 144 | } |
| 145 | /* END_CASE */ |
| 146 | |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 147 | /* BEGIN_CASE */ |
Raef Coles | d6adcb6 | 2022-10-11 15:34:56 +0100 | [diff] [blame] | 148 | void lms_import_export_test ( data_t * pub_key, int expected_import_rc ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 149 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 150 | mbedtls_lms_public_t ctx; |
Raef Coles | d6adcb6 | 2022-10-11 15:34:56 +0100 | [diff] [blame] | 151 | size_t exported_pub_key_buf_size = 0; |
| 152 | size_t exported_pub_key_size = 0; |
| 153 | unsigned char *exported_pub_key = NULL; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 154 | |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 155 | mbedtls_lms_public_init(&ctx); |
Raef Coles | f9b8502 | 2022-10-12 12:42:28 +0100 | [diff] [blame] | 156 | TEST_EQUAL( mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len ), |
| 157 | expected_import_rc ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 158 | |
Raef Coles | d6adcb6 | 2022-10-11 15:34:56 +0100 | [diff] [blame] | 159 | if( expected_import_rc == 0 ) |
| 160 | { |
| 161 | exported_pub_key_buf_size = MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10); |
| 162 | ASSERT_ALLOC( exported_pub_key, exported_pub_key_buf_size ); |
| 163 | |
| 164 | TEST_EQUAL( mbedtls_lms_export_public_key( &ctx, exported_pub_key, |
| 165 | exported_pub_key_buf_size, |
| 166 | &exported_pub_key_size ), 0 ); |
| 167 | |
| 168 | TEST_EQUAL( exported_pub_key_buf_size, exported_pub_key_size ); |
| 169 | ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10), |
| 170 | exported_pub_key, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) ); |
| 171 | mbedtls_free(exported_pub_key); |
| 172 | exported_pub_key = NULL; |
| 173 | |
| 174 | /* Export into too-small buffer should fail */ |
| 175 | exported_pub_key_buf_size = MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) - 1; |
| 176 | ASSERT_ALLOC( exported_pub_key, exported_pub_key_buf_size); |
| 177 | TEST_EQUAL( mbedtls_lms_export_public_key( &ctx, exported_pub_key, |
| 178 | exported_pub_key_buf_size, NULL ), |
| 179 | MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 180 | mbedtls_free(exported_pub_key); |
| 181 | exported_pub_key = NULL; |
| 182 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 183 | |
| 184 | exit: |
Raef Coles | 20d2e06 | 2022-10-13 09:28:18 +0100 | [diff] [blame] | 185 | if( exported_pub_key != NULL ) |
| 186 | mbedtls_free( exported_pub_key ); |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 187 | mbedtls_lms_public_free( &ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 188 | } |
| 189 | /* END_CASE */ |
| 190 | |