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