Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3 | #include "psa/crypto.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4 | |
| 5 | #if(UINT32_MAX > SIZE_MAX) |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6 | #define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX ) |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7 | #else |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 8 | #define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1 |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 9 | #endif |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 10 | |
| 11 | /** Test if a buffer is not all-bits zero. |
| 12 | * |
| 13 | * \param buffer Pointer to the beginning of the buffer. |
| 14 | * \param size Size of the buffer in bytes. |
| 15 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 16 | * \return 1 if the buffer is all-bits-zero. |
| 17 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 18 | */ |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 19 | static int mem_is_zero( void *buffer, size_t size ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 20 | { |
| 21 | size_t i; |
| 22 | for( i = 0; i < size; i++ ) |
| 23 | { |
| 24 | if( ( (unsigned char *) buffer )[i] != 0 ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 25 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 26 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 27 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 28 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 29 | |
| 30 | static int exercise_mac_key( psa_key_slot_t key, |
| 31 | psa_key_usage_t usage, |
| 32 | psa_algorithm_t alg ) |
| 33 | { |
| 34 | psa_mac_operation_t operation; |
| 35 | const unsigned char input[] = "foo"; |
| 36 | unsigned char mac[64] = {0}; |
| 37 | size_t mac_length = sizeof( mac ); |
| 38 | |
| 39 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 40 | { |
| 41 | TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS ); |
| 42 | TEST_ASSERT( psa_mac_update( &operation, |
| 43 | input, sizeof( input ) ) == PSA_SUCCESS ); |
| 44 | TEST_ASSERT( psa_mac_finish( &operation, |
| 45 | mac, sizeof( input ), |
| 46 | &mac_length ) == PSA_SUCCESS ); |
| 47 | } |
| 48 | |
| 49 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 50 | { |
| 51 | psa_status_t verify_status = |
| 52 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 53 | PSA_SUCCESS : |
| 54 | PSA_ERROR_INVALID_SIGNATURE ); |
| 55 | TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS ); |
| 56 | TEST_ASSERT( psa_mac_update( &operation, |
| 57 | input, sizeof( input ) ) == PSA_SUCCESS ); |
| 58 | TEST_ASSERT( psa_mac_verify( &operation, mac, mac_length ) == verify_status ); |
| 59 | } |
| 60 | |
| 61 | return( 1 ); |
| 62 | |
| 63 | exit: |
| 64 | psa_mac_abort( &operation ); |
| 65 | return( 0 ); |
| 66 | } |
| 67 | |
| 68 | static int exercise_cipher_key( psa_key_slot_t key, |
| 69 | psa_key_usage_t usage, |
| 70 | psa_algorithm_t alg ) |
| 71 | { |
| 72 | psa_cipher_operation_t operation; |
| 73 | unsigned char iv[16] = {0}; |
| 74 | size_t iv_length = sizeof( iv ); |
| 75 | const unsigned char plaintext[16] = "Hello, world..."; |
| 76 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 77 | size_t ciphertext_length = sizeof( ciphertext ); |
| 78 | unsigned char decrypted[sizeof( ciphertext )]; |
| 79 | size_t part_length; |
| 80 | |
| 81 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 82 | { |
| 83 | TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS ); |
| 84 | TEST_ASSERT( psa_encrypt_generate_iv( &operation, |
| 85 | iv, sizeof( iv ), |
| 86 | &iv_length ) == PSA_SUCCESS ); |
| 87 | TEST_ASSERT( psa_cipher_update( &operation, |
| 88 | plaintext, sizeof( plaintext ), |
| 89 | ciphertext, sizeof( ciphertext ), |
| 90 | &ciphertext_length ) == PSA_SUCCESS ); |
| 91 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 92 | ciphertext + ciphertext_length, |
| 93 | sizeof( ciphertext ) - ciphertext_length, |
| 94 | &part_length ) == PSA_SUCCESS ); |
| 95 | ciphertext_length += part_length; |
| 96 | } |
| 97 | |
| 98 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 99 | { |
| 100 | psa_status_t status; |
| 101 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 102 | { |
| 103 | psa_key_type_t type; |
| 104 | size_t bits; |
| 105 | TEST_ASSERT( psa_get_key_information( key, &type, &bits ) ); |
| 106 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type ); |
| 107 | } |
| 108 | TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS ); |
| 109 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 110 | iv, iv_length ) == PSA_SUCCESS ); |
| 111 | TEST_ASSERT( psa_cipher_update( &operation, |
| 112 | ciphertext, ciphertext_length, |
| 113 | decrypted, sizeof( decrypted ), |
| 114 | &part_length ) == PSA_SUCCESS ); |
| 115 | status = psa_cipher_finish( &operation, |
| 116 | decrypted + part_length, |
| 117 | sizeof( decrypted ) - part_length, |
| 118 | &part_length ); |
| 119 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 120 | * if the input is some aribtrary data rather than an actual |
| 121 | ciphertext, a padding error is likely. */ |
| 122 | if( ( usage & PSA_KEY_USAGE_DECRYPT ) || |
| 123 | PSA_BLOCK_CIPHER_BLOCK_SIZE( alg ) == 1 ) |
| 124 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 125 | else |
| 126 | TEST_ASSERT( status == PSA_SUCCESS || |
| 127 | status == PSA_ERROR_INVALID_PADDING ); |
| 128 | } |
| 129 | |
| 130 | return( 1 ); |
| 131 | |
| 132 | exit: |
| 133 | psa_cipher_abort( &operation ); |
| 134 | return( 0 ); |
| 135 | } |
| 136 | |
| 137 | static int exercise_aead_key( psa_key_slot_t key, |
| 138 | psa_key_usage_t usage, |
| 139 | psa_algorithm_t alg ) |
| 140 | { |
| 141 | unsigned char nonce[16] = {0}; |
| 142 | size_t nonce_length = sizeof( nonce ); |
| 143 | unsigned char plaintext[16] = "Hello, world..."; |
| 144 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 145 | size_t ciphertext_length = sizeof( ciphertext ); |
| 146 | size_t plaintext_length = sizeof( ciphertext ); |
| 147 | |
| 148 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 149 | { |
| 150 | TEST_ASSERT( psa_aead_encrypt( key, alg, |
| 151 | nonce, nonce_length, |
| 152 | NULL, 0, |
| 153 | plaintext, sizeof( plaintext ), |
| 154 | ciphertext, sizeof( ciphertext ), |
| 155 | &ciphertext_length ) == PSA_SUCCESS ); |
| 156 | } |
| 157 | |
| 158 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 159 | { |
| 160 | psa_status_t verify_status = |
| 161 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 162 | PSA_SUCCESS : |
| 163 | PSA_ERROR_INVALID_SIGNATURE ); |
| 164 | TEST_ASSERT( psa_aead_decrypt( key, alg, |
| 165 | nonce, nonce_length, |
| 166 | NULL, 0, |
| 167 | ciphertext, ciphertext_length, |
| 168 | plaintext, sizeof( plaintext ), |
| 169 | &plaintext_length ) == verify_status ); |
| 170 | } |
| 171 | |
| 172 | return( 1 ); |
| 173 | |
| 174 | exit: |
| 175 | return( 0 ); |
| 176 | } |
| 177 | |
| 178 | static int exercise_signature_key( psa_key_slot_t key, |
| 179 | psa_key_usage_t usage, |
| 180 | psa_algorithm_t alg ) |
| 181 | { |
| 182 | unsigned char payload[16] = {0}; |
| 183 | size_t payload_length = sizeof( payload ); |
| 184 | unsigned char signature[256] = {0}; |
| 185 | size_t signature_length = sizeof( signature ); |
| 186 | |
| 187 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 188 | { |
| 189 | TEST_ASSERT( psa_asymmetric_sign( key, alg, |
| 190 | payload, payload_length, |
| 191 | NULL, 0, |
| 192 | signature, sizeof( signature ), |
| 193 | &signature_length ) == PSA_SUCCESS ); |
| 194 | } |
| 195 | |
| 196 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 197 | { |
| 198 | psa_status_t verify_status = |
| 199 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 200 | PSA_SUCCESS : |
| 201 | PSA_ERROR_INVALID_SIGNATURE ); |
| 202 | TEST_ASSERT( psa_asymmetric_verify( key, alg, |
| 203 | payload, payload_length, |
| 204 | NULL, 0, |
| 205 | signature, signature_length ) == |
| 206 | verify_status ); |
| 207 | } |
| 208 | |
| 209 | return( 1 ); |
| 210 | |
| 211 | exit: |
| 212 | return( 0 ); |
| 213 | } |
| 214 | |
| 215 | static int exercise_asymmetric_encryption_key( psa_key_slot_t key, |
| 216 | psa_key_usage_t usage, |
| 217 | psa_algorithm_t alg ) |
| 218 | { |
| 219 | unsigned char plaintext[256] = "Hello, world..."; |
| 220 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 221 | size_t ciphertext_length = sizeof( ciphertext ); |
| 222 | size_t plaintext_length = 16; |
| 223 | |
| 224 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 225 | { |
| 226 | TEST_ASSERT( |
| 227 | psa_asymmetric_encrypt( key, alg, |
| 228 | plaintext, plaintext_length, |
| 229 | NULL, 0, |
| 230 | ciphertext, sizeof( ciphertext ), |
| 231 | &ciphertext_length ) == PSA_SUCCESS ); |
| 232 | } |
| 233 | |
| 234 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 235 | { |
| 236 | psa_status_t status = |
| 237 | psa_asymmetric_decrypt( key, alg, |
| 238 | ciphertext, ciphertext_length, |
| 239 | NULL, 0, |
| 240 | plaintext, sizeof( plaintext ), |
| 241 | &plaintext_length ); |
| 242 | TEST_ASSERT( status == PSA_SUCCESS || |
| 243 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 244 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 245 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 246 | } |
| 247 | |
| 248 | return( 1 ); |
| 249 | |
| 250 | exit: |
| 251 | return( 0 ); |
| 252 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 253 | /* END_HEADER */ |
| 254 | |
| 255 | /* BEGIN_DEPENDENCIES |
| 256 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 257 | * END_DEPENDENCIES |
| 258 | */ |
| 259 | |
| 260 | /* BEGIN_CASE */ |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 261 | void init_deinit( ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 262 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 263 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 264 | int i; |
| 265 | for( i = 0; i <= 1; i++ ) |
| 266 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 267 | status = psa_crypto_init( ); |
| 268 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 269 | status = psa_crypto_init( ); |
| 270 | TEST_ASSERT( status == PSA_SUCCESS ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 271 | mbedtls_psa_crypto_free( ); |
| 272 | } |
| 273 | } |
| 274 | /* END_CASE */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 275 | |
| 276 | /* BEGIN_CASE */ |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 277 | void import( data_t *data, int type, int expected_status_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 278 | { |
| 279 | int slot = 1; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 280 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 281 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 282 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 283 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 284 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 285 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 286 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 287 | status = psa_import_key( slot, type, data->x, data->len ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 288 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 289 | if( status == PSA_SUCCESS ) |
| 290 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 291 | |
| 292 | exit: |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 293 | mbedtls_psa_crypto_free( ); |
| 294 | } |
| 295 | /* END_CASE */ |
| 296 | |
| 297 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 298 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 299 | int type_arg, |
| 300 | int alg_arg, |
| 301 | int usage_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 302 | int expected_bits, |
| 303 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 304 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 305 | int canonical_input ) |
| 306 | { |
| 307 | int slot = 1; |
| 308 | int slot2 = slot + 1; |
| 309 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 310 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 311 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 312 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 313 | unsigned char *exported = NULL; |
| 314 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 315 | size_t export_size; |
| 316 | size_t exported_length; |
| 317 | size_t reexported_length; |
| 318 | psa_key_type_t got_type; |
| 319 | size_t got_bits; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 320 | psa_key_policy_t policy; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 321 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 322 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 323 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
| 324 | export_size = (ssize_t) data->len + export_size_delta; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 325 | exported = mbedtls_calloc( 1, export_size ); |
| 326 | TEST_ASSERT( exported != NULL ); |
| 327 | if( ! canonical_input ) |
| 328 | { |
| 329 | reexported = mbedtls_calloc( 1, export_size ); |
| 330 | TEST_ASSERT( reexported != NULL ); |
| 331 | } |
| 332 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 333 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 334 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 335 | psa_key_policy_set_usage( &policy, usage_arg, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 336 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 337 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 338 | /* Import the key */ |
| 339 | TEST_ASSERT( psa_import_key( slot, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 340 | data->x, data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 341 | |
| 342 | /* Test the key information */ |
| 343 | TEST_ASSERT( psa_get_key_information( slot, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 344 | &got_type, |
| 345 | &got_bits ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 346 | TEST_ASSERT( got_type == type ); |
| 347 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 348 | |
| 349 | /* Export the key */ |
| 350 | status = psa_export_key( slot, |
| 351 | exported, export_size, |
| 352 | &exported_length ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 353 | TEST_ASSERT( status == expected_export_status ); |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 354 | TEST_ASSERT( mem_is_zero( exported + exported_length, |
| 355 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 356 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 357 | { |
| 358 | TEST_ASSERT( exported_length == 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 359 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 360 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 361 | |
| 362 | if( canonical_input ) |
| 363 | { |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 364 | TEST_ASSERT( exported_length == data->len ); |
| 365 | TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 366 | } |
| 367 | else |
| 368 | { |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 369 | TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS ); |
| 370 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 371 | TEST_ASSERT( psa_import_key( slot2, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 372 | exported, |
| 373 | export_size ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 374 | TEST_ASSERT( psa_export_key( slot2, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 375 | reexported, |
| 376 | export_size, |
| 377 | &reexported_length ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 378 | TEST_ASSERT( reexported_length == exported_length ); |
| 379 | TEST_ASSERT( memcmp( reexported, exported, |
| 380 | exported_length ) == 0 ); |
| 381 | } |
| 382 | |
| 383 | destroy: |
| 384 | /* Destroy the key */ |
| 385 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 386 | TEST_ASSERT( psa_get_key_information( |
| 387 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 388 | |
| 389 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 390 | mbedtls_free( exported ); |
| 391 | mbedtls_free( reexported ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 392 | mbedtls_psa_crypto_free( ); |
| 393 | } |
| 394 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 395 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 396 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 397 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 398 | int type_arg, |
| 399 | int alg_arg, |
| 400 | int expected_bits, |
| 401 | int public_key_expected_length, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 402 | int expected_export_status_arg ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 403 | { |
| 404 | int slot = 1; |
| 405 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 406 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 407 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 408 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 409 | unsigned char *exported = NULL; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 410 | size_t export_size; |
| 411 | size_t exported_length; |
| 412 | psa_key_type_t got_type; |
| 413 | size_t got_bits; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 414 | psa_key_policy_t policy; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 415 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 416 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 417 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
| 418 | export_size = (ssize_t) data->len; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 419 | exported = mbedtls_calloc( 1, export_size ); |
| 420 | TEST_ASSERT( exported != NULL ); |
| 421 | |
| 422 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 423 | |
| 424 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 425 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 426 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 427 | |
| 428 | /* Import the key */ |
| 429 | TEST_ASSERT( psa_import_key( slot, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 430 | data->x, data->len ) == PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 431 | |
| 432 | /* Test the key information */ |
| 433 | TEST_ASSERT( psa_get_key_information( slot, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 434 | &got_type, |
| 435 | &got_bits ) == PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 436 | TEST_ASSERT( got_type == type ); |
| 437 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 438 | |
| 439 | /* Export the key */ |
| 440 | status = psa_export_public_key( slot, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 441 | exported, export_size, |
| 442 | &exported_length ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 443 | TEST_ASSERT( status == expected_export_status ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 444 | if( status != PSA_SUCCESS ) |
| 445 | goto destroy; |
| 446 | |
| 447 | TEST_ASSERT( exported_length == (size_t) public_key_expected_length ); |
| 448 | |
| 449 | destroy: |
| 450 | /* Destroy the key */ |
| 451 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 452 | TEST_ASSERT( psa_get_key_information( |
| 453 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 454 | |
| 455 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 456 | mbedtls_free( exported ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 457 | mbedtls_psa_crypto_free( ); |
| 458 | } |
| 459 | /* END_CASE */ |
| 460 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 461 | /* BEGIN_CASE */ |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 462 | void key_policy( int usage_arg, int alg_arg ) |
| 463 | { |
| 464 | int key_slot = 1; |
| 465 | psa_algorithm_t alg = alg_arg; |
| 466 | psa_key_usage_t usage = usage_arg; |
| 467 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 468 | unsigned char key[32] = {0}; |
| 469 | psa_key_policy_t policy_set; |
| 470 | psa_key_policy_t policy_get; |
| 471 | |
| 472 | memset( key, 0x2a, sizeof( key ) ); |
| 473 | |
| 474 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 475 | |
| 476 | psa_key_policy_init( &policy_set ); |
| 477 | psa_key_policy_init( &policy_get ); |
| 478 | |
| 479 | psa_key_policy_set_usage( &policy_set, usage, alg ); |
| 480 | |
| 481 | TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage ); |
| 482 | TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg ); |
| 483 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); |
| 484 | |
| 485 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 486 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 487 | |
| 488 | TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); |
| 489 | |
| 490 | TEST_ASSERT( policy_get.usage == policy_set.usage ); |
| 491 | TEST_ASSERT( policy_get.alg == policy_set.alg ); |
| 492 | |
| 493 | exit: |
| 494 | psa_destroy_key( key_slot ); |
| 495 | mbedtls_psa_crypto_free( ); |
| 496 | } |
| 497 | /* END_CASE */ |
| 498 | |
| 499 | /* BEGIN_CASE */ |
| 500 | void key_policy_fail( int usage_arg, int alg_arg, int expected_status, |
| 501 | data_t *keypair ) |
| 502 | { |
| 503 | int key_slot = 1; |
| 504 | psa_algorithm_t alg = alg_arg; |
| 505 | psa_key_usage_t usage = usage_arg; |
| 506 | size_t signature_length = 0; |
| 507 | psa_key_policy_t policy; |
| 508 | int actual_status = PSA_SUCCESS; |
| 509 | |
| 510 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 511 | |
| 512 | psa_key_policy_init( &policy ); |
| 513 | psa_key_policy_set_usage( &policy, usage, alg ); |
| 514 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 515 | |
| 516 | if( usage & PSA_KEY_USAGE_EXPORT ) |
| 517 | { |
| 518 | TEST_ASSERT( keypair != NULL ); |
| 519 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) ); |
| 520 | TEST_ASSERT( psa_import_key( key_slot, |
| 521 | PSA_KEY_TYPE_RSA_KEYPAIR, |
| 522 | keypair->x, |
| 523 | keypair->len ) == PSA_SUCCESS ); |
| 524 | actual_status = psa_asymmetric_sign( key_slot, alg, |
| 525 | NULL, 0, |
| 526 | NULL, 0, |
| 527 | NULL, 0, &signature_length ); |
| 528 | } |
| 529 | |
| 530 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 531 | { |
| 532 | TEST_ASSERT( keypair != NULL ); |
| 533 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) ); |
| 534 | TEST_ASSERT( psa_import_key( key_slot, |
| 535 | PSA_KEY_TYPE_RSA_KEYPAIR, |
| 536 | keypair->x, |
| 537 | keypair->len ) == PSA_SUCCESS ); |
| 538 | actual_status = psa_export_key( key_slot, NULL, 0, NULL ); |
| 539 | } |
| 540 | |
| 541 | TEST_ASSERT( actual_status == expected_status ); |
| 542 | |
| 543 | exit: |
| 544 | psa_destroy_key( key_slot ); |
| 545 | mbedtls_psa_crypto_free( ); |
| 546 | } |
| 547 | /* END_CASE */ |
| 548 | |
| 549 | /* BEGIN_CASE */ |
| 550 | void key_lifetime( int lifetime_arg ) |
| 551 | { |
| 552 | int key_slot = 1; |
| 553 | psa_key_type_t key_type = PSA_ALG_CBC_BASE; |
| 554 | unsigned char key[32] = {0}; |
| 555 | psa_key_lifetime_t lifetime_set = lifetime_arg; |
| 556 | psa_key_lifetime_t lifetime_get; |
| 557 | |
| 558 | memset( key, 0x2a, sizeof( key ) ); |
| 559 | |
| 560 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 561 | |
| 562 | TEST_ASSERT( psa_set_key_lifetime( key_slot, |
| 563 | lifetime_set ) == PSA_SUCCESS ); |
| 564 | |
| 565 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 566 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 567 | |
| 568 | TEST_ASSERT( psa_get_key_lifetime( key_slot, |
| 569 | &lifetime_get ) == PSA_SUCCESS ); |
| 570 | |
| 571 | TEST_ASSERT( lifetime_get == lifetime_set ); |
| 572 | |
| 573 | exit: |
| 574 | psa_destroy_key( key_slot ); |
| 575 | mbedtls_psa_crypto_free( ); |
| 576 | } |
| 577 | /* END_CASE */ |
| 578 | |
| 579 | /* BEGIN_CASE */ |
| 580 | void key_lifetime_set_fail( int key_slot_arg, |
| 581 | int lifetime_arg, |
| 582 | int expected_status_arg ) |
| 583 | { |
| 584 | psa_key_slot_t key_slot = key_slot_arg; |
| 585 | psa_key_lifetime_t lifetime_set = lifetime_arg; |
| 586 | psa_status_t actual_status; |
| 587 | psa_status_t expected_status = expected_status_arg; |
| 588 | |
| 589 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 590 | |
| 591 | actual_status = psa_set_key_lifetime( key_slot, lifetime_set ); |
| 592 | |
| 593 | if( actual_status == PSA_SUCCESS ) |
| 594 | actual_status = psa_set_key_lifetime( key_slot, lifetime_set ); |
| 595 | |
| 596 | TEST_ASSERT( expected_status == actual_status ); |
| 597 | |
| 598 | exit: |
| 599 | psa_destroy_key( key_slot ); |
| 600 | mbedtls_psa_crypto_free( ); |
| 601 | } |
| 602 | /* END_CASE */ |
| 603 | |
| 604 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 605 | void hash_setup( int alg_arg, |
| 606 | int expected_status_arg ) |
| 607 | { |
| 608 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 609 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 610 | psa_hash_operation_t operation; |
| 611 | psa_status_t status; |
| 612 | |
| 613 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 614 | |
| 615 | status = psa_hash_start( &operation, alg ); |
| 616 | psa_hash_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 617 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 618 | |
| 619 | exit: |
| 620 | mbedtls_psa_crypto_free( ); |
| 621 | } |
| 622 | /* END_CASE */ |
| 623 | |
| 624 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 625 | void hash_finish( int alg_arg, data_t *input, data_t *expected_hash ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 626 | { |
| 627 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b3e6e5d | 2018-06-18 22:16:43 +0200 | [diff] [blame] | 628 | unsigned char actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 629 | size_t actual_hash_length; |
| 630 | psa_hash_operation_t operation; |
| 631 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 632 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 633 | TEST_ASSERT( expected_hash != NULL ); |
| 634 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 635 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 636 | |
| 637 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 638 | |
| 639 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 640 | TEST_ASSERT( psa_hash_update( &operation, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 641 | input->x, input->len ) == PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 642 | TEST_ASSERT( psa_hash_finish( &operation, |
| 643 | actual_hash, sizeof( actual_hash ), |
| 644 | &actual_hash_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 645 | TEST_ASSERT( actual_hash_length == expected_hash->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 646 | TEST_ASSERT( memcmp( expected_hash->x, actual_hash, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 647 | expected_hash->len ) == 0 ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 648 | |
| 649 | exit: |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 650 | mbedtls_psa_crypto_free( ); |
| 651 | } |
| 652 | /* END_CASE */ |
| 653 | |
| 654 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 655 | void hash_verify( int alg_arg, data_t *input, data_t *expected_hash ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 656 | { |
| 657 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 658 | psa_hash_operation_t operation; |
| 659 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 660 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 661 | TEST_ASSERT( expected_hash != NULL ); |
| 662 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 663 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 664 | |
| 665 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 666 | |
| 667 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 668 | TEST_ASSERT( psa_hash_update( &operation, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 669 | input->x, |
| 670 | input->len ) == PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 671 | TEST_ASSERT( psa_hash_verify( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 672 | expected_hash->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 673 | expected_hash->len ) == PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 674 | |
| 675 | exit: |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 676 | mbedtls_psa_crypto_free( ); |
| 677 | } |
| 678 | /* END_CASE */ |
| 679 | |
| 680 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 681 | void mac_setup( int key_type_arg, |
| 682 | data_t *key, |
| 683 | int alg_arg, |
| 684 | int expected_status_arg ) |
| 685 | { |
| 686 | int key_slot = 1; |
| 687 | psa_key_type_t key_type = key_type_arg; |
| 688 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 689 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 690 | psa_mac_operation_t operation; |
| 691 | psa_key_policy_t policy; |
| 692 | psa_status_t status; |
| 693 | |
| 694 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 695 | |
| 696 | psa_key_policy_init( &policy ); |
| 697 | psa_key_policy_set_usage( &policy, |
| 698 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY, |
| 699 | alg ); |
| 700 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 701 | |
| 702 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 703 | key->x, key->len ) == PSA_SUCCESS ); |
| 704 | |
| 705 | status = psa_mac_start( &operation, key_slot, alg ); |
| 706 | psa_mac_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 707 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 708 | |
| 709 | exit: |
| 710 | psa_destroy_key( key_slot ); |
| 711 | mbedtls_psa_crypto_free( ); |
| 712 | } |
| 713 | /* END_CASE */ |
| 714 | |
| 715 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 716 | void mac_verify( int key_type_arg, |
| 717 | data_t *key, |
| 718 | int alg_arg, |
| 719 | data_t *input, |
| 720 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 721 | { |
| 722 | int key_slot = 1; |
| 723 | psa_key_type_t key_type = key_type_arg; |
| 724 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 725 | psa_mac_operation_t operation; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 726 | psa_key_policy_t policy; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 727 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 728 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 729 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 730 | TEST_ASSERT( expected_mac != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 731 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 732 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 733 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 734 | |
| 735 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 736 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 737 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 738 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 739 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 740 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 741 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 742 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 743 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 744 | TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 745 | TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS ); |
| 746 | TEST_ASSERT( psa_mac_update( &operation, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 747 | input->x, input->len ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 748 | TEST_ASSERT( psa_mac_verify( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 749 | expected_mac->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 750 | expected_mac->len ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 751 | |
| 752 | exit: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 753 | psa_destroy_key( key_slot ); |
| 754 | mbedtls_psa_crypto_free( ); |
| 755 | } |
| 756 | /* END_CASE */ |
| 757 | |
| 758 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 759 | void cipher_setup( int key_type_arg, |
| 760 | data_t *key, |
| 761 | int alg_arg, |
| 762 | int expected_status_arg ) |
| 763 | { |
| 764 | int key_slot = 1; |
| 765 | psa_key_type_t key_type = key_type_arg; |
| 766 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 767 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 768 | psa_cipher_operation_t operation; |
| 769 | psa_key_policy_t policy; |
| 770 | psa_status_t status; |
| 771 | |
| 772 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 773 | |
| 774 | psa_key_policy_init( &policy ); |
| 775 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
| 776 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 777 | |
| 778 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 779 | key->x, key->len ) == PSA_SUCCESS ); |
| 780 | |
| 781 | status = psa_encrypt_setup( &operation, key_slot, alg ); |
| 782 | psa_cipher_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 783 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 784 | |
| 785 | exit: |
| 786 | psa_destroy_key( key_slot ); |
| 787 | mbedtls_psa_crypto_free( ); |
| 788 | } |
| 789 | /* END_CASE */ |
| 790 | |
| 791 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 792 | void cipher_encrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 793 | data_t *key, |
| 794 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 795 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 796 | { |
| 797 | int key_slot = 1; |
| 798 | psa_status_t status; |
| 799 | psa_key_type_t key_type = key_type_arg; |
| 800 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 801 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 802 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame^] | 803 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 804 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 805 | size_t output_buffer_size = 0; |
| 806 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 807 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 808 | psa_cipher_operation_t operation; |
| 809 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 810 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 811 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 812 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 813 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 814 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 815 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 816 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame^] | 817 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 818 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 819 | |
| 820 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 821 | |
| 822 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 823 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 824 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 825 | TEST_ASSERT( psa_encrypt_setup( &operation, |
| 826 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 827 | |
| 828 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame^] | 829 | iv, iv_size ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 830 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 831 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 832 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 833 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 834 | TEST_ASSERT( psa_cipher_update( &operation, |
| 835 | input->x, input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 836 | output, output_buffer_size, |
| 837 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 838 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 839 | status = psa_cipher_finish( &operation, |
| 840 | output + function_output_length, |
| 841 | output_buffer_size, |
| 842 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 843 | total_output_length += function_output_length; |
| 844 | |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 845 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 846 | if( expected_status == PSA_SUCCESS ) |
| 847 | { |
| 848 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 849 | TEST_ASSERT( total_output_length == expected_output->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 850 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 851 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 852 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 853 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 854 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 855 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 856 | psa_destroy_key( key_slot ); |
| 857 | mbedtls_psa_crypto_free( ); |
| 858 | } |
| 859 | /* END_CASE */ |
| 860 | |
| 861 | /* BEGIN_CASE */ |
| 862 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 863 | data_t *key, |
| 864 | data_t *input, |
| 865 | int first_part_size, |
| 866 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 867 | { |
| 868 | int key_slot = 1; |
| 869 | psa_key_type_t key_type = key_type_arg; |
| 870 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 871 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame^] | 872 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 873 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 874 | size_t output_buffer_size = 0; |
| 875 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 876 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 877 | psa_cipher_operation_t operation; |
| 878 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 879 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 880 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 881 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 882 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 883 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 884 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 885 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame^] | 886 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 887 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 888 | |
| 889 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 890 | |
| 891 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 892 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 893 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 894 | TEST_ASSERT( psa_encrypt_setup( &operation, |
| 895 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 896 | |
| 897 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 898 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 899 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 900 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 901 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 902 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 903 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 904 | TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 905 | output, output_buffer_size, |
| 906 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 907 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 908 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 909 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 910 | input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 911 | output, output_buffer_size, |
| 912 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 913 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 914 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 915 | output + function_output_length, |
| 916 | output_buffer_size, |
| 917 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 918 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 919 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 920 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 921 | TEST_ASSERT( total_output_length == expected_output->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 922 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 923 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 924 | |
| 925 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 926 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 927 | psa_destroy_key( key_slot ); |
| 928 | mbedtls_psa_crypto_free( ); |
| 929 | } |
| 930 | /* END_CASE */ |
| 931 | |
| 932 | /* BEGIN_CASE */ |
| 933 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 934 | data_t *key, |
| 935 | data_t *input, |
| 936 | int first_part_size, |
| 937 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 938 | { |
| 939 | int key_slot = 1; |
| 940 | |
| 941 | psa_key_type_t key_type = key_type_arg; |
| 942 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 943 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame^] | 944 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 945 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 946 | size_t output_buffer_size = 0; |
| 947 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 948 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 949 | psa_cipher_operation_t operation; |
| 950 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 951 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 952 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 953 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 954 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 955 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 956 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 957 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame^] | 958 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 959 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 960 | |
| 961 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 962 | |
| 963 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 964 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 965 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 966 | TEST_ASSERT( psa_decrypt_setup( &operation, |
| 967 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 968 | |
| 969 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 970 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 971 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 972 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 973 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 974 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 975 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 976 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
| 977 | TEST_ASSERT( psa_cipher_update( &operation, |
| 978 | input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 979 | output, output_buffer_size, |
| 980 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 981 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 982 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 983 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 984 | input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 985 | output, output_buffer_size, |
| 986 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 987 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 988 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 989 | output + function_output_length, |
| 990 | output_buffer_size, |
| 991 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 992 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 993 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 994 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 995 | TEST_ASSERT( total_output_length == expected_output->len ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 996 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 997 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 998 | |
| 999 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1000 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1001 | psa_destroy_key( key_slot ); |
| 1002 | mbedtls_psa_crypto_free( ); |
| 1003 | } |
| 1004 | /* END_CASE */ |
| 1005 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1006 | /* BEGIN_CASE */ |
| 1007 | void cipher_decrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1008 | data_t *key, |
| 1009 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1010 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1011 | { |
| 1012 | int key_slot = 1; |
| 1013 | psa_status_t status; |
| 1014 | psa_key_type_t key_type = key_type_arg; |
| 1015 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1016 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1017 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame^] | 1018 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1019 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1020 | size_t output_buffer_size = 0; |
| 1021 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1022 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1023 | psa_cipher_operation_t operation; |
| 1024 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1025 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1026 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1027 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1028 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1029 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 1030 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1031 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame^] | 1032 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 1033 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1034 | |
| 1035 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1036 | |
| 1037 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1038 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1039 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1040 | TEST_ASSERT( psa_decrypt_setup( &operation, |
| 1041 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1042 | |
| 1043 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame^] | 1044 | iv, iv_size ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1045 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1046 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1047 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1048 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1049 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1050 | TEST_ASSERT( psa_cipher_update( &operation, |
| 1051 | input->x, input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1052 | output, output_buffer_size, |
| 1053 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1054 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1055 | status = psa_cipher_finish( &operation, |
| 1056 | output + function_output_length, |
| 1057 | output_buffer_size, |
| 1058 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1059 | total_output_length += function_output_length; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1060 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1061 | |
| 1062 | if( expected_status == PSA_SUCCESS ) |
| 1063 | { |
| 1064 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1065 | TEST_ASSERT( total_output_length == expected_output->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1066 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1067 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1070 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1071 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1072 | psa_destroy_key( key_slot ); |
| 1073 | mbedtls_psa_crypto_free( ); |
| 1074 | } |
| 1075 | /* END_CASE */ |
| 1076 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1077 | /* BEGIN_CASE */ |
| 1078 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1079 | data_t *key, |
| 1080 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1081 | { |
| 1082 | int key_slot = 1; |
| 1083 | psa_key_type_t key_type = key_type_arg; |
| 1084 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 1085 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1086 | size_t iv_size = 16; |
| 1087 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1088 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1089 | size_t output1_size = 0; |
| 1090 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1091 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1092 | size_t output2_size = 0; |
| 1093 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1094 | size_t function_output_length = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1095 | psa_cipher_operation_t operation1; |
| 1096 | psa_cipher_operation_t operation2; |
| 1097 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1098 | TEST_ASSERT( key != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1099 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1100 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1101 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1102 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1103 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1104 | |
| 1105 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1106 | key->x, key->len ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1107 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1108 | TEST_ASSERT( psa_encrypt_setup( &operation1, |
| 1109 | key_slot, alg ) == PSA_SUCCESS ); |
| 1110 | TEST_ASSERT( psa_decrypt_setup( &operation2, |
| 1111 | key_slot, alg ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1112 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1113 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 1114 | iv, iv_size, |
| 1115 | &iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1116 | output1_size = input->len + operation1.block_size; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1117 | output1 = mbedtls_calloc( 1, output1_size ); |
| 1118 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1119 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1120 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1121 | output1, output1_size, |
| 1122 | &output1_length ) == PSA_SUCCESS ); |
| 1123 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 1124 | output1 + output1_length, output1_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1125 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1126 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1127 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1128 | |
| 1129 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1130 | |
| 1131 | output2_size = output1_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1132 | output2 = mbedtls_calloc( 1, output2_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1133 | TEST_ASSERT( output2 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1134 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1135 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 1136 | iv, iv_length ) == PSA_SUCCESS ); |
| 1137 | TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 1138 | output2, output2_size, |
| 1139 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1140 | function_output_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1141 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 1142 | output2 + output2_length, |
| 1143 | output2_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1144 | &function_output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1145 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1146 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1147 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1148 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1149 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1150 | TEST_ASSERT( input->len == output2_length ); |
| 1151 | TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1152 | |
| 1153 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1154 | mbedtls_free( output1 ); |
| 1155 | mbedtls_free( output2 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1156 | psa_destroy_key( key_slot ); |
| 1157 | mbedtls_psa_crypto_free( ); |
| 1158 | } |
| 1159 | /* END_CASE */ |
| 1160 | |
| 1161 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1162 | void cipher_verify_output_multipart( int alg_arg, |
| 1163 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1164 | data_t *key, |
| 1165 | data_t *input, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1166 | int first_part_size ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1167 | { |
| 1168 | int key_slot = 1; |
| 1169 | psa_key_type_t key_type = key_type_arg; |
| 1170 | psa_algorithm_t alg = alg_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1171 | unsigned char iv[16] = {0}; |
| 1172 | size_t iv_size = 16; |
| 1173 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1174 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1175 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1176 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1177 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1178 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1179 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1180 | size_t function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1181 | psa_cipher_operation_t operation1; |
| 1182 | psa_cipher_operation_t operation2; |
| 1183 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1184 | TEST_ASSERT( key != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1185 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1186 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1187 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1188 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1189 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1190 | |
| 1191 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1192 | key->x, key->len ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1193 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1194 | TEST_ASSERT( psa_encrypt_setup( &operation1, |
| 1195 | key_slot, alg ) == PSA_SUCCESS ); |
| 1196 | TEST_ASSERT( psa_decrypt_setup( &operation2, |
| 1197 | key_slot, alg ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1198 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1199 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 1200 | iv, iv_size, |
| 1201 | &iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1202 | output1_buffer_size = input->len + operation1.block_size; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1203 | output1 = mbedtls_calloc( 1, output1_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1204 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1205 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1206 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1207 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1208 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1209 | output1, output1_buffer_size, |
| 1210 | &function_output_length ) == PSA_SUCCESS ); |
| 1211 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1212 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1213 | TEST_ASSERT( psa_cipher_update( &operation1, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1214 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1215 | input->len - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1216 | output1, output1_buffer_size, |
| 1217 | &function_output_length ) == PSA_SUCCESS ); |
| 1218 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1219 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1220 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 1221 | output1 + output1_length, |
| 1222 | output1_buffer_size - output1_length, |
| 1223 | &function_output_length ) == PSA_SUCCESS ); |
| 1224 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1225 | |
| 1226 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1227 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1228 | output2_buffer_size = output1_length; |
| 1229 | output2 = mbedtls_calloc( 1, output2_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1230 | TEST_ASSERT( output2 != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1231 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1232 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 1233 | iv, iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1234 | |
| 1235 | TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1236 | output2, output2_buffer_size, |
| 1237 | &function_output_length ) == PSA_SUCCESS ); |
| 1238 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1239 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1240 | TEST_ASSERT( psa_cipher_update( &operation2, |
| 1241 | output1 + first_part_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1242 | output1_length - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1243 | output2, output2_buffer_size, |
| 1244 | &function_output_length ) == PSA_SUCCESS ); |
| 1245 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1246 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1247 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 1248 | output2 + output2_length, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1249 | output2_buffer_size - output2_length, |
| 1250 | &function_output_length ) == PSA_SUCCESS ); |
| 1251 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1252 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1253 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1254 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1255 | TEST_ASSERT( input->len == output2_length ); |
| 1256 | TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1257 | |
| 1258 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1259 | mbedtls_free( output1 ); |
| 1260 | mbedtls_free( output2 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1261 | psa_destroy_key( key_slot ); |
| 1262 | mbedtls_psa_crypto_free( ); |
| 1263 | } |
| 1264 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1265 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1266 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1267 | void aead_encrypt_decrypt( int key_type_arg, |
| 1268 | data_t * key_data, |
| 1269 | int alg_arg, |
| 1270 | data_t * input_data, |
| 1271 | data_t * nonce, |
| 1272 | data_t * additional_data, |
| 1273 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1274 | { |
| 1275 | int slot = 1; |
| 1276 | psa_key_type_t key_type = key_type_arg; |
| 1277 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1278 | unsigned char *output_data = NULL; |
| 1279 | size_t output_size = 0; |
| 1280 | size_t output_length = 0; |
| 1281 | unsigned char *output_data2 = NULL; |
| 1282 | size_t output_length2 = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1283 | size_t tag_length = 16; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1284 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1285 | psa_key_policy_t policy; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1286 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1287 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1288 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1289 | TEST_ASSERT( nonce != NULL ); |
| 1290 | TEST_ASSERT( additional_data != NULL ); |
| 1291 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1292 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1293 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 1294 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 1295 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1296 | output_size = input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1297 | output_data = mbedtls_calloc( 1, output_size ); |
| 1298 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1299 | |
| 1300 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1301 | |
| 1302 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1303 | psa_key_policy_set_usage( &policy, |
| 1304 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
| 1305 | alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1306 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1307 | |
| 1308 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1309 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1310 | |
| 1311 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1312 | nonce->x, nonce->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1313 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1314 | additional_data->len, |
| 1315 | input_data->x, input_data->len, |
| 1316 | output_data, output_size, |
| 1317 | &output_length ) == expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1318 | |
| 1319 | if( PSA_SUCCESS == expected_result ) |
| 1320 | { |
| 1321 | output_data2 = mbedtls_calloc( 1, output_length ); |
| 1322 | TEST_ASSERT( output_data2 != NULL ); |
| 1323 | |
| 1324 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1325 | nonce->x, nonce->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1326 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1327 | additional_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1328 | output_data, output_length, |
| 1329 | output_data2, output_length, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1330 | &output_length2 ) == expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1331 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1332 | TEST_ASSERT( memcmp( input_data->x, output_data2, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1333 | input_data->len ) == 0 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1334 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1335 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1336 | exit: |
| 1337 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1338 | mbedtls_free( output_data ); |
| 1339 | mbedtls_free( output_data2 ); |
| 1340 | mbedtls_psa_crypto_free( ); |
| 1341 | } |
| 1342 | /* END_CASE */ |
| 1343 | |
| 1344 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1345 | void aead_encrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1346 | int alg_arg, data_t * input_data, |
| 1347 | data_t * additional_data, data_t * nonce, |
| 1348 | data_t * expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1349 | { |
| 1350 | int slot = 1; |
| 1351 | psa_key_type_t key_type = key_type_arg; |
| 1352 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1353 | unsigned char *output_data = NULL; |
| 1354 | size_t output_size = 0; |
| 1355 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1356 | size_t tag_length = 16; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1357 | psa_key_policy_t policy; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1358 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1359 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1360 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1361 | TEST_ASSERT( additional_data != NULL ); |
| 1362 | TEST_ASSERT( nonce != NULL ); |
| 1363 | TEST_ASSERT( expected_result != NULL ); |
| 1364 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1365 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1366 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 1367 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 1368 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) ); |
| 1369 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1370 | output_size = input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1371 | output_data = mbedtls_calloc( 1, output_size ); |
| 1372 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1373 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1374 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1375 | |
| 1376 | psa_key_policy_init( &policy ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1377 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1378 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1379 | |
| 1380 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1381 | key_data->x, |
| 1382 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1383 | |
| 1384 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1385 | nonce->x, nonce->len, |
| 1386 | additional_data->x, additional_data->len, |
| 1387 | input_data->x, input_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1388 | output_data, output_size, |
| 1389 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1390 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1391 | TEST_ASSERT( memcmp( output_data, expected_result->x, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1392 | output_length ) == 0 ); |
| 1393 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1394 | exit: |
| 1395 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1396 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1397 | mbedtls_psa_crypto_free( ); |
| 1398 | } |
| 1399 | /* END_CASE */ |
| 1400 | |
| 1401 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1402 | void aead_decrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1403 | int alg_arg, data_t * input_data, |
| 1404 | data_t * additional_data, data_t * nonce, |
| 1405 | data_t * expected_data, int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1406 | { |
| 1407 | int slot = 1; |
| 1408 | psa_key_type_t key_type = key_type_arg; |
| 1409 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1410 | unsigned char *output_data = NULL; |
| 1411 | size_t output_size = 0; |
| 1412 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1413 | size_t tag_length = 16; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1414 | psa_key_policy_t policy; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1415 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1416 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1417 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1418 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1419 | TEST_ASSERT( additional_data != NULL ); |
| 1420 | TEST_ASSERT( nonce != NULL ); |
| 1421 | TEST_ASSERT( expected_data != NULL ); |
| 1422 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1423 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1424 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 1425 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 1426 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 1427 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1428 | output_size = input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1429 | output_data = mbedtls_calloc( 1, output_size ); |
| 1430 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1431 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1432 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1433 | |
| 1434 | psa_key_policy_init( &policy ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1435 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1436 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1437 | |
| 1438 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1439 | key_data->x, |
| 1440 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1441 | |
| 1442 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1443 | nonce->x, nonce->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1444 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1445 | additional_data->len, |
| 1446 | input_data->x, input_data->len, |
| 1447 | output_data, output_size, |
| 1448 | &output_length ) == expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1449 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1450 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1451 | { |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1452 | TEST_ASSERT( memcmp( output_data, expected_data->x, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1453 | output_length ) == 0 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1454 | } |
| 1455 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1456 | exit: |
| 1457 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1458 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1459 | mbedtls_psa_crypto_free( ); |
| 1460 | } |
| 1461 | /* END_CASE */ |
| 1462 | |
| 1463 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1464 | void signature_size( int type_arg, |
| 1465 | int bits, |
| 1466 | int alg_arg, |
| 1467 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1468 | { |
| 1469 | psa_key_type_t type = type_arg; |
| 1470 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1471 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1472 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 1473 | exit: |
| 1474 | ; |
| 1475 | } |
| 1476 | /* END_CASE */ |
| 1477 | |
| 1478 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1479 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 1480 | int alg_arg, data_t *input_data, |
| 1481 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1482 | { |
| 1483 | int slot = 1; |
| 1484 | psa_key_type_t key_type = key_type_arg; |
| 1485 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1486 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1487 | unsigned char *signature = NULL; |
| 1488 | size_t signature_size; |
| 1489 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1490 | psa_key_policy_t policy; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1491 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1492 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1493 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1494 | TEST_ASSERT( output_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1495 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1496 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1497 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1498 | |
| 1499 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1500 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1501 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1502 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1503 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1504 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1505 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1506 | key_data->x, |
| 1507 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1508 | TEST_ASSERT( psa_get_key_information( slot, |
| 1509 | NULL, |
| 1510 | &key_bits ) == PSA_SUCCESS ); |
| 1511 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1512 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 1513 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1514 | TEST_ASSERT( signature_size != 0 ); |
| 1515 | signature = mbedtls_calloc( 1, signature_size ); |
| 1516 | TEST_ASSERT( signature != NULL ); |
| 1517 | |
| 1518 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1519 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1520 | NULL, 0, |
| 1521 | signature, signature_size, |
| 1522 | &signature_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1523 | TEST_ASSERT( signature_length == output_data->len ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1524 | TEST_ASSERT( memcmp( signature, output_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1525 | output_data->len ) == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1526 | |
| 1527 | exit: |
| 1528 | psa_destroy_key( slot ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1529 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1530 | mbedtls_psa_crypto_free( ); |
| 1531 | } |
| 1532 | /* END_CASE */ |
| 1533 | |
| 1534 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1535 | void sign_fail( int key_type_arg, data_t *key_data, |
| 1536 | int alg_arg, data_t *input_data, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1537 | int signature_size, int expected_status_arg ) |
| 1538 | { |
| 1539 | int slot = 1; |
| 1540 | psa_key_type_t key_type = key_type_arg; |
| 1541 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1542 | psa_status_t actual_status; |
| 1543 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 1544 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 1545 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1546 | psa_key_policy_t policy; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1547 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1548 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1549 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1550 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1551 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1552 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1553 | signature = mbedtls_calloc( 1, signature_size ); |
| 1554 | TEST_ASSERT( signature != NULL ); |
| 1555 | |
| 1556 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1557 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1558 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1559 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1560 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1561 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1562 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1563 | key_data->x, |
| 1564 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1565 | |
| 1566 | actual_status = psa_asymmetric_sign( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1567 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1568 | NULL, 0, |
| 1569 | signature, signature_size, |
| 1570 | &signature_length ); |
| 1571 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 1572 | TEST_ASSERT( signature_length == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1573 | |
| 1574 | exit: |
| 1575 | psa_destroy_key( slot ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1576 | mbedtls_free( signature ); |
| 1577 | mbedtls_psa_crypto_free( ); |
| 1578 | } |
| 1579 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1580 | |
| 1581 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1582 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 1583 | int alg_arg, data_t *hash_data, |
| 1584 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1585 | { |
| 1586 | int slot = 1; |
| 1587 | psa_key_type_t key_type = key_type_arg; |
| 1588 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1589 | psa_key_policy_t policy; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1590 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1591 | TEST_ASSERT( key_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1592 | TEST_ASSERT( hash_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1593 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1594 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1595 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 1596 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1597 | |
| 1598 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1599 | |
| 1600 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1601 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1602 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1603 | |
| 1604 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1605 | key_data->x, |
| 1606 | key_data->len ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1607 | |
| 1608 | TEST_ASSERT( psa_asymmetric_verify( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1609 | hash_data->x, hash_data->len, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1610 | NULL, 0, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1611 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1612 | signature_data->len ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1613 | exit: |
| 1614 | psa_destroy_key( slot ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1615 | mbedtls_psa_crypto_free( ); |
| 1616 | } |
| 1617 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1618 | |
| 1619 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1620 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 1621 | int alg_arg, data_t *hash_data, |
| 1622 | data_t *signature_data, |
| 1623 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1624 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1625 | int slot = 1; |
| 1626 | psa_key_type_t key_type = key_type_arg; |
| 1627 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1628 | psa_status_t actual_status; |
| 1629 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1630 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1631 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1632 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1633 | TEST_ASSERT( hash_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1634 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1635 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1636 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 1637 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1638 | |
| 1639 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1640 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1641 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1642 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1643 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1644 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1645 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1646 | key_data->x, |
| 1647 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1648 | |
| 1649 | actual_status = psa_asymmetric_verify( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1650 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1651 | NULL, 0, |
| 1652 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1653 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1654 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1655 | TEST_ASSERT( actual_status == expected_status ); |
| 1656 | |
| 1657 | exit: |
| 1658 | psa_destroy_key( slot ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1659 | mbedtls_psa_crypto_free( ); |
| 1660 | } |
| 1661 | /* END_CASE */ |
| 1662 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1663 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1664 | void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data, |
| 1665 | int alg_arg, data_t *input_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1666 | { |
| 1667 | int slot = 1; |
| 1668 | psa_key_type_t key_type = key_type_arg; |
| 1669 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1670 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1671 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1672 | size_t output_length = 0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1673 | unsigned char *output2 = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1674 | size_t output2_size = 0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1675 | size_t output2_length = 0; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1676 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1677 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1678 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1679 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1680 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1681 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1682 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1683 | output_size = key_data->len; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1684 | output2_size = output_size; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1685 | output = mbedtls_calloc( 1, output_size ); |
| 1686 | TEST_ASSERT( output != NULL ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1687 | output2 = mbedtls_calloc( 1, output2_size ); |
| 1688 | TEST_ASSERT( output2 != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1689 | |
| 1690 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1691 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1692 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1693 | psa_key_policy_set_usage( &policy, |
| 1694 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1695 | alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1696 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1697 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1698 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1699 | key_data->x, |
| 1700 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1701 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 1702 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 1703 | * the original plaintext because of the non-optional random |
| 1704 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1705 | TEST_ASSERT( psa_asymmetric_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1706 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1707 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1708 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1709 | &output_length ) == PSA_SUCCESS ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1710 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1711 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1712 | output, output_length, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1713 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1714 | output2, output2_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1715 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1716 | TEST_ASSERT( memcmp( input_data->x, output2, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1717 | input_data->len ) == 0 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1718 | |
| 1719 | exit: |
| 1720 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1721 | mbedtls_free( output ); |
| 1722 | mbedtls_free( output2 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1723 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1724 | } |
| 1725 | /* END_CASE */ |
| 1726 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1727 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1728 | void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1729 | int alg_arg, data_t *input_data, |
| 1730 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1731 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1732 | int slot = 1; |
| 1733 | psa_key_type_t key_type = key_type_arg; |
| 1734 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1735 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1736 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1737 | size_t output_length = 0; |
| 1738 | psa_status_t actual_status; |
| 1739 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1740 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1741 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1742 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1743 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1744 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1745 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1746 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1747 | output_size = key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1748 | output = mbedtls_calloc( 1, output_size ); |
| 1749 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1750 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1751 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1752 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1753 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1754 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1755 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1756 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1757 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1758 | key_data->x, |
| 1759 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1760 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1761 | actual_status = psa_asymmetric_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1762 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1763 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1764 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1765 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1766 | TEST_ASSERT( actual_status == expected_status ); |
| 1767 | |
| 1768 | exit: |
| 1769 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1770 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1771 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1772 | } |
| 1773 | /* END_CASE */ |
| 1774 | |
| 1775 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1776 | void asymmetric_decrypt( int key_type_arg, data_t *key_data, |
| 1777 | int alg_arg, data_t *input_data, |
| 1778 | data_t *expected_data, int expected_size ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1779 | { |
| 1780 | int slot = 1; |
| 1781 | psa_key_type_t key_type = key_type_arg; |
| 1782 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1783 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1784 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1785 | size_t output_length = 0; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1786 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1787 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1788 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1789 | TEST_ASSERT( input_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1790 | TEST_ASSERT( expected_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1791 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1792 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1793 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 1794 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1795 | output_size = key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1796 | output = mbedtls_calloc( 1, output_size ); |
| 1797 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1798 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1799 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1800 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1801 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1802 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1803 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1804 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1805 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1806 | key_data->x, |
| 1807 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1808 | |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1809 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1810 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1811 | NULL, 0, |
| 1812 | output, |
| 1813 | output_size, |
| 1814 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1815 | TEST_ASSERT( (size_t) expected_size == output_length ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1816 | TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1817 | |
| 1818 | exit: |
| 1819 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1820 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1821 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1822 | } |
| 1823 | /* END_CASE */ |
| 1824 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1825 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1826 | void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1827 | int alg_arg, data_t *input_data, |
| 1828 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1829 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1830 | int slot = 1; |
| 1831 | psa_key_type_t key_type = key_type_arg; |
| 1832 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1833 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1834 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1835 | size_t output_length = 0; |
| 1836 | psa_status_t actual_status; |
| 1837 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1838 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1839 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1840 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1841 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1842 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1843 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1844 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1845 | output_size = key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1846 | output = mbedtls_calloc( 1, output_size ); |
| 1847 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1848 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1849 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1850 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1851 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1852 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1853 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1854 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1855 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1856 | key_data->x, |
| 1857 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1858 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1859 | actual_status = psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1860 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1861 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1862 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1863 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1864 | TEST_ASSERT( actual_status == expected_status ); |
| 1865 | |
| 1866 | exit: |
| 1867 | psa_destroy_key( slot ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1868 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1869 | mbedtls_psa_crypto_free( ); |
| 1870 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1871 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 1872 | |
| 1873 | /* BEGIN_CASE */ |
| 1874 | void generate_random( int bytes, int retries ) |
| 1875 | { |
| 1876 | const unsigned char trail[] = "foobar"; |
| 1877 | unsigned char *buffer1 = mbedtls_calloc( 1, bytes + sizeof( trail ) ); |
| 1878 | unsigned char *buffer2 = mbedtls_calloc( 1, bytes ); |
| 1879 | |
| 1880 | TEST_ASSERT( buffer1 != NULL ); |
| 1881 | TEST_ASSERT( buffer2 != NULL ); |
| 1882 | memcpy( buffer1 + bytes, trail, sizeof( trail ) ); |
| 1883 | |
| 1884 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1885 | |
| 1886 | TEST_ASSERT( psa_generate_random( buffer1, bytes ) == PSA_SUCCESS ); |
| 1887 | |
| 1888 | /* Check that no more than bytes have been overwritten */ |
| 1889 | TEST_ASSERT( memcmp( buffer1 + bytes, trail, sizeof( trail ) ) == 0 ); |
| 1890 | |
| 1891 | if( bytes == 0 ) |
| 1892 | goto exit; |
| 1893 | |
| 1894 | /* We can't validate that the data is really random, but we can |
| 1895 | * validate that it doesn't repeat between calls. There's a |
| 1896 | * 1/256^bytes chance that it does repeat, of course, so allow |
| 1897 | * a few retries. */ |
| 1898 | ++retries; /* The first time isn't a REtry */ |
| 1899 | do |
| 1900 | { |
| 1901 | --retries; |
| 1902 | TEST_ASSERT( psa_generate_random( buffer2, bytes ) == PSA_SUCCESS ); |
| 1903 | } |
| 1904 | while( memcmp( buffer1, buffer2, bytes ) == 0 && retries >= -1 ); |
| 1905 | TEST_ASSERT( retries >= 0 ); |
| 1906 | |
| 1907 | exit: |
| 1908 | mbedtls_psa_crypto_free( ); |
| 1909 | mbedtls_free( buffer1 ); |
| 1910 | mbedtls_free( buffer2 ); |
| 1911 | } |
| 1912 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 1913 | |
| 1914 | /* BEGIN_CASE */ |
| 1915 | void generate_key( int type_arg, |
| 1916 | int bits_arg, |
| 1917 | int usage_arg, |
| 1918 | int alg_arg, |
| 1919 | int expected_status_arg ) |
| 1920 | { |
| 1921 | int slot = 1; |
| 1922 | psa_key_type_t type = type_arg; |
| 1923 | psa_key_usage_t usage = usage_arg; |
| 1924 | size_t bits = bits_arg; |
| 1925 | psa_algorithm_t alg = alg_arg; |
| 1926 | psa_status_t expected_status = expected_status_arg; |
| 1927 | psa_key_type_t got_type; |
| 1928 | size_t got_bits; |
| 1929 | unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */ |
| 1930 | size_t exported_length; |
| 1931 | psa_status_t expected_export_status = |
| 1932 | usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED; |
| 1933 | psa_status_t expected_info_status = |
| 1934 | expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT; |
| 1935 | psa_key_policy_t policy; |
| 1936 | |
| 1937 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1938 | |
| 1939 | psa_key_policy_init( &policy ); |
| 1940 | psa_key_policy_set_usage( &policy, usage, alg ); |
| 1941 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1942 | |
| 1943 | /* Generate a key */ |
| 1944 | TEST_ASSERT( psa_generate_key( slot, type, bits, |
| 1945 | NULL, 0 ) == expected_status ); |
| 1946 | |
| 1947 | /* Test the key information */ |
| 1948 | TEST_ASSERT( psa_get_key_information( slot, |
| 1949 | &got_type, |
| 1950 | &got_bits ) == expected_info_status ); |
| 1951 | if( expected_info_status != PSA_SUCCESS ) |
| 1952 | goto exit; |
| 1953 | TEST_ASSERT( got_type == type ); |
| 1954 | TEST_ASSERT( got_bits == bits ); |
| 1955 | |
| 1956 | /* Export the key */ |
| 1957 | TEST_ASSERT( psa_export_key( slot, |
| 1958 | exported, sizeof( exported ), |
| 1959 | &exported_length ) == expected_export_status ); |
| 1960 | if( expected_export_status == PSA_SUCCESS ) |
| 1961 | { |
| 1962 | if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) ) |
| 1963 | TEST_ASSERT( exported_length == ( bits + 7 ) / 8 ); |
| 1964 | #if defined(MBEDTLS_DES_C) |
| 1965 | if( type == PSA_KEY_TYPE_DES ) |
| 1966 | { |
| 1967 | /* Check the parity bits. */ |
| 1968 | unsigned i; |
| 1969 | for( i = 0; i < bits / 8; i++ ) |
| 1970 | { |
| 1971 | unsigned bit_count = 0; |
| 1972 | unsigned m; |
| 1973 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 1974 | { |
| 1975 | if( exported[i] & m ) |
| 1976 | ++bit_count; |
| 1977 | } |
| 1978 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 1979 | } |
| 1980 | } |
| 1981 | #endif |
| 1982 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
| 1983 | if( type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 1984 | { |
| 1985 | /* Sanity check: does this look like the beginning of a PKCS#8 |
| 1986 | * RSA key pair? Assumes bits is a multiple of 8. */ |
| 1987 | size_t n_bytes = bits / 8 + 1; |
| 1988 | size_t n_encoded_bytes; |
| 1989 | unsigned char *n_end; |
| 1990 | TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 ); |
| 1991 | TEST_ASSERT( exported[0] == 0x30 ); |
| 1992 | TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key |
| 1993 | TEST_ASSERT( exported[4] == 0x02 ); |
| 1994 | TEST_ASSERT( exported[5] == 0x01 ); |
| 1995 | TEST_ASSERT( exported[6] == 0x00 ); |
| 1996 | TEST_ASSERT( exported[7] == 0x02 ); |
| 1997 | n_encoded_bytes = exported[8]; |
| 1998 | n_end = exported + 9 + n_encoded_bytes; |
| 1999 | if( n_encoded_bytes & 0x80 ) |
| 2000 | { |
| 2001 | n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7; |
| 2002 | n_encoded_bytes |= exported[9] & 0x7f; |
| 2003 | n_end += 1; |
| 2004 | } |
| 2005 | /* The encoding of n should start with a 0 byte since it should |
| 2006 | * have its high bit set. However Mbed TLS is not compliant and |
| 2007 | * generates an invalid, but widely tolerated, encoding of |
| 2008 | * positive INTEGERs with a bit size that is a multiple of 8 |
| 2009 | * with no leading 0 byte. Accept this here. */ |
| 2010 | TEST_ASSERT( n_bytes == n_encoded_bytes || |
| 2011 | n_bytes == n_encoded_bytes + 1 ); |
| 2012 | if( n_bytes == n_encoded_bytes ) |
| 2013 | TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 ); |
| 2014 | /* Sanity check: e must be 3 */ |
| 2015 | TEST_ASSERT( n_end[0] == 0x02 ); |
| 2016 | TEST_ASSERT( n_end[1] == 0x03 ); |
| 2017 | TEST_ASSERT( n_end[2] == 0x01 ); |
| 2018 | TEST_ASSERT( n_end[3] == 0x00 ); |
| 2019 | TEST_ASSERT( n_end[4] == 0x01 ); |
| 2020 | TEST_ASSERT( n_end[5] == 0x02 ); |
| 2021 | } |
| 2022 | #endif /* MBEDTLS_RSA_C */ |
| 2023 | #if defined(MBEDTLS_ECP_C) |
| 2024 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 2025 | { |
| 2026 | /* Sanity check: does this look like the beginning of a PKCS#8 |
| 2027 | * elliptic curve key pair? */ |
| 2028 | TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 ); |
| 2029 | TEST_ASSERT( exported[0] == 0x30 ); |
| 2030 | } |
| 2031 | #endif /* MBEDTLS_ECP_C */ |
| 2032 | } |
| 2033 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 2034 | /* Do something with the key according to its type and permitted usage. */ |
| 2035 | if( PSA_ALG_IS_MAC( alg ) ) |
| 2036 | exercise_mac_key( slot, usage, alg ); |
| 2037 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
| 2038 | exercise_cipher_key( slot, usage, alg ); |
| 2039 | else if( PSA_ALG_IS_AEAD( alg ) ) |
| 2040 | exercise_aead_key( slot, usage, alg ); |
| 2041 | else if( PSA_ALG_IS_SIGN( alg ) ) |
| 2042 | exercise_signature_key( slot, usage, alg ); |
| 2043 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 2044 | exercise_asymmetric_encryption_key( slot, usage, alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 2045 | |
| 2046 | exit: |
| 2047 | psa_destroy_key( slot ); |
| 2048 | mbedtls_psa_crypto_free( ); |
| 2049 | } |
| 2050 | /* END_CASE */ |