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