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