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