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