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