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