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 ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1060 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1061 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1062 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1063 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1064 | TEST_ASSERT( psa_cipher_update( &operation, |
| 1065 | input->x, input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1066 | output, output_buffer_size, |
| 1067 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1068 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1069 | status = psa_cipher_finish( &operation, |
| 1070 | output + function_output_length, |
| 1071 | output_buffer_size, |
| 1072 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1073 | total_output_length += function_output_length; |
| 1074 | |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1075 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1076 | if( expected_status == PSA_SUCCESS ) |
| 1077 | { |
| 1078 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1079 | TEST_ASSERT( total_output_length == expected_output->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1080 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1081 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1082 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1083 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1084 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1085 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1086 | psa_destroy_key( key_slot ); |
| 1087 | mbedtls_psa_crypto_free( ); |
| 1088 | } |
| 1089 | /* END_CASE */ |
| 1090 | |
| 1091 | /* BEGIN_CASE */ |
| 1092 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1093 | data_t *key, |
| 1094 | data_t *input, |
| 1095 | int first_part_size, |
| 1096 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1097 | { |
| 1098 | int key_slot = 1; |
| 1099 | psa_key_type_t key_type = key_type_arg; |
| 1100 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1101 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1102 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1103 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1104 | size_t output_buffer_size = 0; |
| 1105 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1106 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1107 | psa_cipher_operation_t operation; |
| 1108 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1109 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1110 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1111 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1112 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1113 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 1114 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1115 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1116 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 1117 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1118 | |
| 1119 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1120 | |
| 1121 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1122 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1123 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1124 | TEST_ASSERT( psa_encrypt_setup( &operation, |
| 1125 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1126 | |
| 1127 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 1128 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1129 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1130 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1131 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1132 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1133 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1134 | TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1135 | output, output_buffer_size, |
| 1136 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1137 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1138 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1139 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1140 | input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1141 | output, output_buffer_size, |
| 1142 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1143 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1144 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 1145 | output + function_output_length, |
| 1146 | output_buffer_size, |
| 1147 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1148 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1149 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 1150 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1151 | TEST_ASSERT( total_output_length == expected_output->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1152 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1153 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1154 | |
| 1155 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1156 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1157 | psa_destroy_key( key_slot ); |
| 1158 | mbedtls_psa_crypto_free( ); |
| 1159 | } |
| 1160 | /* END_CASE */ |
| 1161 | |
| 1162 | /* BEGIN_CASE */ |
| 1163 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1164 | data_t *key, |
| 1165 | data_t *input, |
| 1166 | int first_part_size, |
| 1167 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1168 | { |
| 1169 | int key_slot = 1; |
| 1170 | |
| 1171 | psa_key_type_t key_type = key_type_arg; |
| 1172 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1173 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1174 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1175 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1176 | size_t output_buffer_size = 0; |
| 1177 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1178 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1179 | psa_cipher_operation_t operation; |
| 1180 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1181 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1182 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1183 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1184 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1185 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 1186 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1187 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1188 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 1189 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1190 | |
| 1191 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1192 | |
| 1193 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1194 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1195 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1196 | TEST_ASSERT( psa_decrypt_setup( &operation, |
| 1197 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1198 | |
| 1199 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 1200 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 1201 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1202 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1203 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1204 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1205 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1206 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
| 1207 | TEST_ASSERT( psa_cipher_update( &operation, |
| 1208 | input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1209 | output, output_buffer_size, |
| 1210 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1211 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1212 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1213 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1214 | input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1215 | output, output_buffer_size, |
| 1216 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1217 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1218 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 1219 | output + function_output_length, |
| 1220 | output_buffer_size, |
| 1221 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1222 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1223 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 1224 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1225 | TEST_ASSERT( total_output_length == expected_output->len ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1226 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1227 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1228 | |
| 1229 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1230 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1231 | psa_destroy_key( key_slot ); |
| 1232 | mbedtls_psa_crypto_free( ); |
| 1233 | } |
| 1234 | /* END_CASE */ |
| 1235 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1236 | /* BEGIN_CASE */ |
| 1237 | void cipher_decrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1238 | data_t *key, |
| 1239 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1240 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1241 | { |
| 1242 | int key_slot = 1; |
| 1243 | psa_status_t status; |
| 1244 | psa_key_type_t key_type = key_type_arg; |
| 1245 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1246 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1247 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1248 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1249 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1250 | size_t output_buffer_size = 0; |
| 1251 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1252 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1253 | psa_cipher_operation_t operation; |
| 1254 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1255 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1256 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1257 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1258 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1259 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 1260 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1261 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1262 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 1263 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1264 | |
| 1265 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1266 | |
| 1267 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1268 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1269 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1270 | TEST_ASSERT( psa_decrypt_setup( &operation, |
| 1271 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1272 | |
| 1273 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1274 | iv, iv_size ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1275 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1276 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1277 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1278 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1279 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1280 | TEST_ASSERT( psa_cipher_update( &operation, |
| 1281 | input->x, input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1282 | output, output_buffer_size, |
| 1283 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1284 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1285 | status = psa_cipher_finish( &operation, |
| 1286 | output + function_output_length, |
| 1287 | output_buffer_size, |
| 1288 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1289 | total_output_length += function_output_length; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1290 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1291 | |
| 1292 | if( expected_status == PSA_SUCCESS ) |
| 1293 | { |
| 1294 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1295 | TEST_ASSERT( total_output_length == expected_output->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1296 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1297 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1298 | } |
| 1299 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1300 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1301 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1302 | psa_destroy_key( key_slot ); |
| 1303 | mbedtls_psa_crypto_free( ); |
| 1304 | } |
| 1305 | /* END_CASE */ |
| 1306 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1307 | /* BEGIN_CASE */ |
| 1308 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1309 | data_t *key, |
| 1310 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1311 | { |
| 1312 | int key_slot = 1; |
| 1313 | psa_key_type_t key_type = key_type_arg; |
| 1314 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 1315 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1316 | size_t iv_size = 16; |
| 1317 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1318 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1319 | size_t output1_size = 0; |
| 1320 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1321 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1322 | size_t output2_size = 0; |
| 1323 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1324 | size_t function_output_length = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1325 | psa_cipher_operation_t operation1; |
| 1326 | psa_cipher_operation_t operation2; |
| 1327 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1328 | TEST_ASSERT( key != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1329 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1330 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1331 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1332 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1333 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1334 | |
| 1335 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1336 | key->x, key->len ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1337 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1338 | TEST_ASSERT( psa_encrypt_setup( &operation1, |
| 1339 | key_slot, alg ) == PSA_SUCCESS ); |
| 1340 | TEST_ASSERT( psa_decrypt_setup( &operation2, |
| 1341 | key_slot, alg ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1342 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1343 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 1344 | iv, iv_size, |
| 1345 | &iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1346 | output1_size = input->len + operation1.block_size; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1347 | output1 = mbedtls_calloc( 1, output1_size ); |
| 1348 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1349 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1350 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1351 | output1, output1_size, |
| 1352 | &output1_length ) == PSA_SUCCESS ); |
| 1353 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 1354 | output1 + output1_length, output1_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1355 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1356 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1357 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1358 | |
| 1359 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1360 | |
| 1361 | output2_size = output1_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1362 | output2 = mbedtls_calloc( 1, output2_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1363 | TEST_ASSERT( output2 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1364 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1365 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 1366 | iv, iv_length ) == PSA_SUCCESS ); |
| 1367 | TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 1368 | output2, output2_size, |
| 1369 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1370 | function_output_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1371 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 1372 | output2 + output2_length, |
| 1373 | output2_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1374 | &function_output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1375 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1376 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1377 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1378 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1379 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1380 | TEST_ASSERT( input->len == output2_length ); |
| 1381 | TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1382 | |
| 1383 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1384 | mbedtls_free( output1 ); |
| 1385 | mbedtls_free( output2 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1386 | psa_destroy_key( key_slot ); |
| 1387 | mbedtls_psa_crypto_free( ); |
| 1388 | } |
| 1389 | /* END_CASE */ |
| 1390 | |
| 1391 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1392 | void cipher_verify_output_multipart( int alg_arg, |
| 1393 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1394 | data_t *key, |
| 1395 | data_t *input, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1396 | int first_part_size ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1397 | { |
| 1398 | int key_slot = 1; |
| 1399 | psa_key_type_t key_type = key_type_arg; |
| 1400 | psa_algorithm_t alg = alg_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1401 | unsigned char iv[16] = {0}; |
| 1402 | size_t iv_size = 16; |
| 1403 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1404 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1405 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1406 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1407 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1408 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1409 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1410 | size_t function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1411 | psa_cipher_operation_t operation1; |
| 1412 | psa_cipher_operation_t operation2; |
| 1413 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1414 | TEST_ASSERT( key != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1415 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1416 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1417 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1418 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1419 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1420 | |
| 1421 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1422 | key->x, key->len ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1423 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1424 | TEST_ASSERT( psa_encrypt_setup( &operation1, |
| 1425 | key_slot, alg ) == PSA_SUCCESS ); |
| 1426 | TEST_ASSERT( psa_decrypt_setup( &operation2, |
| 1427 | key_slot, alg ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1428 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1429 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 1430 | iv, iv_size, |
| 1431 | &iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1432 | output1_buffer_size = input->len + operation1.block_size; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1433 | output1 = mbedtls_calloc( 1, output1_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1434 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1435 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1436 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1437 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1438 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1439 | output1, output1_buffer_size, |
| 1440 | &function_output_length ) == PSA_SUCCESS ); |
| 1441 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1442 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1443 | TEST_ASSERT( psa_cipher_update( &operation1, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1444 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1445 | input->len - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1446 | output1, output1_buffer_size, |
| 1447 | &function_output_length ) == PSA_SUCCESS ); |
| 1448 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1449 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1450 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 1451 | output1 + output1_length, |
| 1452 | output1_buffer_size - output1_length, |
| 1453 | &function_output_length ) == PSA_SUCCESS ); |
| 1454 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1455 | |
| 1456 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1457 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1458 | output2_buffer_size = output1_length; |
| 1459 | output2 = mbedtls_calloc( 1, output2_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1460 | TEST_ASSERT( output2 != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1461 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1462 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 1463 | iv, iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1464 | |
| 1465 | TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1466 | output2, output2_buffer_size, |
| 1467 | &function_output_length ) == PSA_SUCCESS ); |
| 1468 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1469 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1470 | TEST_ASSERT( psa_cipher_update( &operation2, |
| 1471 | output1 + first_part_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1472 | output1_length - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1473 | output2, output2_buffer_size, |
| 1474 | &function_output_length ) == PSA_SUCCESS ); |
| 1475 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1476 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1477 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 1478 | output2 + output2_length, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1479 | output2_buffer_size - output2_length, |
| 1480 | &function_output_length ) == PSA_SUCCESS ); |
| 1481 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1482 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1483 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1484 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1485 | TEST_ASSERT( input->len == output2_length ); |
| 1486 | TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1487 | |
| 1488 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1489 | mbedtls_free( output1 ); |
| 1490 | mbedtls_free( output2 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1491 | psa_destroy_key( key_slot ); |
| 1492 | mbedtls_psa_crypto_free( ); |
| 1493 | } |
| 1494 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1495 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1496 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1497 | void aead_encrypt_decrypt( int key_type_arg, |
| 1498 | data_t * key_data, |
| 1499 | int alg_arg, |
| 1500 | data_t * input_data, |
| 1501 | data_t * nonce, |
| 1502 | data_t * additional_data, |
| 1503 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1504 | { |
| 1505 | int slot = 1; |
| 1506 | psa_key_type_t key_type = key_type_arg; |
| 1507 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1508 | unsigned char *output_data = NULL; |
| 1509 | size_t output_size = 0; |
| 1510 | size_t output_length = 0; |
| 1511 | unsigned char *output_data2 = NULL; |
| 1512 | size_t output_length2 = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1513 | size_t tag_length = 16; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1514 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1515 | psa_key_policy_t policy; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1516 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1517 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1518 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1519 | TEST_ASSERT( nonce != NULL ); |
| 1520 | TEST_ASSERT( additional_data != NULL ); |
| 1521 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1522 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1523 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 1524 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 1525 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1526 | output_size = input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1527 | output_data = mbedtls_calloc( 1, output_size ); |
| 1528 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1529 | |
| 1530 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1531 | |
| 1532 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1533 | psa_key_policy_set_usage( &policy, |
| 1534 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
| 1535 | alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1536 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1537 | |
| 1538 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1539 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1540 | |
| 1541 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1542 | nonce->x, nonce->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1543 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1544 | additional_data->len, |
| 1545 | input_data->x, input_data->len, |
| 1546 | output_data, output_size, |
| 1547 | &output_length ) == expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1548 | |
| 1549 | if( PSA_SUCCESS == expected_result ) |
| 1550 | { |
| 1551 | output_data2 = mbedtls_calloc( 1, output_length ); |
| 1552 | TEST_ASSERT( output_data2 != NULL ); |
| 1553 | |
| 1554 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1555 | nonce->x, nonce->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1556 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1557 | additional_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1558 | output_data, output_length, |
| 1559 | output_data2, output_length, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1560 | &output_length2 ) == expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1561 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1562 | TEST_ASSERT( memcmp( input_data->x, output_data2, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1563 | input_data->len ) == 0 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1564 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1565 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1566 | exit: |
| 1567 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1568 | mbedtls_free( output_data ); |
| 1569 | mbedtls_free( output_data2 ); |
| 1570 | mbedtls_psa_crypto_free( ); |
| 1571 | } |
| 1572 | /* END_CASE */ |
| 1573 | |
| 1574 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1575 | void aead_encrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1576 | int alg_arg, data_t * input_data, |
| 1577 | data_t * additional_data, data_t * nonce, |
| 1578 | data_t * expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1579 | { |
| 1580 | int slot = 1; |
| 1581 | psa_key_type_t key_type = key_type_arg; |
| 1582 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1583 | unsigned char *output_data = NULL; |
| 1584 | size_t output_size = 0; |
| 1585 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1586 | size_t tag_length = 16; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1587 | psa_key_policy_t policy; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1588 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1589 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1590 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1591 | TEST_ASSERT( additional_data != NULL ); |
| 1592 | TEST_ASSERT( nonce != NULL ); |
| 1593 | TEST_ASSERT( expected_result != NULL ); |
| 1594 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1595 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1596 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 1597 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 1598 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) ); |
| 1599 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1600 | output_size = input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1601 | output_data = mbedtls_calloc( 1, output_size ); |
| 1602 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1603 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1604 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1605 | |
| 1606 | psa_key_policy_init( &policy ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1607 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1608 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1609 | |
| 1610 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1611 | key_data->x, |
| 1612 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1613 | |
| 1614 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1615 | nonce->x, nonce->len, |
| 1616 | additional_data->x, additional_data->len, |
| 1617 | input_data->x, input_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1618 | output_data, output_size, |
| 1619 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1620 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1621 | TEST_ASSERT( memcmp( output_data, expected_result->x, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1622 | output_length ) == 0 ); |
| 1623 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1624 | exit: |
| 1625 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1626 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1627 | mbedtls_psa_crypto_free( ); |
| 1628 | } |
| 1629 | /* END_CASE */ |
| 1630 | |
| 1631 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1632 | void aead_decrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1633 | int alg_arg, data_t * input_data, |
| 1634 | data_t * additional_data, data_t * nonce, |
| 1635 | data_t * expected_data, int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1636 | { |
| 1637 | int slot = 1; |
| 1638 | psa_key_type_t key_type = key_type_arg; |
| 1639 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1640 | unsigned char *output_data = NULL; |
| 1641 | size_t output_size = 0; |
| 1642 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1643 | size_t tag_length = 16; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1644 | psa_key_policy_t policy; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1645 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1646 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1647 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1648 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1649 | TEST_ASSERT( additional_data != NULL ); |
| 1650 | TEST_ASSERT( nonce != NULL ); |
| 1651 | TEST_ASSERT( expected_data != NULL ); |
| 1652 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1653 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1654 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 1655 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 1656 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 1657 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1658 | output_size = input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1659 | output_data = mbedtls_calloc( 1, output_size ); |
| 1660 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1661 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1662 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1663 | |
| 1664 | psa_key_policy_init( &policy ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1665 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1666 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1667 | |
| 1668 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1669 | key_data->x, |
| 1670 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1671 | |
| 1672 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1673 | nonce->x, nonce->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1674 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1675 | additional_data->len, |
| 1676 | input_data->x, input_data->len, |
| 1677 | output_data, output_size, |
| 1678 | &output_length ) == expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1679 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1680 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1681 | { |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1682 | TEST_ASSERT( memcmp( output_data, expected_data->x, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1683 | output_length ) == 0 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1684 | } |
| 1685 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1686 | exit: |
| 1687 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1688 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1689 | mbedtls_psa_crypto_free( ); |
| 1690 | } |
| 1691 | /* END_CASE */ |
| 1692 | |
| 1693 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1694 | void signature_size( int type_arg, |
| 1695 | int bits, |
| 1696 | int alg_arg, |
| 1697 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1698 | { |
| 1699 | psa_key_type_t type = type_arg; |
| 1700 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1701 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1702 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 1703 | exit: |
| 1704 | ; |
| 1705 | } |
| 1706 | /* END_CASE */ |
| 1707 | |
| 1708 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1709 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 1710 | int alg_arg, data_t *input_data, |
| 1711 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1712 | { |
| 1713 | int slot = 1; |
| 1714 | psa_key_type_t key_type = key_type_arg; |
| 1715 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1716 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1717 | unsigned char *signature = NULL; |
| 1718 | size_t signature_size; |
| 1719 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1720 | psa_key_policy_t policy; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1721 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1722 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1723 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1724 | TEST_ASSERT( output_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1725 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1726 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1727 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1728 | |
| 1729 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1730 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1731 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1732 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1733 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1734 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1735 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1736 | key_data->x, |
| 1737 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1738 | TEST_ASSERT( psa_get_key_information( slot, |
| 1739 | NULL, |
| 1740 | &key_bits ) == PSA_SUCCESS ); |
| 1741 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1742 | /* Allocate a buffer which has the size advertized by the |
| 1743 | * library. */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1744 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 1745 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1746 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 1747 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1748 | signature = mbedtls_calloc( 1, signature_size ); |
| 1749 | TEST_ASSERT( signature != NULL ); |
| 1750 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1751 | /* Perform the signature. */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1752 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1753 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1754 | NULL, 0, |
| 1755 | signature, signature_size, |
| 1756 | &signature_length ) == PSA_SUCCESS ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1757 | /* Verify that the signature is correct. */ |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1758 | TEST_ASSERT( signature_length == output_data->len ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1759 | TEST_ASSERT( memcmp( signature, output_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1760 | output_data->len ) == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1761 | |
| 1762 | exit: |
| 1763 | psa_destroy_key( slot ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1764 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1765 | mbedtls_psa_crypto_free( ); |
| 1766 | } |
| 1767 | /* END_CASE */ |
| 1768 | |
| 1769 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1770 | void sign_fail( int key_type_arg, data_t *key_data, |
| 1771 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1772 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1773 | { |
| 1774 | int slot = 1; |
| 1775 | psa_key_type_t key_type = key_type_arg; |
| 1776 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1777 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1778 | psa_status_t actual_status; |
| 1779 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 1780 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 1781 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1782 | psa_key_policy_t policy; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1783 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1784 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1785 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1786 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1787 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1788 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1789 | signature = mbedtls_calloc( 1, signature_size ); |
| 1790 | TEST_ASSERT( signature != NULL ); |
| 1791 | |
| 1792 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1793 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1794 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1795 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1796 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1797 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1798 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1799 | key_data->x, |
| 1800 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1801 | |
| 1802 | actual_status = psa_asymmetric_sign( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1803 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1804 | NULL, 0, |
| 1805 | signature, signature_size, |
| 1806 | &signature_length ); |
| 1807 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1808 | /* The value of *signature_length is unspecified on error, but |
| 1809 | * whatever it is, it should be less than signature_size, so that |
| 1810 | * if the caller tries to read *signature_length bytes without |
| 1811 | * checking the error code then they don't overflow a buffer. */ |
| 1812 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1813 | |
| 1814 | exit: |
| 1815 | psa_destroy_key( slot ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1816 | mbedtls_free( signature ); |
| 1817 | mbedtls_psa_crypto_free( ); |
| 1818 | } |
| 1819 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1820 | |
| 1821 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1822 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 1823 | int alg_arg, data_t *hash_data, |
| 1824 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1825 | { |
| 1826 | int slot = 1; |
| 1827 | psa_key_type_t key_type = key_type_arg; |
| 1828 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1829 | psa_key_policy_t policy; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1830 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 1831 | TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
| 1832 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1833 | TEST_ASSERT( key_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1834 | TEST_ASSERT( hash_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1835 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1836 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1837 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 1838 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1839 | |
| 1840 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1841 | |
| 1842 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1843 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1844 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1845 | |
| 1846 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1847 | key_data->x, |
| 1848 | key_data->len ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1849 | |
| 1850 | TEST_ASSERT( psa_asymmetric_verify( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1851 | hash_data->x, hash_data->len, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1852 | NULL, 0, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1853 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1854 | signature_data->len ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1855 | exit: |
| 1856 | psa_destroy_key( slot ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1857 | mbedtls_psa_crypto_free( ); |
| 1858 | } |
| 1859 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1860 | |
| 1861 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1862 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 1863 | int alg_arg, data_t *hash_data, |
| 1864 | data_t *signature_data, |
| 1865 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1866 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1867 | int slot = 1; |
| 1868 | psa_key_type_t key_type = key_type_arg; |
| 1869 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1870 | psa_status_t actual_status; |
| 1871 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1872 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1873 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1874 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1875 | TEST_ASSERT( hash_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1876 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1877 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1878 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 1879 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1880 | |
| 1881 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1882 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1883 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1884 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1885 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1886 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1887 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1888 | key_data->x, |
| 1889 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1890 | |
| 1891 | actual_status = psa_asymmetric_verify( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1892 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1893 | NULL, 0, |
| 1894 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1895 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1896 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1897 | TEST_ASSERT( actual_status == expected_status ); |
| 1898 | |
| 1899 | exit: |
| 1900 | psa_destroy_key( slot ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1901 | mbedtls_psa_crypto_free( ); |
| 1902 | } |
| 1903 | /* END_CASE */ |
| 1904 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1905 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1906 | void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data, |
| 1907 | int alg_arg, data_t *input_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1908 | { |
| 1909 | int slot = 1; |
| 1910 | psa_key_type_t key_type = key_type_arg; |
| 1911 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1912 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1913 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1914 | size_t output_length = 0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1915 | unsigned char *output2 = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1916 | size_t output2_size = 0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1917 | size_t output2_length = 0; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1918 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1919 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1920 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1921 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1922 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1923 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1924 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1925 | output_size = key_data->len; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1926 | output2_size = output_size; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1927 | output = mbedtls_calloc( 1, output_size ); |
| 1928 | TEST_ASSERT( output != NULL ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1929 | output2 = mbedtls_calloc( 1, output2_size ); |
| 1930 | TEST_ASSERT( output2 != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1931 | |
| 1932 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1933 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1934 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1935 | psa_key_policy_set_usage( &policy, |
| 1936 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1937 | alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1938 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1939 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1940 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1941 | key_data->x, |
| 1942 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1943 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 1944 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 1945 | * the original plaintext because of the non-optional random |
| 1946 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1947 | TEST_ASSERT( psa_asymmetric_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1948 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1949 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1950 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1951 | &output_length ) == PSA_SUCCESS ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1952 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1953 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1954 | output, output_length, |
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 | output2, output2_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1957 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1958 | TEST_ASSERT( memcmp( input_data->x, output2, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1959 | input_data->len ) == 0 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1960 | |
| 1961 | exit: |
| 1962 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1963 | mbedtls_free( output ); |
| 1964 | mbedtls_free( output2 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1965 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1966 | } |
| 1967 | /* END_CASE */ |
| 1968 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1969 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1970 | void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1971 | int alg_arg, data_t *input_data, |
| 1972 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1973 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1974 | int slot = 1; |
| 1975 | psa_key_type_t key_type = key_type_arg; |
| 1976 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1977 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1978 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1979 | size_t output_length = 0; |
| 1980 | psa_status_t actual_status; |
| 1981 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1982 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1983 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1984 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1985 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1986 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1987 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1988 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1989 | output_size = key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1990 | output = mbedtls_calloc( 1, output_size ); |
| 1991 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1992 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1993 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1994 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1995 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1996 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1997 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1998 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1999 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2000 | key_data->x, |
| 2001 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2002 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2003 | actual_status = psa_asymmetric_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2004 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2005 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2006 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2007 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2008 | TEST_ASSERT( actual_status == expected_status ); |
| 2009 | |
| 2010 | exit: |
| 2011 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2012 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2013 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2014 | } |
| 2015 | /* END_CASE */ |
| 2016 | |
| 2017 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2018 | void asymmetric_decrypt( int key_type_arg, data_t *key_data, |
| 2019 | int alg_arg, data_t *input_data, |
| 2020 | data_t *expected_data, int expected_size ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2021 | { |
| 2022 | int slot = 1; |
| 2023 | psa_key_type_t key_type = key_type_arg; |
| 2024 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2025 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 2026 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2027 | size_t output_length = 0; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2028 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2029 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2030 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2031 | TEST_ASSERT( input_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2032 | TEST_ASSERT( expected_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2033 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2034 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2035 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 2036 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2037 | output_size = key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2038 | output = mbedtls_calloc( 1, output_size ); |
| 2039 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2040 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2041 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2042 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 2043 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2044 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 2045 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2046 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2047 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2048 | key_data->x, |
| 2049 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2050 | |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 2051 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2052 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2053 | NULL, 0, |
| 2054 | output, |
| 2055 | output_size, |
| 2056 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2057 | TEST_ASSERT( (size_t) expected_size == output_length ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2058 | TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2059 | |
| 2060 | exit: |
| 2061 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2062 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2063 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2064 | } |
| 2065 | /* END_CASE */ |
| 2066 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2067 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2068 | void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2069 | int alg_arg, data_t *input_data, |
| 2070 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2071 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2072 | int slot = 1; |
| 2073 | psa_key_type_t key_type = key_type_arg; |
| 2074 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2075 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 2076 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2077 | size_t output_length = 0; |
| 2078 | psa_status_t actual_status; |
| 2079 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2080 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2081 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2082 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2083 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2084 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2085 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2086 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2087 | output_size = key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2088 | output = mbedtls_calloc( 1, output_size ); |
| 2089 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2090 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2091 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2092 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 2093 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2094 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 2095 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2096 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2097 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2098 | key_data->x, |
| 2099 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2100 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2101 | actual_status = psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2102 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2103 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2104 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2105 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2106 | TEST_ASSERT( actual_status == expected_status ); |
| 2107 | |
| 2108 | exit: |
| 2109 | psa_destroy_key( slot ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2110 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2111 | mbedtls_psa_crypto_free( ); |
| 2112 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2113 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2114 | |
| 2115 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2116 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2117 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2118 | size_t bytes = bytes_arg; |
| 2119 | const unsigned char trail[] = "don't overwrite me"; |
| 2120 | unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) ); |
| 2121 | unsigned char *changed = mbedtls_calloc( 1, bytes ); |
| 2122 | size_t i; |
| 2123 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2124 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2125 | TEST_ASSERT( output != NULL ); |
| 2126 | TEST_ASSERT( changed != NULL ); |
| 2127 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2128 | |
| 2129 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2130 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2131 | /* Run several times, to ensure that every output byte will be |
| 2132 | * nonzero at least once with overwhelming probability |
| 2133 | * (2^(-8*number_of_runs)). */ |
| 2134 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2135 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2136 | memset( output, 0, bytes ); |
| 2137 | TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS ); |
| 2138 | |
| 2139 | /* Check that no more than bytes have been overwritten */ |
| 2140 | TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 ); |
| 2141 | |
| 2142 | for( i = 0; i < bytes; i++ ) |
| 2143 | { |
| 2144 | if( output[i] != 0 ) |
| 2145 | ++changed[i]; |
| 2146 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2147 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2148 | |
| 2149 | /* Check that every byte was changed to nonzero at least once. This |
| 2150 | * validates that psa_generate_random is overwriting every byte of |
| 2151 | * the output buffer. */ |
| 2152 | for( i = 0; i < bytes; i++ ) |
| 2153 | { |
| 2154 | TEST_ASSERT( changed[i] != 0 ); |
| 2155 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2156 | |
| 2157 | exit: |
| 2158 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2159 | mbedtls_free( output ); |
| 2160 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2161 | } |
| 2162 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 2163 | |
| 2164 | /* BEGIN_CASE */ |
| 2165 | void generate_key( int type_arg, |
| 2166 | int bits_arg, |
| 2167 | int usage_arg, |
| 2168 | int alg_arg, |
| 2169 | int expected_status_arg ) |
| 2170 | { |
| 2171 | int slot = 1; |
| 2172 | psa_key_type_t type = type_arg; |
| 2173 | psa_key_usage_t usage = usage_arg; |
| 2174 | size_t bits = bits_arg; |
| 2175 | psa_algorithm_t alg = alg_arg; |
| 2176 | psa_status_t expected_status = expected_status_arg; |
| 2177 | psa_key_type_t got_type; |
| 2178 | size_t got_bits; |
| 2179 | unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */ |
| 2180 | size_t exported_length; |
| 2181 | psa_status_t expected_export_status = |
| 2182 | usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED; |
| 2183 | psa_status_t expected_info_status = |
| 2184 | expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT; |
| 2185 | psa_key_policy_t policy; |
| 2186 | |
| 2187 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2188 | |
| 2189 | psa_key_policy_init( &policy ); |
| 2190 | psa_key_policy_set_usage( &policy, usage, alg ); |
| 2191 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2192 | |
| 2193 | /* Generate a key */ |
| 2194 | TEST_ASSERT( psa_generate_key( slot, type, bits, |
| 2195 | NULL, 0 ) == expected_status ); |
| 2196 | |
| 2197 | /* Test the key information */ |
| 2198 | TEST_ASSERT( psa_get_key_information( slot, |
| 2199 | &got_type, |
| 2200 | &got_bits ) == expected_info_status ); |
| 2201 | if( expected_info_status != PSA_SUCCESS ) |
| 2202 | goto exit; |
| 2203 | TEST_ASSERT( got_type == type ); |
| 2204 | TEST_ASSERT( got_bits == bits ); |
| 2205 | |
| 2206 | /* Export the key */ |
| 2207 | TEST_ASSERT( psa_export_key( slot, |
| 2208 | exported, sizeof( exported ), |
| 2209 | &exported_length ) == expected_export_status ); |
| 2210 | if( expected_export_status == PSA_SUCCESS ) |
| 2211 | { |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 2212 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 2213 | TEST_ASSERT( exported_length == ( bits + 7 ) / 8 ); |
| 2214 | #if defined(MBEDTLS_DES_C) |
| 2215 | if( type == PSA_KEY_TYPE_DES ) |
| 2216 | { |
| 2217 | /* Check the parity bits. */ |
| 2218 | unsigned i; |
| 2219 | for( i = 0; i < bits / 8; i++ ) |
| 2220 | { |
| 2221 | unsigned bit_count = 0; |
| 2222 | unsigned m; |
| 2223 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 2224 | { |
| 2225 | if( exported[i] & m ) |
| 2226 | ++bit_count; |
| 2227 | } |
| 2228 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 2229 | } |
| 2230 | } |
| 2231 | #endif |
| 2232 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
| 2233 | if( type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 2234 | { |
| 2235 | /* Sanity check: does this look like the beginning of a PKCS#8 |
| 2236 | * RSA key pair? Assumes bits is a multiple of 8. */ |
| 2237 | size_t n_bytes = bits / 8 + 1; |
| 2238 | size_t n_encoded_bytes; |
| 2239 | unsigned char *n_end; |
| 2240 | TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 ); |
| 2241 | TEST_ASSERT( exported[0] == 0x30 ); |
| 2242 | TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key |
| 2243 | TEST_ASSERT( exported[4] == 0x02 ); |
| 2244 | TEST_ASSERT( exported[5] == 0x01 ); |
| 2245 | TEST_ASSERT( exported[6] == 0x00 ); |
| 2246 | TEST_ASSERT( exported[7] == 0x02 ); |
| 2247 | n_encoded_bytes = exported[8]; |
| 2248 | n_end = exported + 9 + n_encoded_bytes; |
| 2249 | if( n_encoded_bytes & 0x80 ) |
| 2250 | { |
| 2251 | n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7; |
| 2252 | n_encoded_bytes |= exported[9] & 0x7f; |
| 2253 | n_end += 1; |
| 2254 | } |
| 2255 | /* The encoding of n should start with a 0 byte since it should |
| 2256 | * have its high bit set. However Mbed TLS is not compliant and |
| 2257 | * generates an invalid, but widely tolerated, encoding of |
| 2258 | * positive INTEGERs with a bit size that is a multiple of 8 |
| 2259 | * with no leading 0 byte. Accept this here. */ |
| 2260 | TEST_ASSERT( n_bytes == n_encoded_bytes || |
| 2261 | n_bytes == n_encoded_bytes + 1 ); |
| 2262 | if( n_bytes == n_encoded_bytes ) |
| 2263 | TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 ); |
| 2264 | /* Sanity check: e must be 3 */ |
| 2265 | TEST_ASSERT( n_end[0] == 0x02 ); |
| 2266 | TEST_ASSERT( n_end[1] == 0x03 ); |
| 2267 | TEST_ASSERT( n_end[2] == 0x01 ); |
| 2268 | TEST_ASSERT( n_end[3] == 0x00 ); |
| 2269 | TEST_ASSERT( n_end[4] == 0x01 ); |
| 2270 | TEST_ASSERT( n_end[5] == 0x02 ); |
| 2271 | } |
| 2272 | #endif /* MBEDTLS_RSA_C */ |
| 2273 | #if defined(MBEDTLS_ECP_C) |
| 2274 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 2275 | { |
| 2276 | /* Sanity check: does this look like the beginning of a PKCS#8 |
| 2277 | * elliptic curve key pair? */ |
| 2278 | TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 ); |
| 2279 | TEST_ASSERT( exported[0] == 0x30 ); |
| 2280 | } |
| 2281 | #endif /* MBEDTLS_ECP_C */ |
| 2282 | } |
| 2283 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 2284 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 2285 | if( ! exercise_key( slot, usage, alg ) ) |
| 2286 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 2287 | |
| 2288 | exit: |
| 2289 | psa_destroy_key( slot ); |
| 2290 | mbedtls_psa_crypto_free( ); |
| 2291 | } |
| 2292 | /* END_CASE */ |