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 | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 8 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 9 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 10 | #include "mbedtls/oid.h" |
| 11 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 12 | #include "psa/crypto.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 13 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 14 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 15 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 16 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 17 | /** Test if a buffer contains a constant byte value. |
| 18 | * |
| 19 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 20 | * |
| 21 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 22 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 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 | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 28 | static int mem_is_char( void *buffer, unsigned char c, 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 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 33 | if( ( (unsigned char *) buffer )[i] != c ) |
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 | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 39 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 40 | static int asn1_write_10x( unsigned char **p, |
| 41 | unsigned char *start, |
| 42 | size_t bits, |
| 43 | unsigned char x ) |
| 44 | { |
| 45 | int ret; |
| 46 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 47 | if( bits == 0 ) |
| 48 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 49 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 50 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 51 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 52 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 53 | *p -= len; |
| 54 | ( *p )[len-1] = x; |
| 55 | if( bits % 8 == 0 ) |
| 56 | ( *p )[1] |= 1; |
| 57 | else |
| 58 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 59 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 60 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 61 | MBEDTLS_ASN1_INTEGER ) ); |
| 62 | return( len ); |
| 63 | } |
| 64 | |
| 65 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 66 | size_t buffer_size, |
| 67 | unsigned char **p, |
| 68 | size_t bits, |
| 69 | int keypair ) |
| 70 | { |
| 71 | size_t half_bits = ( bits + 1 ) / 2; |
| 72 | int ret; |
| 73 | int len = 0; |
| 74 | /* Construct something that looks like a DER encoding of |
| 75 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 76 | * RSAPrivateKey ::= SEQUENCE { |
| 77 | * version Version, |
| 78 | * modulus INTEGER, -- n |
| 79 | * publicExponent INTEGER, -- e |
| 80 | * privateExponent INTEGER, -- d |
| 81 | * prime1 INTEGER, -- p |
| 82 | * prime2 INTEGER, -- q |
| 83 | * exponent1 INTEGER, -- d mod (p-1) |
| 84 | * exponent2 INTEGER, -- d mod (q-1) |
| 85 | * coefficient INTEGER, -- (inverse of q) mod p |
| 86 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 87 | * } |
| 88 | * Or, for a public key, the same structure with only |
| 89 | * version, modulus and publicExponent. |
| 90 | */ |
| 91 | *p = buffer + buffer_size; |
| 92 | if( keypair ) |
| 93 | { |
| 94 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 95 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 96 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 97 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 98 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 99 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 100 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 101 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 102 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 103 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 104 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 105 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 106 | } |
| 107 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 108 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 109 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 110 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 111 | if( keypair ) |
| 112 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 113 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 114 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 115 | { |
| 116 | const unsigned char tag = |
| 117 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 118 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 119 | } |
| 120 | return( len ); |
| 121 | } |
| 122 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 123 | static int exercise_mac_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 124 | psa_key_usage_t usage, |
| 125 | psa_algorithm_t alg ) |
| 126 | { |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 127 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 128 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 129 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 130 | size_t mac_length = sizeof( mac ); |
| 131 | |
| 132 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 133 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 134 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 135 | handle, alg ) ); |
| 136 | PSA_ASSERT( psa_mac_update( &operation, |
| 137 | input, sizeof( input ) ) ); |
| 138 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 139 | mac, sizeof( mac ), |
| 140 | &mac_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 144 | { |
| 145 | psa_status_t verify_status = |
| 146 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 147 | PSA_SUCCESS : |
| 148 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 149 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 150 | handle, alg ) ); |
| 151 | PSA_ASSERT( psa_mac_update( &operation, |
| 152 | input, sizeof( input ) ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 153 | TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ), |
| 154 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return( 1 ); |
| 158 | |
| 159 | exit: |
| 160 | psa_mac_abort( &operation ); |
| 161 | return( 0 ); |
| 162 | } |
| 163 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 164 | static int exercise_cipher_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 165 | psa_key_usage_t usage, |
| 166 | psa_algorithm_t alg ) |
| 167 | { |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 168 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 169 | unsigned char iv[16] = {0}; |
| 170 | size_t iv_length = sizeof( iv ); |
| 171 | const unsigned char plaintext[16] = "Hello, world..."; |
| 172 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 173 | size_t ciphertext_length = sizeof( ciphertext ); |
| 174 | unsigned char decrypted[sizeof( ciphertext )]; |
| 175 | size_t part_length; |
| 176 | |
| 177 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 178 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 179 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 180 | handle, alg ) ); |
| 181 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 182 | iv, sizeof( iv ), |
| 183 | &iv_length ) ); |
| 184 | PSA_ASSERT( psa_cipher_update( &operation, |
| 185 | plaintext, sizeof( plaintext ), |
| 186 | ciphertext, sizeof( ciphertext ), |
| 187 | &ciphertext_length ) ); |
| 188 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 189 | ciphertext + ciphertext_length, |
| 190 | sizeof( ciphertext ) - ciphertext_length, |
| 191 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 192 | ciphertext_length += part_length; |
| 193 | } |
| 194 | |
| 195 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 196 | { |
| 197 | psa_status_t status; |
Mohammad AboMokh | adb9b23 | 2018-06-28 01:52:54 -0700 | [diff] [blame] | 198 | psa_key_type_t type = PSA_KEY_TYPE_NONE; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 199 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 200 | { |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 201 | size_t bits; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 202 | TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 203 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type ); |
| 204 | } |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 205 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 206 | handle, alg ) ); |
| 207 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 208 | iv, iv_length ) ); |
| 209 | PSA_ASSERT( psa_cipher_update( &operation, |
| 210 | ciphertext, ciphertext_length, |
| 211 | decrypted, sizeof( decrypted ), |
| 212 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 213 | status = psa_cipher_finish( &operation, |
| 214 | decrypted + part_length, |
| 215 | sizeof( decrypted ) - part_length, |
| 216 | &part_length ); |
| 217 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 218 | * if the input is some aribtrary data rather than an actual |
| 219 | ciphertext, a padding error is likely. */ |
Mohammad AboMokh | 65fa0b8 | 2018-06-28 02:14:00 -0700 | [diff] [blame] | 220 | if( ( usage & PSA_KEY_USAGE_ENCRYPT ) || |
Mohammad AboMokh | adb9b23 | 2018-06-28 01:52:54 -0700 | [diff] [blame] | 221 | PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 222 | PSA_ASSERT( status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 223 | else |
| 224 | TEST_ASSERT( status == PSA_SUCCESS || |
| 225 | status == PSA_ERROR_INVALID_PADDING ); |
| 226 | } |
| 227 | |
| 228 | return( 1 ); |
| 229 | |
| 230 | exit: |
| 231 | psa_cipher_abort( &operation ); |
| 232 | return( 0 ); |
| 233 | } |
| 234 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 235 | static int exercise_aead_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 236 | psa_key_usage_t usage, |
| 237 | psa_algorithm_t alg ) |
| 238 | { |
| 239 | unsigned char nonce[16] = {0}; |
| 240 | size_t nonce_length = sizeof( nonce ); |
| 241 | unsigned char plaintext[16] = "Hello, world..."; |
| 242 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 243 | size_t ciphertext_length = sizeof( ciphertext ); |
| 244 | size_t plaintext_length = sizeof( ciphertext ); |
| 245 | |
| 246 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 247 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 248 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 249 | nonce, nonce_length, |
| 250 | NULL, 0, |
| 251 | plaintext, sizeof( plaintext ), |
| 252 | ciphertext, sizeof( ciphertext ), |
| 253 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 257 | { |
| 258 | psa_status_t verify_status = |
| 259 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 260 | PSA_SUCCESS : |
| 261 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 262 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 263 | nonce, nonce_length, |
| 264 | NULL, 0, |
| 265 | ciphertext, ciphertext_length, |
| 266 | plaintext, sizeof( plaintext ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 267 | &plaintext_length ), |
| 268 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return( 1 ); |
| 272 | |
| 273 | exit: |
| 274 | return( 0 ); |
| 275 | } |
| 276 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 277 | static int exercise_signature_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 278 | psa_key_usage_t usage, |
| 279 | psa_algorithm_t alg ) |
| 280 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 281 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 282 | size_t payload_length = 16; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 283 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 284 | size_t signature_length = sizeof( signature ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 285 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 286 | |
| 287 | /* If the policy allows signing with any hash, just pick one. */ |
| 288 | if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH ) |
| 289 | { |
| 290 | #if defined(MBEDTLS_MD2_C) |
| 291 | hash_alg = PSA_ALG_MD2; |
| 292 | #elif defined(MBEDTLS_MD4_C) |
| 293 | hash_alg = PSA_ALG_MD4; |
| 294 | #elif defined(MBEDTLS_MD5_C) |
| 295 | hash_alg = PSA_ALG_MD5; |
Gilles Peskine | 6b156df | 2019-01-28 15:43:19 +0100 | [diff] [blame] | 296 | /* MBEDTLS_RIPEMD160_C omitted because Mbed TLS doesn't |
| 297 | * support it in RSA PKCS#1v1.5 signatures. */ |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 298 | #elif defined(MBEDTLS_SHA1_C) |
| 299 | hash_alg = PSA_ALG_SHA_1; |
| 300 | #elif defined(MBEDTLS_SHA256_C) |
| 301 | hash_alg = PSA_ALG_SHA_256; |
| 302 | #elif defined(MBEDTLS_SHA512_C) |
| 303 | hash_alg = PSA_ALG_SHA_384; |
| 304 | #elif defined(MBEDTLS_SHA3_C) |
| 305 | hash_alg = PSA_ALG_SHA3_256; |
| 306 | #else |
| 307 | test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ ); |
| 308 | #endif |
| 309 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
| 310 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 311 | |
| 312 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 313 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 314 | /* Some algorithms require the payload to have the size of |
| 315 | * the hash encoded in the algorithm. Use this input size |
| 316 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 317 | if( hash_alg != 0 ) |
| 318 | payload_length = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 319 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 320 | payload, payload_length, |
| 321 | signature, sizeof( signature ), |
| 322 | &signature_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 326 | { |
| 327 | psa_status_t verify_status = |
| 328 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 329 | PSA_SUCCESS : |
| 330 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 331 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 332 | payload, payload_length, |
| 333 | signature, signature_length ), |
| 334 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | return( 1 ); |
| 338 | |
| 339 | exit: |
| 340 | return( 0 ); |
| 341 | } |
| 342 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 343 | static int exercise_asymmetric_encryption_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 344 | psa_key_usage_t usage, |
| 345 | psa_algorithm_t alg ) |
| 346 | { |
| 347 | unsigned char plaintext[256] = "Hello, world..."; |
| 348 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 349 | size_t ciphertext_length = sizeof( ciphertext ); |
| 350 | size_t plaintext_length = 16; |
| 351 | |
| 352 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 353 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 354 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 355 | plaintext, plaintext_length, |
| 356 | NULL, 0, |
| 357 | ciphertext, sizeof( ciphertext ), |
| 358 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 362 | { |
| 363 | psa_status_t status = |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 364 | psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 365 | ciphertext, ciphertext_length, |
| 366 | NULL, 0, |
| 367 | plaintext, sizeof( plaintext ), |
| 368 | &plaintext_length ); |
| 369 | TEST_ASSERT( status == PSA_SUCCESS || |
| 370 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 371 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 372 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 373 | } |
| 374 | |
| 375 | return( 1 ); |
| 376 | |
| 377 | exit: |
| 378 | return( 0 ); |
| 379 | } |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 380 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 381 | static int exercise_key_derivation_key( psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 382 | psa_key_usage_t usage, |
| 383 | psa_algorithm_t alg ) |
| 384 | { |
| 385 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 386 | unsigned char label[16] = "This is a label."; |
| 387 | size_t label_length = sizeof( label ); |
| 388 | unsigned char seed[16] = "abcdefghijklmnop"; |
| 389 | size_t seed_length = sizeof( seed ); |
| 390 | unsigned char output[1]; |
| 391 | |
| 392 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 393 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 394 | PSA_ASSERT( psa_key_derivation( &generator, |
| 395 | handle, alg, |
| 396 | label, label_length, |
| 397 | seed, seed_length, |
| 398 | sizeof( output ) ) ); |
| 399 | PSA_ASSERT( psa_generator_read( &generator, |
| 400 | output, |
| 401 | sizeof( output ) ) ); |
| 402 | PSA_ASSERT( psa_generator_abort( &generator ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | return( 1 ); |
| 406 | |
| 407 | exit: |
| 408 | return( 0 ); |
| 409 | } |
| 410 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 411 | /* We need two keys to exercise key agreement. Exercise the |
| 412 | * private key against its own public key. */ |
| 413 | static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 414 | psa_key_handle_t handle, |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 415 | psa_algorithm_t alg ) |
| 416 | { |
| 417 | psa_key_type_t private_key_type; |
| 418 | psa_key_type_t public_key_type; |
| 419 | size_t key_bits; |
| 420 | uint8_t *public_key = NULL; |
| 421 | size_t public_key_length; |
| 422 | /* Return UNKNOWN_ERROR if something other than the final call to |
| 423 | * psa_key_agreement fails. This isn't fully satisfactory, but it's |
| 424 | * good enough: callers will report it as a failed test anyway. */ |
| 425 | psa_status_t status = PSA_ERROR_UNKNOWN_ERROR; |
| 426 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 427 | PSA_ASSERT( psa_get_key_information( handle, |
| 428 | &private_key_type, |
| 429 | &key_bits ) ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 430 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type ); |
| 431 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 432 | ASSERT_ALLOC( public_key, public_key_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 433 | PSA_ASSERT( psa_export_public_key( handle, |
| 434 | public_key, public_key_length, |
| 435 | &public_key_length ) ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 436 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 437 | status = psa_key_agreement( generator, handle, |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 438 | public_key, public_key_length, |
| 439 | alg ); |
| 440 | exit: |
| 441 | mbedtls_free( public_key ); |
| 442 | return( status ); |
| 443 | } |
| 444 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 445 | static int exercise_key_agreement_key( psa_key_handle_t handle, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 446 | psa_key_usage_t usage, |
| 447 | psa_algorithm_t alg ) |
| 448 | { |
| 449 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 450 | unsigned char output[1]; |
| 451 | int ok = 0; |
| 452 | |
| 453 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 454 | { |
| 455 | /* We need two keys to exercise key agreement. Exercise the |
| 456 | * private key against its own public key. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 457 | PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) ); |
| 458 | PSA_ASSERT( psa_generator_read( &generator, |
| 459 | output, |
| 460 | sizeof( output ) ) ); |
| 461 | PSA_ASSERT( psa_generator_abort( &generator ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 462 | } |
| 463 | ok = 1; |
| 464 | |
| 465 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 466 | return( ok ); |
| 467 | } |
| 468 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 469 | static int is_oid_of_key_type( psa_key_type_t type, |
| 470 | const uint8_t *oid, size_t oid_length ) |
| 471 | { |
Gilles Peskine | ae3d2a2 | 2018-08-13 14:14:22 +0200 | [diff] [blame] | 472 | const uint8_t *expected_oid = NULL; |
| 473 | size_t expected_oid_length = 0; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 474 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | ae3d2a2 | 2018-08-13 14:14:22 +0200 | [diff] [blame] | 475 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
| 476 | { |
| 477 | expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA; |
| 478 | expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1; |
| 479 | } |
| 480 | else |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 481 | #endif /* MBEDTLS_RSA_C */ |
| 482 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | ae3d2a2 | 2018-08-13 14:14:22 +0200 | [diff] [blame] | 483 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 484 | { |
| 485 | expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED; |
| 486 | expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1; |
| 487 | } |
| 488 | else |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 489 | #endif /* MBEDTLS_ECP_C */ |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 490 | { |
| 491 | char message[40]; |
| 492 | mbedtls_snprintf( message, sizeof( message ), |
| 493 | "OID not known for key type=0x%08lx", |
| 494 | (unsigned long) type ); |
| 495 | test_fail( message, __LINE__, __FILE__ ); |
| 496 | return( 0 ); |
| 497 | } |
| 498 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 499 | ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 500 | return( 1 ); |
| 501 | |
| 502 | exit: |
| 503 | return( 0 ); |
| 504 | } |
| 505 | |
| 506 | static int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 507 | size_t min_bits, size_t max_bits, |
| 508 | int must_be_odd ) |
| 509 | { |
| 510 | size_t len; |
| 511 | size_t actual_bits; |
| 512 | unsigned char msb; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 513 | TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 514 | MBEDTLS_ASN1_INTEGER ), |
| 515 | 0 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 516 | /* Tolerate a slight departure from DER encoding: |
| 517 | * - 0 may be represented by an empty string or a 1-byte string. |
| 518 | * - The sign bit may be used as a value bit. */ |
| 519 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 520 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 521 | { |
| 522 | ++( *p ); |
| 523 | --len; |
| 524 | } |
| 525 | if( min_bits == 0 && len == 0 ) |
| 526 | return( 1 ); |
| 527 | msb = ( *p )[0]; |
| 528 | TEST_ASSERT( msb != 0 ); |
| 529 | actual_bits = 8 * ( len - 1 ); |
| 530 | while( msb != 0 ) |
| 531 | { |
| 532 | msb >>= 1; |
| 533 | ++actual_bits; |
| 534 | } |
| 535 | TEST_ASSERT( actual_bits >= min_bits ); |
| 536 | TEST_ASSERT( actual_bits <= max_bits ); |
| 537 | if( must_be_odd ) |
| 538 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 539 | *p += len; |
| 540 | return( 1 ); |
| 541 | exit: |
| 542 | return( 0 ); |
| 543 | } |
| 544 | |
| 545 | static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end, |
| 546 | size_t *len, |
| 547 | unsigned char n, unsigned char tag ) |
| 548 | { |
| 549 | int ret; |
| 550 | ret = mbedtls_asn1_get_tag( p, end, len, |
| 551 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 552 | MBEDTLS_ASN1_CONSTRUCTED | ( n ) ); |
| 553 | if( ret != 0 ) |
| 554 | return( ret ); |
| 555 | end = *p + *len; |
| 556 | ret = mbedtls_asn1_get_tag( p, end, len, tag ); |
| 557 | if( ret != 0 ) |
| 558 | return( ret ); |
| 559 | if( *p + *len != end ) |
| 560 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
| 561 | return( 0 ); |
| 562 | } |
| 563 | |
| 564 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 565 | uint8_t *exported, size_t exported_length ) |
| 566 | { |
| 567 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 568 | TEST_EQUAL( exported_length, ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 569 | else |
| 570 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 571 | |
| 572 | #if defined(MBEDTLS_DES_C) |
| 573 | if( type == PSA_KEY_TYPE_DES ) |
| 574 | { |
| 575 | /* Check the parity bits. */ |
| 576 | unsigned i; |
| 577 | for( i = 0; i < bits / 8; i++ ) |
| 578 | { |
| 579 | unsigned bit_count = 0; |
| 580 | unsigned m; |
| 581 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 582 | { |
| 583 | if( exported[i] & m ) |
| 584 | ++bit_count; |
| 585 | } |
| 586 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 587 | } |
| 588 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 589 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 590 | #endif |
| 591 | |
| 592 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
| 593 | if( type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 594 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 595 | uint8_t *p = exported; |
| 596 | uint8_t *end = exported + exported_length; |
| 597 | size_t len; |
| 598 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 599 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 600 | * modulus INTEGER, -- n |
| 601 | * publicExponent INTEGER, -- e |
| 602 | * privateExponent INTEGER, -- d |
| 603 | * prime1 INTEGER, -- p |
| 604 | * prime2 INTEGER, -- q |
| 605 | * exponent1 INTEGER, -- d mod (p-1) |
| 606 | * exponent2 INTEGER, -- d mod (q-1) |
| 607 | * coefficient INTEGER, -- (inverse of q) mod p |
| 608 | * } |
| 609 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 610 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 611 | MBEDTLS_ASN1_SEQUENCE | |
| 612 | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); |
| 613 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 614 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 615 | goto exit; |
| 616 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 617 | goto exit; |
| 618 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 619 | goto exit; |
| 620 | /* Require d to be at least half the size of n. */ |
| 621 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 622 | goto exit; |
| 623 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 624 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 625 | goto exit; |
| 626 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 627 | goto exit; |
| 628 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 629 | goto exit; |
| 630 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 631 | goto exit; |
| 632 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 633 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 634 | TEST_EQUAL( p, end ); |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 635 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 636 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 637 | #endif /* MBEDTLS_RSA_C */ |
| 638 | |
| 639 | #if defined(MBEDTLS_ECP_C) |
| 640 | if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) ) |
| 641 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 642 | /* Just the secret value */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 643 | TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 644 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 645 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 646 | #endif /* MBEDTLS_ECP_C */ |
| 647 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 648 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 649 | { |
| 650 | uint8_t *p = exported; |
| 651 | uint8_t *end = exported + exported_length; |
| 652 | size_t len; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 653 | #if defined(MBEDTLS_RSA_C) |
| 654 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 655 | { |
| 656 | /* RSAPublicKey ::= SEQUENCE { |
| 657 | * modulus INTEGER, -- n |
| 658 | * publicExponent INTEGER } -- e |
| 659 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 660 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 661 | MBEDTLS_ASN1_SEQUENCE | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 662 | MBEDTLS_ASN1_CONSTRUCTED ), |
| 663 | 0 ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 664 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 665 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 666 | goto exit; |
| 667 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 668 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 669 | TEST_EQUAL( p, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 670 | } |
| 671 | else |
| 672 | #endif /* MBEDTLS_RSA_C */ |
| 673 | #if defined(MBEDTLS_ECP_C) |
| 674 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 675 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 676 | /* The representation of an ECC public key is: |
| 677 | * - The byte 0x04; |
| 678 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 679 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 680 | * - where m is the bit size associated with the curve. |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 681 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 682 | TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end ); |
| 683 | TEST_EQUAL( p[0], 4 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 684 | } |
| 685 | else |
| 686 | #endif /* MBEDTLS_ECP_C */ |
| 687 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 688 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 689 | mbedtls_snprintf( message, sizeof( message ), |
| 690 | "No sanity check for public key type=0x%08lx", |
| 691 | (unsigned long) type ); |
| 692 | test_fail( message, __LINE__, __FILE__ ); |
| 693 | return( 0 ); |
| 694 | } |
| 695 | } |
| 696 | else |
| 697 | |
| 698 | { |
| 699 | /* No sanity checks for other types */ |
| 700 | } |
| 701 | |
| 702 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 703 | |
| 704 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 705 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 706 | } |
| 707 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 708 | static int exercise_export_key( psa_key_handle_t handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 709 | psa_key_usage_t usage ) |
| 710 | { |
| 711 | psa_key_type_t type; |
| 712 | size_t bits; |
| 713 | uint8_t *exported = NULL; |
| 714 | size_t exported_size = 0; |
| 715 | size_t exported_length = 0; |
| 716 | int ok = 0; |
| 717 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 718 | PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) ); |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 719 | |
| 720 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
| 721 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 722 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 723 | TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ), |
| 724 | PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 725 | return( 1 ); |
| 726 | } |
| 727 | |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 728 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 729 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 730 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 731 | PSA_ASSERT( psa_export_key( handle, |
| 732 | exported, exported_size, |
| 733 | &exported_length ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 734 | ok = exported_key_sanity_check( type, bits, exported, exported_length ); |
| 735 | |
| 736 | exit: |
| 737 | mbedtls_free( exported ); |
| 738 | return( ok ); |
| 739 | } |
| 740 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 741 | static int exercise_export_public_key( psa_key_handle_t handle ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 742 | { |
| 743 | psa_key_type_t type; |
| 744 | psa_key_type_t public_type; |
| 745 | size_t bits; |
| 746 | uint8_t *exported = NULL; |
| 747 | size_t exported_size = 0; |
| 748 | size_t exported_length = 0; |
| 749 | int ok = 0; |
| 750 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 751 | PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 752 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
| 753 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 754 | TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 755 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 756 | return( 1 ); |
| 757 | } |
| 758 | |
| 759 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type ); |
| 760 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 761 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 762 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 763 | PSA_ASSERT( psa_export_public_key( handle, |
| 764 | exported, exported_size, |
| 765 | &exported_length ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 766 | ok = exported_key_sanity_check( public_type, bits, |
| 767 | exported, exported_length ); |
| 768 | |
| 769 | exit: |
| 770 | mbedtls_free( exported ); |
| 771 | return( ok ); |
| 772 | } |
| 773 | |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 774 | /** Do smoke tests on a key. |
| 775 | * |
| 776 | * Perform one of each operation indicated by \p alg (decrypt/encrypt, |
| 777 | * sign/verify, or derivation) that is permitted according to \p usage. |
| 778 | * \p usage and \p alg should correspond to the expected policy on the |
| 779 | * key. |
| 780 | * |
| 781 | * Export the key if permitted by \p usage, and check that the output |
| 782 | * looks sensible. If \p usage forbids export, check that |
| 783 | * \p psa_export_key correctly rejects the attempt. If the key is |
| 784 | * asymmetric, also check \p psa_export_public_key. |
| 785 | * |
| 786 | * If the key fails the tests, this function calls the test framework's |
| 787 | * `test_fail` function and returns false. Otherwise this function returns |
| 788 | * true. Therefore it should be used as follows: |
| 789 | * ``` |
| 790 | * if( ! exercise_key( ... ) ) goto exit; |
| 791 | * ``` |
| 792 | * |
| 793 | * \param handle The key to exercise. It should be capable of performing |
| 794 | * \p alg. |
| 795 | * \param usage The usage flags to assume. |
| 796 | * \param alg The algorithm to exercise. |
| 797 | * |
| 798 | * \retval 0 The key failed the smoke tests. |
| 799 | * \retval 1 The key passed the smoke tests. |
| 800 | */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 801 | static int exercise_key( psa_key_handle_t handle, |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 802 | psa_key_usage_t usage, |
| 803 | psa_algorithm_t alg ) |
| 804 | { |
| 805 | int ok; |
| 806 | if( alg == 0 ) |
| 807 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 808 | else if( PSA_ALG_IS_MAC( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 809 | ok = exercise_mac_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 810 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 811 | ok = exercise_cipher_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 812 | else if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 813 | ok = exercise_aead_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 814 | else if( PSA_ALG_IS_SIGN( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 815 | ok = exercise_signature_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 816 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 817 | ok = exercise_asymmetric_encryption_key( handle, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 818 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 819 | ok = exercise_key_derivation_key( handle, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 820 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 821 | ok = exercise_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 822 | else |
| 823 | { |
| 824 | char message[40]; |
| 825 | mbedtls_snprintf( message, sizeof( message ), |
| 826 | "No code to exercise alg=0x%08lx", |
| 827 | (unsigned long) alg ); |
| 828 | test_fail( message, __LINE__, __FILE__ ); |
| 829 | ok = 0; |
| 830 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 831 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 832 | ok = ok && exercise_export_key( handle, usage ); |
| 833 | ok = ok && exercise_export_public_key( handle ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 834 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 835 | return( ok ); |
| 836 | } |
| 837 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 838 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 839 | psa_algorithm_t alg ) |
| 840 | { |
| 841 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 842 | { |
| 843 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 844 | PSA_KEY_USAGE_VERIFY : |
| 845 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 846 | } |
| 847 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 848 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 849 | { |
| 850 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 851 | PSA_KEY_USAGE_ENCRYPT : |
| 852 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 853 | } |
| 854 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 855 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 856 | { |
| 857 | return( PSA_KEY_USAGE_DERIVE ); |
| 858 | } |
| 859 | else |
| 860 | { |
| 861 | return( 0 ); |
| 862 | } |
| 863 | |
| 864 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 865 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 866 | /* An overapproximation of the amount of storage needed for a key of the |
| 867 | * given type and with the given content. The API doesn't make it easy |
| 868 | * to find a good value for the size. The current implementation doesn't |
| 869 | * care about the value anyway. */ |
| 870 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 871 | ( data )->len |
| 872 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 873 | typedef enum { |
| 874 | IMPORT_KEY = 0, |
| 875 | GENERATE_KEY = 1, |
| 876 | DERIVE_KEY = 2 |
| 877 | } generate_method; |
| 878 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 879 | /* END_HEADER */ |
| 880 | |
| 881 | /* BEGIN_DEPENDENCIES |
| 882 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 883 | * END_DEPENDENCIES |
| 884 | */ |
| 885 | |
| 886 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 887 | void static_checks( ) |
| 888 | { |
| 889 | size_t max_truncated_mac_size = |
| 890 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 891 | |
| 892 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 893 | * encoding. The shifted mask is the maximum truncated value. The |
| 894 | * untruncated algorithm may be one byte larger. */ |
| 895 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 896 | } |
| 897 | /* END_CASE */ |
| 898 | |
| 899 | /* BEGIN_CASE */ |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 900 | void import( data_t *data, int type, int expected_status_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 901 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 902 | psa_key_handle_t handle = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 903 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 904 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 905 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 906 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 907 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 908 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 909 | status = psa_import_key( handle, type, data->x, data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 910 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 911 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 912 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 913 | |
| 914 | exit: |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 915 | mbedtls_psa_crypto_free( ); |
| 916 | } |
| 917 | /* END_CASE */ |
| 918 | |
| 919 | /* BEGIN_CASE */ |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 920 | void import_twice( int alg_arg, int usage_arg, |
| 921 | int type1_arg, data_t *data1, |
| 922 | int expected_import1_status_arg, |
| 923 | int type2_arg, data_t *data2, |
| 924 | int expected_import2_status_arg ) |
| 925 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 926 | psa_key_handle_t handle = 0; |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 927 | psa_algorithm_t alg = alg_arg; |
| 928 | psa_key_usage_t usage = usage_arg; |
| 929 | psa_key_type_t type1 = type1_arg; |
| 930 | psa_status_t expected_import1_status = expected_import1_status_arg; |
| 931 | psa_key_type_t type2 = type2_arg; |
| 932 | psa_status_t expected_import2_status = expected_import2_status_arg; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 933 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 934 | psa_status_t status; |
| 935 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 936 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 937 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 938 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 939 | psa_key_policy_set_usage( &policy, usage, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 940 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 941 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 942 | status = psa_import_key( handle, type1, data1->x, data1->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 943 | TEST_EQUAL( status, expected_import1_status ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 944 | status = psa_import_key( handle, type2, data2->x, data2->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 945 | TEST_EQUAL( status, expected_import2_status ); |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 946 | |
| 947 | if( expected_import1_status == PSA_SUCCESS || |
| 948 | expected_import2_status == PSA_SUCCESS ) |
| 949 | { |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 950 | if( ! exercise_key( handle, usage, alg ) ) |
| 951 | goto exit; |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | exit: |
| 955 | mbedtls_psa_crypto_free( ); |
| 956 | } |
| 957 | /* END_CASE */ |
| 958 | |
| 959 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 960 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 961 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 962 | psa_key_handle_t handle = 0; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 963 | size_t bits = bits_arg; |
| 964 | psa_status_t expected_status = expected_status_arg; |
| 965 | psa_status_t status; |
| 966 | psa_key_type_t type = |
| 967 | keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY; |
| 968 | size_t buffer_size = /* Slight overapproximations */ |
| 969 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 970 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 971 | unsigned char *p; |
| 972 | int ret; |
| 973 | size_t length; |
| 974 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 975 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 976 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 977 | |
| 978 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 979 | bits, keypair ) ) >= 0 ); |
| 980 | length = ret; |
| 981 | |
| 982 | /* Try importing the key */ |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 983 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 984 | status = psa_import_key( handle, type, p, length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 985 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 986 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 987 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 988 | |
| 989 | exit: |
| 990 | mbedtls_free( buffer ); |
| 991 | mbedtls_psa_crypto_free( ); |
| 992 | } |
| 993 | /* END_CASE */ |
| 994 | |
| 995 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 996 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 997 | int type_arg, |
| 998 | int alg_arg, |
| 999 | int usage_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1000 | int expected_bits, |
| 1001 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1002 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1003 | int canonical_input ) |
| 1004 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1005 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1006 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1007 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1008 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1009 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1010 | unsigned char *exported = NULL; |
| 1011 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1012 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1013 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1014 | size_t reexported_length; |
| 1015 | psa_key_type_t got_type; |
| 1016 | size_t got_bits; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1017 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1018 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1019 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1020 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1021 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1022 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1023 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1024 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1025 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1026 | psa_key_policy_set_usage( &policy, usage_arg, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1027 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1028 | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1029 | TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ), |
| 1030 | PSA_ERROR_EMPTY_SLOT ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1031 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1032 | /* Import the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1033 | PSA_ASSERT( psa_import_key( handle, type, |
| 1034 | data->x, data->len ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1035 | |
| 1036 | /* Test the key information */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1037 | PSA_ASSERT( psa_get_key_information( handle, |
| 1038 | &got_type, |
| 1039 | &got_bits ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1040 | TEST_EQUAL( got_type, type ); |
| 1041 | TEST_EQUAL( got_bits, (size_t) expected_bits ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1042 | |
| 1043 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1044 | status = psa_export_key( handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1045 | exported, export_size, |
| 1046 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1047 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1048 | |
| 1049 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1050 | * and export_size. On errors, the exported length must be 0. */ |
| 1051 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1052 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1053 | TEST_ASSERT( exported_length <= export_size ); |
| 1054 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1055 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1056 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1057 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1058 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1059 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1060 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1061 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1062 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1063 | if( ! exercise_export_key( handle, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1064 | goto exit; |
| 1065 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1066 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1067 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1068 | else |
| 1069 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1070 | psa_key_handle_t handle2; |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1071 | PSA_ASSERT( psa_allocate_key( &handle2 ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1072 | PSA_ASSERT( psa_set_key_policy( handle2, &policy ) ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1073 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1074 | PSA_ASSERT( psa_import_key( handle2, type, |
| 1075 | exported, |
| 1076 | exported_length ) ); |
| 1077 | PSA_ASSERT( psa_export_key( handle2, |
| 1078 | reexported, |
| 1079 | export_size, |
| 1080 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1081 | ASSERT_COMPARE( exported, exported_length, |
| 1082 | reexported, reexported_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1083 | PSA_ASSERT( psa_close_key( handle2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1084 | } |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1085 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1086 | |
| 1087 | destroy: |
| 1088 | /* Destroy the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1089 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1090 | TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ), |
| 1091 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1092 | |
| 1093 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1094 | mbedtls_free( exported ); |
| 1095 | mbedtls_free( reexported ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1096 | mbedtls_psa_crypto_free( ); |
| 1097 | } |
| 1098 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1099 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1100 | /* BEGIN_CASE */ |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1101 | void import_key_nonempty_slot( ) |
| 1102 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1103 | psa_key_handle_t handle = 0; |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1104 | psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA; |
| 1105 | psa_status_t status; |
| 1106 | const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 }; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1107 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1108 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1109 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1110 | |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1111 | /* Import the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1112 | PSA_ASSERT( psa_import_key( handle, type, |
| 1113 | data, sizeof( data ) ) ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1114 | |
| 1115 | /* Import the key again */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1116 | status = psa_import_key( handle, type, data, sizeof( data ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1117 | TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1118 | |
| 1119 | exit: |
| 1120 | mbedtls_psa_crypto_free( ); |
| 1121 | } |
| 1122 | /* END_CASE */ |
| 1123 | |
| 1124 | /* BEGIN_CASE */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1125 | void export_invalid_handle( int handle, int expected_export_status_arg ) |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1126 | { |
| 1127 | psa_status_t status; |
| 1128 | unsigned char *exported = NULL; |
| 1129 | size_t export_size = 0; |
| 1130 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1131 | psa_status_t expected_export_status = expected_export_status_arg; |
| 1132 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1133 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1134 | |
| 1135 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1136 | status = psa_export_key( (psa_key_handle_t) handle, |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1137 | exported, export_size, |
| 1138 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1139 | TEST_EQUAL( status, expected_export_status ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1140 | |
| 1141 | exit: |
| 1142 | mbedtls_psa_crypto_free( ); |
| 1143 | } |
| 1144 | /* END_CASE */ |
| 1145 | |
| 1146 | /* BEGIN_CASE */ |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1147 | void export_with_no_key_activity( ) |
| 1148 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1149 | psa_key_handle_t handle = 0; |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1150 | psa_algorithm_t alg = PSA_ALG_CTR; |
| 1151 | psa_status_t status; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1152 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1153 | unsigned char *exported = NULL; |
| 1154 | size_t export_size = 0; |
| 1155 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1156 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1157 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1158 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1159 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1160 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1161 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1162 | |
| 1163 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1164 | status = psa_export_key( handle, |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1165 | exported, export_size, |
| 1166 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1167 | TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1168 | |
| 1169 | exit: |
| 1170 | mbedtls_psa_crypto_free( ); |
| 1171 | } |
| 1172 | /* END_CASE */ |
| 1173 | |
| 1174 | /* BEGIN_CASE */ |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1175 | void cipher_with_no_key_activity( ) |
| 1176 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1177 | psa_key_handle_t handle = 0; |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1178 | psa_status_t status; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1179 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1180 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1181 | int exercise_alg = PSA_ALG_CTR; |
| 1182 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1183 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1184 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1185 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1186 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1187 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1188 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1189 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1190 | TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1191 | |
| 1192 | exit: |
| 1193 | psa_cipher_abort( &operation ); |
| 1194 | mbedtls_psa_crypto_free( ); |
| 1195 | } |
| 1196 | /* END_CASE */ |
| 1197 | |
| 1198 | /* BEGIN_CASE */ |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1199 | void export_after_import_failure( data_t *data, int type_arg, |
| 1200 | int expected_import_status_arg ) |
| 1201 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1202 | psa_key_handle_t handle = 0; |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1203 | psa_key_type_t type = type_arg; |
| 1204 | psa_status_t status; |
| 1205 | unsigned char *exported = NULL; |
| 1206 | size_t export_size = 0; |
| 1207 | psa_status_t expected_import_status = expected_import_status_arg; |
| 1208 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1209 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1210 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1211 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1212 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1213 | |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1214 | /* Import the key - expect failure */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1215 | status = psa_import_key( handle, type, |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 1216 | data->x, data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1217 | TEST_EQUAL( status, expected_import_status ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1218 | |
| 1219 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1220 | status = psa_export_key( handle, |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1221 | exported, export_size, |
| 1222 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1223 | TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1224 | |
| 1225 | exit: |
| 1226 | mbedtls_psa_crypto_free( ); |
| 1227 | } |
| 1228 | /* END_CASE */ |
| 1229 | |
| 1230 | /* BEGIN_CASE */ |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1231 | void cipher_after_import_failure( data_t *data, int type_arg, |
| 1232 | int expected_import_status_arg ) |
| 1233 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1234 | psa_key_handle_t handle = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1235 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1236 | psa_key_type_t type = type_arg; |
| 1237 | psa_status_t status; |
| 1238 | psa_status_t expected_import_status = expected_import_status_arg; |
| 1239 | int exercise_alg = PSA_ALG_CTR; |
| 1240 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1241 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1242 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1243 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1244 | |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1245 | /* Import the key - expect failure */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1246 | status = psa_import_key( handle, type, |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 1247 | data->x, data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1248 | TEST_EQUAL( status, expected_import_status ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1249 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1250 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1251 | TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1252 | |
| 1253 | exit: |
| 1254 | psa_cipher_abort( &operation ); |
| 1255 | mbedtls_psa_crypto_free( ); |
| 1256 | } |
| 1257 | /* END_CASE */ |
| 1258 | |
| 1259 | /* BEGIN_CASE */ |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1260 | void export_after_destroy_key( data_t *data, int type_arg ) |
| 1261 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1262 | psa_key_handle_t handle = 0; |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1263 | psa_key_type_t type = type_arg; |
| 1264 | psa_status_t status; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1265 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1266 | psa_algorithm_t alg = PSA_ALG_CTR; |
| 1267 | unsigned char *exported = NULL; |
| 1268 | size_t export_size = 0; |
| 1269 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1270 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1271 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1272 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1273 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1274 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1275 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1276 | export_size = (ptrdiff_t) data->len; |
| 1277 | ASSERT_ALLOC( exported, export_size ); |
| 1278 | |
| 1279 | /* Import the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1280 | PSA_ASSERT( psa_import_key( handle, type, |
| 1281 | data->x, data->len ) ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1282 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1283 | PSA_ASSERT( psa_export_key( handle, exported, export_size, |
| 1284 | &exported_length ) ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1285 | |
| 1286 | /* Destroy the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1287 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1288 | |
| 1289 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1290 | status = psa_export_key( handle, exported, export_size, |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1291 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1292 | TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1293 | |
| 1294 | exit: |
| 1295 | mbedtls_free( exported ); |
| 1296 | mbedtls_psa_crypto_free( ); |
| 1297 | } |
| 1298 | /* END_CASE */ |
| 1299 | |
| 1300 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1301 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1302 | int type_arg, |
| 1303 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1304 | int export_size_delta, |
| 1305 | int expected_export_status_arg, |
| 1306 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1307 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1308 | psa_key_handle_t handle = 0; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1309 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1310 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1311 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1312 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1313 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1314 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1315 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1316 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1317 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1318 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1319 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1320 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1321 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1322 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1323 | |
| 1324 | /* Import the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1325 | PSA_ASSERT( psa_import_key( handle, type, |
| 1326 | data->x, data->len ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1327 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1328 | /* Export the public key */ |
| 1329 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1330 | status = psa_export_public_key( handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1331 | exported, export_size, |
| 1332 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1333 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1334 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1335 | { |
| 1336 | psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type ); |
| 1337 | size_t bits; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1338 | PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1339 | TEST_ASSERT( expected_public_key->len <= |
| 1340 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1341 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1342 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1343 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1344 | |
| 1345 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1346 | mbedtls_free( exported ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1347 | psa_destroy_key( handle ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1348 | mbedtls_psa_crypto_free( ); |
| 1349 | } |
| 1350 | /* END_CASE */ |
| 1351 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1352 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1353 | void import_and_exercise_key( data_t *data, |
| 1354 | int type_arg, |
| 1355 | int bits_arg, |
| 1356 | int alg_arg ) |
| 1357 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1358 | psa_key_handle_t handle = 0; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1359 | psa_key_type_t type = type_arg; |
| 1360 | size_t bits = bits_arg; |
| 1361 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1362 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1363 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1364 | psa_key_type_t got_type; |
| 1365 | size_t got_bits; |
| 1366 | psa_status_t status; |
| 1367 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1368 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1369 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1370 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1371 | psa_key_policy_set_usage( &policy, usage, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1372 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1373 | |
| 1374 | /* Import the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1375 | status = psa_import_key( handle, type, data->x, data->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1376 | PSA_ASSERT( status ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1377 | |
| 1378 | /* Test the key information */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1379 | PSA_ASSERT( psa_get_key_information( handle, |
| 1380 | &got_type, |
| 1381 | &got_bits ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1382 | TEST_EQUAL( got_type, type ); |
| 1383 | TEST_EQUAL( got_bits, bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1384 | |
| 1385 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1386 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1387 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1388 | |
| 1389 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1390 | psa_destroy_key( handle ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1391 | mbedtls_psa_crypto_free( ); |
| 1392 | } |
| 1393 | /* END_CASE */ |
| 1394 | |
| 1395 | /* BEGIN_CASE */ |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1396 | void key_policy( int usage_arg, int alg_arg ) |
| 1397 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1398 | psa_key_handle_t handle = 0; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1399 | psa_algorithm_t alg = alg_arg; |
| 1400 | psa_key_usage_t usage = usage_arg; |
| 1401 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 1402 | unsigned char key[32] = {0}; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1403 | psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT; |
| 1404 | psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1405 | |
| 1406 | memset( key, 0x2a, sizeof( key ) ); |
| 1407 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1408 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1409 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1410 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1411 | psa_key_policy_set_usage( &policy_set, usage, alg ); |
| 1412 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1413 | TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage ); |
| 1414 | TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1415 | PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1416 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1417 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 1418 | key, sizeof( key ) ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1419 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1420 | PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1421 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1422 | TEST_EQUAL( policy_get.usage, policy_set.usage ); |
| 1423 | TEST_EQUAL( policy_get.alg, policy_set.alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1424 | |
| 1425 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1426 | psa_destroy_key( handle ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1427 | mbedtls_psa_crypto_free( ); |
| 1428 | } |
| 1429 | /* END_CASE */ |
| 1430 | |
| 1431 | /* BEGIN_CASE */ |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1432 | void key_policy_init( ) |
| 1433 | { |
| 1434 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1435 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1436 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1437 | * to supress the Clang warning for the test. */ |
| 1438 | psa_key_policy_t func = psa_key_policy_init( ); |
| 1439 | psa_key_policy_t init = PSA_KEY_POLICY_INIT; |
| 1440 | psa_key_policy_t zero; |
| 1441 | |
| 1442 | memset( &zero, 0, sizeof( zero ) ); |
| 1443 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1444 | /* A default key policy should not permit any usage. */ |
| 1445 | TEST_EQUAL( psa_key_policy_get_usage( &func ), 0 ); |
| 1446 | TEST_EQUAL( psa_key_policy_get_usage( &init ), 0 ); |
| 1447 | TEST_EQUAL( psa_key_policy_get_usage( &zero ), 0 ); |
| 1448 | |
| 1449 | /* A default key policy should not permit any algorithm. */ |
| 1450 | TEST_EQUAL( psa_key_policy_get_algorithm( &func ), 0 ); |
| 1451 | TEST_EQUAL( psa_key_policy_get_algorithm( &init ), 0 ); |
| 1452 | TEST_EQUAL( psa_key_policy_get_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1453 | } |
| 1454 | /* END_CASE */ |
| 1455 | |
| 1456 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1457 | void mac_key_policy( int policy_usage, |
| 1458 | int policy_alg, |
| 1459 | int key_type, |
| 1460 | data_t *key_data, |
| 1461 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1462 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1463 | psa_key_handle_t handle = 0; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1464 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1465 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1466 | psa_status_t status; |
| 1467 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1468 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1469 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1470 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1471 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1472 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1473 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1474 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1475 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 1476 | key_data->x, key_data->len ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1477 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1478 | status = psa_mac_sign_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1479 | if( policy_alg == exercise_alg && |
| 1480 | ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1481 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1482 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1483 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1484 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1485 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1486 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1487 | status = psa_mac_verify_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1488 | if( policy_alg == exercise_alg && |
| 1489 | ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1490 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1491 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1492 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1493 | |
| 1494 | exit: |
| 1495 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1496 | psa_destroy_key( handle ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1497 | mbedtls_psa_crypto_free( ); |
| 1498 | } |
| 1499 | /* END_CASE */ |
| 1500 | |
| 1501 | /* BEGIN_CASE */ |
| 1502 | void cipher_key_policy( int policy_usage, |
| 1503 | int policy_alg, |
| 1504 | int key_type, |
| 1505 | data_t *key_data, |
| 1506 | int exercise_alg ) |
| 1507 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1508 | psa_key_handle_t handle = 0; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1509 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1510 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1511 | psa_status_t status; |
| 1512 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1513 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1514 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1515 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1516 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1517 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1518 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1519 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 1520 | key_data->x, key_data->len ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1521 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1522 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1523 | if( policy_alg == exercise_alg && |
| 1524 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1525 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1526 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1527 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1528 | psa_cipher_abort( &operation ); |
| 1529 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1530 | status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1531 | if( policy_alg == exercise_alg && |
| 1532 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1533 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1534 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1535 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1536 | |
| 1537 | exit: |
| 1538 | psa_cipher_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1539 | psa_destroy_key( handle ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1540 | mbedtls_psa_crypto_free( ); |
| 1541 | } |
| 1542 | /* END_CASE */ |
| 1543 | |
| 1544 | /* BEGIN_CASE */ |
| 1545 | void aead_key_policy( int policy_usage, |
| 1546 | int policy_alg, |
| 1547 | int key_type, |
| 1548 | data_t *key_data, |
| 1549 | int nonce_length_arg, |
| 1550 | int tag_length_arg, |
| 1551 | int exercise_alg ) |
| 1552 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1553 | psa_key_handle_t handle = 0; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1554 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1555 | psa_status_t status; |
| 1556 | unsigned char nonce[16] = {0}; |
| 1557 | size_t nonce_length = nonce_length_arg; |
| 1558 | unsigned char tag[16]; |
| 1559 | size_t tag_length = tag_length_arg; |
| 1560 | size_t output_length; |
| 1561 | |
| 1562 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1563 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1564 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1565 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1566 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1567 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1568 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1569 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1570 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1571 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 1572 | key_data->x, key_data->len ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1573 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1574 | status = psa_aead_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1575 | nonce, nonce_length, |
| 1576 | NULL, 0, |
| 1577 | NULL, 0, |
| 1578 | tag, tag_length, |
| 1579 | &output_length ); |
| 1580 | if( policy_alg == exercise_alg && |
| 1581 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1582 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1583 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1584 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1585 | |
| 1586 | memset( tag, 0, sizeof( tag ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1587 | status = psa_aead_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1588 | nonce, nonce_length, |
| 1589 | NULL, 0, |
| 1590 | tag, tag_length, |
| 1591 | NULL, 0, |
| 1592 | &output_length ); |
| 1593 | if( policy_alg == exercise_alg && |
| 1594 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1595 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1596 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1597 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1598 | |
| 1599 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1600 | psa_destroy_key( handle ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1601 | mbedtls_psa_crypto_free( ); |
| 1602 | } |
| 1603 | /* END_CASE */ |
| 1604 | |
| 1605 | /* BEGIN_CASE */ |
| 1606 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1607 | int policy_alg, |
| 1608 | int key_type, |
| 1609 | data_t *key_data, |
| 1610 | int exercise_alg ) |
| 1611 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1612 | psa_key_handle_t handle = 0; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1613 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1614 | psa_status_t status; |
| 1615 | size_t key_bits; |
| 1616 | size_t buffer_length; |
| 1617 | unsigned char *buffer = NULL; |
| 1618 | size_t output_length; |
| 1619 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1620 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1621 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1622 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1623 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1624 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1625 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1626 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 1627 | key_data->x, key_data->len ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1628 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1629 | PSA_ASSERT( psa_get_key_information( handle, |
| 1630 | NULL, |
| 1631 | &key_bits ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1632 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1633 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1634 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1635 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1636 | status = psa_asymmetric_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1637 | NULL, 0, |
| 1638 | NULL, 0, |
| 1639 | buffer, buffer_length, |
| 1640 | &output_length ); |
| 1641 | if( policy_alg == exercise_alg && |
| 1642 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1643 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1644 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1645 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1646 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1647 | if( buffer_length != 0 ) |
| 1648 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1649 | status = psa_asymmetric_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1650 | buffer, buffer_length, |
| 1651 | NULL, 0, |
| 1652 | buffer, buffer_length, |
| 1653 | &output_length ); |
| 1654 | if( policy_alg == exercise_alg && |
| 1655 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1656 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1657 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1658 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1659 | |
| 1660 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1661 | psa_destroy_key( handle ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1662 | mbedtls_psa_crypto_free( ); |
| 1663 | mbedtls_free( buffer ); |
| 1664 | } |
| 1665 | /* END_CASE */ |
| 1666 | |
| 1667 | /* BEGIN_CASE */ |
| 1668 | void asymmetric_signature_key_policy( int policy_usage, |
| 1669 | int policy_alg, |
| 1670 | int key_type, |
| 1671 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1672 | int exercise_alg, |
| 1673 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1674 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1675 | psa_key_handle_t handle = 0; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1676 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1677 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1678 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1679 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1680 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1681 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1682 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1683 | int compatible_alg = payload_length_arg > 0; |
| 1684 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1685 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
| 1686 | size_t signature_length; |
| 1687 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1688 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1689 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1690 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1691 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1692 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1693 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1694 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 1695 | key_data->x, key_data->len ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1696 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1697 | status = psa_asymmetric_sign( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1698 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1699 | signature, sizeof( signature ), |
| 1700 | &signature_length ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1701 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1702 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1703 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1704 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1705 | |
| 1706 | memset( signature, 0, sizeof( signature ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1707 | status = psa_asymmetric_verify( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1708 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1709 | signature, sizeof( signature ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1710 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1711 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1712 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1713 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1714 | |
| 1715 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1716 | psa_destroy_key( handle ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1717 | mbedtls_psa_crypto_free( ); |
| 1718 | } |
| 1719 | /* END_CASE */ |
| 1720 | |
| 1721 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1722 | void derive_key_policy( int policy_usage, |
| 1723 | int policy_alg, |
| 1724 | int key_type, |
| 1725 | data_t *key_data, |
| 1726 | int exercise_alg ) |
| 1727 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1728 | psa_key_handle_t handle = 0; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1729 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1730 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 1731 | psa_status_t status; |
| 1732 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1733 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1734 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1735 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1736 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1737 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1738 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1739 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 1740 | key_data->x, key_data->len ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1741 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1742 | status = psa_key_derivation( &generator, handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1743 | exercise_alg, |
| 1744 | NULL, 0, |
| 1745 | NULL, 0, |
| 1746 | 1 ); |
| 1747 | if( policy_alg == exercise_alg && |
| 1748 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1749 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1750 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1751 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1752 | |
| 1753 | exit: |
| 1754 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1755 | psa_destroy_key( handle ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1756 | mbedtls_psa_crypto_free( ); |
| 1757 | } |
| 1758 | /* END_CASE */ |
| 1759 | |
| 1760 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1761 | void agreement_key_policy( int policy_usage, |
| 1762 | int policy_alg, |
| 1763 | int key_type_arg, |
| 1764 | data_t *key_data, |
| 1765 | int exercise_alg ) |
| 1766 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1767 | psa_key_handle_t handle = 0; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1768 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1769 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1770 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 1771 | psa_status_t status; |
| 1772 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1773 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1774 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 1775 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1776 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1777 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1778 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1779 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 1780 | key_data->x, key_data->len ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1781 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1782 | status = key_agreement_with_self( &generator, handle, exercise_alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1783 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1784 | if( policy_alg == exercise_alg && |
| 1785 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1786 | PSA_ASSERT( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1787 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1788 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1789 | |
| 1790 | exit: |
| 1791 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1792 | psa_destroy_key( handle ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1793 | mbedtls_psa_crypto_free( ); |
| 1794 | } |
| 1795 | /* END_CASE */ |
| 1796 | |
| 1797 | /* BEGIN_CASE */ |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1798 | void copy_key_policy( int source_usage_arg, int source_alg_arg, |
| 1799 | int type_arg, data_t *material, |
| 1800 | int target_usage_arg, int target_alg_arg, |
| 1801 | int constraint_usage_arg, int constraint_alg_arg, |
| 1802 | int expected_usage_arg, int expected_alg_arg ) |
| 1803 | { |
| 1804 | psa_key_usage_t source_usage = source_usage_arg; |
| 1805 | psa_algorithm_t source_alg = source_alg_arg; |
| 1806 | psa_key_handle_t source_handle = 0; |
| 1807 | psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT; |
| 1808 | psa_key_type_t source_type = type_arg; |
| 1809 | size_t source_bits; |
| 1810 | psa_key_usage_t target_usage = target_usage_arg; |
| 1811 | psa_algorithm_t target_alg = target_alg_arg; |
| 1812 | psa_key_handle_t target_handle = 0; |
| 1813 | psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT; |
| 1814 | psa_key_type_t target_type; |
| 1815 | size_t target_bits; |
| 1816 | psa_key_usage_t constraint_usage = constraint_usage_arg; |
| 1817 | psa_algorithm_t constraint_alg = constraint_alg_arg; |
| 1818 | psa_key_policy_t constraint = PSA_KEY_POLICY_INIT; |
| 1819 | psa_key_policy_t *p_constraint = NULL; |
| 1820 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1821 | psa_algorithm_t expected_alg = expected_alg_arg; |
| 1822 | uint8_t *export_buffer = NULL; |
| 1823 | |
| 1824 | if( constraint_usage_arg != -1 ) |
| 1825 | { |
| 1826 | p_constraint = &constraint; |
| 1827 | psa_key_policy_set_usage( p_constraint, |
| 1828 | constraint_usage, constraint_alg ); |
| 1829 | } |
| 1830 | |
| 1831 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1832 | |
| 1833 | /* Populate the source slot. */ |
| 1834 | PSA_ASSERT( psa_allocate_key( &source_handle ) ); |
| 1835 | psa_key_policy_set_usage( &source_policy, source_usage, source_alg ); |
| 1836 | PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) ); |
| 1837 | PSA_ASSERT( psa_import_key( source_handle, source_type, |
| 1838 | material->x, material->len ) ); |
| 1839 | PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) ); |
| 1840 | |
| 1841 | /* Prepare the target slot. */ |
| 1842 | PSA_ASSERT( psa_allocate_key( &target_handle ) ); |
| 1843 | psa_key_policy_set_usage( &target_policy, target_usage, target_alg ); |
| 1844 | PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) ); |
| 1845 | target_policy = psa_key_policy_init(); |
| 1846 | |
| 1847 | /* Copy the key. */ |
| 1848 | PSA_ASSERT( psa_copy_key( source_handle, target_handle, p_constraint ) ); |
| 1849 | |
| 1850 | /* Destroy the source to ensure that this doesn't affect the target. */ |
| 1851 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 1852 | |
| 1853 | /* Test that the target slot has the expected content and policy. */ |
| 1854 | PSA_ASSERT( psa_get_key_information( target_handle, |
| 1855 | &target_type, &target_bits ) ); |
| 1856 | TEST_EQUAL( source_type, target_type ); |
| 1857 | TEST_EQUAL( source_bits, target_bits ); |
| 1858 | PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) ); |
| 1859 | TEST_EQUAL( expected_usage, psa_key_policy_get_usage( &target_policy ) ); |
| 1860 | TEST_EQUAL( expected_alg, psa_key_policy_get_algorithm( &target_policy ) ); |
| 1861 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 1862 | { |
| 1863 | size_t length; |
| 1864 | ASSERT_ALLOC( export_buffer, material->len ); |
| 1865 | PSA_ASSERT( psa_export_key( target_handle, export_buffer, |
| 1866 | material->len, &length ) ); |
| 1867 | ASSERT_COMPARE( material->x, material->len, |
| 1868 | export_buffer, length ); |
| 1869 | } |
| 1870 | if( ! exercise_key( target_handle, expected_usage, expected_alg ) ) |
| 1871 | goto exit; |
| 1872 | |
| 1873 | PSA_ASSERT( psa_close_key( target_handle ) ); |
| 1874 | |
| 1875 | exit: |
| 1876 | mbedtls_psa_crypto_free( ); |
| 1877 | mbedtls_free( export_buffer ); |
| 1878 | } |
| 1879 | /* END_CASE */ |
| 1880 | |
| 1881 | /* BEGIN_CASE */ |
| 1882 | void copy_fail( int source_usage_arg, int source_alg_arg, |
| 1883 | int type_arg, data_t *material, |
| 1884 | int target_usage_arg, int target_alg_arg, |
| 1885 | int constraint_usage_arg, int constraint_alg_arg, |
| 1886 | int expected_status_arg ) |
| 1887 | { |
| 1888 | /* Test copy failure into an empty slot. There is a test for copy failure |
| 1889 | * into an occupied slot in |
| 1890 | * test_suite_psa_crypto_slot_management.function. */ |
| 1891 | |
| 1892 | psa_key_usage_t source_usage = source_usage_arg; |
| 1893 | psa_algorithm_t source_alg = source_alg_arg; |
| 1894 | psa_key_handle_t source_handle = 0; |
| 1895 | psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT; |
| 1896 | psa_key_type_t source_type = type_arg; |
| 1897 | size_t source_bits; |
| 1898 | psa_key_usage_t target_usage = target_usage_arg; |
| 1899 | psa_algorithm_t target_alg = target_alg_arg; |
| 1900 | psa_key_handle_t target_handle = 0; |
| 1901 | psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT; |
| 1902 | psa_key_type_t target_type; |
| 1903 | size_t target_bits; |
| 1904 | psa_key_usage_t constraint_usage = constraint_usage_arg; |
| 1905 | psa_algorithm_t constraint_alg = constraint_alg_arg; |
| 1906 | psa_key_policy_t constraint = PSA_KEY_POLICY_INIT; |
| 1907 | psa_key_policy_t *p_constraint = NULL; |
| 1908 | psa_status_t expected_status = expected_status_arg; |
| 1909 | |
| 1910 | if( constraint_usage_arg != -1 ) |
| 1911 | { |
| 1912 | p_constraint = &constraint; |
| 1913 | psa_key_policy_set_usage( p_constraint, |
| 1914 | constraint_usage, constraint_alg ); |
| 1915 | } |
| 1916 | |
| 1917 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1918 | |
| 1919 | /* Populate the source slot. */ |
| 1920 | PSA_ASSERT( psa_allocate_key( &source_handle ) ); |
| 1921 | psa_key_policy_set_usage( &source_policy, source_usage, source_alg ); |
| 1922 | PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) ); |
| 1923 | PSA_ASSERT( psa_import_key( source_handle, source_type, |
| 1924 | material->x, material->len ) ); |
| 1925 | PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) ); |
| 1926 | |
| 1927 | /* Prepare the target slot. */ |
| 1928 | PSA_ASSERT( psa_allocate_key( &target_handle ) ); |
| 1929 | psa_key_policy_set_usage( &target_policy, target_usage, target_alg ); |
| 1930 | PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) ); |
| 1931 | target_policy = psa_key_policy_init(); |
| 1932 | |
| 1933 | /* Copy the key. */ |
| 1934 | TEST_EQUAL( psa_copy_key( source_handle, target_handle, p_constraint ), |
| 1935 | expected_status ); |
| 1936 | |
| 1937 | /* Test that the target slot is unaffected. */ |
| 1938 | TEST_EQUAL( psa_get_key_information( target_handle, |
| 1939 | &target_type, &target_bits ), |
| 1940 | PSA_ERROR_EMPTY_SLOT ); |
| 1941 | PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) ); |
| 1942 | TEST_EQUAL( target_usage, psa_key_policy_get_usage( &target_policy ) ); |
| 1943 | TEST_EQUAL( target_alg, psa_key_policy_get_algorithm( &target_policy ) ); |
| 1944 | |
| 1945 | exit: |
| 1946 | mbedtls_psa_crypto_free( ); |
| 1947 | } |
| 1948 | /* END_CASE */ |
| 1949 | |
| 1950 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1951 | void hash_operation_init( ) |
| 1952 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1953 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1954 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1955 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1956 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1957 | * to supress the Clang warning for the test. */ |
| 1958 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 1959 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1960 | psa_hash_operation_t zero; |
| 1961 | |
| 1962 | memset( &zero, 0, sizeof( zero ) ); |
| 1963 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame^] | 1964 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1965 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 1966 | PSA_ERROR_BAD_STATE ); |
| 1967 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 1968 | PSA_ERROR_BAD_STATE ); |
| 1969 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 1970 | PSA_ERROR_BAD_STATE ); |
| 1971 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1972 | /* A default hash operation should be abortable without error. */ |
| 1973 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 1974 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 1975 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1976 | } |
| 1977 | /* END_CASE */ |
| 1978 | |
| 1979 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1980 | void hash_setup( int alg_arg, |
| 1981 | int expected_status_arg ) |
| 1982 | { |
| 1983 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1984 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1985 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1986 | psa_status_t status; |
| 1987 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1988 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1989 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1990 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1991 | psa_hash_abort( &operation ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1992 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1993 | |
| 1994 | exit: |
| 1995 | mbedtls_psa_crypto_free( ); |
| 1996 | } |
| 1997 | /* END_CASE */ |
| 1998 | |
| 1999 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2000 | void hash_bad_order( ) |
| 2001 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame^] | 2002 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2003 | unsigned char input[] = ""; |
| 2004 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame^] | 2005 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2006 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2007 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2008 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame^] | 2009 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2010 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2011 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2012 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2013 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2014 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame^] | 2015 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2016 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2017 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame^] | 2018 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2019 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame^] | 2020 | /* Call update after finish. */ |
| 2021 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2022 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2023 | hash, sizeof( hash ), &hash_len ) ); |
| 2024 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2025 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame^] | 2026 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2027 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame^] | 2028 | /* Call verify without calling setup beforehand. */ |
| 2029 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2030 | valid_hash, sizeof( valid_hash ) ), |
| 2031 | PSA_ERROR_BAD_STATE ); |
| 2032 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2033 | |
| 2034 | /* Call verify after finish. */ |
| 2035 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2036 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2037 | hash, sizeof( hash ), &hash_len ) ); |
| 2038 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2039 | valid_hash, sizeof( valid_hash ) ), |
| 2040 | PSA_ERROR_BAD_STATE ); |
| 2041 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2042 | |
| 2043 | /* Call verify twice in a row. */ |
| 2044 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2045 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2046 | valid_hash, sizeof( valid_hash ) ) ); |
| 2047 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2048 | valid_hash, sizeof( valid_hash ) ), |
| 2049 | PSA_ERROR_BAD_STATE ); |
| 2050 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2051 | |
| 2052 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2053 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2054 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2055 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame^] | 2056 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2057 | |
| 2058 | /* Call finish twice in a row. */ |
| 2059 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2060 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2061 | hash, sizeof( hash ), &hash_len ) ); |
| 2062 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2063 | hash, sizeof( hash ), &hash_len ), |
| 2064 | PSA_ERROR_BAD_STATE ); |
| 2065 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2066 | |
| 2067 | /* Call finish after calling verify. */ |
| 2068 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2069 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2070 | valid_hash, sizeof( valid_hash ) ) ); |
| 2071 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2072 | hash, sizeof( hash ), &hash_len ), |
| 2073 | PSA_ERROR_BAD_STATE ); |
| 2074 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2075 | |
| 2076 | exit: |
| 2077 | mbedtls_psa_crypto_free( ); |
| 2078 | } |
| 2079 | /* END_CASE */ |
| 2080 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2081 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2082 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2083 | { |
| 2084 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2085 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2086 | * appended to it */ |
| 2087 | unsigned char hash[] = { |
| 2088 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2089 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2090 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2091 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2092 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2093 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2094 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2095 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2096 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2097 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2098 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2099 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2100 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2101 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2102 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2103 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2104 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2105 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2106 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2107 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2108 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2109 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2110 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2111 | exit: |
| 2112 | mbedtls_psa_crypto_free( ); |
| 2113 | } |
| 2114 | /* END_CASE */ |
| 2115 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2116 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2117 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2118 | { |
| 2119 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2120 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2121 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2122 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2123 | size_t hash_len; |
| 2124 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2125 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2126 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2127 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2128 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2129 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2130 | hash, expected_size - 1, &hash_len ), |
| 2131 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2132 | |
| 2133 | exit: |
| 2134 | mbedtls_psa_crypto_free( ); |
| 2135 | } |
| 2136 | /* END_CASE */ |
| 2137 | |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2138 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2139 | void hash_clone_source_state( ) |
| 2140 | { |
| 2141 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2142 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2143 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2144 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2145 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2146 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2147 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2148 | size_t hash_len; |
| 2149 | |
| 2150 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2151 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2152 | |
| 2153 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2154 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2155 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2156 | hash, sizeof( hash ), &hash_len ) ); |
| 2157 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2158 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2159 | |
| 2160 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2161 | PSA_ERROR_BAD_STATE ); |
| 2162 | |
| 2163 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2164 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2165 | hash, sizeof( hash ), &hash_len ) ); |
| 2166 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2167 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2168 | hash, sizeof( hash ), &hash_len ) ); |
| 2169 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2170 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2171 | hash, sizeof( hash ), &hash_len ) ); |
| 2172 | |
| 2173 | exit: |
| 2174 | psa_hash_abort( &op_source ); |
| 2175 | psa_hash_abort( &op_init ); |
| 2176 | psa_hash_abort( &op_setup ); |
| 2177 | psa_hash_abort( &op_finished ); |
| 2178 | psa_hash_abort( &op_aborted ); |
| 2179 | mbedtls_psa_crypto_free( ); |
| 2180 | } |
| 2181 | /* END_CASE */ |
| 2182 | |
| 2183 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2184 | void hash_clone_target_state( ) |
| 2185 | { |
| 2186 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2187 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2188 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2189 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2190 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2191 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2192 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2193 | size_t hash_len; |
| 2194 | |
| 2195 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2196 | |
| 2197 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2198 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2199 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2200 | hash, sizeof( hash ), &hash_len ) ); |
| 2201 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2202 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2203 | |
| 2204 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2205 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2206 | hash, sizeof( hash ), &hash_len ) ); |
| 2207 | |
| 2208 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2209 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2210 | PSA_ERROR_BAD_STATE ); |
| 2211 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2212 | PSA_ERROR_BAD_STATE ); |
| 2213 | |
| 2214 | exit: |
| 2215 | psa_hash_abort( &op_target ); |
| 2216 | psa_hash_abort( &op_init ); |
| 2217 | psa_hash_abort( &op_setup ); |
| 2218 | psa_hash_abort( &op_finished ); |
| 2219 | psa_hash_abort( &op_aborted ); |
| 2220 | mbedtls_psa_crypto_free( ); |
| 2221 | } |
| 2222 | /* END_CASE */ |
| 2223 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2224 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2225 | void mac_operation_init( ) |
| 2226 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2227 | const uint8_t input[1] = { 0 }; |
| 2228 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2229 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2230 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2231 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2232 | * to supress the Clang warning for the test. */ |
| 2233 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2234 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2235 | psa_mac_operation_t zero; |
| 2236 | |
| 2237 | memset( &zero, 0, sizeof( zero ) ); |
| 2238 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2239 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2240 | TEST_EQUAL( psa_mac_update( &func, |
| 2241 | input, sizeof( input ) ), |
| 2242 | PSA_ERROR_BAD_STATE ); |
| 2243 | TEST_EQUAL( psa_mac_update( &init, |
| 2244 | input, sizeof( input ) ), |
| 2245 | PSA_ERROR_BAD_STATE ); |
| 2246 | TEST_EQUAL( psa_mac_update( &zero, |
| 2247 | input, sizeof( input ) ), |
| 2248 | PSA_ERROR_BAD_STATE ); |
| 2249 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2250 | /* A default MAC operation should be abortable without error. */ |
| 2251 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2252 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2253 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2254 | } |
| 2255 | /* END_CASE */ |
| 2256 | |
| 2257 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2258 | void mac_setup( int key_type_arg, |
| 2259 | data_t *key, |
| 2260 | int alg_arg, |
| 2261 | int expected_status_arg ) |
| 2262 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2263 | psa_key_handle_t handle = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2264 | psa_key_type_t key_type = key_type_arg; |
| 2265 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2266 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2267 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2268 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2269 | psa_status_t status; |
| 2270 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2271 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2272 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 2273 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2274 | psa_key_policy_set_usage( &policy, |
| 2275 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY, |
| 2276 | alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2277 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2278 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2279 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 2280 | key->x, key->len ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2281 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2282 | status = psa_mac_sign_setup( &operation, handle, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2283 | psa_mac_abort( &operation ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2284 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2285 | |
| 2286 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2287 | psa_destroy_key( handle ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2288 | mbedtls_psa_crypto_free( ); |
| 2289 | } |
| 2290 | /* END_CASE */ |
| 2291 | |
| 2292 | /* BEGIN_CASE */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2293 | void mac_bad_order( ) |
| 2294 | { |
| 2295 | psa_key_handle_t handle = 0; |
| 2296 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2297 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
| 2298 | const uint8_t key[] = { |
| 2299 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2300 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2301 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2302 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
| 2303 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2304 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2305 | size_t sign_mac_length = 0; |
| 2306 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2307 | const uint8_t verify_mac[] = { |
| 2308 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2309 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2310 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2311 | |
| 2312 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2313 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
| 2314 | psa_key_policy_set_usage( &policy, |
| 2315 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY, |
| 2316 | alg ); |
| 2317 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
| 2318 | |
| 2319 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 2320 | key, sizeof(key) ) ); |
| 2321 | |
| 2322 | /* Call update without calling setup beforehand. */ |
| 2323 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2324 | PSA_ERROR_BAD_STATE ); |
| 2325 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2326 | |
| 2327 | /* Call sign finish without calling setup beforehand. */ |
| 2328 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2329 | &sign_mac_length), |
| 2330 | PSA_ERROR_BAD_STATE ); |
| 2331 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2332 | |
| 2333 | /* Call verify finish without calling setup beforehand. */ |
| 2334 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2335 | verify_mac, sizeof( verify_mac ) ), |
| 2336 | PSA_ERROR_BAD_STATE ); |
| 2337 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2338 | |
| 2339 | /* Call update after sign finish. */ |
| 2340 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2341 | handle, alg ) ); |
| 2342 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2343 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2344 | sign_mac, sizeof( sign_mac ), |
| 2345 | &sign_mac_length ) ); |
| 2346 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2347 | PSA_ERROR_BAD_STATE ); |
| 2348 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2349 | |
| 2350 | /* Call update after verify finish. */ |
| 2351 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 2352 | handle, alg ) ); |
| 2353 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2354 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2355 | verify_mac, sizeof( verify_mac ) ) ); |
| 2356 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2357 | PSA_ERROR_BAD_STATE ); |
| 2358 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2359 | |
| 2360 | /* Call sign finish twice in a row. */ |
| 2361 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2362 | handle, alg ) ); |
| 2363 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2364 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2365 | sign_mac, sizeof( sign_mac ), |
| 2366 | &sign_mac_length ) ); |
| 2367 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2368 | sign_mac, sizeof( sign_mac ), |
| 2369 | &sign_mac_length ), |
| 2370 | PSA_ERROR_BAD_STATE ); |
| 2371 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2372 | |
| 2373 | /* Call verify finish twice in a row. */ |
| 2374 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 2375 | handle, alg ) ); |
| 2376 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2377 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2378 | verify_mac, sizeof( verify_mac ) ) ); |
| 2379 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2380 | verify_mac, sizeof( verify_mac ) ), |
| 2381 | PSA_ERROR_BAD_STATE ); |
| 2382 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2383 | |
| 2384 | /* Setup sign but try verify. */ |
| 2385 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2386 | handle, alg ) ); |
| 2387 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2388 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2389 | verify_mac, sizeof( verify_mac ) ), |
| 2390 | PSA_ERROR_BAD_STATE ); |
| 2391 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2392 | |
| 2393 | /* Setup verify but try sign. */ |
| 2394 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 2395 | handle, alg ) ); |
| 2396 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2397 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2398 | sign_mac, sizeof( sign_mac ), |
| 2399 | &sign_mac_length ), |
| 2400 | PSA_ERROR_BAD_STATE ); |
| 2401 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2402 | |
| 2403 | exit: |
| 2404 | mbedtls_psa_crypto_free( ); |
| 2405 | } |
| 2406 | /* END_CASE */ |
| 2407 | |
| 2408 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2409 | void mac_sign( int key_type_arg, |
| 2410 | data_t *key, |
| 2411 | int alg_arg, |
| 2412 | data_t *input, |
| 2413 | data_t *expected_mac ) |
| 2414 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2415 | psa_key_handle_t handle = 0; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2416 | psa_key_type_t key_type = key_type_arg; |
| 2417 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2418 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2419 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2420 | /* Leave a little extra room in the output buffer. At the end of the |
| 2421 | * test, we'll check that the implementation didn't overwrite onto |
| 2422 | * this extra room. */ |
| 2423 | uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10]; |
| 2424 | size_t mac_buffer_size = |
| 2425 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 2426 | size_t mac_length = 0; |
| 2427 | |
| 2428 | memset( actual_mac, '+', sizeof( actual_mac ) ); |
| 2429 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
| 2430 | TEST_ASSERT( expected_mac->len <= mac_buffer_size ); |
| 2431 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2432 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2433 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 2434 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2435 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2436 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2437 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2438 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 2439 | key->x, key->len ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2440 | |
| 2441 | /* Calculate the MAC. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2442 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2443 | handle, alg ) ); |
| 2444 | PSA_ASSERT( psa_mac_update( &operation, |
| 2445 | input->x, input->len ) ); |
| 2446 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2447 | actual_mac, mac_buffer_size, |
| 2448 | &mac_length ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2449 | |
| 2450 | /* Compare with the expected value. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 2451 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2452 | actual_mac, mac_length ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2453 | |
| 2454 | /* Verify that the end of the buffer is untouched. */ |
| 2455 | TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+', |
| 2456 | sizeof( actual_mac ) - mac_length ) ); |
| 2457 | |
| 2458 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2459 | psa_destroy_key( handle ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2460 | mbedtls_psa_crypto_free( ); |
| 2461 | } |
| 2462 | /* END_CASE */ |
| 2463 | |
| 2464 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2465 | void mac_verify( int key_type_arg, |
| 2466 | data_t *key, |
| 2467 | int alg_arg, |
| 2468 | data_t *input, |
| 2469 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2470 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2471 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2472 | psa_key_type_t key_type = key_type_arg; |
| 2473 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2474 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2475 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2476 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2477 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2478 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2479 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2480 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 2481 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2482 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2483 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2484 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2485 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 2486 | key->x, key->len ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2487 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2488 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 2489 | handle, alg ) ); |
| 2490 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2491 | PSA_ASSERT( psa_mac_update( &operation, |
| 2492 | input->x, input->len ) ); |
| 2493 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2494 | expected_mac->x, |
| 2495 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2496 | |
| 2497 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2498 | psa_destroy_key( handle ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2499 | mbedtls_psa_crypto_free( ); |
| 2500 | } |
| 2501 | /* END_CASE */ |
| 2502 | |
| 2503 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2504 | void cipher_operation_init( ) |
| 2505 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2506 | const uint8_t input[1] = { 0 }; |
| 2507 | unsigned char output[1] = { 0 }; |
| 2508 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2509 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2510 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2511 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2512 | * to supress the Clang warning for the test. */ |
| 2513 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2514 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2515 | psa_cipher_operation_t zero; |
| 2516 | |
| 2517 | memset( &zero, 0, sizeof( zero ) ); |
| 2518 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2519 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2520 | TEST_EQUAL( psa_cipher_update( &func, |
| 2521 | input, sizeof( input ), |
| 2522 | output, sizeof( output ), |
| 2523 | &output_length ), |
| 2524 | PSA_ERROR_BAD_STATE ); |
| 2525 | TEST_EQUAL( psa_cipher_update( &init, |
| 2526 | input, sizeof( input ), |
| 2527 | output, sizeof( output ), |
| 2528 | &output_length ), |
| 2529 | PSA_ERROR_BAD_STATE ); |
| 2530 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2531 | input, sizeof( input ), |
| 2532 | output, sizeof( output ), |
| 2533 | &output_length ), |
| 2534 | PSA_ERROR_BAD_STATE ); |
| 2535 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2536 | /* A default cipher operation should be abortable without error. */ |
| 2537 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2538 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2539 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2540 | } |
| 2541 | /* END_CASE */ |
| 2542 | |
| 2543 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2544 | void cipher_setup( int key_type_arg, |
| 2545 | data_t *key, |
| 2546 | int alg_arg, |
| 2547 | int expected_status_arg ) |
| 2548 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2549 | psa_key_handle_t handle = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2550 | psa_key_type_t key_type = key_type_arg; |
| 2551 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2552 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2553 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2554 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2555 | psa_status_t status; |
| 2556 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2557 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2558 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 2559 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2560 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2561 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2562 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2563 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 2564 | key->x, key->len ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2565 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2566 | status = psa_cipher_encrypt_setup( &operation, handle, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2567 | psa_cipher_abort( &operation ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2568 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2569 | |
| 2570 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2571 | psa_destroy_key( handle ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2572 | mbedtls_psa_crypto_free( ); |
| 2573 | } |
| 2574 | /* END_CASE */ |
| 2575 | |
| 2576 | /* BEGIN_CASE */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2577 | void cipher_bad_order( ) |
| 2578 | { |
| 2579 | psa_key_handle_t handle = 0; |
| 2580 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2581 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
| 2582 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
| 2583 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2584 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2585 | const uint8_t key[] = { |
| 2586 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2587 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2588 | const uint8_t text[] = { |
| 2589 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2590 | 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2591 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2592 | size_t length = 0; |
| 2593 | |
| 2594 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2595 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
| 2596 | psa_key_policy_set_usage( &policy, |
| 2597 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
| 2598 | alg ); |
| 2599 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
| 2600 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 2601 | key, sizeof(key) ) ); |
| 2602 | |
| 2603 | |
| 2604 | /* Generate an IV without calling setup beforehand. */ |
| 2605 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2606 | buffer, sizeof( buffer ), |
| 2607 | &length ), |
| 2608 | PSA_ERROR_BAD_STATE ); |
| 2609 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2610 | |
| 2611 | /* Generate an IV twice in a row. */ |
| 2612 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2613 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2614 | buffer, sizeof( buffer ), |
| 2615 | &length ) ); |
| 2616 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2617 | buffer, sizeof( buffer ), |
| 2618 | &length ), |
| 2619 | PSA_ERROR_BAD_STATE ); |
| 2620 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2621 | |
| 2622 | /* Generate an IV after it's already set. */ |
| 2623 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2624 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2625 | iv, sizeof( iv ) ) ); |
| 2626 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2627 | buffer, sizeof( buffer ), |
| 2628 | &length ), |
| 2629 | PSA_ERROR_BAD_STATE ); |
| 2630 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2631 | |
| 2632 | /* Set an IV without calling setup beforehand. */ |
| 2633 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2634 | iv, sizeof( iv ) ), |
| 2635 | PSA_ERROR_BAD_STATE ); |
| 2636 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2637 | |
| 2638 | /* Set an IV after it's already set. */ |
| 2639 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2640 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2641 | iv, sizeof( iv ) ) ); |
| 2642 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2643 | iv, sizeof( iv ) ), |
| 2644 | PSA_ERROR_BAD_STATE ); |
| 2645 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2646 | |
| 2647 | /* Set an IV after it's already generated. */ |
| 2648 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2649 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2650 | buffer, sizeof( buffer ), |
| 2651 | &length ) ); |
| 2652 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2653 | iv, sizeof( iv ) ), |
| 2654 | PSA_ERROR_BAD_STATE ); |
| 2655 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2656 | |
| 2657 | /* Call update without calling setup beforehand. */ |
| 2658 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2659 | text, sizeof( text ), |
| 2660 | buffer, sizeof( buffer ), |
| 2661 | &length ), |
| 2662 | PSA_ERROR_BAD_STATE ); |
| 2663 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2664 | |
| 2665 | /* Call update without an IV where an IV is required. */ |
| 2666 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2667 | text, sizeof( text ), |
| 2668 | buffer, sizeof( buffer ), |
| 2669 | &length ), |
| 2670 | PSA_ERROR_BAD_STATE ); |
| 2671 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2672 | |
| 2673 | /* Call update after finish. */ |
| 2674 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2675 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2676 | iv, sizeof( iv ) ) ); |
| 2677 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2678 | buffer, sizeof( buffer ), &length ) ); |
| 2679 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2680 | text, sizeof( text ), |
| 2681 | buffer, sizeof( buffer ), |
| 2682 | &length ), |
| 2683 | PSA_ERROR_BAD_STATE ); |
| 2684 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2685 | |
| 2686 | /* Call finish without calling setup beforehand. */ |
| 2687 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2688 | buffer, sizeof( buffer ), &length ), |
| 2689 | PSA_ERROR_BAD_STATE ); |
| 2690 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2691 | |
| 2692 | /* Call finish without an IV where an IV is required. */ |
| 2693 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2694 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2695 | * for cipher modes with padding. */ |
| 2696 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2697 | buffer, sizeof( buffer ), &length ), |
| 2698 | PSA_ERROR_BAD_STATE ); |
| 2699 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2700 | |
| 2701 | /* Call finish twice in a row. */ |
| 2702 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2703 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2704 | iv, sizeof( iv ) ) ); |
| 2705 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2706 | buffer, sizeof( buffer ), &length ) ); |
| 2707 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2708 | buffer, sizeof( buffer ), &length ), |
| 2709 | PSA_ERROR_BAD_STATE ); |
| 2710 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2711 | |
| 2712 | exit: |
| 2713 | mbedtls_psa_crypto_free( ); |
| 2714 | } |
| 2715 | /* END_CASE */ |
| 2716 | |
| 2717 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2718 | void cipher_encrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2719 | data_t *key, |
| 2720 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2721 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2722 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2723 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2724 | psa_status_t status; |
| 2725 | psa_key_type_t key_type = key_type_arg; |
| 2726 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2727 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2728 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2729 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2730 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2731 | size_t output_buffer_size = 0; |
| 2732 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2733 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2734 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2735 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2736 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2737 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2738 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2739 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2740 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2741 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 2742 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2743 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2744 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2745 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2746 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 2747 | key->x, key->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2748 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2749 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 2750 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2751 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2752 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2753 | iv, iv_size ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 2754 | output_buffer_size = ( (size_t) input->len + |
| 2755 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2756 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2757 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2758 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2759 | input->x, input->len, |
| 2760 | output, output_buffer_size, |
| 2761 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2762 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2763 | status = psa_cipher_finish( &operation, |
| 2764 | output + function_output_length, |
| 2765 | output_buffer_size, |
| 2766 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2767 | total_output_length += function_output_length; |
| 2768 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2769 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2770 | if( expected_status == PSA_SUCCESS ) |
| 2771 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2772 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2773 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2774 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2775 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2776 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2777 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2778 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2779 | psa_destroy_key( handle ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2780 | mbedtls_psa_crypto_free( ); |
| 2781 | } |
| 2782 | /* END_CASE */ |
| 2783 | |
| 2784 | /* BEGIN_CASE */ |
| 2785 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2786 | data_t *key, |
| 2787 | data_t *input, |
| 2788 | int first_part_size, |
| 2789 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2790 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2791 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2792 | psa_key_type_t key_type = key_type_arg; |
| 2793 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2794 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2795 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2796 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2797 | size_t output_buffer_size = 0; |
| 2798 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2799 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2800 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2801 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2802 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2803 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2804 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2805 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2806 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2807 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 2808 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2809 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2810 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2811 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2812 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 2813 | key->x, key->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2814 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2815 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 2816 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2817 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2818 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2819 | iv, sizeof( iv ) ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 2820 | output_buffer_size = ( (size_t) input->len + |
| 2821 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2822 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2823 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2824 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2825 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 2826 | output, output_buffer_size, |
| 2827 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2828 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2829 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2830 | input->x + first_part_size, |
| 2831 | input->len - first_part_size, |
| 2832 | output, output_buffer_size, |
| 2833 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2834 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2835 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2836 | output + function_output_length, |
| 2837 | output_buffer_size, |
| 2838 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2839 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2840 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2841 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2842 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2843 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2844 | |
| 2845 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2846 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2847 | psa_destroy_key( handle ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2848 | mbedtls_psa_crypto_free( ); |
| 2849 | } |
| 2850 | /* END_CASE */ |
| 2851 | |
| 2852 | /* BEGIN_CASE */ |
| 2853 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2854 | data_t *key, |
| 2855 | data_t *input, |
| 2856 | int first_part_size, |
| 2857 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2858 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2859 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2860 | |
| 2861 | psa_key_type_t key_type = key_type_arg; |
| 2862 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2863 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2864 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2865 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2866 | size_t output_buffer_size = 0; |
| 2867 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2868 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2869 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2870 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2871 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2872 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2873 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2874 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2875 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2876 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 2877 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2878 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2879 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2880 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2881 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 2882 | key->x, key->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2883 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2884 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 2885 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2886 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2887 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2888 | iv, sizeof( iv ) ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2889 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 2890 | output_buffer_size = ( (size_t) input->len + |
| 2891 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2892 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2893 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2894 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2895 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2896 | input->x, first_part_size, |
| 2897 | output, output_buffer_size, |
| 2898 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2899 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2900 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2901 | input->x + first_part_size, |
| 2902 | input->len - first_part_size, |
| 2903 | output, output_buffer_size, |
| 2904 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2905 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2906 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2907 | output + function_output_length, |
| 2908 | output_buffer_size, |
| 2909 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2910 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2911 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2912 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2913 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2914 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2915 | |
| 2916 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2917 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2918 | psa_destroy_key( handle ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2919 | mbedtls_psa_crypto_free( ); |
| 2920 | } |
| 2921 | /* END_CASE */ |
| 2922 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2923 | /* BEGIN_CASE */ |
| 2924 | void cipher_decrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2925 | data_t *key, |
| 2926 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2927 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2928 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2929 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2930 | psa_status_t status; |
| 2931 | psa_key_type_t key_type = key_type_arg; |
| 2932 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2933 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2934 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2935 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2936 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2937 | size_t output_buffer_size = 0; |
| 2938 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2939 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2940 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2941 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2942 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2943 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2944 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2945 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2946 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2947 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 2948 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2949 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2950 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2951 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2952 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 2953 | key->x, key->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2954 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2955 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 2956 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2957 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2958 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2959 | iv, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2960 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 2961 | output_buffer_size = ( (size_t) input->len + |
| 2962 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2963 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2964 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2965 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2966 | input->x, input->len, |
| 2967 | output, output_buffer_size, |
| 2968 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2969 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2970 | status = psa_cipher_finish( &operation, |
| 2971 | output + function_output_length, |
| 2972 | output_buffer_size, |
| 2973 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2974 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2975 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2976 | |
| 2977 | if( expected_status == PSA_SUCCESS ) |
| 2978 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2979 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2980 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2981 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2982 | } |
| 2983 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2984 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2985 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2986 | psa_destroy_key( handle ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2987 | mbedtls_psa_crypto_free( ); |
| 2988 | } |
| 2989 | /* END_CASE */ |
| 2990 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2991 | /* BEGIN_CASE */ |
| 2992 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2993 | data_t *key, |
| 2994 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2995 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2996 | psa_key_handle_t handle = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2997 | psa_key_type_t key_type = key_type_arg; |
| 2998 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 2999 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3000 | size_t iv_size = 16; |
| 3001 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3002 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3003 | size_t output1_size = 0; |
| 3004 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3005 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3006 | size_t output2_size = 0; |
| 3007 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3008 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3009 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3010 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3011 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3012 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3013 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3014 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3015 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3016 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3017 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3018 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3019 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3020 | key->x, key->len ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3021 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3022 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3023 | handle, alg ) ); |
| 3024 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3025 | handle, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3026 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3027 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3028 | iv, iv_size, |
| 3029 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3030 | output1_size = ( (size_t) input->len + |
| 3031 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3032 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3033 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3034 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3035 | output1, output1_size, |
| 3036 | &output1_length ) ); |
| 3037 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3038 | output1 + output1_length, output1_size, |
| 3039 | &function_output_length ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3040 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3041 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3042 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3043 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3044 | |
| 3045 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3046 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3047 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3048 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3049 | iv, iv_length ) ); |
| 3050 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3051 | output2, output2_size, |
| 3052 | &output2_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3053 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3054 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3055 | output2 + output2_length, |
| 3056 | output2_size, |
| 3057 | &function_output_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3058 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3059 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3060 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3061 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3062 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3063 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3064 | |
| 3065 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3066 | mbedtls_free( output1 ); |
| 3067 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3068 | psa_destroy_key( handle ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3069 | mbedtls_psa_crypto_free( ); |
| 3070 | } |
| 3071 | /* END_CASE */ |
| 3072 | |
| 3073 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3074 | void cipher_verify_output_multipart( int alg_arg, |
| 3075 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3076 | data_t *key, |
| 3077 | data_t *input, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3078 | int first_part_size ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3079 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3080 | psa_key_handle_t handle = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3081 | psa_key_type_t key_type = key_type_arg; |
| 3082 | psa_algorithm_t alg = alg_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3083 | unsigned char iv[16] = {0}; |
| 3084 | size_t iv_size = 16; |
| 3085 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3086 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3087 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3088 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3089 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3090 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3091 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3092 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3093 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3094 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3095 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3096 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3097 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3098 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3099 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3100 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3101 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3102 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3103 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3104 | key->x, key->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3105 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3106 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3107 | handle, alg ) ); |
| 3108 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3109 | handle, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3110 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3111 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3112 | iv, iv_size, |
| 3113 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3114 | output1_buffer_size = ( (size_t) input->len + |
| 3115 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3116 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3117 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3118 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3119 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3120 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3121 | output1, output1_buffer_size, |
| 3122 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3123 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3124 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3125 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3126 | input->x + first_part_size, |
| 3127 | input->len - first_part_size, |
| 3128 | output1, output1_buffer_size, |
| 3129 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3130 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3131 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3132 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3133 | output1 + output1_length, |
| 3134 | output1_buffer_size - output1_length, |
| 3135 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3136 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3137 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3138 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3139 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3140 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3141 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3142 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3143 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3144 | iv, iv_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3145 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3146 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3147 | output2, output2_buffer_size, |
| 3148 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3149 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3150 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3151 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3152 | output1 + first_part_size, |
| 3153 | output1_length - first_part_size, |
| 3154 | output2, output2_buffer_size, |
| 3155 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3156 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3157 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3158 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3159 | output2 + output2_length, |
| 3160 | output2_buffer_size - output2_length, |
| 3161 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3162 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3163 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3164 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3165 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3166 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3167 | |
| 3168 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3169 | mbedtls_free( output1 ); |
| 3170 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3171 | psa_destroy_key( handle ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3172 | mbedtls_psa_crypto_free( ); |
| 3173 | } |
| 3174 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3175 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3176 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3177 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3178 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3179 | data_t *nonce, |
| 3180 | data_t *additional_data, |
| 3181 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3182 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3183 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3184 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3185 | psa_key_type_t key_type = key_type_arg; |
| 3186 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3187 | unsigned char *output_data = NULL; |
| 3188 | size_t output_size = 0; |
| 3189 | size_t output_length = 0; |
| 3190 | unsigned char *output_data2 = NULL; |
| 3191 | size_t output_length2 = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3192 | size_t tag_length = 16; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3193 | psa_status_t expected_result = expected_result_arg; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3194 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3195 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3196 | output_size = input_data->len + tag_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3197 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3198 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3199 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3200 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3201 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3202 | psa_key_policy_set_usage( &policy, |
| 3203 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
| 3204 | alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3205 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3206 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3207 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3208 | key_data->x, key_data->len ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3209 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3210 | TEST_EQUAL( psa_aead_encrypt( handle, alg, |
| 3211 | nonce->x, nonce->len, |
| 3212 | additional_data->x, |
| 3213 | additional_data->len, |
| 3214 | input_data->x, input_data->len, |
| 3215 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3216 | &output_length ), |
| 3217 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3218 | |
| 3219 | if( PSA_SUCCESS == expected_result ) |
| 3220 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3221 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3222 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3223 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3224 | nonce->x, nonce->len, |
| 3225 | additional_data->x, |
| 3226 | additional_data->len, |
| 3227 | output_data, output_length, |
| 3228 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3229 | &output_length2 ), |
| 3230 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3231 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3232 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3233 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3234 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3235 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3236 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3237 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3238 | mbedtls_free( output_data ); |
| 3239 | mbedtls_free( output_data2 ); |
| 3240 | mbedtls_psa_crypto_free( ); |
| 3241 | } |
| 3242 | /* END_CASE */ |
| 3243 | |
| 3244 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3245 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3246 | int alg_arg, |
| 3247 | data_t *nonce, |
| 3248 | data_t *additional_data, |
| 3249 | data_t *input_data, |
| 3250 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3251 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3252 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3253 | psa_key_type_t key_type = key_type_arg; |
| 3254 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3255 | unsigned char *output_data = NULL; |
| 3256 | size_t output_size = 0; |
| 3257 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3258 | size_t tag_length = 16; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3259 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3260 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3261 | output_size = input_data->len + tag_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3262 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3263 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3264 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3265 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3266 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3267 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3268 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3269 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3270 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3271 | key_data->x, |
| 3272 | key_data->len ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3273 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3274 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 3275 | nonce->x, nonce->len, |
| 3276 | additional_data->x, additional_data->len, |
| 3277 | input_data->x, input_data->len, |
| 3278 | output_data, output_size, |
| 3279 | &output_length ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3280 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3281 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3282 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3283 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3284 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3285 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3286 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3287 | mbedtls_psa_crypto_free( ); |
| 3288 | } |
| 3289 | /* END_CASE */ |
| 3290 | |
| 3291 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3292 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3293 | int alg_arg, |
| 3294 | data_t *nonce, |
| 3295 | data_t *additional_data, |
| 3296 | data_t *input_data, |
| 3297 | data_t *expected_data, |
| 3298 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3299 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3300 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3301 | psa_key_type_t key_type = key_type_arg; |
| 3302 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3303 | unsigned char *output_data = NULL; |
| 3304 | size_t output_size = 0; |
| 3305 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3306 | size_t tag_length = 16; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3307 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3308 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3309 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3310 | output_size = input_data->len + tag_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3311 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3312 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3313 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3314 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3315 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3316 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3317 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3318 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3319 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3320 | key_data->x, |
| 3321 | key_data->len ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3322 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3323 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3324 | nonce->x, nonce->len, |
| 3325 | additional_data->x, |
| 3326 | additional_data->len, |
| 3327 | input_data->x, input_data->len, |
| 3328 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3329 | &output_length ), |
| 3330 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3331 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3332 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3333 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3334 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3335 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3336 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3337 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3338 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3339 | mbedtls_psa_crypto_free( ); |
| 3340 | } |
| 3341 | /* END_CASE */ |
| 3342 | |
| 3343 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3344 | void signature_size( int type_arg, |
| 3345 | int bits, |
| 3346 | int alg_arg, |
| 3347 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3348 | { |
| 3349 | psa_key_type_t type = type_arg; |
| 3350 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3351 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3352 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3353 | exit: |
| 3354 | ; |
| 3355 | } |
| 3356 | /* END_CASE */ |
| 3357 | |
| 3358 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3359 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 3360 | int alg_arg, data_t *input_data, |
| 3361 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3362 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3363 | psa_key_handle_t handle = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3364 | psa_key_type_t key_type = key_type_arg; |
| 3365 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3366 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3367 | unsigned char *signature = NULL; |
| 3368 | size_t signature_size; |
| 3369 | size_t signature_length = 0xdeadbeef; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3370 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3371 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3372 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3373 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3374 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3375 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3376 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3377 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3378 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3379 | key_data->x, |
| 3380 | key_data->len ) ); |
| 3381 | PSA_ASSERT( psa_get_key_information( handle, |
| 3382 | NULL, |
| 3383 | &key_bits ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3384 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3385 | /* Allocate a buffer which has the size advertized by the |
| 3386 | * library. */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3387 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3388 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3389 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3390 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3391 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3392 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3393 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3394 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3395 | input_data->x, input_data->len, |
| 3396 | signature, signature_size, |
| 3397 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3398 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3399 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3400 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3401 | |
| 3402 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3403 | psa_destroy_key( handle ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3404 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3405 | mbedtls_psa_crypto_free( ); |
| 3406 | } |
| 3407 | /* END_CASE */ |
| 3408 | |
| 3409 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3410 | void sign_fail( int key_type_arg, data_t *key_data, |
| 3411 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3412 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3413 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3414 | psa_key_handle_t handle = 0; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3415 | psa_key_type_t key_type = key_type_arg; |
| 3416 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3417 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3418 | psa_status_t actual_status; |
| 3419 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3420 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3421 | size_t signature_length = 0xdeadbeef; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3422 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3423 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3424 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3425 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3426 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3427 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3428 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3429 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3430 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3431 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3432 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3433 | key_data->x, |
| 3434 | key_data->len ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3435 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3436 | actual_status = psa_asymmetric_sign( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3437 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3438 | signature, signature_size, |
| 3439 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3440 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3441 | /* The value of *signature_length is unspecified on error, but |
| 3442 | * whatever it is, it should be less than signature_size, so that |
| 3443 | * if the caller tries to read *signature_length bytes without |
| 3444 | * checking the error code then they don't overflow a buffer. */ |
| 3445 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3446 | |
| 3447 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3448 | psa_destroy_key( handle ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3449 | mbedtls_free( signature ); |
| 3450 | mbedtls_psa_crypto_free( ); |
| 3451 | } |
| 3452 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3453 | |
| 3454 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3455 | void sign_verify( int key_type_arg, data_t *key_data, |
| 3456 | int alg_arg, data_t *input_data ) |
| 3457 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3458 | psa_key_handle_t handle = 0; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3459 | psa_key_type_t key_type = key_type_arg; |
| 3460 | psa_algorithm_t alg = alg_arg; |
| 3461 | size_t key_bits; |
| 3462 | unsigned char *signature = NULL; |
| 3463 | size_t signature_size; |
| 3464 | size_t signature_length = 0xdeadbeef; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3465 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3466 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3467 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3468 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3469 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3470 | psa_key_policy_set_usage( &policy, |
| 3471 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY, |
| 3472 | alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3473 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3474 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3475 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3476 | key_data->x, |
| 3477 | key_data->len ) ); |
| 3478 | PSA_ASSERT( psa_get_key_information( handle, |
| 3479 | NULL, |
| 3480 | &key_bits ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3481 | |
| 3482 | /* Allocate a buffer which has the size advertized by the |
| 3483 | * library. */ |
| 3484 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3485 | key_bits, alg ); |
| 3486 | TEST_ASSERT( signature_size != 0 ); |
| 3487 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3488 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3489 | |
| 3490 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3491 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3492 | input_data->x, input_data->len, |
| 3493 | signature, signature_size, |
| 3494 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3495 | /* Check that the signature length looks sensible. */ |
| 3496 | TEST_ASSERT( signature_length <= signature_size ); |
| 3497 | TEST_ASSERT( signature_length > 0 ); |
| 3498 | |
| 3499 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3500 | PSA_ASSERT( psa_asymmetric_verify( |
| 3501 | handle, alg, |
| 3502 | input_data->x, input_data->len, |
| 3503 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3504 | |
| 3505 | if( input_data->len != 0 ) |
| 3506 | { |
| 3507 | /* Flip a bit in the input and verify that the signature is now |
| 3508 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3509 | * because ECDSA may ignore the last few bits of the input. */ |
| 3510 | input_data->x[0] ^= 1; |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3511 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 3512 | input_data->x, input_data->len, |
| 3513 | signature, signature_length ), |
| 3514 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3518 | psa_destroy_key( handle ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3519 | mbedtls_free( signature ); |
| 3520 | mbedtls_psa_crypto_free( ); |
| 3521 | } |
| 3522 | /* END_CASE */ |
| 3523 | |
| 3524 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3525 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 3526 | int alg_arg, data_t *hash_data, |
| 3527 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3528 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3529 | psa_key_handle_t handle = 0; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3530 | psa_key_type_t key_type = key_type_arg; |
| 3531 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3532 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3533 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3534 | TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
| 3535 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3536 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3537 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3538 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3539 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3540 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3541 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3542 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3543 | key_data->x, |
| 3544 | key_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3545 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3546 | PSA_ASSERT( psa_asymmetric_verify( handle, alg, |
| 3547 | hash_data->x, hash_data->len, |
| 3548 | signature_data->x, |
| 3549 | signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3550 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3551 | psa_destroy_key( handle ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3552 | mbedtls_psa_crypto_free( ); |
| 3553 | } |
| 3554 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3555 | |
| 3556 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3557 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 3558 | int alg_arg, data_t *hash_data, |
| 3559 | data_t *signature_data, |
| 3560 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3561 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3562 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3563 | psa_key_type_t key_type = key_type_arg; |
| 3564 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3565 | psa_status_t actual_status; |
| 3566 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3567 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3568 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3569 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3570 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3571 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3572 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3573 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3574 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3575 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3576 | key_data->x, |
| 3577 | key_data->len ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3578 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3579 | actual_status = psa_asymmetric_verify( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3580 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3581 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3582 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3583 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3584 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3585 | |
| 3586 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3587 | psa_destroy_key( handle ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3588 | mbedtls_psa_crypto_free( ); |
| 3589 | } |
| 3590 | /* END_CASE */ |
| 3591 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3592 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3593 | void asymmetric_encrypt( int key_type_arg, |
| 3594 | data_t *key_data, |
| 3595 | int alg_arg, |
| 3596 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3597 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3598 | int expected_output_length_arg, |
| 3599 | int expected_status_arg ) |
| 3600 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3601 | psa_key_handle_t handle = 0; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3602 | psa_key_type_t key_type = key_type_arg; |
| 3603 | psa_algorithm_t alg = alg_arg; |
| 3604 | size_t expected_output_length = expected_output_length_arg; |
| 3605 | size_t key_bits; |
| 3606 | unsigned char *output = NULL; |
| 3607 | size_t output_size; |
| 3608 | size_t output_length = ~0; |
| 3609 | psa_status_t actual_status; |
| 3610 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3611 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3612 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3613 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3614 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3615 | /* Import the key */ |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3616 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3617 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3618 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
| 3619 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3620 | key_data->x, |
| 3621 | key_data->len ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3622 | |
| 3623 | /* Determine the maximum output length */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3624 | PSA_ASSERT( psa_get_key_information( handle, |
| 3625 | NULL, |
| 3626 | &key_bits ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3627 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3628 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3629 | |
| 3630 | /* Encrypt the input */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3631 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3632 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3633 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3634 | output, output_size, |
| 3635 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3636 | TEST_EQUAL( actual_status, expected_status ); |
| 3637 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3638 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3639 | /* If the label is empty, the test framework puts a non-null pointer |
| 3640 | * in label->x. Test that a null pointer works as well. */ |
| 3641 | if( label->len == 0 ) |
| 3642 | { |
| 3643 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3644 | if( output_size != 0 ) |
| 3645 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3646 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3647 | input_data->x, input_data->len, |
| 3648 | NULL, label->len, |
| 3649 | output, output_size, |
| 3650 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3651 | TEST_EQUAL( actual_status, expected_status ); |
| 3652 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3653 | } |
| 3654 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3655 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3656 | psa_destroy_key( handle ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3657 | mbedtls_free( output ); |
| 3658 | mbedtls_psa_crypto_free( ); |
| 3659 | } |
| 3660 | /* END_CASE */ |
| 3661 | |
| 3662 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3663 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 3664 | data_t *key_data, |
| 3665 | int alg_arg, |
| 3666 | data_t *input_data, |
| 3667 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3668 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3669 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3670 | psa_key_type_t key_type = key_type_arg; |
| 3671 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3672 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3673 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3674 | size_t output_size; |
| 3675 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3676 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3677 | size_t output2_size; |
| 3678 | size_t output2_length = ~0; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3679 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3680 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3681 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3682 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3683 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3684 | psa_key_policy_set_usage( &policy, |
| 3685 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3686 | alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3687 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3688 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3689 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3690 | key_data->x, |
| 3691 | key_data->len ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3692 | |
| 3693 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3694 | PSA_ASSERT( psa_get_key_information( handle, |
| 3695 | NULL, |
| 3696 | &key_bits ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3697 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3698 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3699 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3700 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3701 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 3702 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 3703 | * the original plaintext because of the non-optional random |
| 3704 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3705 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 3706 | input_data->x, input_data->len, |
| 3707 | label->x, label->len, |
| 3708 | output, output_size, |
| 3709 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3710 | /* We don't know what ciphertext length to expect, but check that |
| 3711 | * it looks sensible. */ |
| 3712 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3713 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3714 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 3715 | output, output_length, |
| 3716 | label->x, label->len, |
| 3717 | output2, output2_size, |
| 3718 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3719 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3720 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3721 | |
| 3722 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3723 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3724 | mbedtls_free( output ); |
| 3725 | mbedtls_free( output2 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3726 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3727 | } |
| 3728 | /* END_CASE */ |
| 3729 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3730 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3731 | void asymmetric_decrypt( int key_type_arg, |
| 3732 | data_t *key_data, |
| 3733 | int alg_arg, |
| 3734 | data_t *input_data, |
| 3735 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 3736 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3737 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3738 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3739 | psa_key_type_t key_type = key_type_arg; |
| 3740 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3741 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 3742 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3743 | size_t output_length = ~0; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3744 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3745 | |
Jaeden Amero | 412654a | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 3746 | output_size = expected_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3747 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3748 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3749 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3750 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3751 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3752 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3753 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3754 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3755 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3756 | key_data->x, |
| 3757 | key_data->len ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3758 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3759 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 3760 | input_data->x, input_data->len, |
| 3761 | label->x, label->len, |
| 3762 | output, |
| 3763 | output_size, |
| 3764 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3765 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3766 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3767 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3768 | /* If the label is empty, the test framework puts a non-null pointer |
| 3769 | * in label->x. Test that a null pointer works as well. */ |
| 3770 | if( label->len == 0 ) |
| 3771 | { |
| 3772 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3773 | if( output_size != 0 ) |
| 3774 | memset( output, 0, output_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3775 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 3776 | input_data->x, input_data->len, |
| 3777 | NULL, label->len, |
| 3778 | output, |
| 3779 | output_size, |
| 3780 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3781 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3782 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3783 | } |
| 3784 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3785 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3786 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3787 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3788 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3789 | } |
| 3790 | /* END_CASE */ |
| 3791 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3792 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3793 | void asymmetric_decrypt_fail( int key_type_arg, |
| 3794 | data_t *key_data, |
| 3795 | int alg_arg, |
| 3796 | data_t *input_data, |
| 3797 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 3798 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3799 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3800 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3801 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3802 | psa_key_type_t key_type = key_type_arg; |
| 3803 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3804 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 3805 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3806 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3807 | psa_status_t actual_status; |
| 3808 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3809 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3810 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3811 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3812 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3813 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3814 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3815 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3816 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3817 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3818 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3819 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3820 | key_data->x, |
| 3821 | key_data->len ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3822 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3823 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3824 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3825 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3826 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3827 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3828 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3829 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3830 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3831 | /* If the label is empty, the test framework puts a non-null pointer |
| 3832 | * in label->x. Test that a null pointer works as well. */ |
| 3833 | if( label->len == 0 ) |
| 3834 | { |
| 3835 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3836 | if( output_size != 0 ) |
| 3837 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3838 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3839 | input_data->x, input_data->len, |
| 3840 | NULL, label->len, |
| 3841 | output, output_size, |
| 3842 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3843 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3844 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3845 | } |
| 3846 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3847 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3848 | psa_destroy_key( handle ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3849 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3850 | mbedtls_psa_crypto_free( ); |
| 3851 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3852 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3853 | |
| 3854 | /* BEGIN_CASE */ |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 3855 | void crypto_generator_init( ) |
| 3856 | { |
| 3857 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3858 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3859 | * though it's OK by the C standard. We could test for this, but we'd need |
| 3860 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3861 | size_t capacity; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 3862 | psa_crypto_generator_t func = psa_crypto_generator_init( ); |
| 3863 | psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT; |
| 3864 | psa_crypto_generator_t zero; |
| 3865 | |
| 3866 | memset( &zero, 0, sizeof( zero ) ); |
| 3867 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3868 | /* A default generator should have no capacity. */ |
| 3869 | PSA_ASSERT( psa_get_generator_capacity( &func, &capacity ) ); |
| 3870 | TEST_EQUAL( capacity, 0 ); |
| 3871 | PSA_ASSERT( psa_get_generator_capacity( &init, &capacity ) ); |
| 3872 | TEST_EQUAL( capacity, 0 ); |
| 3873 | PSA_ASSERT( psa_get_generator_capacity( &zero, &capacity ) ); |
| 3874 | TEST_EQUAL( capacity, 0 ); |
| 3875 | |
| 3876 | /* A default generator should be abortable without error. */ |
| 3877 | PSA_ASSERT( psa_generator_abort(&func) ); |
| 3878 | PSA_ASSERT( psa_generator_abort(&init) ); |
| 3879 | PSA_ASSERT( psa_generator_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 3880 | } |
| 3881 | /* END_CASE */ |
| 3882 | |
| 3883 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3884 | void derive_setup( int key_type_arg, |
| 3885 | data_t *key_data, |
| 3886 | int alg_arg, |
| 3887 | data_t *salt, |
| 3888 | data_t *label, |
| 3889 | int requested_capacity_arg, |
| 3890 | int expected_status_arg ) |
| 3891 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3892 | psa_key_handle_t handle = 0; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3893 | size_t key_type = key_type_arg; |
| 3894 | psa_algorithm_t alg = alg_arg; |
| 3895 | size_t requested_capacity = requested_capacity_arg; |
| 3896 | psa_status_t expected_status = expected_status_arg; |
| 3897 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3898 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3899 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3900 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3901 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3902 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3903 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3904 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3905 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3906 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3907 | key_data->x, |
| 3908 | key_data->len ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3909 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3910 | TEST_EQUAL( psa_key_derivation( &generator, handle, alg, |
| 3911 | salt->x, salt->len, |
| 3912 | label->x, label->len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3913 | requested_capacity ), |
| 3914 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3915 | |
| 3916 | exit: |
| 3917 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3918 | psa_destroy_key( handle ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3919 | mbedtls_psa_crypto_free( ); |
| 3920 | } |
| 3921 | /* END_CASE */ |
| 3922 | |
| 3923 | /* BEGIN_CASE */ |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 3924 | void test_derive_invalid_generator_state( ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3925 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3926 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 3927 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3928 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3929 | psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3930 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3931 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 3932 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 3933 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 3934 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 3935 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3936 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3937 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3938 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 3939 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3940 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3941 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3942 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3943 | PSA_ASSERT( psa_import_key( handle, key_type, |
| 3944 | key_data, |
| 3945 | sizeof( key_data ) ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3946 | |
| 3947 | /* valid key derivation */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3948 | PSA_ASSERT( psa_key_derivation( &generator, handle, alg, |
| 3949 | NULL, 0, |
| 3950 | NULL, 0, |
| 3951 | capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3952 | |
| 3953 | /* state of generator shouldn't allow additional generation */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3954 | TEST_EQUAL( psa_key_derivation( &generator, handle, alg, |
| 3955 | NULL, 0, |
| 3956 | NULL, 0, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3957 | capacity ), |
| 3958 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3959 | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3960 | PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3961 | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3962 | TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ), |
| 3963 | PSA_ERROR_INSUFFICIENT_CAPACITY ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3964 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3965 | exit: |
| 3966 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3967 | psa_destroy_key( handle ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3968 | mbedtls_psa_crypto_free( ); |
| 3969 | } |
| 3970 | /* END_CASE */ |
| 3971 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3972 | /* BEGIN_CASE */ |
| 3973 | void test_derive_invalid_generator_tests( ) |
| 3974 | { |
| 3975 | uint8_t output_buffer[16]; |
| 3976 | size_t buffer_size = 16; |
| 3977 | size_t capacity = 0; |
| 3978 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3979 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3980 | TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3981 | == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3982 | |
| 3983 | TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3984 | == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3985 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3986 | PSA_ASSERT( psa_generator_abort( &generator ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3987 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3988 | TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3989 | == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3990 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3991 | TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3992 | == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3993 | |
| 3994 | exit: |
| 3995 | psa_generator_abort( &generator ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3996 | } |
| 3997 | /* END_CASE */ |
| 3998 | |
| 3999 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4000 | void derive_output( int alg_arg, |
| 4001 | data_t *key_data, |
| 4002 | data_t *salt, |
| 4003 | data_t *label, |
| 4004 | int requested_capacity_arg, |
| 4005 | data_t *expected_output1, |
| 4006 | data_t *expected_output2 ) |
| 4007 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4008 | psa_key_handle_t handle = 0; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4009 | psa_algorithm_t alg = alg_arg; |
| 4010 | size_t requested_capacity = requested_capacity_arg; |
| 4011 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 4012 | uint8_t *expected_outputs[2] = |
| 4013 | {expected_output1->x, expected_output2->x}; |
| 4014 | size_t output_sizes[2] = |
| 4015 | {expected_output1->len, expected_output2->len}; |
| 4016 | size_t output_buffer_size = 0; |
| 4017 | uint8_t *output_buffer = NULL; |
| 4018 | size_t expected_capacity; |
| 4019 | size_t current_capacity; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 4020 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4021 | psa_status_t status; |
| 4022 | unsigned i; |
| 4023 | |
| 4024 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4025 | { |
| 4026 | if( output_sizes[i] > output_buffer_size ) |
| 4027 | output_buffer_size = output_sizes[i]; |
| 4028 | if( output_sizes[i] == 0 ) |
| 4029 | expected_outputs[i] = NULL; |
| 4030 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4031 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4032 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4033 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4034 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4035 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4036 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4037 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4038 | PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE, |
| 4039 | key_data->x, |
| 4040 | key_data->len ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4041 | |
| 4042 | /* Extraction phase. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4043 | PSA_ASSERT( psa_key_derivation( &generator, handle, alg, |
| 4044 | salt->x, salt->len, |
| 4045 | label->x, label->len, |
| 4046 | requested_capacity ) ); |
| 4047 | PSA_ASSERT( psa_get_generator_capacity( &generator, |
| 4048 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4049 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4050 | expected_capacity = requested_capacity; |
| 4051 | |
| 4052 | /* Expansion phase. */ |
| 4053 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4054 | { |
| 4055 | /* Read some bytes. */ |
| 4056 | status = psa_generator_read( &generator, |
| 4057 | output_buffer, output_sizes[i] ); |
| 4058 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4059 | { |
| 4060 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4061 | TEST_ASSERT( status == PSA_SUCCESS || |
| 4062 | status == PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 4063 | continue; |
| 4064 | } |
| 4065 | else if( expected_capacity == 0 || |
| 4066 | output_sizes[i] > expected_capacity ) |
| 4067 | { |
| 4068 | /* Capacity exceeded. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4069 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4070 | expected_capacity = 0; |
| 4071 | continue; |
| 4072 | } |
| 4073 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4074 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4075 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4076 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4077 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4078 | /* Check the generator status. */ |
| 4079 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4080 | PSA_ASSERT( psa_get_generator_capacity( &generator, |
| 4081 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4082 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4083 | } |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4084 | PSA_ASSERT( psa_generator_abort( &generator ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4085 | |
| 4086 | exit: |
| 4087 | mbedtls_free( output_buffer ); |
| 4088 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4089 | psa_destroy_key( handle ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4090 | mbedtls_psa_crypto_free( ); |
| 4091 | } |
| 4092 | /* END_CASE */ |
| 4093 | |
| 4094 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4095 | void derive_full( int alg_arg, |
| 4096 | data_t *key_data, |
| 4097 | data_t *salt, |
| 4098 | data_t *label, |
| 4099 | int requested_capacity_arg ) |
| 4100 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4101 | psa_key_handle_t handle = 0; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4102 | psa_algorithm_t alg = alg_arg; |
| 4103 | size_t requested_capacity = requested_capacity_arg; |
| 4104 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 4105 | unsigned char output_buffer[16]; |
| 4106 | size_t expected_capacity = requested_capacity; |
| 4107 | size_t current_capacity; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 4108 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4109 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4110 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4111 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4112 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4113 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4114 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4115 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4116 | PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE, |
| 4117 | key_data->x, |
| 4118 | key_data->len ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4119 | |
| 4120 | /* Extraction phase. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4121 | PSA_ASSERT( psa_key_derivation( &generator, handle, alg, |
| 4122 | salt->x, salt->len, |
| 4123 | label->x, label->len, |
| 4124 | requested_capacity ) ); |
| 4125 | PSA_ASSERT( psa_get_generator_capacity( &generator, |
| 4126 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4127 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4128 | |
| 4129 | /* Expansion phase. */ |
| 4130 | while( current_capacity > 0 ) |
| 4131 | { |
| 4132 | size_t read_size = sizeof( output_buffer ); |
| 4133 | if( read_size > current_capacity ) |
| 4134 | read_size = current_capacity; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4135 | PSA_ASSERT( psa_generator_read( &generator, |
| 4136 | output_buffer, |
| 4137 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4138 | expected_capacity -= read_size; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4139 | PSA_ASSERT( psa_get_generator_capacity( &generator, |
| 4140 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4141 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | /* Check that the generator refuses to go over capacity. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4145 | TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ), |
| 4146 | PSA_ERROR_INSUFFICIENT_CAPACITY ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4147 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4148 | PSA_ASSERT( psa_generator_abort( &generator ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4149 | |
| 4150 | exit: |
| 4151 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4152 | psa_destroy_key( handle ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4153 | mbedtls_psa_crypto_free( ); |
| 4154 | } |
| 4155 | /* END_CASE */ |
| 4156 | |
| 4157 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4158 | void derive_key_exercise( int alg_arg, |
| 4159 | data_t *key_data, |
| 4160 | data_t *salt, |
| 4161 | data_t *label, |
| 4162 | int derived_type_arg, |
| 4163 | int derived_bits_arg, |
| 4164 | int derived_usage_arg, |
| 4165 | int derived_alg_arg ) |
| 4166 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4167 | psa_key_handle_t base_handle = 0; |
| 4168 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4169 | psa_algorithm_t alg = alg_arg; |
| 4170 | psa_key_type_t derived_type = derived_type_arg; |
| 4171 | size_t derived_bits = derived_bits_arg; |
| 4172 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4173 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4174 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
| 4175 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 4176 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4177 | psa_key_type_t got_type; |
| 4178 | size_t got_bits; |
| 4179 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4180 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4181 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4182 | PSA_ASSERT( psa_allocate_key( &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4183 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4184 | PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) ); |
| 4185 | PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE, |
| 4186 | key_data->x, |
| 4187 | key_data->len ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4188 | |
| 4189 | /* Derive a key. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4190 | PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg, |
| 4191 | salt->x, salt->len, |
| 4192 | label->x, label->len, |
| 4193 | capacity ) ); |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4194 | PSA_ASSERT( psa_allocate_key( &derived_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4195 | psa_key_policy_set_usage( &policy, derived_usage, derived_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4196 | PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) ); |
| 4197 | PSA_ASSERT( psa_generator_import_key( derived_handle, |
| 4198 | derived_type, |
| 4199 | derived_bits, |
| 4200 | &generator ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4201 | |
| 4202 | /* Test the key information */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4203 | PSA_ASSERT( psa_get_key_information( derived_handle, |
| 4204 | &got_type, |
| 4205 | &got_bits ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4206 | TEST_EQUAL( got_type, derived_type ); |
| 4207 | TEST_EQUAL( got_bits, derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4208 | |
| 4209 | /* Exercise the derived key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4210 | if( ! exercise_key( derived_handle, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4211 | goto exit; |
| 4212 | |
| 4213 | exit: |
| 4214 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4215 | psa_destroy_key( base_handle ); |
| 4216 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4217 | mbedtls_psa_crypto_free( ); |
| 4218 | } |
| 4219 | /* END_CASE */ |
| 4220 | |
| 4221 | /* BEGIN_CASE */ |
| 4222 | void derive_key_export( int alg_arg, |
| 4223 | data_t *key_data, |
| 4224 | data_t *salt, |
| 4225 | data_t *label, |
| 4226 | int bytes1_arg, |
| 4227 | int bytes2_arg ) |
| 4228 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4229 | psa_key_handle_t base_handle = 0; |
| 4230 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4231 | psa_algorithm_t alg = alg_arg; |
| 4232 | size_t bytes1 = bytes1_arg; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4233 | size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4234 | size_t bytes2 = bytes2_arg; |
| 4235 | size_t capacity = bytes1 + bytes2; |
| 4236 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4237 | uint8_t *output_buffer = NULL; |
| 4238 | uint8_t *export_buffer = NULL; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 4239 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4240 | size_t length; |
| 4241 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4242 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4243 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4244 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4245 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4246 | PSA_ASSERT( psa_allocate_key( &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4247 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4248 | PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) ); |
| 4249 | PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE, |
| 4250 | key_data->x, |
| 4251 | key_data->len ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4252 | |
| 4253 | /* Derive some material and output it. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4254 | PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg, |
| 4255 | salt->x, salt->len, |
| 4256 | label->x, label->len, |
| 4257 | capacity ) ); |
| 4258 | PSA_ASSERT( psa_generator_read( &generator, |
| 4259 | output_buffer, |
| 4260 | capacity ) ); |
| 4261 | PSA_ASSERT( psa_generator_abort( &generator ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4262 | |
| 4263 | /* Derive the same output again, but this time store it in key objects. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4264 | PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg, |
| 4265 | salt->x, salt->len, |
| 4266 | label->x, label->len, |
| 4267 | capacity ) ); |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4268 | PSA_ASSERT( psa_allocate_key( &derived_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4269 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4270 | PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) ); |
| 4271 | PSA_ASSERT( psa_generator_import_key( derived_handle, |
| 4272 | PSA_KEY_TYPE_RAW_DATA, |
| 4273 | derived_bits, |
| 4274 | &generator ) ); |
| 4275 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4276 | export_buffer, bytes1, |
| 4277 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4278 | TEST_EQUAL( length, bytes1 ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4279 | PSA_ASSERT( psa_destroy_key( derived_handle ) ); |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4280 | PSA_ASSERT( psa_allocate_key( &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4281 | PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) ); |
| 4282 | PSA_ASSERT( psa_generator_import_key( derived_handle, |
| 4283 | PSA_KEY_TYPE_RAW_DATA, |
| 4284 | PSA_BYTES_TO_BITS( bytes2 ), |
| 4285 | &generator ) ); |
| 4286 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4287 | export_buffer + bytes1, bytes2, |
| 4288 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4289 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4290 | |
| 4291 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4292 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 4293 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4294 | |
| 4295 | exit: |
| 4296 | mbedtls_free( output_buffer ); |
| 4297 | mbedtls_free( export_buffer ); |
| 4298 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4299 | psa_destroy_key( base_handle ); |
| 4300 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4301 | mbedtls_psa_crypto_free( ); |
| 4302 | } |
| 4303 | /* END_CASE */ |
| 4304 | |
| 4305 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4306 | void key_agreement_setup( int alg_arg, |
| 4307 | int our_key_type_arg, data_t *our_key_data, |
| 4308 | data_t *peer_key_data, |
| 4309 | int expected_status_arg ) |
| 4310 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4311 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4312 | psa_algorithm_t alg = alg_arg; |
| 4313 | psa_key_type_t our_key_type = our_key_type_arg; |
| 4314 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 4315 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4316 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4317 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4318 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4319 | PSA_ASSERT( psa_allocate_key( &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4320 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4321 | PSA_ASSERT( psa_set_key_policy( our_key, &policy ) ); |
| 4322 | PSA_ASSERT( psa_import_key( our_key, our_key_type, |
| 4323 | our_key_data->x, |
| 4324 | our_key_data->len ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4325 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4326 | TEST_EQUAL( psa_key_agreement( &generator, |
| 4327 | our_key, |
| 4328 | peer_key_data->x, peer_key_data->len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4329 | alg ), |
| 4330 | expected_status_arg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4331 | |
| 4332 | exit: |
| 4333 | psa_generator_abort( &generator ); |
| 4334 | psa_destroy_key( our_key ); |
| 4335 | mbedtls_psa_crypto_free( ); |
| 4336 | } |
| 4337 | /* END_CASE */ |
| 4338 | |
| 4339 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4340 | void key_agreement_capacity( int alg_arg, |
| 4341 | int our_key_type_arg, data_t *our_key_data, |
| 4342 | data_t *peer_key_data, |
| 4343 | int expected_capacity_arg ) |
| 4344 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4345 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4346 | psa_algorithm_t alg = alg_arg; |
| 4347 | psa_key_type_t our_key_type = our_key_type_arg; |
| 4348 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 4349 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4350 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4351 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4352 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4353 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4354 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4355 | PSA_ASSERT( psa_allocate_key( &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4356 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4357 | PSA_ASSERT( psa_set_key_policy( our_key, &policy ) ); |
| 4358 | PSA_ASSERT( psa_import_key( our_key, our_key_type, |
| 4359 | our_key_data->x, |
| 4360 | our_key_data->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4361 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4362 | PSA_ASSERT( psa_key_agreement( &generator, |
| 4363 | our_key, |
| 4364 | peer_key_data->x, peer_key_data->len, |
| 4365 | alg ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4366 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4367 | /* Test the advertized capacity. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4368 | PSA_ASSERT( psa_get_generator_capacity( |
| 4369 | &generator, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4370 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4371 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4372 | /* Test the actual capacity by reading the output. */ |
| 4373 | while( actual_capacity > sizeof( output ) ) |
| 4374 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4375 | PSA_ASSERT( psa_generator_read( &generator, |
| 4376 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4377 | actual_capacity -= sizeof( output ); |
| 4378 | } |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4379 | PSA_ASSERT( psa_generator_read( &generator, |
| 4380 | output, actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4381 | TEST_EQUAL( psa_generator_read( &generator, output, 1 ), |
| 4382 | PSA_ERROR_INSUFFICIENT_CAPACITY ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4383 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4384 | exit: |
| 4385 | psa_generator_abort( &generator ); |
| 4386 | psa_destroy_key( our_key ); |
| 4387 | mbedtls_psa_crypto_free( ); |
| 4388 | } |
| 4389 | /* END_CASE */ |
| 4390 | |
| 4391 | /* BEGIN_CASE */ |
| 4392 | void key_agreement_output( int alg_arg, |
| 4393 | int our_key_type_arg, data_t *our_key_data, |
| 4394 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4395 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4396 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4397 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4398 | psa_algorithm_t alg = alg_arg; |
| 4399 | psa_key_type_t our_key_type = our_key_type_arg; |
| 4400 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 4401 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4402 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4403 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4404 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 4405 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4406 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4407 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4408 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4409 | PSA_ASSERT( psa_allocate_key( &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4410 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4411 | PSA_ASSERT( psa_set_key_policy( our_key, &policy ) ); |
| 4412 | PSA_ASSERT( psa_import_key( our_key, our_key_type, |
| 4413 | our_key_data->x, |
| 4414 | our_key_data->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4415 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4416 | PSA_ASSERT( psa_key_agreement( &generator, |
| 4417 | our_key, |
| 4418 | peer_key_data->x, peer_key_data->len, |
| 4419 | alg ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4420 | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4421 | PSA_ASSERT( psa_generator_read( &generator, |
| 4422 | actual_output, |
| 4423 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4424 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 4425 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4426 | if( expected_output2->len != 0 ) |
| 4427 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4428 | PSA_ASSERT( psa_generator_read( &generator, |
| 4429 | actual_output, |
| 4430 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4431 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 4432 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4433 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4434 | |
| 4435 | exit: |
| 4436 | psa_generator_abort( &generator ); |
| 4437 | psa_destroy_key( our_key ); |
| 4438 | mbedtls_psa_crypto_free( ); |
| 4439 | mbedtls_free( actual_output ); |
| 4440 | } |
| 4441 | /* END_CASE */ |
| 4442 | |
| 4443 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4444 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4445 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4446 | size_t bytes = bytes_arg; |
| 4447 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4448 | unsigned char *output = NULL; |
| 4449 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4450 | size_t i; |
| 4451 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4452 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4453 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 4454 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4455 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4456 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4457 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4458 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4459 | /* Run several times, to ensure that every output byte will be |
| 4460 | * nonzero at least once with overwhelming probability |
| 4461 | * (2^(-8*number_of_runs)). */ |
| 4462 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4463 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4464 | if( bytes != 0 ) |
| 4465 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4466 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4467 | |
| 4468 | /* Check that no more than bytes have been overwritten */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4469 | ASSERT_COMPARE( output + bytes, sizeof( trail ), |
| 4470 | trail, sizeof( trail ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4471 | |
| 4472 | for( i = 0; i < bytes; i++ ) |
| 4473 | { |
| 4474 | if( output[i] != 0 ) |
| 4475 | ++changed[i]; |
| 4476 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4477 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4478 | |
| 4479 | /* Check that every byte was changed to nonzero at least once. This |
| 4480 | * validates that psa_generate_random is overwriting every byte of |
| 4481 | * the output buffer. */ |
| 4482 | for( i = 0; i < bytes; i++ ) |
| 4483 | { |
| 4484 | TEST_ASSERT( changed[i] != 0 ); |
| 4485 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4486 | |
| 4487 | exit: |
| 4488 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4489 | mbedtls_free( output ); |
| 4490 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4491 | } |
| 4492 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4493 | |
| 4494 | /* BEGIN_CASE */ |
| 4495 | void generate_key( int type_arg, |
| 4496 | int bits_arg, |
| 4497 | int usage_arg, |
| 4498 | int alg_arg, |
| 4499 | int expected_status_arg ) |
| 4500 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4501 | psa_key_handle_t handle = 0; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4502 | psa_key_type_t type = type_arg; |
| 4503 | psa_key_usage_t usage = usage_arg; |
| 4504 | size_t bits = bits_arg; |
| 4505 | psa_algorithm_t alg = alg_arg; |
| 4506 | psa_status_t expected_status = expected_status_arg; |
| 4507 | psa_key_type_t got_type; |
| 4508 | size_t got_bits; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4509 | psa_status_t expected_info_status = |
| 4510 | expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 4511 | psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4512 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4513 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4514 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4515 | PSA_ASSERT( psa_allocate_key( &handle ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4516 | psa_key_policy_set_usage( &policy, usage, alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4517 | PSA_ASSERT( psa_set_key_policy( handle, &policy ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4518 | |
| 4519 | /* Generate a key */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4520 | TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ), |
| 4521 | expected_status ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4522 | |
| 4523 | /* Test the key information */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4524 | TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ), |
| 4525 | expected_info_status ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4526 | if( expected_info_status != PSA_SUCCESS ) |
| 4527 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4528 | TEST_EQUAL( got_type, type ); |
| 4529 | TEST_EQUAL( got_bits, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4530 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 4531 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4532 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 4533 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4534 | |
| 4535 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4536 | psa_destroy_key( handle ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4537 | mbedtls_psa_crypto_free( ); |
| 4538 | } |
| 4539 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 4540 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4541 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 4542 | void persistent_key_load_key_from_storage( data_t *data, int type_arg, |
| 4543 | int bits, int usage_arg, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4544 | int alg_arg, int generation_method, |
| 4545 | int export_status ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4546 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4547 | psa_key_handle_t handle = 0; |
| 4548 | psa_key_handle_t base_key; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4549 | psa_key_type_t type = (psa_key_type_t) type_arg; |
| 4550 | psa_key_type_t type_get; |
| 4551 | size_t bits_get; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 4552 | psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT; |
| 4553 | psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4554 | psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg; |
| 4555 | psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 4556 | psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4557 | psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 4558 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4559 | unsigned char *first_export = NULL; |
| 4560 | unsigned char *second_export = NULL; |
| 4561 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 4562 | size_t first_exported_length; |
| 4563 | size_t second_exported_length; |
| 4564 | |
| 4565 | ASSERT_ALLOC( first_export, export_size ); |
| 4566 | ASSERT_ALLOC( second_export, export_size ); |
| 4567 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4568 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4569 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4570 | PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4571 | &handle ) ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4572 | psa_key_policy_set_usage( &policy_set, policy_usage, |
| 4573 | policy_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4574 | PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4575 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4576 | switch( generation_method ) |
| 4577 | { |
| 4578 | case IMPORT_KEY: |
| 4579 | /* Import the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4580 | PSA_ASSERT( psa_import_key( handle, type, |
| 4581 | data->x, data->len ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4582 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4583 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4584 | case GENERATE_KEY: |
| 4585 | /* Generate a key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4586 | PSA_ASSERT( psa_generate_key( handle, type, bits, |
| 4587 | NULL, 0 ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4588 | break; |
| 4589 | |
| 4590 | case DERIVE_KEY: |
| 4591 | /* Create base key */ |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 4592 | PSA_ASSERT( psa_allocate_key( &base_key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4593 | psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE, |
| 4594 | base_policy_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4595 | PSA_ASSERT( psa_set_key_policy( |
| 4596 | base_key, &base_policy_set ) ); |
| 4597 | PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE, |
| 4598 | data->x, data->len ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4599 | /* Derive a key. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4600 | PSA_ASSERT( psa_key_derivation( &generator, base_key, |
| 4601 | base_policy_alg, |
| 4602 | NULL, 0, NULL, 0, |
| 4603 | export_size ) ); |
| 4604 | PSA_ASSERT( psa_generator_import_key( |
| 4605 | handle, PSA_KEY_TYPE_RAW_DATA, |
| 4606 | bits, &generator ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4607 | break; |
| 4608 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4609 | |
| 4610 | /* Export the key */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4611 | TEST_EQUAL( psa_export_key( handle, |
| 4612 | first_export, export_size, |
| 4613 | &first_exported_length ), |
| 4614 | export_status ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4615 | |
| 4616 | /* Shutdown and restart */ |
| 4617 | mbedtls_psa_crypto_free(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4618 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4619 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4620 | /* Check key slot still contains key data */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4621 | PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1, |
| 4622 | &handle ) ); |
| 4623 | PSA_ASSERT( psa_get_key_information( |
| 4624 | handle, &type_get, &bits_get ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4625 | TEST_EQUAL( type_get, type ); |
| 4626 | TEST_EQUAL( bits_get, (size_t) bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4627 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4628 | PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4629 | TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage ); |
| 4630 | TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4631 | |
| 4632 | /* Export the key again */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4633 | TEST_EQUAL( psa_export_key( handle, |
| 4634 | second_export, export_size, |
| 4635 | &second_exported_length ), |
| 4636 | export_status ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4637 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4638 | if( export_status == PSA_SUCCESS ) |
| 4639 | { |
| 4640 | ASSERT_COMPARE( first_export, first_exported_length, |
| 4641 | second_export, second_exported_length ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4642 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4643 | switch( generation_method ) |
| 4644 | { |
| 4645 | case IMPORT_KEY: |
| 4646 | ASSERT_COMPARE( data->x, data->len, |
| 4647 | first_export, first_exported_length ); |
| 4648 | break; |
| 4649 | default: |
| 4650 | break; |
| 4651 | } |
| 4652 | } |
| 4653 | |
| 4654 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4655 | if( ! exercise_key( handle, policy_usage, policy_alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4656 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4657 | |
| 4658 | exit: |
| 4659 | mbedtls_free( first_export ); |
| 4660 | mbedtls_free( second_export ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4661 | psa_destroy_key( handle ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4662 | mbedtls_psa_crypto_free(); |
| 4663 | } |
| 4664 | /* END_CASE */ |