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