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 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
| 7 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 8 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 9 | * uses mbedtls_ctr_drbg internally. */ |
| 10 | #include "mbedtls/ctr_drbg.h" |
| 11 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 12 | #include "psa_crypto_helpers.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 13 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 14 | /* Tests that require more than 128kB of RAM plus change have this symbol |
| 15 | * as a dependency. Currently we always define this symbol, so the tests |
| 16 | * are always executed. In the future we should make this conditional |
| 17 | * so that tests that require a lot of memory are skipped on constrained |
| 18 | * platforms. */ |
Gilles Peskine | 49232e8 | 2019-08-07 11:01:30 +0200 | [diff] [blame] | 19 | #define HAVE_RAM_AVAILABLE_128K |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 20 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 21 | #include "psa/crypto.h" |
| 22 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 23 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 24 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 25 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 26 | /* A hash algorithm that is known to be supported. |
| 27 | * |
| 28 | * This is used in some smoke tests. |
| 29 | */ |
| 30 | #if defined(MBEDTLS_MD2_C) |
| 31 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2 |
| 32 | #elif defined(MBEDTLS_MD4_C) |
| 33 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4 |
| 34 | #elif defined(MBEDTLS_MD5_C) |
| 35 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5 |
| 36 | /* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of |
| 37 | * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160 |
| 38 | * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be |
| 39 | * implausible anyway. */ |
| 40 | #elif defined(MBEDTLS_SHA1_C) |
| 41 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1 |
| 42 | #elif defined(MBEDTLS_SHA256_C) |
| 43 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256 |
| 44 | #elif defined(MBEDTLS_SHA512_C) |
| 45 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384 |
| 46 | #elif defined(MBEDTLS_SHA3_C) |
| 47 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256 |
| 48 | #else |
| 49 | #undef KNOWN_SUPPORTED_HASH_ALG |
| 50 | #endif |
| 51 | |
| 52 | /* A block cipher that is known to be supported. |
| 53 | * |
| 54 | * For simplicity's sake, stick to block ciphers with 16-byte blocks. |
| 55 | */ |
| 56 | #if defined(MBEDTLS_AES_C) |
| 57 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES |
| 58 | #elif defined(MBEDTLS_ARIA_C) |
| 59 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA |
| 60 | #elif defined(MBEDTLS_CAMELLIA_C) |
| 61 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA |
| 62 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER |
| 63 | #endif |
| 64 | |
| 65 | /* A MAC mode that is known to be supported. |
| 66 | * |
| 67 | * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or |
| 68 | * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER. |
| 69 | * |
| 70 | * This is used in some smoke tests. |
| 71 | */ |
| 72 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 73 | #define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) ) |
| 74 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC |
| 75 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C) |
| 76 | #define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC |
| 77 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 78 | #else |
| 79 | #undef KNOWN_SUPPORTED_MAC_ALG |
| 80 | #undef KNOWN_SUPPORTED_MAC_KEY_TYPE |
| 81 | #endif |
| 82 | |
| 83 | /* A cipher algorithm and key type that are known to be supported. |
| 84 | * |
| 85 | * This is used in some smoke tests. |
| 86 | */ |
| 87 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR) |
| 88 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR |
| 89 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 90 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING |
| 91 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB) |
| 92 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB |
| 93 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB) |
| 94 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB |
| 95 | #else |
| 96 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 97 | #endif |
| 98 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG) |
| 99 | #define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 100 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 101 | #elif defined(MBEDTLS_RC4_C) |
| 102 | #define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4 |
| 103 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4 |
| 104 | #else |
| 105 | #undef KNOWN_SUPPORTED_CIPHER_ALG |
| 106 | #undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE |
| 107 | #endif |
| 108 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 109 | /** Test if a buffer contains a constant byte value. |
| 110 | * |
| 111 | * `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] | 112 | * |
| 113 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 114 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 115 | * \param size Size of the buffer in bytes. |
| 116 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 117 | * \return 1 if the buffer is all-bits-zero. |
| 118 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 119 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 120 | 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] | 121 | { |
| 122 | size_t i; |
| 123 | for( i = 0; i < size; i++ ) |
| 124 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 125 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 126 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 127 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 128 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 129 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 130 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 131 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 132 | static int asn1_write_10x( unsigned char **p, |
| 133 | unsigned char *start, |
| 134 | size_t bits, |
| 135 | unsigned char x ) |
| 136 | { |
| 137 | int ret; |
| 138 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 139 | if( bits == 0 ) |
| 140 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 141 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 142 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 143 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 144 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 145 | *p -= len; |
| 146 | ( *p )[len-1] = x; |
| 147 | if( bits % 8 == 0 ) |
| 148 | ( *p )[1] |= 1; |
| 149 | else |
| 150 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 151 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 152 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 153 | MBEDTLS_ASN1_INTEGER ) ); |
| 154 | return( len ); |
| 155 | } |
| 156 | |
| 157 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 158 | size_t buffer_size, |
| 159 | unsigned char **p, |
| 160 | size_t bits, |
| 161 | int keypair ) |
| 162 | { |
| 163 | size_t half_bits = ( bits + 1 ) / 2; |
| 164 | int ret; |
| 165 | int len = 0; |
| 166 | /* Construct something that looks like a DER encoding of |
| 167 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 168 | * RSAPrivateKey ::= SEQUENCE { |
| 169 | * version Version, |
| 170 | * modulus INTEGER, -- n |
| 171 | * publicExponent INTEGER, -- e |
| 172 | * privateExponent INTEGER, -- d |
| 173 | * prime1 INTEGER, -- p |
| 174 | * prime2 INTEGER, -- q |
| 175 | * exponent1 INTEGER, -- d mod (p-1) |
| 176 | * exponent2 INTEGER, -- d mod (q-1) |
| 177 | * coefficient INTEGER, -- (inverse of q) mod p |
| 178 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 179 | * } |
| 180 | * Or, for a public key, the same structure with only |
| 181 | * version, modulus and publicExponent. |
| 182 | */ |
| 183 | *p = buffer + buffer_size; |
| 184 | if( keypair ) |
| 185 | { |
| 186 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 187 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 188 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 189 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 190 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 191 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 192 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 193 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 194 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 195 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 196 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 197 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 198 | } |
| 199 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 200 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 201 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 202 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 203 | if( keypair ) |
| 204 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 205 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 206 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 207 | { |
| 208 | const unsigned char tag = |
| 209 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 210 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 211 | } |
| 212 | return( len ); |
| 213 | } |
| 214 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 215 | int exercise_mac_setup( psa_key_type_t key_type, |
| 216 | const unsigned char *key_bytes, |
| 217 | size_t key_length, |
| 218 | psa_algorithm_t alg, |
| 219 | psa_mac_operation_t *operation, |
| 220 | psa_status_t *status ) |
| 221 | { |
| 222 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 223 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 224 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 225 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 226 | psa_set_key_algorithm( &attributes, alg ); |
| 227 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 228 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 229 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 230 | |
| 231 | *status = psa_mac_sign_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 232 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 233 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 234 | /* If setup failed, reproduce the failure, so that the caller can |
| 235 | * test the resulting state of the operation object. */ |
| 236 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 237 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 238 | TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ), |
| 239 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | psa_destroy_key( handle ); |
| 243 | return( 1 ); |
| 244 | |
| 245 | exit: |
| 246 | psa_destroy_key( handle ); |
| 247 | return( 0 ); |
| 248 | } |
| 249 | |
| 250 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 251 | const unsigned char *key_bytes, |
| 252 | size_t key_length, |
| 253 | psa_algorithm_t alg, |
| 254 | psa_cipher_operation_t *operation, |
| 255 | psa_status_t *status ) |
| 256 | { |
| 257 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 258 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 259 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 260 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 261 | psa_set_key_algorithm( &attributes, alg ); |
| 262 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 263 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 264 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 265 | |
| 266 | *status = psa_cipher_encrypt_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 267 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 268 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 269 | /* If setup failed, reproduce the failure, so that the caller can |
| 270 | * test the resulting state of the operation object. */ |
| 271 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 272 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 273 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ), |
| 274 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | psa_destroy_key( handle ); |
| 278 | return( 1 ); |
| 279 | |
| 280 | exit: |
| 281 | psa_destroy_key( handle ); |
| 282 | return( 0 ); |
| 283 | } |
| 284 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 285 | static int exercise_mac_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 286 | psa_key_usage_t usage, |
| 287 | psa_algorithm_t alg ) |
| 288 | { |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 289 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 290 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 291 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 292 | size_t mac_length = sizeof( mac ); |
| 293 | |
| 294 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 295 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 296 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 297 | handle, alg ) ); |
| 298 | PSA_ASSERT( psa_mac_update( &operation, |
| 299 | input, sizeof( input ) ) ); |
| 300 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 301 | mac, sizeof( mac ), |
| 302 | &mac_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 306 | { |
| 307 | psa_status_t verify_status = |
| 308 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 309 | PSA_SUCCESS : |
| 310 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 311 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 312 | handle, alg ) ); |
| 313 | PSA_ASSERT( psa_mac_update( &operation, |
| 314 | input, sizeof( input ) ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 315 | TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ), |
| 316 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | return( 1 ); |
| 320 | |
| 321 | exit: |
| 322 | psa_mac_abort( &operation ); |
| 323 | return( 0 ); |
| 324 | } |
| 325 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 326 | static int exercise_cipher_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 327 | psa_key_usage_t usage, |
| 328 | psa_algorithm_t alg ) |
| 329 | { |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 330 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 331 | unsigned char iv[16] = {0}; |
| 332 | size_t iv_length = sizeof( iv ); |
| 333 | const unsigned char plaintext[16] = "Hello, world..."; |
| 334 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 335 | size_t ciphertext_length = sizeof( ciphertext ); |
| 336 | unsigned char decrypted[sizeof( ciphertext )]; |
| 337 | size_t part_length; |
| 338 | |
| 339 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 340 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 341 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 342 | handle, alg ) ); |
| 343 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 344 | iv, sizeof( iv ), |
| 345 | &iv_length ) ); |
| 346 | PSA_ASSERT( psa_cipher_update( &operation, |
| 347 | plaintext, sizeof( plaintext ), |
| 348 | ciphertext, sizeof( ciphertext ), |
| 349 | &ciphertext_length ) ); |
| 350 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 351 | ciphertext + ciphertext_length, |
| 352 | sizeof( ciphertext ) - ciphertext_length, |
| 353 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 354 | ciphertext_length += part_length; |
| 355 | } |
| 356 | |
| 357 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 358 | { |
| 359 | psa_status_t status; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 360 | int maybe_invalid_padding = 0; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 361 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 362 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 363 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 364 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 365 | /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't |
| 366 | * have this macro yet. */ |
| 367 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( |
| 368 | psa_get_key_type( &attributes ) ); |
| 369 | maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 370 | } |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 371 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 372 | handle, alg ) ); |
| 373 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 374 | iv, iv_length ) ); |
| 375 | PSA_ASSERT( psa_cipher_update( &operation, |
| 376 | ciphertext, ciphertext_length, |
| 377 | decrypted, sizeof( decrypted ), |
| 378 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 379 | status = psa_cipher_finish( &operation, |
| 380 | decrypted + part_length, |
| 381 | sizeof( decrypted ) - part_length, |
| 382 | &part_length ); |
| 383 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 384 | * if the input is some aribtrary data rather than an actual |
| 385 | ciphertext, a padding error is likely. */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 386 | if( maybe_invalid_padding ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 387 | TEST_ASSERT( status == PSA_SUCCESS || |
| 388 | status == PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 389 | else |
| 390 | PSA_ASSERT( status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return( 1 ); |
| 394 | |
| 395 | exit: |
| 396 | psa_cipher_abort( &operation ); |
| 397 | return( 0 ); |
| 398 | } |
| 399 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 400 | static int exercise_aead_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 401 | psa_key_usage_t usage, |
| 402 | psa_algorithm_t alg ) |
| 403 | { |
| 404 | unsigned char nonce[16] = {0}; |
| 405 | size_t nonce_length = sizeof( nonce ); |
| 406 | unsigned char plaintext[16] = "Hello, world..."; |
| 407 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 408 | size_t ciphertext_length = sizeof( ciphertext ); |
| 409 | size_t plaintext_length = sizeof( ciphertext ); |
| 410 | |
| 411 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 412 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 413 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 414 | nonce, nonce_length, |
| 415 | NULL, 0, |
| 416 | plaintext, sizeof( plaintext ), |
| 417 | ciphertext, sizeof( ciphertext ), |
| 418 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 422 | { |
| 423 | psa_status_t verify_status = |
| 424 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 425 | PSA_SUCCESS : |
| 426 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 427 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 428 | nonce, nonce_length, |
| 429 | NULL, 0, |
| 430 | ciphertext, ciphertext_length, |
| 431 | plaintext, sizeof( plaintext ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 432 | &plaintext_length ), |
| 433 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | return( 1 ); |
| 437 | |
| 438 | exit: |
| 439 | return( 0 ); |
| 440 | } |
| 441 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 442 | static int exercise_signature_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 443 | psa_key_usage_t usage, |
| 444 | psa_algorithm_t alg ) |
| 445 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 446 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 447 | size_t payload_length = 16; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 448 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 449 | size_t signature_length = sizeof( signature ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 450 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 451 | |
| 452 | /* If the policy allows signing with any hash, just pick one. */ |
| 453 | if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH ) |
| 454 | { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 455 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 456 | hash_alg = KNOWN_SUPPORTED_HASH_ALG; |
| 457 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 458 | #else |
| 459 | test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 460 | return( 1 ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 461 | #endif |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 462 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 463 | |
| 464 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 465 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 466 | /* Some algorithms require the payload to have the size of |
| 467 | * the hash encoded in the algorithm. Use this input size |
| 468 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 469 | if( hash_alg != 0 ) |
| 470 | payload_length = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 471 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 472 | payload, payload_length, |
| 473 | signature, sizeof( signature ), |
| 474 | &signature_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 478 | { |
| 479 | psa_status_t verify_status = |
| 480 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 481 | PSA_SUCCESS : |
| 482 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 483 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 484 | payload, payload_length, |
| 485 | signature, signature_length ), |
| 486 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | return( 1 ); |
| 490 | |
| 491 | exit: |
| 492 | return( 0 ); |
| 493 | } |
| 494 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 495 | static int exercise_asymmetric_encryption_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 496 | psa_key_usage_t usage, |
| 497 | psa_algorithm_t alg ) |
| 498 | { |
| 499 | unsigned char plaintext[256] = "Hello, world..."; |
| 500 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 501 | size_t ciphertext_length = sizeof( ciphertext ); |
| 502 | size_t plaintext_length = 16; |
| 503 | |
| 504 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 505 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 506 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 507 | plaintext, plaintext_length, |
| 508 | NULL, 0, |
| 509 | ciphertext, sizeof( ciphertext ), |
| 510 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 514 | { |
| 515 | psa_status_t status = |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 516 | psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 517 | ciphertext, ciphertext_length, |
| 518 | NULL, 0, |
| 519 | plaintext, sizeof( plaintext ), |
| 520 | &plaintext_length ); |
| 521 | TEST_ASSERT( status == PSA_SUCCESS || |
| 522 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 523 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 524 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 525 | } |
| 526 | |
| 527 | return( 1 ); |
| 528 | |
| 529 | exit: |
| 530 | return( 0 ); |
| 531 | } |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 532 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 533 | static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation, |
| 534 | psa_key_handle_t handle, |
| 535 | psa_algorithm_t alg, |
| 536 | unsigned char* input1, size_t input1_length, |
| 537 | unsigned char* input2, size_t input2_length, |
| 538 | size_t capacity ) |
| 539 | { |
| 540 | PSA_ASSERT( psa_key_derivation_setup( operation, alg ) ); |
| 541 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 542 | { |
| 543 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 544 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 545 | input1, input1_length ) ); |
| 546 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 547 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 548 | handle ) ); |
| 549 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 550 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 551 | input2, |
| 552 | input2_length ) ); |
| 553 | } |
| 554 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 555 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 556 | { |
| 557 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 558 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 559 | input1, input1_length ) ); |
| 560 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 561 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 562 | handle ) ); |
| 563 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 564 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 565 | input2, input2_length ) ); |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | TEST_ASSERT( ! "Key derivation algorithm not supported" ); |
| 570 | } |
| 571 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 572 | if( capacity != SIZE_MAX ) |
| 573 | PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) ); |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 574 | |
| 575 | return( 1 ); |
| 576 | |
| 577 | exit: |
| 578 | return( 0 ); |
| 579 | } |
| 580 | |
| 581 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 582 | static int exercise_key_derivation_key( psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 583 | psa_key_usage_t usage, |
| 584 | psa_algorithm_t alg ) |
| 585 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 586 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 587 | unsigned char input1[] = "Input 1"; |
| 588 | size_t input1_length = sizeof( input1 ); |
| 589 | unsigned char input2[] = "Input 2"; |
| 590 | size_t input2_length = sizeof( input2 ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 591 | unsigned char output[1]; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 592 | size_t capacity = sizeof( output ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 593 | |
| 594 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 595 | { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 596 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 597 | input1, input1_length, |
| 598 | input2, input2_length, capacity ) ) |
| 599 | goto exit; |
Gilles Peskine | 7607cd6 | 2019-05-29 17:35:00 +0200 | [diff] [blame] | 600 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 601 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 602 | output, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 603 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 604 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | return( 1 ); |
| 608 | |
| 609 | exit: |
| 610 | return( 0 ); |
| 611 | } |
| 612 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 613 | /* We need two keys to exercise key agreement. Exercise the |
| 614 | * private key against its own public key. */ |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 615 | static psa_status_t key_agreement_with_self( |
| 616 | psa_key_derivation_operation_t *operation, |
| 617 | psa_key_handle_t handle ) |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 618 | { |
| 619 | psa_key_type_t private_key_type; |
| 620 | psa_key_type_t public_key_type; |
| 621 | size_t key_bits; |
| 622 | uint8_t *public_key = NULL; |
| 623 | size_t public_key_length; |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 624 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 625 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 626 | * but it's good enough: callers will report it as a failed test anyway. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 627 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 628 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 629 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 630 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 631 | private_key_type = psa_get_key_type( &attributes ); |
| 632 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 633 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 634 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 635 | ASSERT_ALLOC( public_key, public_key_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 636 | PSA_ASSERT( psa_export_public_key( handle, |
| 637 | public_key, public_key_length, |
| 638 | &public_key_length ) ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 639 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 640 | status = psa_key_derivation_key_agreement( |
| 641 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle, |
| 642 | public_key, public_key_length ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 643 | exit: |
| 644 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 645 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 646 | return( status ); |
| 647 | } |
| 648 | |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 649 | /* We need two keys to exercise key agreement. Exercise the |
| 650 | * private key against its own public key. */ |
| 651 | static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg, |
| 652 | psa_key_handle_t handle ) |
| 653 | { |
| 654 | psa_key_type_t private_key_type; |
| 655 | psa_key_type_t public_key_type; |
| 656 | size_t key_bits; |
| 657 | uint8_t *public_key = NULL; |
| 658 | size_t public_key_length; |
| 659 | uint8_t output[1024]; |
| 660 | size_t output_length; |
| 661 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 662 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 663 | * but it's good enough: callers will report it as a failed test anyway. */ |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 664 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 665 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 666 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 667 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 668 | private_key_type = psa_get_key_type( &attributes ); |
| 669 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 670 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 671 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 672 | ASSERT_ALLOC( public_key, public_key_length ); |
| 673 | PSA_ASSERT( psa_export_public_key( handle, |
| 674 | public_key, public_key_length, |
| 675 | &public_key_length ) ); |
| 676 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 677 | status = psa_raw_key_agreement( alg, handle, |
| 678 | public_key, public_key_length, |
| 679 | output, sizeof( output ), &output_length ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 680 | exit: |
| 681 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 682 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 683 | return( status ); |
| 684 | } |
| 685 | |
| 686 | static int exercise_raw_key_agreement_key( psa_key_handle_t handle, |
| 687 | psa_key_usage_t usage, |
| 688 | psa_algorithm_t alg ) |
| 689 | { |
| 690 | int ok = 0; |
| 691 | |
| 692 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 693 | { |
| 694 | /* We need two keys to exercise key agreement. Exercise the |
| 695 | * private key against its own public key. */ |
| 696 | PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) ); |
| 697 | } |
| 698 | ok = 1; |
| 699 | |
| 700 | exit: |
| 701 | return( ok ); |
| 702 | } |
| 703 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 704 | static int exercise_key_agreement_key( psa_key_handle_t handle, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 705 | psa_key_usage_t usage, |
| 706 | psa_algorithm_t alg ) |
| 707 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 708 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 709 | unsigned char output[1]; |
| 710 | int ok = 0; |
| 711 | |
| 712 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 713 | { |
| 714 | /* We need two keys to exercise key agreement. Exercise the |
| 715 | * private key against its own public key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 716 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 717 | PSA_ASSERT( key_agreement_with_self( &operation, handle ) ); |
| 718 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 719 | output, |
| 720 | sizeof( output ) ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 721 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 722 | } |
| 723 | ok = 1; |
| 724 | |
| 725 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 726 | return( ok ); |
| 727 | } |
| 728 | |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 729 | int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 730 | size_t min_bits, size_t max_bits, |
| 731 | int must_be_odd ) |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 732 | { |
| 733 | size_t len; |
| 734 | size_t actual_bits; |
| 735 | unsigned char msb; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 736 | TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 737 | MBEDTLS_ASN1_INTEGER ), |
| 738 | 0 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 739 | /* Tolerate a slight departure from DER encoding: |
| 740 | * - 0 may be represented by an empty string or a 1-byte string. |
| 741 | * - The sign bit may be used as a value bit. */ |
| 742 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 743 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 744 | { |
| 745 | ++( *p ); |
| 746 | --len; |
| 747 | } |
| 748 | if( min_bits == 0 && len == 0 ) |
| 749 | return( 1 ); |
| 750 | msb = ( *p )[0]; |
| 751 | TEST_ASSERT( msb != 0 ); |
| 752 | actual_bits = 8 * ( len - 1 ); |
| 753 | while( msb != 0 ) |
| 754 | { |
| 755 | msb >>= 1; |
| 756 | ++actual_bits; |
| 757 | } |
| 758 | TEST_ASSERT( actual_bits >= min_bits ); |
| 759 | TEST_ASSERT( actual_bits <= max_bits ); |
| 760 | if( must_be_odd ) |
| 761 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 762 | *p += len; |
| 763 | return( 1 ); |
| 764 | exit: |
| 765 | return( 0 ); |
| 766 | } |
| 767 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 768 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 769 | uint8_t *exported, size_t exported_length ) |
| 770 | { |
| 771 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 772 | TEST_EQUAL( exported_length, ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 773 | else |
| 774 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 775 | |
| 776 | #if defined(MBEDTLS_DES_C) |
| 777 | if( type == PSA_KEY_TYPE_DES ) |
| 778 | { |
| 779 | /* Check the parity bits. */ |
| 780 | unsigned i; |
| 781 | for( i = 0; i < bits / 8; i++ ) |
| 782 | { |
| 783 | unsigned bit_count = 0; |
| 784 | unsigned m; |
| 785 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 786 | { |
| 787 | if( exported[i] & m ) |
| 788 | ++bit_count; |
| 789 | } |
| 790 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 791 | } |
| 792 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 793 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 794 | #endif |
| 795 | |
| 796 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 797 | if( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 798 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 799 | uint8_t *p = exported; |
| 800 | uint8_t *end = exported + exported_length; |
| 801 | size_t len; |
| 802 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 803 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 804 | * modulus INTEGER, -- n |
| 805 | * publicExponent INTEGER, -- e |
| 806 | * privateExponent INTEGER, -- d |
| 807 | * prime1 INTEGER, -- p |
| 808 | * prime2 INTEGER, -- q |
| 809 | * exponent1 INTEGER, -- d mod (p-1) |
| 810 | * exponent2 INTEGER, -- d mod (q-1) |
| 811 | * coefficient INTEGER, -- (inverse of q) mod p |
| 812 | * } |
| 813 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 814 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 815 | MBEDTLS_ASN1_SEQUENCE | |
| 816 | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); |
| 817 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 818 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 819 | goto exit; |
| 820 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 821 | goto exit; |
| 822 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 823 | goto exit; |
| 824 | /* Require d to be at least half the size of n. */ |
| 825 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 826 | goto exit; |
| 827 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 828 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 829 | goto exit; |
| 830 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 831 | goto exit; |
| 832 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 833 | goto exit; |
| 834 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 835 | goto exit; |
| 836 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 837 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 838 | TEST_EQUAL( p, end ); |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 839 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 840 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 841 | #endif /* MBEDTLS_RSA_C */ |
| 842 | |
| 843 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 844 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 845 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 846 | /* Just the secret value */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 847 | TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 848 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 849 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 850 | #endif /* MBEDTLS_ECP_C */ |
| 851 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 852 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 853 | { |
| 854 | uint8_t *p = exported; |
| 855 | uint8_t *end = exported + exported_length; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 856 | #if defined(MBEDTLS_RSA_C) |
| 857 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 858 | { |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 859 | size_t len; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 860 | /* RSAPublicKey ::= SEQUENCE { |
| 861 | * modulus INTEGER, -- n |
| 862 | * publicExponent INTEGER } -- e |
| 863 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 864 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 865 | MBEDTLS_ASN1_SEQUENCE | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 866 | MBEDTLS_ASN1_CONSTRUCTED ), |
| 867 | 0 ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 868 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 869 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 870 | goto exit; |
| 871 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 872 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 873 | TEST_EQUAL( p, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 874 | } |
| 875 | else |
| 876 | #endif /* MBEDTLS_RSA_C */ |
| 877 | #if defined(MBEDTLS_ECP_C) |
| 878 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 879 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 880 | /* The representation of an ECC public key is: |
| 881 | * - The byte 0x04; |
| 882 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 883 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 884 | * - where m is the bit size associated with the curve. |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 885 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 886 | TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end ); |
| 887 | TEST_EQUAL( p[0], 4 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 888 | } |
| 889 | else |
| 890 | #endif /* MBEDTLS_ECP_C */ |
| 891 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 892 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 893 | mbedtls_snprintf( message, sizeof( message ), |
| 894 | "No sanity check for public key type=0x%08lx", |
| 895 | (unsigned long) type ); |
| 896 | test_fail( message, __LINE__, __FILE__ ); |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame^] | 897 | (void) p; |
| 898 | (void) end; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 899 | return( 0 ); |
| 900 | } |
| 901 | } |
| 902 | else |
| 903 | |
| 904 | { |
| 905 | /* No sanity checks for other types */ |
| 906 | } |
| 907 | |
| 908 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 909 | |
| 910 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 911 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 912 | } |
| 913 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 914 | static int exercise_export_key( psa_key_handle_t handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 915 | psa_key_usage_t usage ) |
| 916 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 917 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 918 | uint8_t *exported = NULL; |
| 919 | size_t exported_size = 0; |
| 920 | size_t exported_length = 0; |
| 921 | int ok = 0; |
| 922 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 923 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 924 | |
| 925 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 926 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 927 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 928 | TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ), |
| 929 | PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 930 | ok = 1; |
| 931 | goto exit; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 932 | } |
| 933 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 934 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ), |
| 935 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 936 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 937 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 938 | PSA_ASSERT( psa_export_key( handle, |
| 939 | exported, exported_size, |
| 940 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 941 | ok = exported_key_sanity_check( psa_get_key_type( &attributes ), |
| 942 | psa_get_key_bits( &attributes ), |
| 943 | exported, exported_length ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 944 | |
| 945 | exit: |
| 946 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 947 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 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 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 953 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 954 | psa_key_type_t public_type; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 955 | uint8_t *exported = NULL; |
| 956 | size_t exported_size = 0; |
| 957 | size_t exported_length = 0; |
| 958 | int ok = 0; |
| 959 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 960 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 961 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 962 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 963 | TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 964 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 965 | return( 1 ); |
| 966 | } |
| 967 | |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 968 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 969 | psa_get_key_type( &attributes ) ); |
| 970 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, |
| 971 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 972 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 973 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 974 | PSA_ASSERT( psa_export_public_key( handle, |
| 975 | exported, exported_size, |
| 976 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 977 | ok = exported_key_sanity_check( public_type, |
| 978 | psa_get_key_bits( &attributes ), |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 979 | exported, exported_length ); |
| 980 | |
| 981 | exit: |
| 982 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 983 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 984 | return( ok ); |
| 985 | } |
| 986 | |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 987 | /** Do smoke tests on a key. |
| 988 | * |
| 989 | * Perform one of each operation indicated by \p alg (decrypt/encrypt, |
| 990 | * sign/verify, or derivation) that is permitted according to \p usage. |
| 991 | * \p usage and \p alg should correspond to the expected policy on the |
| 992 | * key. |
| 993 | * |
| 994 | * Export the key if permitted by \p usage, and check that the output |
| 995 | * looks sensible. If \p usage forbids export, check that |
| 996 | * \p psa_export_key correctly rejects the attempt. If the key is |
| 997 | * asymmetric, also check \p psa_export_public_key. |
| 998 | * |
| 999 | * If the key fails the tests, this function calls the test framework's |
| 1000 | * `test_fail` function and returns false. Otherwise this function returns |
| 1001 | * true. Therefore it should be used as follows: |
| 1002 | * ``` |
| 1003 | * if( ! exercise_key( ... ) ) goto exit; |
| 1004 | * ``` |
| 1005 | * |
| 1006 | * \param handle The key to exercise. It should be capable of performing |
| 1007 | * \p alg. |
| 1008 | * \param usage The usage flags to assume. |
| 1009 | * \param alg The algorithm to exercise. |
| 1010 | * |
| 1011 | * \retval 0 The key failed the smoke tests. |
| 1012 | * \retval 1 The key passed the smoke tests. |
| 1013 | */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1014 | static int exercise_key( psa_key_handle_t handle, |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1015 | psa_key_usage_t usage, |
| 1016 | psa_algorithm_t alg ) |
| 1017 | { |
| 1018 | int ok; |
| 1019 | if( alg == 0 ) |
| 1020 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 1021 | else if( PSA_ALG_IS_MAC( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1022 | ok = exercise_mac_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1023 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1024 | ok = exercise_cipher_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1025 | else if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1026 | ok = exercise_aead_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1027 | else if( PSA_ALG_IS_SIGN( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1028 | ok = exercise_signature_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1029 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1030 | ok = exercise_asymmetric_encryption_key( handle, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1031 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1032 | ok = exercise_key_derivation_key( handle, usage, alg ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 1033 | else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 1034 | ok = exercise_raw_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1035 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1036 | ok = exercise_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1037 | else |
| 1038 | { |
| 1039 | char message[40]; |
| 1040 | mbedtls_snprintf( message, sizeof( message ), |
| 1041 | "No code to exercise alg=0x%08lx", |
| 1042 | (unsigned long) alg ); |
| 1043 | test_fail( message, __LINE__, __FILE__ ); |
| 1044 | ok = 0; |
| 1045 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1046 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1047 | ok = ok && exercise_export_key( handle, usage ); |
| 1048 | ok = ok && exercise_export_public_key( handle ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1049 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1050 | return( ok ); |
| 1051 | } |
| 1052 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1053 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 1054 | psa_algorithm_t alg ) |
| 1055 | { |
| 1056 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 1057 | { |
| 1058 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1059 | PSA_KEY_USAGE_VERIFY : |
| 1060 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 1061 | } |
| 1062 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 1063 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 1064 | { |
| 1065 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1066 | PSA_KEY_USAGE_ENCRYPT : |
| 1067 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 1068 | } |
| 1069 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 1070 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 1071 | { |
| 1072 | return( PSA_KEY_USAGE_DERIVE ); |
| 1073 | } |
| 1074 | else |
| 1075 | { |
| 1076 | return( 0 ); |
| 1077 | } |
| 1078 | |
| 1079 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1080 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1081 | static int test_operations_on_invalid_handle( psa_key_handle_t handle ) |
| 1082 | { |
| 1083 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1084 | uint8_t buffer[1]; |
| 1085 | size_t length; |
| 1086 | int ok = 0; |
| 1087 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1088 | psa_set_key_id( &attributes, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1089 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 1090 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 1091 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
| 1092 | TEST_EQUAL( psa_get_key_attributes( handle, &attributes ), |
| 1093 | PSA_ERROR_INVALID_HANDLE ); |
| 1094 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 1095 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1096 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1097 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1098 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 1099 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 1100 | |
| 1101 | TEST_EQUAL( psa_export_key( handle, |
| 1102 | buffer, sizeof( buffer ), &length ), |
| 1103 | PSA_ERROR_INVALID_HANDLE ); |
| 1104 | TEST_EQUAL( psa_export_public_key( handle, |
| 1105 | buffer, sizeof( buffer ), &length ), |
| 1106 | PSA_ERROR_INVALID_HANDLE ); |
| 1107 | |
| 1108 | TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE ); |
| 1109 | TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE ); |
| 1110 | |
| 1111 | ok = 1; |
| 1112 | |
| 1113 | exit: |
| 1114 | psa_reset_key_attributes( &attributes ); |
| 1115 | return( ok ); |
| 1116 | } |
| 1117 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1118 | /* Assert that a key isn't reported as having a slot number. */ |
| 1119 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1120 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1121 | do \ |
| 1122 | { \ |
| 1123 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 1124 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 1125 | attributes, \ |
| 1126 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 1127 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 1128 | } \ |
| 1129 | while( 0 ) |
| 1130 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1131 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1132 | ( (void) 0 ) |
| 1133 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1134 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1135 | /* An overapproximation of the amount of storage needed for a key of the |
| 1136 | * given type and with the given content. The API doesn't make it easy |
| 1137 | * to find a good value for the size. The current implementation doesn't |
| 1138 | * care about the value anyway. */ |
| 1139 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 1140 | ( data )->len |
| 1141 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1142 | typedef enum { |
| 1143 | IMPORT_KEY = 0, |
| 1144 | GENERATE_KEY = 1, |
| 1145 | DERIVE_KEY = 2 |
| 1146 | } generate_method; |
| 1147 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1148 | /* END_HEADER */ |
| 1149 | |
| 1150 | /* BEGIN_DEPENDENCIES |
| 1151 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1152 | * END_DEPENDENCIES |
| 1153 | */ |
| 1154 | |
| 1155 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1156 | void static_checks( ) |
| 1157 | { |
| 1158 | size_t max_truncated_mac_size = |
| 1159 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1160 | |
| 1161 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1162 | * encoding. The shifted mask is the maximum truncated value. The |
| 1163 | * untruncated algorithm may be one byte larger. */ |
| 1164 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 1165 | } |
| 1166 | /* END_CASE */ |
| 1167 | |
| 1168 | /* BEGIN_CASE */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1169 | void attributes_set_get( int id_arg, int lifetime_arg, |
| 1170 | int usage_flags_arg, int alg_arg, |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1171 | int type_arg, int bits_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1172 | { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1173 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1174 | psa_key_id_t id = id_arg; |
| 1175 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1176 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 1177 | psa_algorithm_t alg = alg_arg; |
| 1178 | psa_key_type_t type = type_arg; |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1179 | size_t bits = bits_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1180 | |
| 1181 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1182 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1183 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1184 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1185 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1186 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1187 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1188 | psa_set_key_id( &attributes, id ); |
| 1189 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1190 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 1191 | psa_set_key_algorithm( &attributes, alg ); |
| 1192 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1193 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1194 | |
| 1195 | TEST_EQUAL( psa_get_key_id( &attributes ), id ); |
| 1196 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime ); |
| 1197 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 1198 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
| 1199 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1200 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1201 | |
| 1202 | psa_reset_key_attributes( &attributes ); |
| 1203 | |
| 1204 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1205 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1206 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1207 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1208 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1209 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1210 | } |
| 1211 | /* END_CASE */ |
| 1212 | |
| 1213 | /* BEGIN_CASE */ |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1214 | void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg, |
| 1215 | int expected_id_arg, int expected_lifetime_arg ) |
| 1216 | { |
| 1217 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1218 | psa_key_id_t id1 = id1_arg; |
| 1219 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1220 | psa_key_id_t id2 = id2_arg; |
| 1221 | psa_key_id_t expected_id = expected_id_arg; |
| 1222 | psa_key_lifetime_t expected_lifetime = expected_lifetime_arg; |
| 1223 | |
| 1224 | if( id1_arg != -1 ) |
| 1225 | psa_set_key_id( &attributes, id1 ); |
| 1226 | if( lifetime_arg != -1 ) |
| 1227 | psa_set_key_lifetime( &attributes, lifetime ); |
| 1228 | if( id2_arg != -1 ) |
| 1229 | psa_set_key_id( &attributes, id2 ); |
| 1230 | |
| 1231 | TEST_EQUAL( psa_get_key_id( &attributes ), expected_id ); |
| 1232 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime ); |
| 1233 | } |
| 1234 | /* END_CASE */ |
| 1235 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1236 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1237 | void slot_number_attribute( ) |
| 1238 | { |
| 1239 | psa_key_slot_number_t slot_number = 0xdeadbeef; |
| 1240 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1241 | |
| 1242 | /* Initially, there is no slot number. */ |
| 1243 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1244 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1245 | |
| 1246 | /* Test setting a slot number. */ |
| 1247 | psa_set_key_slot_number( &attributes, 0 ); |
| 1248 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1249 | TEST_EQUAL( slot_number, 0 ); |
| 1250 | |
| 1251 | /* Test changing the slot number. */ |
| 1252 | psa_set_key_slot_number( &attributes, 42 ); |
| 1253 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1254 | TEST_EQUAL( slot_number, 42 ); |
| 1255 | |
| 1256 | /* Test clearing the slot number. */ |
| 1257 | psa_clear_key_slot_number( &attributes ); |
| 1258 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1259 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1260 | |
| 1261 | /* Clearing again should have no effect. */ |
| 1262 | psa_clear_key_slot_number( &attributes ); |
| 1263 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1264 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1265 | |
| 1266 | /* Test that reset clears the slot number. */ |
| 1267 | psa_set_key_slot_number( &attributes, 42 ); |
| 1268 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1269 | TEST_EQUAL( slot_number, 42 ); |
| 1270 | psa_reset_key_attributes( &attributes ); |
| 1271 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1272 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1273 | } |
| 1274 | /* END_CASE */ |
| 1275 | |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1276 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1277 | void import_with_policy( int type_arg, |
| 1278 | int usage_arg, int alg_arg, |
| 1279 | int expected_status_arg ) |
| 1280 | { |
| 1281 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1282 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1283 | psa_key_handle_t handle = 0; |
| 1284 | psa_key_type_t type = type_arg; |
| 1285 | psa_key_usage_t usage = usage_arg; |
| 1286 | psa_algorithm_t alg = alg_arg; |
| 1287 | psa_status_t expected_status = expected_status_arg; |
| 1288 | const uint8_t key_material[16] = {0}; |
| 1289 | psa_status_t status; |
| 1290 | |
| 1291 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1292 | |
| 1293 | psa_set_key_type( &attributes, type ); |
| 1294 | psa_set_key_usage_flags( &attributes, usage ); |
| 1295 | psa_set_key_algorithm( &attributes, alg ); |
| 1296 | |
| 1297 | status = psa_import_key( &attributes, |
| 1298 | key_material, sizeof( key_material ), |
| 1299 | &handle ); |
| 1300 | TEST_EQUAL( status, expected_status ); |
| 1301 | if( status != PSA_SUCCESS ) |
| 1302 | goto exit; |
| 1303 | |
| 1304 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1305 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1306 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1307 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1308 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1309 | |
| 1310 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1311 | test_operations_on_invalid_handle( handle ); |
| 1312 | |
| 1313 | exit: |
| 1314 | psa_destroy_key( handle ); |
| 1315 | psa_reset_key_attributes( &got_attributes ); |
| 1316 | PSA_DONE( ); |
| 1317 | } |
| 1318 | /* END_CASE */ |
| 1319 | |
| 1320 | /* BEGIN_CASE */ |
| 1321 | void import_with_data( data_t *data, int type_arg, |
| 1322 | int attr_bits_arg, |
| 1323 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1324 | { |
| 1325 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1326 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1327 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1328 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1329 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1330 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1331 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1332 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1333 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1334 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1335 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1336 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1337 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1338 | status = psa_import_key( &attributes, data->x, data->len, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1339 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1340 | if( status != PSA_SUCCESS ) |
| 1341 | goto exit; |
| 1342 | |
| 1343 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1344 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1345 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1346 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1347 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1348 | |
| 1349 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1350 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1351 | |
| 1352 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1353 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1354 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1355 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1356 | } |
| 1357 | /* END_CASE */ |
| 1358 | |
| 1359 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1360 | void import_large_key( int type_arg, int byte_size_arg, |
| 1361 | int expected_status_arg ) |
| 1362 | { |
| 1363 | psa_key_type_t type = type_arg; |
| 1364 | size_t byte_size = byte_size_arg; |
| 1365 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1366 | psa_status_t expected_status = expected_status_arg; |
| 1367 | psa_key_handle_t handle = 0; |
| 1368 | psa_status_t status; |
| 1369 | uint8_t *buffer = NULL; |
| 1370 | size_t buffer_size = byte_size + 1; |
| 1371 | size_t n; |
| 1372 | |
| 1373 | /* It would be better to skip the test than fail it if the allocation |
| 1374 | * fails, but the test framework doesn't support this yet. */ |
| 1375 | ASSERT_ALLOC( buffer, buffer_size ); |
| 1376 | memset( buffer, 'K', byte_size ); |
| 1377 | |
| 1378 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1379 | |
| 1380 | /* Try importing the key */ |
| 1381 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1382 | psa_set_key_type( &attributes, type ); |
| 1383 | status = psa_import_key( &attributes, buffer, byte_size, &handle ); |
| 1384 | TEST_EQUAL( status, expected_status ); |
| 1385 | |
| 1386 | if( status == PSA_SUCCESS ) |
| 1387 | { |
| 1388 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1389 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1390 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1391 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1392 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1393 | memset( buffer, 0, byte_size + 1 ); |
| 1394 | PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) ); |
| 1395 | for( n = 0; n < byte_size; n++ ) |
| 1396 | TEST_EQUAL( buffer[n], 'K' ); |
| 1397 | for( n = byte_size; n < buffer_size; n++ ) |
| 1398 | TEST_EQUAL( buffer[n], 0 ); |
| 1399 | } |
| 1400 | |
| 1401 | exit: |
| 1402 | psa_destroy_key( handle ); |
| 1403 | PSA_DONE( ); |
| 1404 | mbedtls_free( buffer ); |
| 1405 | } |
| 1406 | /* END_CASE */ |
| 1407 | |
| 1408 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1409 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1410 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1411 | psa_key_handle_t handle = 0; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1412 | size_t bits = bits_arg; |
| 1413 | psa_status_t expected_status = expected_status_arg; |
| 1414 | psa_status_t status; |
| 1415 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1416 | keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1417 | size_t buffer_size = /* Slight overapproximations */ |
| 1418 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1419 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1420 | unsigned char *p; |
| 1421 | int ret; |
| 1422 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1423 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1424 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1425 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1426 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1427 | |
| 1428 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1429 | bits, keypair ) ) >= 0 ); |
| 1430 | length = ret; |
| 1431 | |
| 1432 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1433 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1434 | status = psa_import_key( &attributes, p, length, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1435 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1436 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1437 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1438 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1439 | |
| 1440 | exit: |
| 1441 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1442 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1443 | } |
| 1444 | /* END_CASE */ |
| 1445 | |
| 1446 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1447 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1448 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1449 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1450 | int expected_bits, |
| 1451 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1452 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1453 | int canonical_input ) |
| 1454 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1455 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1456 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1457 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1458 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1459 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1460 | unsigned char *exported = NULL; |
| 1461 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1462 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1463 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1464 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1465 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1466 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1467 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1468 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1469 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1470 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1471 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1472 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1473 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1474 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1475 | psa_set_key_algorithm( &attributes, alg ); |
| 1476 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1477 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1478 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1479 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1480 | |
| 1481 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1482 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1483 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1484 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1485 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1486 | |
| 1487 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1488 | status = psa_export_key( handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1489 | exported, export_size, |
| 1490 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1491 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1492 | |
| 1493 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1494 | * and export_size. On errors, the exported length must be 0. */ |
| 1495 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1496 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1497 | TEST_ASSERT( exported_length <= export_size ); |
| 1498 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1499 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1500 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1501 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1502 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1503 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1504 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1505 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1506 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1507 | if( ! exercise_export_key( handle, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1508 | goto exit; |
| 1509 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1510 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1511 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1512 | else |
| 1513 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1514 | psa_key_handle_t handle2; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1515 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
| 1516 | &handle2 ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1517 | PSA_ASSERT( psa_export_key( handle2, |
| 1518 | reexported, |
| 1519 | export_size, |
| 1520 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1521 | ASSERT_COMPARE( exported, exported_length, |
| 1522 | reexported, reexported_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1523 | PSA_ASSERT( psa_close_key( handle2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1524 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1525 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1526 | |
| 1527 | destroy: |
| 1528 | /* Destroy the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1529 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1530 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1531 | |
| 1532 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1533 | mbedtls_free( exported ); |
| 1534 | mbedtls_free( reexported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1535 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1536 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1537 | } |
| 1538 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1539 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1540 | /* BEGIN_CASE */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1541 | void invalid_handle( int handle ) |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1542 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1543 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1544 | test_operations_on_invalid_handle( handle ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1545 | |
| 1546 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1547 | PSA_DONE( ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1548 | } |
| 1549 | /* END_CASE */ |
| 1550 | |
| 1551 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1552 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1553 | int type_arg, |
| 1554 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1555 | int export_size_delta, |
| 1556 | int expected_export_status_arg, |
| 1557 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1558 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1559 | psa_key_handle_t handle = 0; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1560 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1561 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1562 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1563 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1564 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1565 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1566 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1567 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1568 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1569 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1570 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1571 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1572 | psa_set_key_algorithm( &attributes, alg ); |
| 1573 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1574 | |
| 1575 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1576 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1577 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1578 | /* Export the public key */ |
| 1579 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1580 | status = psa_export_public_key( handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1581 | exported, export_size, |
| 1582 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1583 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1584 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1585 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1586 | psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1587 | size_t bits; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1588 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1589 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1590 | TEST_ASSERT( expected_public_key->len <= |
| 1591 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1592 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1593 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1594 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1595 | |
| 1596 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1597 | mbedtls_free( exported ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1598 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1599 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1600 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1601 | } |
| 1602 | /* END_CASE */ |
| 1603 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1604 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1605 | void import_and_exercise_key( data_t *data, |
| 1606 | int type_arg, |
| 1607 | int bits_arg, |
| 1608 | 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 | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1611 | psa_key_type_t type = type_arg; |
| 1612 | size_t bits = bits_arg; |
| 1613 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1614 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1615 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1616 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1617 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1618 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1619 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1620 | psa_set_key_usage_flags( &attributes, usage ); |
| 1621 | psa_set_key_algorithm( &attributes, alg ); |
| 1622 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1623 | |
| 1624 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1625 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1626 | |
| 1627 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1628 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1629 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1630 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1631 | |
| 1632 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1633 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1634 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1635 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1636 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1637 | test_operations_on_invalid_handle( handle ); |
| 1638 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1639 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1640 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1641 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1642 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1643 | } |
| 1644 | /* END_CASE */ |
| 1645 | |
| 1646 | /* BEGIN_CASE */ |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1647 | void key_policy( int usage_arg, int alg_arg ) |
| 1648 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1649 | psa_key_handle_t handle = 0; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1650 | psa_algorithm_t alg = alg_arg; |
| 1651 | psa_key_usage_t usage = usage_arg; |
| 1652 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 1653 | unsigned char key[32] = {0}; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1654 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1655 | |
| 1656 | memset( key, 0x2a, sizeof( key ) ); |
| 1657 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1658 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1659 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1660 | psa_set_key_usage_flags( &attributes, usage ); |
| 1661 | psa_set_key_algorithm( &attributes, alg ); |
| 1662 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1663 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1664 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1665 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1666 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1667 | TEST_EQUAL( psa_get_key_type( &attributes ), key_type ); |
| 1668 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage ); |
| 1669 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1670 | |
| 1671 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1672 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1673 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1674 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1675 | } |
| 1676 | /* END_CASE */ |
| 1677 | |
| 1678 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1679 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1680 | { |
| 1681 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1682 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1683 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1684 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1685 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1686 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1687 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1688 | |
| 1689 | memset( &zero, 0, sizeof( zero ) ); |
| 1690 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1691 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1692 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1693 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1694 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1695 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1696 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1697 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1698 | |
| 1699 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1700 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1701 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1702 | |
| 1703 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1704 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1705 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1706 | |
| 1707 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1708 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1709 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1710 | } |
| 1711 | /* END_CASE */ |
| 1712 | |
| 1713 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1714 | void mac_key_policy( int policy_usage, |
| 1715 | int policy_alg, |
| 1716 | int key_type, |
| 1717 | data_t *key_data, |
| 1718 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1719 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1720 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1721 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1722 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1723 | psa_status_t status; |
| 1724 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1725 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1726 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1727 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1728 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1729 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1730 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1731 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1732 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1733 | &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1734 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1735 | status = psa_mac_sign_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1736 | if( policy_alg == exercise_alg && |
| 1737 | ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1738 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1739 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1740 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1741 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1742 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1743 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1744 | status = psa_mac_verify_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1745 | if( policy_alg == exercise_alg && |
| 1746 | ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1747 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1748 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1749 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1750 | |
| 1751 | exit: |
| 1752 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1753 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1754 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1755 | } |
| 1756 | /* END_CASE */ |
| 1757 | |
| 1758 | /* BEGIN_CASE */ |
| 1759 | void cipher_key_policy( int policy_usage, |
| 1760 | int policy_alg, |
| 1761 | int key_type, |
| 1762 | data_t *key_data, |
| 1763 | int exercise_alg ) |
| 1764 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1765 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1766 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1767 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1768 | psa_status_t status; |
| 1769 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1770 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1771 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1772 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1773 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1774 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1775 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1776 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1777 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1778 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1779 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1780 | if( policy_alg == exercise_alg && |
| 1781 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1782 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1783 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1784 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1785 | psa_cipher_abort( &operation ); |
| 1786 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1787 | status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1788 | if( policy_alg == exercise_alg && |
| 1789 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1790 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1791 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1792 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1793 | |
| 1794 | exit: |
| 1795 | psa_cipher_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1796 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1797 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1798 | } |
| 1799 | /* END_CASE */ |
| 1800 | |
| 1801 | /* BEGIN_CASE */ |
| 1802 | void aead_key_policy( int policy_usage, |
| 1803 | int policy_alg, |
| 1804 | int key_type, |
| 1805 | data_t *key_data, |
| 1806 | int nonce_length_arg, |
| 1807 | int tag_length_arg, |
| 1808 | int exercise_alg ) |
| 1809 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1810 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1811 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1812 | psa_status_t status; |
| 1813 | unsigned char nonce[16] = {0}; |
| 1814 | size_t nonce_length = nonce_length_arg; |
| 1815 | unsigned char tag[16]; |
| 1816 | size_t tag_length = tag_length_arg; |
| 1817 | size_t output_length; |
| 1818 | |
| 1819 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1820 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1821 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1822 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1823 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1824 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1825 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1826 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1827 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1828 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1829 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1830 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1831 | status = psa_aead_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1832 | nonce, nonce_length, |
| 1833 | NULL, 0, |
| 1834 | NULL, 0, |
| 1835 | tag, tag_length, |
| 1836 | &output_length ); |
| 1837 | if( policy_alg == exercise_alg && |
| 1838 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1839 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1840 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1841 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1842 | |
| 1843 | memset( tag, 0, sizeof( tag ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1844 | status = psa_aead_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1845 | nonce, nonce_length, |
| 1846 | NULL, 0, |
| 1847 | tag, tag_length, |
| 1848 | NULL, 0, |
| 1849 | &output_length ); |
| 1850 | if( policy_alg == exercise_alg && |
| 1851 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1852 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1853 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1854 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1855 | |
| 1856 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1857 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1858 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1859 | } |
| 1860 | /* END_CASE */ |
| 1861 | |
| 1862 | /* BEGIN_CASE */ |
| 1863 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1864 | int policy_alg, |
| 1865 | int key_type, |
| 1866 | data_t *key_data, |
| 1867 | int exercise_alg ) |
| 1868 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1869 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1870 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1871 | psa_status_t status; |
| 1872 | size_t key_bits; |
| 1873 | size_t buffer_length; |
| 1874 | unsigned char *buffer = NULL; |
| 1875 | size_t output_length; |
| 1876 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1877 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1878 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1879 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1880 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1881 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1882 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1883 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1884 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1885 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1886 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1887 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1888 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1889 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1890 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1891 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1892 | status = psa_asymmetric_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1893 | NULL, 0, |
| 1894 | NULL, 0, |
| 1895 | buffer, buffer_length, |
| 1896 | &output_length ); |
| 1897 | if( policy_alg == exercise_alg && |
| 1898 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1899 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1900 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1901 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1902 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1903 | if( buffer_length != 0 ) |
| 1904 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1905 | status = psa_asymmetric_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1906 | buffer, buffer_length, |
| 1907 | NULL, 0, |
| 1908 | buffer, buffer_length, |
| 1909 | &output_length ); |
| 1910 | if( policy_alg == exercise_alg && |
| 1911 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1912 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1913 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1914 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1915 | |
| 1916 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1917 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1918 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1919 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1920 | mbedtls_free( buffer ); |
| 1921 | } |
| 1922 | /* END_CASE */ |
| 1923 | |
| 1924 | /* BEGIN_CASE */ |
| 1925 | void asymmetric_signature_key_policy( int policy_usage, |
| 1926 | int policy_alg, |
| 1927 | int key_type, |
| 1928 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1929 | int exercise_alg, |
| 1930 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1931 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1932 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1933 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1934 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1935 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1936 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1937 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1938 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1939 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1940 | int compatible_alg = payload_length_arg > 0; |
| 1941 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1942 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
| 1943 | size_t signature_length; |
| 1944 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1945 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1946 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1947 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1948 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1949 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1950 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1951 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1952 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1953 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1954 | status = psa_asymmetric_sign( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1955 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1956 | signature, sizeof( signature ), |
| 1957 | &signature_length ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1958 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1959 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1960 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1961 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1962 | |
| 1963 | memset( signature, 0, sizeof( signature ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1964 | status = psa_asymmetric_verify( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1965 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1966 | signature, sizeof( signature ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1967 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1968 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1969 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1970 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1971 | |
| 1972 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1973 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1974 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1975 | } |
| 1976 | /* END_CASE */ |
| 1977 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1978 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1979 | void derive_key_policy( int policy_usage, |
| 1980 | int policy_alg, |
| 1981 | int key_type, |
| 1982 | data_t *key_data, |
| 1983 | int exercise_alg ) |
| 1984 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1985 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1986 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1987 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1988 | psa_status_t status; |
| 1989 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1990 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1991 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1992 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1993 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1994 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1995 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1996 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1997 | &handle ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1998 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1999 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2000 | |
| 2001 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2002 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2003 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2004 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2005 | &operation, |
| 2006 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2007 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2008 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2009 | |
| 2010 | status = psa_key_derivation_input_key( &operation, |
| 2011 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2012 | handle ); |
| 2013 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2014 | if( policy_alg == exercise_alg && |
| 2015 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2016 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2017 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2018 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2019 | |
| 2020 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2021 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2022 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2023 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2024 | } |
| 2025 | /* END_CASE */ |
| 2026 | |
| 2027 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2028 | void agreement_key_policy( int policy_usage, |
| 2029 | int policy_alg, |
| 2030 | int key_type_arg, |
| 2031 | data_t *key_data, |
| 2032 | int exercise_alg ) |
| 2033 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2034 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2035 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2036 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2037 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2038 | psa_status_t status; |
| 2039 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2040 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2041 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2042 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2043 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2044 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2045 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2046 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2047 | &handle ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2048 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2049 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2050 | status = key_agreement_with_self( &operation, handle ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2051 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2052 | if( policy_alg == exercise_alg && |
| 2053 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2054 | PSA_ASSERT( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2055 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2056 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2057 | |
| 2058 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2059 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2060 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2061 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2062 | } |
| 2063 | /* END_CASE */ |
| 2064 | |
| 2065 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2066 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2067 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2068 | { |
| 2069 | psa_key_handle_t handle = 0; |
| 2070 | psa_key_type_t key_type = key_type_arg; |
| 2071 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2072 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2073 | psa_key_usage_t usage = usage_arg; |
| 2074 | psa_algorithm_t alg = alg_arg; |
| 2075 | psa_algorithm_t alg2 = alg2_arg; |
| 2076 | |
| 2077 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2078 | |
| 2079 | psa_set_key_usage_flags( &attributes, usage ); |
| 2080 | psa_set_key_algorithm( &attributes, alg ); |
| 2081 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2082 | psa_set_key_type( &attributes, key_type ); |
| 2083 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2084 | &handle ) ); |
| 2085 | |
| 2086 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 2087 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2088 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2089 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2090 | |
| 2091 | if( ! exercise_key( handle, usage, alg ) ) |
| 2092 | goto exit; |
| 2093 | if( ! exercise_key( handle, usage, alg2 ) ) |
| 2094 | goto exit; |
| 2095 | |
| 2096 | exit: |
| 2097 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2098 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2099 | } |
| 2100 | /* END_CASE */ |
| 2101 | |
| 2102 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2103 | void raw_agreement_key_policy( int policy_usage, |
| 2104 | int policy_alg, |
| 2105 | int key_type_arg, |
| 2106 | data_t *key_data, |
| 2107 | int exercise_alg ) |
| 2108 | { |
| 2109 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2110 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2111 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2112 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2113 | psa_status_t status; |
| 2114 | |
| 2115 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2116 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2117 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2118 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2119 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2120 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2121 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2122 | &handle ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2123 | |
| 2124 | status = raw_key_agreement_with_self( exercise_alg, handle ); |
| 2125 | |
| 2126 | if( policy_alg == exercise_alg && |
| 2127 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 2128 | PSA_ASSERT( status ); |
| 2129 | else |
| 2130 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2131 | |
| 2132 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2133 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2134 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2135 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2136 | } |
| 2137 | /* END_CASE */ |
| 2138 | |
| 2139 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2140 | void copy_success( int source_usage_arg, |
| 2141 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2142 | int type_arg, data_t *material, |
| 2143 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2144 | int target_usage_arg, |
| 2145 | int target_alg_arg, int target_alg2_arg, |
| 2146 | int expected_usage_arg, |
| 2147 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2148 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2149 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2150 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2151 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2152 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2153 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2154 | psa_key_handle_t source_handle = 0; |
| 2155 | psa_key_handle_t target_handle = 0; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2156 | uint8_t *export_buffer = NULL; |
| 2157 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2158 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2159 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2160 | /* Prepare the source key. */ |
| 2161 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2162 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2163 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2164 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2165 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2166 | material->x, material->len, |
| 2167 | &source_handle ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2168 | PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2169 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2170 | /* Prepare the target attributes. */ |
| 2171 | if( copy_attributes ) |
| 2172 | target_attributes = source_attributes; |
| 2173 | if( target_usage_arg != -1 ) |
| 2174 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2175 | if( target_alg_arg != -1 ) |
| 2176 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2177 | if( target_alg2_arg != -1 ) |
| 2178 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2179 | |
| 2180 | /* Copy the key. */ |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2181 | PSA_ASSERT( psa_copy_key( source_handle, |
| 2182 | &target_attributes, &target_handle ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2183 | |
| 2184 | /* Destroy the source to ensure that this doesn't affect the target. */ |
| 2185 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2186 | |
| 2187 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2188 | PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) ); |
| 2189 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2190 | psa_get_key_type( &target_attributes ) ); |
| 2191 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2192 | psa_get_key_bits( &target_attributes ) ); |
| 2193 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2194 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2195 | TEST_EQUAL( expected_alg2, |
| 2196 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2197 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2198 | { |
| 2199 | size_t length; |
| 2200 | ASSERT_ALLOC( export_buffer, material->len ); |
| 2201 | PSA_ASSERT( psa_export_key( target_handle, export_buffer, |
| 2202 | material->len, &length ) ); |
| 2203 | ASSERT_COMPARE( material->x, material->len, |
| 2204 | export_buffer, length ); |
| 2205 | } |
| 2206 | if( ! exercise_key( target_handle, expected_usage, expected_alg ) ) |
| 2207 | goto exit; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2208 | if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) ) |
| 2209 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2210 | |
| 2211 | PSA_ASSERT( psa_close_key( target_handle ) ); |
| 2212 | |
| 2213 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2214 | psa_reset_key_attributes( &source_attributes ); |
| 2215 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2216 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2217 | mbedtls_free( export_buffer ); |
| 2218 | } |
| 2219 | /* END_CASE */ |
| 2220 | |
| 2221 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2222 | void copy_fail( int source_usage_arg, |
| 2223 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2224 | int type_arg, data_t *material, |
| 2225 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2226 | int target_usage_arg, |
| 2227 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2228 | int expected_status_arg ) |
| 2229 | { |
| 2230 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2231 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2232 | psa_key_handle_t source_handle = 0; |
| 2233 | psa_key_handle_t target_handle = 0; |
| 2234 | |
| 2235 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2236 | |
| 2237 | /* Prepare the source key. */ |
| 2238 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2239 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2240 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2241 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2242 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2243 | material->x, material->len, |
| 2244 | &source_handle ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2245 | |
| 2246 | /* Prepare the target attributes. */ |
| 2247 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2248 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2249 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2250 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2251 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2252 | |
| 2253 | /* Try to copy the key. */ |
| 2254 | TEST_EQUAL( psa_copy_key( source_handle, |
| 2255 | &target_attributes, &target_handle ), |
| 2256 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2257 | |
| 2258 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2259 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2260 | exit: |
| 2261 | psa_reset_key_attributes( &source_attributes ); |
| 2262 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2263 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2264 | } |
| 2265 | /* END_CASE */ |
| 2266 | |
| 2267 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2268 | void hash_operation_init( ) |
| 2269 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2270 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2271 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2272 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2273 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2274 | * to supress the Clang warning for the test. */ |
| 2275 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2276 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2277 | psa_hash_operation_t zero; |
| 2278 | |
| 2279 | memset( &zero, 0, sizeof( zero ) ); |
| 2280 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2281 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2282 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2283 | PSA_ERROR_BAD_STATE ); |
| 2284 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2285 | PSA_ERROR_BAD_STATE ); |
| 2286 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2287 | PSA_ERROR_BAD_STATE ); |
| 2288 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2289 | /* A default hash operation should be abortable without error. */ |
| 2290 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2291 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2292 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2293 | } |
| 2294 | /* END_CASE */ |
| 2295 | |
| 2296 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2297 | void hash_setup( int alg_arg, |
| 2298 | int expected_status_arg ) |
| 2299 | { |
| 2300 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2301 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2302 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2303 | psa_status_t status; |
| 2304 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2305 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2306 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2307 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2308 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2309 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2310 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2311 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2312 | |
| 2313 | /* If setup failed, reproduce the failure, so as to |
| 2314 | * test the resulting state of the operation object. */ |
| 2315 | if( status != PSA_SUCCESS ) |
| 2316 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2317 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2318 | /* Now the operation object should be reusable. */ |
| 2319 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2320 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2321 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2322 | #endif |
| 2323 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2324 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2325 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2326 | } |
| 2327 | /* END_CASE */ |
| 2328 | |
| 2329 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2330 | void hash_bad_order( ) |
| 2331 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2332 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2333 | unsigned char input[] = ""; |
| 2334 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2335 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2336 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2337 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2338 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2339 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2340 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2341 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2342 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2343 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2344 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2345 | /* Call setup twice in a row. */ |
| 2346 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2347 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2348 | PSA_ERROR_BAD_STATE ); |
| 2349 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2350 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2351 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2352 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2353 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2354 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2355 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2356 | /* Call update after finish. */ |
| 2357 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2358 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2359 | hash, sizeof( hash ), &hash_len ) ); |
| 2360 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2361 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2362 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2363 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2364 | /* Call verify without calling setup beforehand. */ |
| 2365 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2366 | valid_hash, sizeof( valid_hash ) ), |
| 2367 | PSA_ERROR_BAD_STATE ); |
| 2368 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2369 | |
| 2370 | /* Call verify after finish. */ |
| 2371 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2372 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2373 | hash, sizeof( hash ), &hash_len ) ); |
| 2374 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2375 | valid_hash, sizeof( valid_hash ) ), |
| 2376 | PSA_ERROR_BAD_STATE ); |
| 2377 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2378 | |
| 2379 | /* Call verify twice in a row. */ |
| 2380 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2381 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2382 | valid_hash, sizeof( valid_hash ) ) ); |
| 2383 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2384 | valid_hash, sizeof( valid_hash ) ), |
| 2385 | PSA_ERROR_BAD_STATE ); |
| 2386 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2387 | |
| 2388 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2389 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2390 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2391 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2392 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2393 | |
| 2394 | /* Call finish twice in a row. */ |
| 2395 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2396 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2397 | hash, sizeof( hash ), &hash_len ) ); |
| 2398 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2399 | hash, sizeof( hash ), &hash_len ), |
| 2400 | PSA_ERROR_BAD_STATE ); |
| 2401 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2402 | |
| 2403 | /* Call finish after calling verify. */ |
| 2404 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2405 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2406 | valid_hash, sizeof( valid_hash ) ) ); |
| 2407 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2408 | hash, sizeof( hash ), &hash_len ), |
| 2409 | PSA_ERROR_BAD_STATE ); |
| 2410 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2411 | |
| 2412 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2413 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2414 | } |
| 2415 | /* END_CASE */ |
| 2416 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2417 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2418 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2419 | { |
| 2420 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2421 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2422 | * appended to it */ |
| 2423 | unsigned char hash[] = { |
| 2424 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2425 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2426 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2427 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2428 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2429 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2430 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2431 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2432 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2433 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2434 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2435 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2436 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2437 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2438 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2439 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2440 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2441 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2442 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2443 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2444 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2445 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2446 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2447 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2448 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2449 | } |
| 2450 | /* END_CASE */ |
| 2451 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2452 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2453 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2454 | { |
| 2455 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2456 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2457 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2458 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2459 | size_t hash_len; |
| 2460 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2461 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2462 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2463 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2464 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2465 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2466 | hash, expected_size - 1, &hash_len ), |
| 2467 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2468 | |
| 2469 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2470 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2471 | } |
| 2472 | /* END_CASE */ |
| 2473 | |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2474 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2475 | void hash_clone_source_state( ) |
| 2476 | { |
| 2477 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2478 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2479 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2480 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2481 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2482 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2483 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2484 | size_t hash_len; |
| 2485 | |
| 2486 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2487 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2488 | |
| 2489 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2490 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2491 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2492 | hash, sizeof( hash ), &hash_len ) ); |
| 2493 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2494 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2495 | |
| 2496 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2497 | PSA_ERROR_BAD_STATE ); |
| 2498 | |
| 2499 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2500 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2501 | hash, sizeof( hash ), &hash_len ) ); |
| 2502 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2503 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2504 | hash, sizeof( hash ), &hash_len ) ); |
| 2505 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2506 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2507 | hash, sizeof( hash ), &hash_len ) ); |
| 2508 | |
| 2509 | exit: |
| 2510 | psa_hash_abort( &op_source ); |
| 2511 | psa_hash_abort( &op_init ); |
| 2512 | psa_hash_abort( &op_setup ); |
| 2513 | psa_hash_abort( &op_finished ); |
| 2514 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2515 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2516 | } |
| 2517 | /* END_CASE */ |
| 2518 | |
| 2519 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2520 | void hash_clone_target_state( ) |
| 2521 | { |
| 2522 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2523 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2524 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2525 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2526 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2527 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2528 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2529 | size_t hash_len; |
| 2530 | |
| 2531 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2532 | |
| 2533 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2534 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2535 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2536 | hash, sizeof( hash ), &hash_len ) ); |
| 2537 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2538 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2539 | |
| 2540 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2541 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2542 | hash, sizeof( hash ), &hash_len ) ); |
| 2543 | |
| 2544 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2545 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2546 | PSA_ERROR_BAD_STATE ); |
| 2547 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2548 | PSA_ERROR_BAD_STATE ); |
| 2549 | |
| 2550 | exit: |
| 2551 | psa_hash_abort( &op_target ); |
| 2552 | psa_hash_abort( &op_init ); |
| 2553 | psa_hash_abort( &op_setup ); |
| 2554 | psa_hash_abort( &op_finished ); |
| 2555 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2556 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2557 | } |
| 2558 | /* END_CASE */ |
| 2559 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2560 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2561 | void mac_operation_init( ) |
| 2562 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2563 | const uint8_t input[1] = { 0 }; |
| 2564 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2565 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2566 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2567 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2568 | * to supress the Clang warning for the test. */ |
| 2569 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2570 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2571 | psa_mac_operation_t zero; |
| 2572 | |
| 2573 | memset( &zero, 0, sizeof( zero ) ); |
| 2574 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2575 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2576 | TEST_EQUAL( psa_mac_update( &func, |
| 2577 | input, sizeof( input ) ), |
| 2578 | PSA_ERROR_BAD_STATE ); |
| 2579 | TEST_EQUAL( psa_mac_update( &init, |
| 2580 | input, sizeof( input ) ), |
| 2581 | PSA_ERROR_BAD_STATE ); |
| 2582 | TEST_EQUAL( psa_mac_update( &zero, |
| 2583 | input, sizeof( input ) ), |
| 2584 | PSA_ERROR_BAD_STATE ); |
| 2585 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2586 | /* A default MAC operation should be abortable without error. */ |
| 2587 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2588 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2589 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2590 | } |
| 2591 | /* END_CASE */ |
| 2592 | |
| 2593 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2594 | void mac_setup( int key_type_arg, |
| 2595 | data_t *key, |
| 2596 | int alg_arg, |
| 2597 | int expected_status_arg ) |
| 2598 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2599 | psa_key_type_t key_type = key_type_arg; |
| 2600 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2601 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2602 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2603 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2604 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2605 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2606 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2607 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2608 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2609 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2610 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2611 | &operation, &status ) ) |
| 2612 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2613 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2614 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2615 | /* The operation object should be reusable. */ |
| 2616 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2617 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2618 | smoke_test_key_data, |
| 2619 | sizeof( smoke_test_key_data ), |
| 2620 | KNOWN_SUPPORTED_MAC_ALG, |
| 2621 | &operation, &status ) ) |
| 2622 | goto exit; |
| 2623 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2624 | #endif |
| 2625 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2626 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2627 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2628 | } |
| 2629 | /* END_CASE */ |
| 2630 | |
| 2631 | /* BEGIN_CASE */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2632 | void mac_bad_order( ) |
| 2633 | { |
| 2634 | psa_key_handle_t handle = 0; |
| 2635 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2636 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
| 2637 | const uint8_t key[] = { |
| 2638 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2639 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2640 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2641 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2642 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2643 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2644 | size_t sign_mac_length = 0; |
| 2645 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2646 | const uint8_t verify_mac[] = { |
| 2647 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2648 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2649 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2650 | |
| 2651 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2652 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 2653 | psa_set_key_algorithm( &attributes, alg ); |
| 2654 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2655 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2656 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2657 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2658 | /* Call update without calling setup beforehand. */ |
| 2659 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2660 | PSA_ERROR_BAD_STATE ); |
| 2661 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2662 | |
| 2663 | /* Call sign finish without calling setup beforehand. */ |
| 2664 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2665 | &sign_mac_length), |
| 2666 | PSA_ERROR_BAD_STATE ); |
| 2667 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2668 | |
| 2669 | /* Call verify finish without calling setup beforehand. */ |
| 2670 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2671 | verify_mac, sizeof( verify_mac ) ), |
| 2672 | PSA_ERROR_BAD_STATE ); |
| 2673 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2674 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2675 | /* Call setup twice in a row. */ |
| 2676 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2677 | handle, alg ) ); |
| 2678 | TEST_EQUAL( psa_mac_sign_setup( &operation, |
| 2679 | handle, alg ), |
| 2680 | PSA_ERROR_BAD_STATE ); |
| 2681 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2682 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2683 | /* Call update after sign finish. */ |
| 2684 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2685 | handle, alg ) ); |
| 2686 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2687 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2688 | sign_mac, sizeof( sign_mac ), |
| 2689 | &sign_mac_length ) ); |
| 2690 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2691 | PSA_ERROR_BAD_STATE ); |
| 2692 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2693 | |
| 2694 | /* Call update after verify finish. */ |
| 2695 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2696 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2697 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2698 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2699 | verify_mac, sizeof( verify_mac ) ) ); |
| 2700 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2701 | PSA_ERROR_BAD_STATE ); |
| 2702 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2703 | |
| 2704 | /* Call sign finish twice in a row. */ |
| 2705 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2706 | handle, alg ) ); |
| 2707 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2708 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2709 | sign_mac, sizeof( sign_mac ), |
| 2710 | &sign_mac_length ) ); |
| 2711 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2712 | sign_mac, sizeof( sign_mac ), |
| 2713 | &sign_mac_length ), |
| 2714 | PSA_ERROR_BAD_STATE ); |
| 2715 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2716 | |
| 2717 | /* Call verify finish twice in a row. */ |
| 2718 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2719 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2720 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2721 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2722 | verify_mac, sizeof( verify_mac ) ) ); |
| 2723 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2724 | verify_mac, sizeof( verify_mac ) ), |
| 2725 | PSA_ERROR_BAD_STATE ); |
| 2726 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2727 | |
| 2728 | /* Setup sign but try verify. */ |
| 2729 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2730 | handle, alg ) ); |
| 2731 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2732 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2733 | verify_mac, sizeof( verify_mac ) ), |
| 2734 | PSA_ERROR_BAD_STATE ); |
| 2735 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2736 | |
| 2737 | /* Setup verify but try sign. */ |
| 2738 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2739 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2740 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2741 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2742 | sign_mac, sizeof( sign_mac ), |
| 2743 | &sign_mac_length ), |
| 2744 | PSA_ERROR_BAD_STATE ); |
| 2745 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2746 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2747 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2748 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2749 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2750 | PSA_DONE( ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2751 | } |
| 2752 | /* END_CASE */ |
| 2753 | |
| 2754 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2755 | void mac_sign( int key_type_arg, |
| 2756 | data_t *key, |
| 2757 | int alg_arg, |
| 2758 | data_t *input, |
| 2759 | data_t *expected_mac ) |
| 2760 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2761 | psa_key_handle_t handle = 0; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2762 | psa_key_type_t key_type = key_type_arg; |
| 2763 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2764 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2765 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2766 | /* Leave a little extra room in the output buffer. At the end of the |
| 2767 | * test, we'll check that the implementation didn't overwrite onto |
| 2768 | * this extra room. */ |
| 2769 | uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10]; |
| 2770 | size_t mac_buffer_size = |
| 2771 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 2772 | size_t mac_length = 0; |
| 2773 | |
| 2774 | memset( actual_mac, '+', sizeof( actual_mac ) ); |
| 2775 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
| 2776 | TEST_ASSERT( expected_mac->len <= mac_buffer_size ); |
| 2777 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2778 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2779 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2780 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 2781 | psa_set_key_algorithm( &attributes, alg ); |
| 2782 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2783 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2784 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2785 | |
| 2786 | /* Calculate the MAC. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2787 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2788 | handle, alg ) ); |
| 2789 | PSA_ASSERT( psa_mac_update( &operation, |
| 2790 | input->x, input->len ) ); |
| 2791 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2792 | actual_mac, mac_buffer_size, |
| 2793 | &mac_length ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2794 | |
| 2795 | /* Compare with the expected value. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 2796 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2797 | actual_mac, mac_length ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2798 | |
| 2799 | /* Verify that the end of the buffer is untouched. */ |
| 2800 | TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+', |
| 2801 | sizeof( actual_mac ) - mac_length ) ); |
| 2802 | |
| 2803 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2804 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2805 | PSA_DONE( ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2806 | } |
| 2807 | /* END_CASE */ |
| 2808 | |
| 2809 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2810 | void mac_verify( int key_type_arg, |
| 2811 | data_t *key, |
| 2812 | int alg_arg, |
| 2813 | data_t *input, |
| 2814 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2815 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2816 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2817 | psa_key_type_t key_type = key_type_arg; |
| 2818 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2819 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2820 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2821 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2822 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2823 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2824 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2825 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2826 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 2827 | psa_set_key_algorithm( &attributes, alg ); |
| 2828 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2829 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2830 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2831 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2832 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 2833 | handle, alg ) ); |
| 2834 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2835 | PSA_ASSERT( psa_mac_update( &operation, |
| 2836 | input->x, input->len ) ); |
| 2837 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2838 | expected_mac->x, |
| 2839 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2840 | |
| 2841 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2842 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2843 | PSA_DONE( ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2844 | } |
| 2845 | /* END_CASE */ |
| 2846 | |
| 2847 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2848 | void cipher_operation_init( ) |
| 2849 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2850 | const uint8_t input[1] = { 0 }; |
| 2851 | unsigned char output[1] = { 0 }; |
| 2852 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2853 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2854 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2855 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2856 | * to supress the Clang warning for the test. */ |
| 2857 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2858 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2859 | psa_cipher_operation_t zero; |
| 2860 | |
| 2861 | memset( &zero, 0, sizeof( zero ) ); |
| 2862 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2863 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2864 | TEST_EQUAL( psa_cipher_update( &func, |
| 2865 | input, sizeof( input ), |
| 2866 | output, sizeof( output ), |
| 2867 | &output_length ), |
| 2868 | PSA_ERROR_BAD_STATE ); |
| 2869 | TEST_EQUAL( psa_cipher_update( &init, |
| 2870 | input, sizeof( input ), |
| 2871 | output, sizeof( output ), |
| 2872 | &output_length ), |
| 2873 | PSA_ERROR_BAD_STATE ); |
| 2874 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2875 | input, sizeof( input ), |
| 2876 | output, sizeof( output ), |
| 2877 | &output_length ), |
| 2878 | PSA_ERROR_BAD_STATE ); |
| 2879 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2880 | /* A default cipher operation should be abortable without error. */ |
| 2881 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2882 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2883 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2884 | } |
| 2885 | /* END_CASE */ |
| 2886 | |
| 2887 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2888 | void cipher_setup( int key_type_arg, |
| 2889 | data_t *key, |
| 2890 | int alg_arg, |
| 2891 | int expected_status_arg ) |
| 2892 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2893 | psa_key_type_t key_type = key_type_arg; |
| 2894 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2895 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2896 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2897 | psa_status_t status; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2898 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2899 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2900 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2901 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2902 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2903 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2904 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2905 | &operation, &status ) ) |
| 2906 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2907 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2908 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2909 | /* The operation object should be reusable. */ |
| 2910 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2911 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2912 | smoke_test_key_data, |
| 2913 | sizeof( smoke_test_key_data ), |
| 2914 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2915 | &operation, &status ) ) |
| 2916 | goto exit; |
| 2917 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2918 | #endif |
| 2919 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2920 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2921 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2922 | } |
| 2923 | /* END_CASE */ |
| 2924 | |
| 2925 | /* BEGIN_CASE */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2926 | void cipher_bad_order( ) |
| 2927 | { |
| 2928 | psa_key_handle_t handle = 0; |
| 2929 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2930 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2931 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2932 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2933 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2934 | const uint8_t key[] = { |
| 2935 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2936 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2937 | const uint8_t text[] = { |
| 2938 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2939 | 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2940 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2941 | size_t length = 0; |
| 2942 | |
| 2943 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2944 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2945 | psa_set_key_algorithm( &attributes, alg ); |
| 2946 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2947 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2948 | |
| 2949 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2950 | /* Call encrypt setup twice in a row. */ |
| 2951 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2952 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ), |
| 2953 | PSA_ERROR_BAD_STATE ); |
| 2954 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2955 | |
| 2956 | /* Call decrypt setup twice in a row. */ |
| 2957 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) ); |
| 2958 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ), |
| 2959 | PSA_ERROR_BAD_STATE ); |
| 2960 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2961 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2962 | /* Generate an IV without calling setup beforehand. */ |
| 2963 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2964 | buffer, sizeof( buffer ), |
| 2965 | &length ), |
| 2966 | PSA_ERROR_BAD_STATE ); |
| 2967 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2968 | |
| 2969 | /* Generate an IV twice in a row. */ |
| 2970 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2971 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2972 | buffer, sizeof( buffer ), |
| 2973 | &length ) ); |
| 2974 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2975 | buffer, sizeof( buffer ), |
| 2976 | &length ), |
| 2977 | PSA_ERROR_BAD_STATE ); |
| 2978 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2979 | |
| 2980 | /* Generate an IV after it's already set. */ |
| 2981 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2982 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2983 | iv, sizeof( iv ) ) ); |
| 2984 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2985 | buffer, sizeof( buffer ), |
| 2986 | &length ), |
| 2987 | PSA_ERROR_BAD_STATE ); |
| 2988 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2989 | |
| 2990 | /* Set an IV without calling setup beforehand. */ |
| 2991 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2992 | iv, sizeof( iv ) ), |
| 2993 | PSA_ERROR_BAD_STATE ); |
| 2994 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2995 | |
| 2996 | /* Set an IV after it's already set. */ |
| 2997 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2998 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2999 | iv, sizeof( iv ) ) ); |
| 3000 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3001 | iv, sizeof( iv ) ), |
| 3002 | PSA_ERROR_BAD_STATE ); |
| 3003 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3004 | |
| 3005 | /* Set an IV after it's already generated. */ |
| 3006 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3007 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3008 | buffer, sizeof( buffer ), |
| 3009 | &length ) ); |
| 3010 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3011 | iv, sizeof( iv ) ), |
| 3012 | PSA_ERROR_BAD_STATE ); |
| 3013 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3014 | |
| 3015 | /* Call update without calling setup beforehand. */ |
| 3016 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3017 | text, sizeof( text ), |
| 3018 | buffer, sizeof( buffer ), |
| 3019 | &length ), |
| 3020 | PSA_ERROR_BAD_STATE ); |
| 3021 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3022 | |
| 3023 | /* Call update without an IV where an IV is required. */ |
| 3024 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3025 | text, sizeof( text ), |
| 3026 | buffer, sizeof( buffer ), |
| 3027 | &length ), |
| 3028 | PSA_ERROR_BAD_STATE ); |
| 3029 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3030 | |
| 3031 | /* Call update after finish. */ |
| 3032 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3033 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3034 | iv, sizeof( iv ) ) ); |
| 3035 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3036 | buffer, sizeof( buffer ), &length ) ); |
| 3037 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3038 | text, sizeof( text ), |
| 3039 | buffer, sizeof( buffer ), |
| 3040 | &length ), |
| 3041 | PSA_ERROR_BAD_STATE ); |
| 3042 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3043 | |
| 3044 | /* Call finish without calling setup beforehand. */ |
| 3045 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3046 | buffer, sizeof( buffer ), &length ), |
| 3047 | PSA_ERROR_BAD_STATE ); |
| 3048 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3049 | |
| 3050 | /* Call finish without an IV where an IV is required. */ |
| 3051 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3052 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3053 | * for cipher modes with padding. */ |
| 3054 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3055 | buffer, sizeof( buffer ), &length ), |
| 3056 | PSA_ERROR_BAD_STATE ); |
| 3057 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3058 | |
| 3059 | /* Call finish twice in a row. */ |
| 3060 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3061 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3062 | iv, sizeof( iv ) ) ); |
| 3063 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3064 | buffer, sizeof( buffer ), &length ) ); |
| 3065 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3066 | buffer, sizeof( buffer ), &length ), |
| 3067 | PSA_ERROR_BAD_STATE ); |
| 3068 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3069 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3070 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 3071 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3072 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3073 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3074 | } |
| 3075 | /* END_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3076 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3077 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3078 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3079 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3080 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3081 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3082 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3083 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3084 | psa_status_t status; |
| 3085 | psa_key_type_t key_type = key_type_arg; |
| 3086 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3087 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3088 | unsigned char *output = NULL; |
| 3089 | size_t output_buffer_size = 0; |
| 3090 | size_t function_output_length = 0; |
| 3091 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3092 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3093 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3094 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3095 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3096 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3097 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3098 | psa_set_key_algorithm( &attributes, alg ); |
| 3099 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3100 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3101 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3102 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3103 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3104 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3105 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3106 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3107 | output_buffer_size = ( (size_t) input->len + |
| 3108 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3109 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3110 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3111 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3112 | input->x, input->len, |
| 3113 | output, output_buffer_size, |
| 3114 | &function_output_length ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3115 | total_output_length += function_output_length; |
| 3116 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3117 | output + total_output_length, |
| 3118 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3119 | &function_output_length ); |
| 3120 | total_output_length += function_output_length; |
| 3121 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3122 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3123 | if( expected_status == PSA_SUCCESS ) |
| 3124 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3125 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3126 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3127 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | exit: |
| 3131 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3132 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3133 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3134 | } |
| 3135 | /* END_CASE */ |
| 3136 | |
| 3137 | /* BEGIN_CASE */ |
| 3138 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3139 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3140 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3141 | int first_part_size_arg, |
| 3142 | int output1_length_arg, int output2_length_arg, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3143 | data_t *expected_output ) |
| 3144 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3145 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3146 | psa_key_type_t key_type = key_type_arg; |
| 3147 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3148 | size_t first_part_size = first_part_size_arg; |
| 3149 | size_t output1_length = output1_length_arg; |
| 3150 | size_t output2_length = output2_length_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3151 | unsigned char *output = NULL; |
| 3152 | size_t output_buffer_size = 0; |
| 3153 | size_t function_output_length = 0; |
| 3154 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3155 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3156 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3159 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3160 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3161 | psa_set_key_algorithm( &attributes, alg ); |
| 3162 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3163 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3164 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3165 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3166 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3167 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3168 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3169 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3170 | output_buffer_size = ( (size_t) input->len + |
| 3171 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3172 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3173 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3174 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3175 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3176 | output, output_buffer_size, |
| 3177 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3178 | TEST_ASSERT( function_output_length == output1_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_update( &operation, |
| 3181 | input->x + first_part_size, |
| 3182 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3183 | output + total_output_length, |
| 3184 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3185 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3186 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3187 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3188 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3189 | output + total_output_length, |
| 3190 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3191 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3192 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3193 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3194 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3195 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3196 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3197 | |
| 3198 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3199 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3200 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3201 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3202 | } |
| 3203 | /* END_CASE */ |
| 3204 | |
| 3205 | /* BEGIN_CASE */ |
| 3206 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3207 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3208 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3209 | int first_part_size_arg, |
| 3210 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3211 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3212 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3213 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3214 | |
| 3215 | psa_key_type_t key_type = key_type_arg; |
| 3216 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3217 | size_t first_part_size = first_part_size_arg; |
| 3218 | size_t output1_length = output1_length_arg; |
| 3219 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3220 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3221 | size_t output_buffer_size = 0; |
| 3222 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3223 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3224 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3225 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3226 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3227 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3228 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3229 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3230 | psa_set_key_algorithm( &attributes, alg ); |
| 3231 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3232 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3233 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3234 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3235 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3236 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3237 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3238 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3239 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3240 | output_buffer_size = ( (size_t) input->len + |
| 3241 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3242 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3243 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3244 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3245 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3246 | input->x, first_part_size, |
| 3247 | output, output_buffer_size, |
| 3248 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3249 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3250 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3251 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3252 | input->x + first_part_size, |
| 3253 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3254 | output + total_output_length, |
| 3255 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3256 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3257 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3258 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3259 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3260 | output + total_output_length, |
| 3261 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3262 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3263 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3264 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3265 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3266 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3267 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3268 | |
| 3269 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3270 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3271 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3272 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3273 | } |
| 3274 | /* END_CASE */ |
| 3275 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3276 | /* BEGIN_CASE */ |
| 3277 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3278 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3279 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3280 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3281 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3282 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3283 | psa_status_t status; |
| 3284 | psa_key_type_t key_type = key_type_arg; |
| 3285 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3286 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3287 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3288 | size_t output_buffer_size = 0; |
| 3289 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3290 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3291 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3292 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3293 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3294 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3295 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3296 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3297 | psa_set_key_algorithm( &attributes, alg ); |
| 3298 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3299 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3300 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3301 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3302 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3303 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3304 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3305 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3306 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3307 | output_buffer_size = ( (size_t) input->len + |
| 3308 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3309 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3310 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3311 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3312 | input->x, input->len, |
| 3313 | output, output_buffer_size, |
| 3314 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3315 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3316 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3317 | output + total_output_length, |
| 3318 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3319 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3320 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3321 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3322 | |
| 3323 | if( expected_status == PSA_SUCCESS ) |
| 3324 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3325 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3326 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3327 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3328 | } |
| 3329 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3330 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3331 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3332 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3333 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3334 | } |
| 3335 | /* END_CASE */ |
| 3336 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3337 | /* BEGIN_CASE */ |
| 3338 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3339 | data_t *key, |
| 3340 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3341 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3342 | psa_key_handle_t handle = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3343 | psa_key_type_t key_type = key_type_arg; |
| 3344 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 3345 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3346 | size_t iv_size = 16; |
| 3347 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3348 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3349 | size_t output1_size = 0; |
| 3350 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3351 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3352 | size_t output2_size = 0; |
| 3353 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3354 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3355 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3356 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3357 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3358 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3359 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3360 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3361 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3362 | psa_set_key_algorithm( &attributes, alg ); |
| 3363 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3364 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3365 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3366 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3367 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3368 | handle, alg ) ); |
| 3369 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3370 | handle, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3371 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3372 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3373 | iv, iv_size, |
| 3374 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3375 | output1_size = ( (size_t) input->len + |
| 3376 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3377 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3378 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3379 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3380 | output1, output1_size, |
| 3381 | &output1_length ) ); |
| 3382 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3383 | output1 + output1_length, |
| 3384 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3385 | &function_output_length ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3386 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3387 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3388 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3389 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3390 | |
| 3391 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3392 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3393 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3394 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3395 | iv, iv_length ) ); |
| 3396 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3397 | output2, output2_size, |
| 3398 | &output2_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3399 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3400 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3401 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3402 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3403 | &function_output_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3404 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3405 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3406 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3407 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3408 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3409 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3410 | |
| 3411 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3412 | mbedtls_free( output1 ); |
| 3413 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3414 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3415 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3416 | } |
| 3417 | /* END_CASE */ |
| 3418 | |
| 3419 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3420 | void cipher_verify_output_multipart( int alg_arg, |
| 3421 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3422 | data_t *key, |
| 3423 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3424 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3425 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3426 | psa_key_handle_t handle = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3427 | psa_key_type_t key_type = key_type_arg; |
| 3428 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3429 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3430 | unsigned char iv[16] = {0}; |
| 3431 | size_t iv_size = 16; |
| 3432 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3433 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3434 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3435 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3436 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3437 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3438 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3439 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3440 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3441 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3442 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3443 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3444 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3445 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3446 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3447 | psa_set_key_algorithm( &attributes, alg ); |
| 3448 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3449 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3450 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3451 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3452 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3453 | handle, alg ) ); |
| 3454 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3455 | handle, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3456 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3457 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3458 | iv, iv_size, |
| 3459 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3460 | output1_buffer_size = ( (size_t) input->len + |
| 3461 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3462 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3463 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3464 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3465 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3466 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3467 | output1, output1_buffer_size, |
| 3468 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3469 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3470 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3471 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3472 | input->x + first_part_size, |
| 3473 | input->len - first_part_size, |
| 3474 | output1, output1_buffer_size, |
| 3475 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3476 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3477 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3478 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3479 | output1 + output1_length, |
| 3480 | output1_buffer_size - output1_length, |
| 3481 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3482 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3483 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3484 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3485 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3486 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3487 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3488 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3489 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3490 | iv, iv_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3491 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3492 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3493 | output2, output2_buffer_size, |
| 3494 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3495 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3496 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3497 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3498 | output1 + first_part_size, |
| 3499 | output1_length - first_part_size, |
| 3500 | output2, output2_buffer_size, |
| 3501 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3502 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3503 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3504 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3505 | output2 + output2_length, |
| 3506 | output2_buffer_size - output2_length, |
| 3507 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3508 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3509 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3510 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3511 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3512 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3513 | |
| 3514 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3515 | mbedtls_free( output1 ); |
| 3516 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3517 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3518 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3519 | } |
| 3520 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3521 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3522 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3523 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3524 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3525 | data_t *nonce, |
| 3526 | data_t *additional_data, |
| 3527 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3528 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3529 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3530 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3531 | psa_key_type_t key_type = key_type_arg; |
| 3532 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3533 | unsigned char *output_data = NULL; |
| 3534 | size_t output_size = 0; |
| 3535 | size_t output_length = 0; |
| 3536 | unsigned char *output_data2 = NULL; |
| 3537 | size_t output_length2 = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3538 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3539 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3540 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3541 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3542 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3543 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3544 | * should be exact. */ |
| 3545 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3546 | TEST_EQUAL( output_size, |
| 3547 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3548 | ASSERT_ALLOC( output_data, output_size ); |
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_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3551 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3552 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3553 | psa_set_key_algorithm( &attributes, alg ); |
| 3554 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3555 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3556 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3557 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3558 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3559 | TEST_EQUAL( psa_aead_encrypt( handle, alg, |
| 3560 | nonce->x, nonce->len, |
| 3561 | additional_data->x, |
| 3562 | additional_data->len, |
| 3563 | input_data->x, input_data->len, |
| 3564 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3565 | &output_length ), |
| 3566 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3567 | |
| 3568 | if( PSA_SUCCESS == expected_result ) |
| 3569 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3570 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3571 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3572 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3573 | * should be exact. */ |
| 3574 | TEST_EQUAL( input_data->len, |
| 3575 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) ); |
| 3576 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3577 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3578 | nonce->x, nonce->len, |
| 3579 | additional_data->x, |
| 3580 | additional_data->len, |
| 3581 | output_data, output_length, |
| 3582 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3583 | &output_length2 ), |
| 3584 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3585 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3586 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3587 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3588 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3589 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3590 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3591 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3592 | mbedtls_free( output_data ); |
| 3593 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3594 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3595 | } |
| 3596 | /* END_CASE */ |
| 3597 | |
| 3598 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3599 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3600 | int alg_arg, |
| 3601 | data_t *nonce, |
| 3602 | data_t *additional_data, |
| 3603 | data_t *input_data, |
| 3604 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3605 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3606 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3607 | psa_key_type_t key_type = key_type_arg; |
| 3608 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3609 | unsigned char *output_data = NULL; |
| 3610 | size_t output_size = 0; |
| 3611 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3612 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3613 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3614 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3615 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3616 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3617 | * should be exact. */ |
| 3618 | TEST_EQUAL( output_size, |
| 3619 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3620 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3621 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3622 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3623 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3624 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3625 | psa_set_key_algorithm( &attributes, alg ); |
| 3626 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3627 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3628 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3629 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3630 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3631 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 3632 | nonce->x, nonce->len, |
| 3633 | additional_data->x, additional_data->len, |
| 3634 | input_data->x, input_data->len, |
| 3635 | output_data, output_size, |
| 3636 | &output_length ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3637 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3638 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3639 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3640 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3641 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3642 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3643 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3644 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3645 | } |
| 3646 | /* END_CASE */ |
| 3647 | |
| 3648 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3649 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3650 | int alg_arg, |
| 3651 | data_t *nonce, |
| 3652 | data_t *additional_data, |
| 3653 | data_t *input_data, |
| 3654 | data_t *expected_data, |
| 3655 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3656 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3657 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3658 | psa_key_type_t key_type = key_type_arg; |
| 3659 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3660 | unsigned char *output_data = NULL; |
| 3661 | size_t output_size = 0; |
| 3662 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3663 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3664 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3665 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3666 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3667 | output_size = input_data->len - tag_length; |
| 3668 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3669 | * should be exact. */ |
| 3670 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3671 | TEST_EQUAL( output_size, |
| 3672 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3673 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3674 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3675 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3676 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3677 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3678 | psa_set_key_algorithm( &attributes, alg ); |
| 3679 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3680 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3681 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3682 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3683 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3684 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3685 | nonce->x, nonce->len, |
| 3686 | additional_data->x, |
| 3687 | additional_data->len, |
| 3688 | input_data->x, input_data->len, |
| 3689 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3690 | &output_length ), |
| 3691 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3692 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3693 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3694 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3695 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3696 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3697 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3698 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3699 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3700 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3701 | } |
| 3702 | /* END_CASE */ |
| 3703 | |
| 3704 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3705 | void signature_size( int type_arg, |
| 3706 | int bits, |
| 3707 | int alg_arg, |
| 3708 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3709 | { |
| 3710 | psa_key_type_t type = type_arg; |
| 3711 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3712 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3713 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3714 | exit: |
| 3715 | ; |
| 3716 | } |
| 3717 | /* END_CASE */ |
| 3718 | |
| 3719 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3720 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 3721 | int alg_arg, data_t *input_data, |
| 3722 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3723 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3724 | psa_key_handle_t handle = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3725 | psa_key_type_t key_type = key_type_arg; |
| 3726 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3727 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3728 | unsigned char *signature = NULL; |
| 3729 | size_t signature_size; |
| 3730 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3731 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3732 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3733 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3734 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3735 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 3736 | psa_set_key_algorithm( &attributes, alg ); |
| 3737 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3738 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3739 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3740 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3741 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3742 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3743 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3744 | /* Allocate a buffer which has the size advertized by the |
| 3745 | * library. */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3746 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3747 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3748 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3749 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3750 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3751 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3752 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3753 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3754 | input_data->x, input_data->len, |
| 3755 | signature, signature_size, |
| 3756 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3757 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3758 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3759 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3760 | |
| 3761 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3762 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3763 | psa_destroy_key( handle ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3764 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3765 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3766 | } |
| 3767 | /* END_CASE */ |
| 3768 | |
| 3769 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3770 | void sign_fail( int key_type_arg, data_t *key_data, |
| 3771 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3772 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3773 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3774 | psa_key_handle_t handle = 0; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3775 | psa_key_type_t key_type = key_type_arg; |
| 3776 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3777 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3778 | psa_status_t actual_status; |
| 3779 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3780 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3781 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3782 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3783 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3784 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3785 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3786 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3787 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3788 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 3789 | psa_set_key_algorithm( &attributes, alg ); |
| 3790 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3791 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3792 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3793 | &handle ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3794 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3795 | actual_status = psa_asymmetric_sign( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3796 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3797 | signature, signature_size, |
| 3798 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3799 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3800 | /* The value of *signature_length is unspecified on error, but |
| 3801 | * whatever it is, it should be less than signature_size, so that |
| 3802 | * if the caller tries to read *signature_length bytes without |
| 3803 | * checking the error code then they don't overflow a buffer. */ |
| 3804 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3805 | |
| 3806 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3807 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3808 | psa_destroy_key( handle ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3809 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3810 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3811 | } |
| 3812 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3813 | |
| 3814 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3815 | void sign_verify( int key_type_arg, data_t *key_data, |
| 3816 | int alg_arg, data_t *input_data ) |
| 3817 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3818 | psa_key_handle_t handle = 0; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3819 | psa_key_type_t key_type = key_type_arg; |
| 3820 | psa_algorithm_t alg = alg_arg; |
| 3821 | size_t key_bits; |
| 3822 | unsigned char *signature = NULL; |
| 3823 | size_t signature_size; |
| 3824 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3825 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3826 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3827 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3828 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3829 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 3830 | psa_set_key_algorithm( &attributes, alg ); |
| 3831 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3832 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3833 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3834 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3835 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3836 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3837 | |
| 3838 | /* Allocate a buffer which has the size advertized by the |
| 3839 | * library. */ |
| 3840 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3841 | key_bits, alg ); |
| 3842 | TEST_ASSERT( signature_size != 0 ); |
| 3843 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3844 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3845 | |
| 3846 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3847 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3848 | input_data->x, input_data->len, |
| 3849 | signature, signature_size, |
| 3850 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3851 | /* Check that the signature length looks sensible. */ |
| 3852 | TEST_ASSERT( signature_length <= signature_size ); |
| 3853 | TEST_ASSERT( signature_length > 0 ); |
| 3854 | |
| 3855 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3856 | PSA_ASSERT( psa_asymmetric_verify( |
| 3857 | handle, alg, |
| 3858 | input_data->x, input_data->len, |
| 3859 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3860 | |
| 3861 | if( input_data->len != 0 ) |
| 3862 | { |
| 3863 | /* Flip a bit in the input and verify that the signature is now |
| 3864 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3865 | * because ECDSA may ignore the last few bits of the input. */ |
| 3866 | input_data->x[0] ^= 1; |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3867 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 3868 | input_data->x, input_data->len, |
| 3869 | signature, signature_length ), |
| 3870 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3874 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3875 | psa_destroy_key( handle ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3876 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3877 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3878 | } |
| 3879 | /* END_CASE */ |
| 3880 | |
| 3881 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3882 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 3883 | int alg_arg, data_t *hash_data, |
| 3884 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3885 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3886 | psa_key_handle_t handle = 0; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3887 | psa_key_type_t key_type = key_type_arg; |
| 3888 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3889 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3890 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3891 | TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
| 3892 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3893 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3894 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3895 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 3896 | psa_set_key_algorithm( &attributes, alg ); |
| 3897 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3898 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3899 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3900 | &handle ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3901 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3902 | PSA_ASSERT( psa_asymmetric_verify( handle, alg, |
| 3903 | hash_data->x, hash_data->len, |
| 3904 | signature_data->x, |
| 3905 | signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3906 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3907 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3908 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3909 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3910 | } |
| 3911 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3912 | |
| 3913 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3914 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 3915 | int alg_arg, data_t *hash_data, |
| 3916 | data_t *signature_data, |
| 3917 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3918 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3919 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3920 | psa_key_type_t key_type = key_type_arg; |
| 3921 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3922 | psa_status_t actual_status; |
| 3923 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3924 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3925 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3926 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3927 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3928 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 3929 | psa_set_key_algorithm( &attributes, alg ); |
| 3930 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3931 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3932 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3933 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3934 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3935 | actual_status = psa_asymmetric_verify( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3936 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3937 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3938 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3939 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3940 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3941 | |
| 3942 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3943 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3944 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3945 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3946 | } |
| 3947 | /* END_CASE */ |
| 3948 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3949 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3950 | void asymmetric_encrypt( int key_type_arg, |
| 3951 | data_t *key_data, |
| 3952 | int alg_arg, |
| 3953 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3954 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3955 | int expected_output_length_arg, |
| 3956 | int expected_status_arg ) |
| 3957 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3958 | psa_key_handle_t handle = 0; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3959 | psa_key_type_t key_type = key_type_arg; |
| 3960 | psa_algorithm_t alg = alg_arg; |
| 3961 | size_t expected_output_length = expected_output_length_arg; |
| 3962 | size_t key_bits; |
| 3963 | unsigned char *output = NULL; |
| 3964 | size_t output_size; |
| 3965 | size_t output_length = ~0; |
| 3966 | psa_status_t actual_status; |
| 3967 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3968 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3969 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3970 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3971 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3972 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3973 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3974 | psa_set_key_algorithm( &attributes, alg ); |
| 3975 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3976 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3977 | &handle ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3978 | |
| 3979 | /* Determine the maximum output length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3980 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3981 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3982 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3983 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3984 | |
| 3985 | /* Encrypt the input */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3986 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3987 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3988 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3989 | output, output_size, |
| 3990 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3991 | TEST_EQUAL( actual_status, expected_status ); |
| 3992 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3993 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3994 | /* If the label is empty, the test framework puts a non-null pointer |
| 3995 | * in label->x. Test that a null pointer works as well. */ |
| 3996 | if( label->len == 0 ) |
| 3997 | { |
| 3998 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3999 | if( output_size != 0 ) |
| 4000 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4001 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4002 | input_data->x, input_data->len, |
| 4003 | NULL, label->len, |
| 4004 | output, output_size, |
| 4005 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4006 | TEST_EQUAL( actual_status, expected_status ); |
| 4007 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4008 | } |
| 4009 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4010 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4011 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4012 | psa_destroy_key( handle ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4013 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4014 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4015 | } |
| 4016 | /* END_CASE */ |
| 4017 | |
| 4018 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4019 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4020 | data_t *key_data, |
| 4021 | int alg_arg, |
| 4022 | data_t *input_data, |
| 4023 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4024 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4025 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4026 | psa_key_type_t key_type = key_type_arg; |
| 4027 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4028 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4029 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4030 | size_t output_size; |
| 4031 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4032 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4033 | size_t output2_size; |
| 4034 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4035 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4036 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4037 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4038 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4039 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4040 | psa_set_key_algorithm( &attributes, alg ); |
| 4041 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4042 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4043 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4044 | &handle ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4045 | |
| 4046 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4047 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4048 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4049 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4050 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4051 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4052 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4053 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4054 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4055 | * the original plaintext because of the non-optional random |
| 4056 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4057 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 4058 | input_data->x, input_data->len, |
| 4059 | label->x, label->len, |
| 4060 | output, output_size, |
| 4061 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4062 | /* We don't know what ciphertext length to expect, but check that |
| 4063 | * it looks sensible. */ |
| 4064 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4065 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4066 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4067 | output, output_length, |
| 4068 | label->x, label->len, |
| 4069 | output2, output2_size, |
| 4070 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4071 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4072 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4073 | |
| 4074 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4075 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4076 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4077 | mbedtls_free( output ); |
| 4078 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4079 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4080 | } |
| 4081 | /* END_CASE */ |
| 4082 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4083 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4084 | void asymmetric_decrypt( int key_type_arg, |
| 4085 | data_t *key_data, |
| 4086 | int alg_arg, |
| 4087 | data_t *input_data, |
| 4088 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4089 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4090 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4091 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4092 | psa_key_type_t key_type = key_type_arg; |
| 4093 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4094 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4095 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4096 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4097 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4098 | |
Jaeden Amero | 412654a | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4099 | output_size = expected_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4100 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4101 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4102 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4103 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4104 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4105 | psa_set_key_algorithm( &attributes, alg ); |
| 4106 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4107 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4108 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4109 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4110 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4111 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4112 | input_data->x, input_data->len, |
| 4113 | label->x, label->len, |
| 4114 | output, |
| 4115 | output_size, |
| 4116 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4117 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4118 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4119 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4120 | /* If the label is empty, the test framework puts a non-null pointer |
| 4121 | * in label->x. Test that a null pointer works as well. */ |
| 4122 | if( label->len == 0 ) |
| 4123 | { |
| 4124 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4125 | if( output_size != 0 ) |
| 4126 | memset( output, 0, output_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4127 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4128 | input_data->x, input_data->len, |
| 4129 | NULL, label->len, |
| 4130 | output, |
| 4131 | output_size, |
| 4132 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4133 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4134 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4135 | } |
| 4136 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4137 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4138 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4139 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4140 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4141 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4142 | } |
| 4143 | /* END_CASE */ |
| 4144 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4145 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4146 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4147 | data_t *key_data, |
| 4148 | int alg_arg, |
| 4149 | data_t *input_data, |
| 4150 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4151 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4152 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4153 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4154 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4155 | psa_key_type_t key_type = key_type_arg; |
| 4156 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4157 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4158 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4159 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4160 | psa_status_t actual_status; |
| 4161 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4162 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4163 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4164 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4165 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4166 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4167 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4168 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4169 | psa_set_key_algorithm( &attributes, alg ); |
| 4170 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4171 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4172 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4173 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4174 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4175 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4176 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4177 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4178 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4179 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4180 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4181 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4182 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4183 | /* If the label is empty, the test framework puts a non-null pointer |
| 4184 | * in label->x. Test that a null pointer works as well. */ |
| 4185 | if( label->len == 0 ) |
| 4186 | { |
| 4187 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4188 | if( output_size != 0 ) |
| 4189 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4190 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4191 | input_data->x, input_data->len, |
| 4192 | NULL, label->len, |
| 4193 | output, output_size, |
| 4194 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4195 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4196 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4197 | } |
| 4198 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4199 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4200 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4201 | psa_destroy_key( handle ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4202 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4203 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4204 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4205 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4206 | |
| 4207 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4208 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4209 | { |
| 4210 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4211 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4212 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4213 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4214 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4215 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4216 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4217 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4218 | |
| 4219 | memset( &zero, 0, sizeof( zero ) ); |
| 4220 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4221 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4222 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4223 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4224 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4225 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4226 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4227 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4228 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4229 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4230 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4231 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4232 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4233 | } |
| 4234 | /* END_CASE */ |
| 4235 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4236 | /* BEGIN_CASE */ |
| 4237 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4238 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4239 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4240 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4241 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4242 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4243 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4244 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4245 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4246 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4247 | |
| 4248 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4249 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4250 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4251 | } |
| 4252 | /* END_CASE */ |
| 4253 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4254 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4255 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4256 | int expected_status_arg ) |
| 4257 | { |
| 4258 | psa_algorithm_t alg = alg_arg; |
| 4259 | size_t capacity = capacity_arg; |
| 4260 | psa_status_t expected_status = expected_status_arg; |
| 4261 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4262 | |
| 4263 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4264 | |
| 4265 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4266 | |
| 4267 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4268 | expected_status ); |
| 4269 | |
| 4270 | exit: |
| 4271 | psa_key_derivation_abort( &operation ); |
| 4272 | PSA_DONE( ); |
| 4273 | } |
| 4274 | /* END_CASE */ |
| 4275 | |
| 4276 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4277 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4278 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4279 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4280 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4281 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4282 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4283 | int expected_status_arg3, |
| 4284 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4285 | { |
| 4286 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4287 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 4288 | psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3}; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4289 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4290 | expected_status_arg2, |
| 4291 | expected_status_arg3}; |
| 4292 | data_t *inputs[] = {input1, input2, input3}; |
| 4293 | psa_key_handle_t handles[] = {0, 0, 0}; |
| 4294 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4295 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4296 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4297 | psa_key_type_t output_key_type = output_key_type_arg; |
| 4298 | psa_key_handle_t output_handle = 0; |
| 4299 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4300 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4301 | |
| 4302 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4303 | |
| 4304 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4305 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4306 | |
| 4307 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4308 | |
| 4309 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4310 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4311 | if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4312 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4313 | psa_set_key_type( &attributes, key_types[i] ); |
| 4314 | PSA_ASSERT( psa_import_key( &attributes, |
| 4315 | inputs[i]->x, inputs[i]->len, |
| 4316 | &handles[i] ) ); |
| 4317 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
| 4318 | handles[i] ), |
| 4319 | expected_statuses[i] ); |
| 4320 | } |
| 4321 | else |
| 4322 | { |
| 4323 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4324 | &operation, steps[i], |
| 4325 | inputs[i]->x, inputs[i]->len ), |
| 4326 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4327 | } |
| 4328 | } |
| 4329 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4330 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 4331 | { |
| 4332 | psa_reset_key_attributes( &attributes ); |
| 4333 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 4334 | psa_set_key_bits( &attributes, 8 ); |
| 4335 | actual_output_status = |
| 4336 | psa_key_derivation_output_key( &attributes, &operation, |
| 4337 | &output_handle ); |
| 4338 | } |
| 4339 | else |
| 4340 | { |
| 4341 | uint8_t buffer[1]; |
| 4342 | actual_output_status = |
| 4343 | psa_key_derivation_output_bytes( &operation, |
| 4344 | buffer, sizeof( buffer ) ); |
| 4345 | } |
| 4346 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 4347 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4348 | exit: |
| 4349 | psa_key_derivation_abort( &operation ); |
| 4350 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4351 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4352 | psa_destroy_key( output_handle ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4353 | PSA_DONE( ); |
| 4354 | } |
| 4355 | /* END_CASE */ |
| 4356 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4357 | /* BEGIN_CASE */ |
| 4358 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4359 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4360 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4361 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4362 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4363 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4364 | unsigned char input1[] = "Input 1"; |
| 4365 | size_t input1_length = sizeof( input1 ); |
| 4366 | unsigned char input2[] = "Input 2"; |
| 4367 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4368 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4369 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4370 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4371 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4372 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4373 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4374 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4375 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4376 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4377 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4378 | psa_set_key_algorithm( &attributes, alg ); |
| 4379 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4380 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4381 | PSA_ASSERT( psa_import_key( &attributes, |
| 4382 | key_data, sizeof( key_data ), |
| 4383 | &handle ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4384 | |
| 4385 | /* valid key derivation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4386 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4387 | input1, input1_length, |
| 4388 | input2, input2_length, |
| 4389 | capacity ) ) |
| 4390 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4391 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4392 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4393 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4394 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4395 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4396 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4397 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4398 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4399 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4400 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4401 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4402 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4403 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4404 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4405 | } |
| 4406 | /* END_CASE */ |
| 4407 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4408 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4409 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4410 | { |
| 4411 | uint8_t output_buffer[16]; |
| 4412 | size_t buffer_size = 16; |
| 4413 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4414 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4415 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4416 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4417 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4418 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4419 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4420 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4421 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4422 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4423 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4424 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4425 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4426 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4427 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4428 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4429 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4430 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4431 | |
| 4432 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4433 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4434 | } |
| 4435 | /* END_CASE */ |
| 4436 | |
| 4437 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4438 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4439 | int step1_arg, data_t *input1, |
| 4440 | int step2_arg, data_t *input2, |
| 4441 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4442 | int requested_capacity_arg, |
| 4443 | data_t *expected_output1, |
| 4444 | data_t *expected_output2 ) |
| 4445 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4446 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4447 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4448 | data_t *inputs[] = {input1, input2, input3}; |
| 4449 | psa_key_handle_t handles[] = {0, 0, 0}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4450 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4451 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4452 | uint8_t *expected_outputs[2] = |
| 4453 | {expected_output1->x, expected_output2->x}; |
| 4454 | size_t output_sizes[2] = |
| 4455 | {expected_output1->len, expected_output2->len}; |
| 4456 | size_t output_buffer_size = 0; |
| 4457 | uint8_t *output_buffer = NULL; |
| 4458 | size_t expected_capacity; |
| 4459 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4460 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4461 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4462 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4463 | |
| 4464 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4465 | { |
| 4466 | if( output_sizes[i] > output_buffer_size ) |
| 4467 | output_buffer_size = output_sizes[i]; |
| 4468 | if( output_sizes[i] == 0 ) |
| 4469 | expected_outputs[i] = NULL; |
| 4470 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4471 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4472 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4473 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4474 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4475 | psa_set_key_algorithm( &attributes, alg ); |
| 4476 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4477 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4478 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4479 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4480 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4481 | requested_capacity ) ); |
| 4482 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4483 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4484 | switch( steps[i] ) |
| 4485 | { |
| 4486 | case 0: |
| 4487 | break; |
| 4488 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4489 | PSA_ASSERT( psa_import_key( &attributes, |
| 4490 | inputs[i]->x, inputs[i]->len, |
| 4491 | &handles[i] ) ); |
| 4492 | PSA_ASSERT( psa_key_derivation_input_key( |
| 4493 | &operation, steps[i], |
| 4494 | handles[i] ) ); |
| 4495 | break; |
| 4496 | default: |
| 4497 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4498 | &operation, steps[i], |
| 4499 | inputs[i]->x, inputs[i]->len ) ); |
| 4500 | break; |
| 4501 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4502 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4503 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4504 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4505 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4506 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4507 | expected_capacity = requested_capacity; |
| 4508 | |
| 4509 | /* Expansion phase. */ |
| 4510 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4511 | { |
| 4512 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4513 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4514 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4515 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4516 | { |
| 4517 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4518 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4519 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4520 | continue; |
| 4521 | } |
| 4522 | else if( expected_capacity == 0 || |
| 4523 | output_sizes[i] > expected_capacity ) |
| 4524 | { |
| 4525 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4526 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4527 | expected_capacity = 0; |
| 4528 | continue; |
| 4529 | } |
| 4530 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4531 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4532 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4533 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4534 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4535 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4536 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4537 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4538 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4539 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4540 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4541 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4542 | |
| 4543 | exit: |
| 4544 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4545 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4546 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4547 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4548 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4549 | } |
| 4550 | /* END_CASE */ |
| 4551 | |
| 4552 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4553 | void derive_full( int alg_arg, |
| 4554 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4555 | data_t *input1, |
| 4556 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4557 | int requested_capacity_arg ) |
| 4558 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4559 | psa_key_handle_t handle = 0; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4560 | psa_algorithm_t alg = alg_arg; |
| 4561 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4562 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4563 | unsigned char output_buffer[16]; |
| 4564 | size_t expected_capacity = requested_capacity; |
| 4565 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4566 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4567 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4568 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4569 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4570 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4571 | psa_set_key_algorithm( &attributes, alg ); |
| 4572 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4573 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4574 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4575 | &handle ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4576 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4577 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4578 | input1->x, input1->len, |
| 4579 | input2->x, input2->len, |
| 4580 | requested_capacity ) ) |
| 4581 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4582 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4583 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4584 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4585 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4586 | |
| 4587 | /* Expansion phase. */ |
| 4588 | while( current_capacity > 0 ) |
| 4589 | { |
| 4590 | size_t read_size = sizeof( output_buffer ); |
| 4591 | if( read_size > current_capacity ) |
| 4592 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4593 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4594 | output_buffer, |
| 4595 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4596 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4597 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4598 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4599 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4600 | } |
| 4601 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4602 | /* Check that the operation refuses to go over capacity. */ |
| 4603 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4604 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4605 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4606 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4607 | |
| 4608 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4609 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4610 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4611 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4612 | } |
| 4613 | /* END_CASE */ |
| 4614 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4615 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4616 | void derive_key_exercise( int alg_arg, |
| 4617 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4618 | data_t *input1, |
| 4619 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4620 | int derived_type_arg, |
| 4621 | int derived_bits_arg, |
| 4622 | int derived_usage_arg, |
| 4623 | int derived_alg_arg ) |
| 4624 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4625 | psa_key_handle_t base_handle = 0; |
| 4626 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4627 | psa_algorithm_t alg = alg_arg; |
| 4628 | psa_key_type_t derived_type = derived_type_arg; |
| 4629 | size_t derived_bits = derived_bits_arg; |
| 4630 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4631 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4632 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4633 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4634 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4635 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4636 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4637 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4638 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4639 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4640 | psa_set_key_algorithm( &attributes, alg ); |
| 4641 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4642 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4643 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4644 | |
| 4645 | /* Derive a key. */ |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4646 | if ( setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4647 | input1->x, input1->len, |
| 4648 | input2->x, input2->len, capacity ) ) |
| 4649 | goto exit; |
| 4650 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4651 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4652 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4653 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4654 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4655 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4656 | &derived_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4657 | |
| 4658 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4659 | PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) ); |
| 4660 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4661 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4662 | |
| 4663 | /* Exercise the derived key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4664 | if( ! exercise_key( derived_handle, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4665 | goto exit; |
| 4666 | |
| 4667 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4668 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4669 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4670 | psa_destroy_key( base_handle ); |
| 4671 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4672 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4673 | } |
| 4674 | /* END_CASE */ |
| 4675 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4676 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4677 | void derive_key_export( int alg_arg, |
| 4678 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4679 | data_t *input1, |
| 4680 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4681 | int bytes1_arg, |
| 4682 | int bytes2_arg ) |
| 4683 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4684 | psa_key_handle_t base_handle = 0; |
| 4685 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4686 | psa_algorithm_t alg = alg_arg; |
| 4687 | size_t bytes1 = bytes1_arg; |
| 4688 | size_t bytes2 = bytes2_arg; |
| 4689 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4690 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4691 | uint8_t *output_buffer = NULL; |
| 4692 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4693 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4694 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4695 | size_t length; |
| 4696 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4697 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4698 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4699 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4700 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4701 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4702 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4703 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4704 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 4705 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4706 | |
| 4707 | /* Derive some material and output it. */ |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4708 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4709 | input1->x, input1->len, |
| 4710 | input2->x, input2->len, capacity ) ) |
| 4711 | goto exit; |
| 4712 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4713 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4714 | output_buffer, |
| 4715 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4716 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4717 | |
| 4718 | /* Derive the same output again, but this time store it in key objects. */ |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4719 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4720 | input1->x, input1->len, |
| 4721 | input2->x, input2->len, capacity ) ) |
| 4722 | goto exit; |
| 4723 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4724 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4725 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 4726 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4727 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4728 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4729 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4730 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4731 | export_buffer, bytes1, |
| 4732 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4733 | TEST_EQUAL( length, bytes1 ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4734 | PSA_ASSERT( psa_destroy_key( derived_handle ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4735 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4736 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4737 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4738 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4739 | export_buffer + bytes1, bytes2, |
| 4740 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4741 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4742 | |
| 4743 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4744 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 4745 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4746 | |
| 4747 | exit: |
| 4748 | mbedtls_free( output_buffer ); |
| 4749 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4750 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4751 | psa_destroy_key( base_handle ); |
| 4752 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4753 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4754 | } |
| 4755 | /* END_CASE */ |
| 4756 | |
| 4757 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4758 | void derive_key( int alg_arg, |
| 4759 | data_t *key_data, data_t *input1, data_t *input2, |
| 4760 | int type_arg, int bits_arg, |
| 4761 | int expected_status_arg ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4762 | { |
| 4763 | psa_key_handle_t base_handle = 0; |
| 4764 | psa_key_handle_t derived_handle = 0; |
| 4765 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4766 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4767 | size_t bits = bits_arg; |
| 4768 | psa_status_t expected_status = expected_status_arg; |
| 4769 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4770 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4771 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4772 | |
| 4773 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4774 | |
| 4775 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4776 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4777 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 4778 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 4779 | &base_handle ) ); |
| 4780 | |
| 4781 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4782 | input1->x, input1->len, |
| 4783 | input2->x, input2->len, SIZE_MAX ) ) |
| 4784 | goto exit; |
| 4785 | |
| 4786 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4787 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4788 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4789 | psa_set_key_bits( &derived_attributes, bits ); |
| 4790 | TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 4791 | &derived_handle ), |
| 4792 | expected_status ); |
| 4793 | |
| 4794 | exit: |
| 4795 | psa_key_derivation_abort( &operation ); |
| 4796 | psa_destroy_key( base_handle ); |
| 4797 | psa_destroy_key( derived_handle ); |
| 4798 | PSA_DONE( ); |
| 4799 | } |
| 4800 | /* END_CASE */ |
| 4801 | |
| 4802 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4803 | void key_agreement_setup( int alg_arg, |
| 4804 | int our_key_type_arg, data_t *our_key_data, |
| 4805 | data_t *peer_key_data, |
| 4806 | int expected_status_arg ) |
| 4807 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4808 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4809 | psa_algorithm_t alg = alg_arg; |
| 4810 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4811 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4812 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4813 | psa_status_t expected_status = expected_status_arg; |
| 4814 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4815 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4816 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4817 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4818 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4819 | psa_set_key_algorithm( &attributes, alg ); |
| 4820 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4821 | PSA_ASSERT( psa_import_key( &attributes, |
| 4822 | our_key_data->x, our_key_data->len, |
| 4823 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4824 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4825 | /* The tests currently include inputs that should fail at either step. |
| 4826 | * Test cases that fail at the setup step should be changed to call |
| 4827 | * key_derivation_setup instead, and this function should be renamed |
| 4828 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4829 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4830 | if( status == PSA_SUCCESS ) |
| 4831 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4832 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 4833 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 4834 | our_key, |
| 4835 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4836 | expected_status ); |
| 4837 | } |
| 4838 | else |
| 4839 | { |
| 4840 | TEST_ASSERT( status == expected_status ); |
| 4841 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4842 | |
| 4843 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4844 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4845 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4846 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4847 | } |
| 4848 | /* END_CASE */ |
| 4849 | |
| 4850 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4851 | void raw_key_agreement( int alg_arg, |
| 4852 | int our_key_type_arg, data_t *our_key_data, |
| 4853 | data_t *peer_key_data, |
| 4854 | data_t *expected_output ) |
| 4855 | { |
| 4856 | psa_key_handle_t our_key = 0; |
| 4857 | psa_algorithm_t alg = alg_arg; |
| 4858 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4859 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4860 | unsigned char *output = NULL; |
| 4861 | size_t output_length = ~0; |
| 4862 | |
| 4863 | ASSERT_ALLOC( output, expected_output->len ); |
| 4864 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4865 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4866 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4867 | psa_set_key_algorithm( &attributes, alg ); |
| 4868 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4869 | PSA_ASSERT( psa_import_key( &attributes, |
| 4870 | our_key_data->x, our_key_data->len, |
| 4871 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4872 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4873 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 4874 | peer_key_data->x, peer_key_data->len, |
| 4875 | output, expected_output->len, |
| 4876 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4877 | ASSERT_COMPARE( output, output_length, |
| 4878 | expected_output->x, expected_output->len ); |
| 4879 | |
| 4880 | exit: |
| 4881 | mbedtls_free( output ); |
| 4882 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4883 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4884 | } |
| 4885 | /* END_CASE */ |
| 4886 | |
| 4887 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4888 | void key_agreement_capacity( int alg_arg, |
| 4889 | int our_key_type_arg, data_t *our_key_data, |
| 4890 | data_t *peer_key_data, |
| 4891 | int expected_capacity_arg ) |
| 4892 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4893 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4894 | psa_algorithm_t alg = alg_arg; |
| 4895 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4896 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4897 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4898 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4899 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4900 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4901 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4902 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4903 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4904 | psa_set_key_algorithm( &attributes, alg ); |
| 4905 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4906 | PSA_ASSERT( psa_import_key( &attributes, |
| 4907 | our_key_data->x, our_key_data->len, |
| 4908 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4909 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4910 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4911 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4912 | &operation, |
| 4913 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4914 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4915 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4916 | { |
| 4917 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4918 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4919 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4920 | NULL, 0 ) ); |
| 4921 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4922 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4923 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4924 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4925 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4926 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4927 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4928 | /* Test the actual capacity by reading the output. */ |
| 4929 | while( actual_capacity > sizeof( output ) ) |
| 4930 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4931 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4932 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4933 | actual_capacity -= sizeof( output ); |
| 4934 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4935 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4936 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4937 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4938 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4939 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4940 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4941 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4942 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4943 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4944 | } |
| 4945 | /* END_CASE */ |
| 4946 | |
| 4947 | /* BEGIN_CASE */ |
| 4948 | void key_agreement_output( int alg_arg, |
| 4949 | int our_key_type_arg, data_t *our_key_data, |
| 4950 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4951 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4952 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4953 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4954 | psa_algorithm_t alg = alg_arg; |
| 4955 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4956 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4957 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4958 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4959 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4960 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 4961 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4962 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4963 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4964 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4965 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4966 | psa_set_key_algorithm( &attributes, alg ); |
| 4967 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4968 | PSA_ASSERT( psa_import_key( &attributes, |
| 4969 | our_key_data->x, our_key_data->len, |
| 4970 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4971 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4972 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4973 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4974 | &operation, |
| 4975 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4976 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4977 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4978 | { |
| 4979 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4980 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4981 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4982 | NULL, 0 ) ); |
| 4983 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4984 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4985 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4986 | actual_output, |
| 4987 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4988 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 4989 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4990 | if( expected_output2->len != 0 ) |
| 4991 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4992 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4993 | actual_output, |
| 4994 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4995 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 4996 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4997 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4998 | |
| 4999 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5000 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5001 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5002 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5003 | mbedtls_free( actual_output ); |
| 5004 | } |
| 5005 | /* END_CASE */ |
| 5006 | |
| 5007 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5008 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5009 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5010 | size_t bytes = bytes_arg; |
| 5011 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5012 | unsigned char *output = NULL; |
| 5013 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5014 | size_t i; |
| 5015 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5016 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5017 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 5018 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5019 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5020 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5021 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5022 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5023 | /* Run several times, to ensure that every output byte will be |
| 5024 | * nonzero at least once with overwhelming probability |
| 5025 | * (2^(-8*number_of_runs)). */ |
| 5026 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5027 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5028 | if( bytes != 0 ) |
| 5029 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5030 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5031 | |
| 5032 | /* Check that no more than bytes have been overwritten */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5033 | ASSERT_COMPARE( output + bytes, sizeof( trail ), |
| 5034 | trail, sizeof( trail ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5035 | |
| 5036 | for( i = 0; i < bytes; i++ ) |
| 5037 | { |
| 5038 | if( output[i] != 0 ) |
| 5039 | ++changed[i]; |
| 5040 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5041 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5042 | |
| 5043 | /* Check that every byte was changed to nonzero at least once. This |
| 5044 | * validates that psa_generate_random is overwriting every byte of |
| 5045 | * the output buffer. */ |
| 5046 | for( i = 0; i < bytes; i++ ) |
| 5047 | { |
| 5048 | TEST_ASSERT( changed[i] != 0 ); |
| 5049 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5050 | |
| 5051 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5052 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5053 | mbedtls_free( output ); |
| 5054 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5055 | } |
| 5056 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5057 | |
| 5058 | /* BEGIN_CASE */ |
| 5059 | void generate_key( int type_arg, |
| 5060 | int bits_arg, |
| 5061 | int usage_arg, |
| 5062 | int alg_arg, |
| 5063 | int expected_status_arg ) |
| 5064 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5065 | psa_key_handle_t handle = 0; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5066 | psa_key_type_t type = type_arg; |
| 5067 | psa_key_usage_t usage = usage_arg; |
| 5068 | size_t bits = bits_arg; |
| 5069 | psa_algorithm_t alg = alg_arg; |
| 5070 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5071 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5072 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5073 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5074 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5075 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5076 | psa_set_key_usage_flags( &attributes, usage ); |
| 5077 | psa_set_key_algorithm( &attributes, alg ); |
| 5078 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5079 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5080 | |
| 5081 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5082 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5083 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5084 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5085 | |
| 5086 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5087 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 5088 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5089 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5090 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5091 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5092 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5093 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5094 | |
| 5095 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5096 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5097 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5098 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5099 | } |
| 5100 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5101 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5102 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */ |
| 5103 | void generate_key_rsa( int bits_arg, |
| 5104 | data_t *e_arg, |
| 5105 | int expected_status_arg ) |
| 5106 | { |
| 5107 | psa_key_handle_t handle = 0; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5108 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5109 | size_t bits = bits_arg; |
| 5110 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5111 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5112 | psa_status_t expected_status = expected_status_arg; |
| 5113 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5114 | uint8_t *exported = NULL; |
| 5115 | size_t exported_size = |
| 5116 | PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
| 5117 | size_t exported_length = SIZE_MAX; |
| 5118 | uint8_t *e_read_buffer = NULL; |
| 5119 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5120 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5121 | size_t e_read_length = SIZE_MAX; |
| 5122 | |
| 5123 | if( e_arg->len == 0 || |
| 5124 | ( e_arg->len == 3 && |
| 5125 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5126 | { |
| 5127 | is_default_public_exponent = 1; |
| 5128 | e_read_size = 0; |
| 5129 | } |
| 5130 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5131 | ASSERT_ALLOC( exported, exported_size ); |
| 5132 | |
| 5133 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5134 | |
| 5135 | psa_set_key_usage_flags( &attributes, usage ); |
| 5136 | psa_set_key_algorithm( &attributes, alg ); |
| 5137 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5138 | e_arg->x, e_arg->len ) ); |
| 5139 | psa_set_key_bits( &attributes, bits ); |
| 5140 | |
| 5141 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5142 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5143 | if( expected_status != PSA_SUCCESS ) |
| 5144 | goto exit; |
| 5145 | |
| 5146 | /* Test the key information */ |
| 5147 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5148 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5149 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5150 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5151 | e_read_buffer, e_read_size, |
| 5152 | &e_read_length ) ); |
| 5153 | if( is_default_public_exponent ) |
| 5154 | TEST_EQUAL( e_read_length, 0 ); |
| 5155 | else |
| 5156 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5157 | |
| 5158 | /* Do something with the key according to its type and permitted usage. */ |
| 5159 | if( ! exercise_key( handle, usage, alg ) ) |
| 5160 | goto exit; |
| 5161 | |
| 5162 | /* Export the key and check the public exponent. */ |
| 5163 | PSA_ASSERT( psa_export_public_key( handle, |
| 5164 | exported, exported_size, |
| 5165 | &exported_length ) ); |
| 5166 | { |
| 5167 | uint8_t *p = exported; |
| 5168 | uint8_t *end = exported + exported_length; |
| 5169 | size_t len; |
| 5170 | /* RSAPublicKey ::= SEQUENCE { |
| 5171 | * modulus INTEGER, -- n |
| 5172 | * publicExponent INTEGER } -- e |
| 5173 | */ |
| 5174 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5175 | MBEDTLS_ASN1_SEQUENCE | |
| 5176 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5177 | TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
| 5178 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5179 | MBEDTLS_ASN1_INTEGER ) ); |
| 5180 | if( len >= 1 && p[0] == 0 ) |
| 5181 | { |
| 5182 | ++p; |
| 5183 | --len; |
| 5184 | } |
| 5185 | if( e_arg->len == 0 ) |
| 5186 | { |
| 5187 | TEST_EQUAL( len, 3 ); |
| 5188 | TEST_EQUAL( p[0], 1 ); |
| 5189 | TEST_EQUAL( p[1], 0 ); |
| 5190 | TEST_EQUAL( p[2], 1 ); |
| 5191 | } |
| 5192 | else |
| 5193 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5194 | } |
| 5195 | |
| 5196 | exit: |
| 5197 | psa_reset_key_attributes( &attributes ); |
| 5198 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5199 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5200 | mbedtls_free( e_read_buffer ); |
| 5201 | mbedtls_free( exported ); |
| 5202 | } |
| 5203 | /* END_CASE */ |
| 5204 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5205 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5206 | void persistent_key_load_key_from_storage( data_t *data, |
| 5207 | int type_arg, int bits_arg, |
| 5208 | int usage_flags_arg, int alg_arg, |
| 5209 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5210 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5211 | psa_key_id_t key_id = 1; |
| 5212 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5213 | psa_key_handle_t handle = 0; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5214 | psa_key_handle_t base_key = 0; |
| 5215 | psa_key_type_t type = type_arg; |
| 5216 | size_t bits = bits_arg; |
| 5217 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5218 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5219 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5220 | unsigned char *first_export = NULL; |
| 5221 | unsigned char *second_export = NULL; |
| 5222 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 5223 | size_t first_exported_length; |
| 5224 | size_t second_exported_length; |
| 5225 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5226 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5227 | { |
| 5228 | ASSERT_ALLOC( first_export, export_size ); |
| 5229 | ASSERT_ALLOC( second_export, export_size ); |
| 5230 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5231 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5232 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5233 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5234 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5235 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5236 | psa_set_key_algorithm( &attributes, alg ); |
| 5237 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5238 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5239 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5240 | switch( generation_method ) |
| 5241 | { |
| 5242 | case IMPORT_KEY: |
| 5243 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5244 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
| 5245 | &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5246 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5247 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5248 | case GENERATE_KEY: |
| 5249 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5250 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5251 | break; |
| 5252 | |
| 5253 | case DERIVE_KEY: |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5254 | { |
| 5255 | /* Create base key */ |
| 5256 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5257 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5258 | psa_set_key_usage_flags( &base_attributes, |
| 5259 | PSA_KEY_USAGE_DERIVE ); |
| 5260 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5261 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5262 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5263 | data->x, data->len, |
| 5264 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5265 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5266 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5267 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5268 | &operation, |
| 5269 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5270 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5271 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5272 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5273 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5274 | &operation, |
| 5275 | &handle ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5276 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5277 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
| 5278 | base_key = 0; |
| 5279 | } |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5280 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5281 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5282 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5283 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5284 | /* Export the key if permitted by the key policy. */ |
| 5285 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5286 | { |
| 5287 | PSA_ASSERT( psa_export_key( handle, |
| 5288 | first_export, export_size, |
| 5289 | &first_exported_length ) ); |
| 5290 | if( generation_method == IMPORT_KEY ) |
| 5291 | ASSERT_COMPARE( data->x, data->len, |
| 5292 | first_export, first_exported_length ); |
| 5293 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5294 | |
| 5295 | /* Shutdown and restart */ |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 5296 | PSA_ASSERT( psa_close_key( handle ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5297 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5298 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5299 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5300 | /* Check key slot still contains key data */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5301 | PSA_ASSERT( psa_open_key( key_id, &handle ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5302 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5303 | TEST_EQUAL( psa_get_key_id( &attributes ), key_id ); |
| 5304 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5305 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5306 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5307 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5308 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 5309 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5310 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5311 | /* Export the key again if permitted by the key policy. */ |
| 5312 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5313 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5314 | PSA_ASSERT( psa_export_key( handle, |
| 5315 | second_export, export_size, |
| 5316 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5317 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5318 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5319 | } |
| 5320 | |
| 5321 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5322 | if( ! exercise_key( handle, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5323 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5324 | |
| 5325 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5326 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5327 | mbedtls_free( first_export ); |
| 5328 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5329 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5330 | psa_destroy_key( base_key ); |
| 5331 | if( handle == 0 ) |
| 5332 | { |
| 5333 | /* In case there was a test failure after creating the persistent key |
| 5334 | * but while it was not open, try to re-open the persistent key |
| 5335 | * to delete it. */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5336 | psa_open_key( key_id, &handle ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5337 | } |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5338 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5339 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5340 | } |
| 5341 | /* END_CASE */ |