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