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