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