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