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