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 | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 109 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 110 | int lifetime_is_secure_element( psa_key_lifetime_t lifetime ) |
| 111 | { |
| 112 | /* At the moment, anything that isn't a built-in lifetime is either |
| 113 | * a secure element or unassigned. */ |
| 114 | return( lifetime != PSA_KEY_LIFETIME_VOLATILE && |
| 115 | lifetime != PSA_KEY_LIFETIME_PERSISTENT ); |
| 116 | } |
| 117 | #else |
| 118 | int lifetime_is_secure_element( psa_key_lifetime_t lifetime ) |
| 119 | { |
| 120 | (void) lifetime; |
| 121 | return( 0 ); |
| 122 | } |
| 123 | #endif |
| 124 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 125 | /** Test if a buffer contains a constant byte value. |
| 126 | * |
| 127 | * `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] | 128 | * |
| 129 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 130 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 131 | * \param size Size of the buffer in bytes. |
| 132 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 133 | * \return 1 if the buffer is all-bits-zero. |
| 134 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 135 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 136 | 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] | 137 | { |
| 138 | size_t i; |
| 139 | for( i = 0; i < size; i++ ) |
| 140 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 141 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 142 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 143 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 144 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 145 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 146 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 147 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 148 | static int asn1_write_10x( unsigned char **p, |
| 149 | unsigned char *start, |
| 150 | size_t bits, |
| 151 | unsigned char x ) |
| 152 | { |
| 153 | int ret; |
| 154 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 155 | if( bits == 0 ) |
| 156 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 157 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 158 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 159 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 160 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 161 | *p -= len; |
| 162 | ( *p )[len-1] = x; |
| 163 | if( bits % 8 == 0 ) |
| 164 | ( *p )[1] |= 1; |
| 165 | else |
| 166 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 167 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 168 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 169 | MBEDTLS_ASN1_INTEGER ) ); |
| 170 | return( len ); |
| 171 | } |
| 172 | |
| 173 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 174 | size_t buffer_size, |
| 175 | unsigned char **p, |
| 176 | size_t bits, |
| 177 | int keypair ) |
| 178 | { |
| 179 | size_t half_bits = ( bits + 1 ) / 2; |
| 180 | int ret; |
| 181 | int len = 0; |
| 182 | /* Construct something that looks like a DER encoding of |
| 183 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 184 | * RSAPrivateKey ::= SEQUENCE { |
| 185 | * version Version, |
| 186 | * modulus INTEGER, -- n |
| 187 | * publicExponent INTEGER, -- e |
| 188 | * privateExponent INTEGER, -- d |
| 189 | * prime1 INTEGER, -- p |
| 190 | * prime2 INTEGER, -- q |
| 191 | * exponent1 INTEGER, -- d mod (p-1) |
| 192 | * exponent2 INTEGER, -- d mod (q-1) |
| 193 | * coefficient INTEGER, -- (inverse of q) mod p |
| 194 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 195 | * } |
| 196 | * Or, for a public key, the same structure with only |
| 197 | * version, modulus and publicExponent. |
| 198 | */ |
| 199 | *p = buffer + buffer_size; |
| 200 | if( keypair ) |
| 201 | { |
| 202 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 203 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 204 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 205 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 206 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 207 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 208 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 209 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 210 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 211 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 212 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 213 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 214 | } |
| 215 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 216 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 217 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 218 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 219 | if( keypair ) |
| 220 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 221 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 222 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 223 | { |
| 224 | const unsigned char tag = |
| 225 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 226 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 227 | } |
| 228 | return( len ); |
| 229 | } |
| 230 | |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 231 | int check_key_attributes_sanity( psa_key_handle_t key ) |
| 232 | { |
| 233 | int ok = 0; |
| 234 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 235 | psa_key_lifetime_t lifetime; |
| 236 | psa_key_id_t id; |
| 237 | psa_key_type_t type; |
| 238 | psa_key_type_t bits; |
| 239 | |
| 240 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 241 | lifetime = psa_get_key_lifetime( &attributes ); |
| 242 | id = psa_get_key_id( &attributes ); |
| 243 | type = psa_get_key_type( &attributes ); |
| 244 | bits = psa_get_key_bits( &attributes ); |
| 245 | |
| 246 | /* Persistence */ |
| 247 | if( lifetime == PSA_KEY_LIFETIME_VOLATILE ) |
| 248 | TEST_ASSERT( id == 0 ); |
| 249 | else |
| 250 | { |
| 251 | TEST_ASSERT( |
| 252 | ( PSA_KEY_ID_USER_MIN <= id && id <= PSA_KEY_ID_USER_MAX ) || |
| 253 | ( PSA_KEY_ID_USER_MIN <= id && id <= PSA_KEY_ID_USER_MAX ) ); |
| 254 | } |
| 255 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 256 | /* randomly-generated 64-bit constant, should never appear in test data */ |
| 257 | psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21; |
| 258 | psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number ); |
| 259 | if( lifetime_is_secure_element( lifetime ) ) |
| 260 | { |
| 261 | /* Mbed Crypto currently always exposes the slot number to |
| 262 | * applications. This is not mandated by the PSA specification |
| 263 | * and may change in future versions. */ |
| 264 | TEST_EQUAL( status, 0 ); |
| 265 | TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 ); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 270 | } |
| 271 | #endif |
| 272 | |
| 273 | /* Type and size */ |
| 274 | TEST_ASSERT( type != 0 ); |
| 275 | TEST_ASSERT( bits != 0 ); |
| 276 | TEST_ASSERT( bits <= PSA_MAX_KEY_BITS ); |
| 277 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
| 278 | TEST_ASSERT( bits % 8 == 0 ); |
| 279 | |
| 280 | /* MAX macros concerning specific key types */ |
| 281 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 282 | TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS ); |
| 283 | else if( PSA_KEY_TYPE_IS_RSA( type ) ) |
| 284 | TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS ); |
| 285 | TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) <= PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE ); |
| 286 | |
| 287 | ok = 1; |
| 288 | |
| 289 | exit: |
| 290 | psa_reset_key_attributes( &attributes ); |
| 291 | return( ok ); |
| 292 | } |
| 293 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 294 | int exercise_mac_setup( psa_key_type_t key_type, |
| 295 | const unsigned char *key_bytes, |
| 296 | size_t key_length, |
| 297 | psa_algorithm_t alg, |
| 298 | psa_mac_operation_t *operation, |
| 299 | psa_status_t *status ) |
| 300 | { |
| 301 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 302 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 303 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 304 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 305 | psa_set_key_algorithm( &attributes, alg ); |
| 306 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 307 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 308 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 309 | |
| 310 | *status = psa_mac_sign_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 311 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 312 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 313 | /* If setup failed, reproduce the failure, so that the caller can |
| 314 | * test the resulting state of the operation object. */ |
| 315 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 316 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 317 | TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ), |
| 318 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | psa_destroy_key( handle ); |
| 322 | return( 1 ); |
| 323 | |
| 324 | exit: |
| 325 | psa_destroy_key( handle ); |
| 326 | return( 0 ); |
| 327 | } |
| 328 | |
| 329 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 330 | const unsigned char *key_bytes, |
| 331 | size_t key_length, |
| 332 | psa_algorithm_t alg, |
| 333 | psa_cipher_operation_t *operation, |
| 334 | psa_status_t *status ) |
| 335 | { |
| 336 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 337 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 338 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 339 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 340 | psa_set_key_algorithm( &attributes, alg ); |
| 341 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 342 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 343 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 344 | |
| 345 | *status = psa_cipher_encrypt_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 346 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 347 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 348 | /* If setup failed, reproduce the failure, so that the caller can |
| 349 | * test the resulting state of the operation object. */ |
| 350 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 351 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 352 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ), |
| 353 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | psa_destroy_key( handle ); |
| 357 | return( 1 ); |
| 358 | |
| 359 | exit: |
| 360 | psa_destroy_key( handle ); |
| 361 | return( 0 ); |
| 362 | } |
| 363 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 364 | static int exercise_mac_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 365 | psa_key_usage_t usage, |
| 366 | psa_algorithm_t alg ) |
| 367 | { |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 368 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 369 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 370 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 371 | size_t mac_length = sizeof( mac ); |
| 372 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 373 | if( usage & PSA_KEY_USAGE_SIGN_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 374 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 375 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 376 | handle, alg ) ); |
| 377 | PSA_ASSERT( psa_mac_update( &operation, |
| 378 | input, sizeof( input ) ) ); |
| 379 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 380 | mac, sizeof( mac ), |
| 381 | &mac_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 384 | if( usage & PSA_KEY_USAGE_VERIFY_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 385 | { |
| 386 | psa_status_t verify_status = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 387 | ( usage & PSA_KEY_USAGE_SIGN_HASH ? |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 388 | PSA_SUCCESS : |
| 389 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 390 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 391 | handle, alg ) ); |
| 392 | PSA_ASSERT( psa_mac_update( &operation, |
| 393 | input, sizeof( input ) ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 394 | TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ), |
| 395 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | return( 1 ); |
| 399 | |
| 400 | exit: |
| 401 | psa_mac_abort( &operation ); |
| 402 | return( 0 ); |
| 403 | } |
| 404 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 405 | static int exercise_cipher_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 406 | psa_key_usage_t usage, |
| 407 | psa_algorithm_t alg ) |
| 408 | { |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 409 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 410 | unsigned char iv[16] = {0}; |
| 411 | size_t iv_length = sizeof( iv ); |
| 412 | const unsigned char plaintext[16] = "Hello, world..."; |
| 413 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 414 | size_t ciphertext_length = sizeof( ciphertext ); |
| 415 | unsigned char decrypted[sizeof( ciphertext )]; |
| 416 | size_t part_length; |
| 417 | |
| 418 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 419 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 420 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 421 | handle, alg ) ); |
| 422 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 423 | iv, sizeof( iv ), |
| 424 | &iv_length ) ); |
| 425 | PSA_ASSERT( psa_cipher_update( &operation, |
| 426 | plaintext, sizeof( plaintext ), |
| 427 | ciphertext, sizeof( ciphertext ), |
| 428 | &ciphertext_length ) ); |
| 429 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 430 | ciphertext + ciphertext_length, |
| 431 | sizeof( ciphertext ) - ciphertext_length, |
| 432 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 433 | ciphertext_length += part_length; |
| 434 | } |
| 435 | |
| 436 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 437 | { |
| 438 | psa_status_t status; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 439 | int maybe_invalid_padding = 0; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 440 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 441 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 442 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 443 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 444 | /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't |
| 445 | * have this macro yet. */ |
| 446 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( |
| 447 | psa_get_key_type( &attributes ) ); |
| 448 | maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 449 | } |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 450 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 451 | handle, alg ) ); |
| 452 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 453 | iv, iv_length ) ); |
| 454 | PSA_ASSERT( psa_cipher_update( &operation, |
| 455 | ciphertext, ciphertext_length, |
| 456 | decrypted, sizeof( decrypted ), |
| 457 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 458 | status = psa_cipher_finish( &operation, |
| 459 | decrypted + part_length, |
| 460 | sizeof( decrypted ) - part_length, |
| 461 | &part_length ); |
| 462 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 463 | * if the input is some aribtrary data rather than an actual |
| 464 | ciphertext, a padding error is likely. */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 465 | if( maybe_invalid_padding ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 466 | TEST_ASSERT( status == PSA_SUCCESS || |
| 467 | status == PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 468 | else |
| 469 | PSA_ASSERT( status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | return( 1 ); |
| 473 | |
| 474 | exit: |
| 475 | psa_cipher_abort( &operation ); |
| 476 | return( 0 ); |
| 477 | } |
| 478 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 479 | static int exercise_aead_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 480 | psa_key_usage_t usage, |
| 481 | psa_algorithm_t alg ) |
| 482 | { |
| 483 | unsigned char nonce[16] = {0}; |
| 484 | size_t nonce_length = sizeof( nonce ); |
| 485 | unsigned char plaintext[16] = "Hello, world..."; |
| 486 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 487 | size_t ciphertext_length = sizeof( ciphertext ); |
| 488 | size_t plaintext_length = sizeof( ciphertext ); |
| 489 | |
| 490 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 491 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 492 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 493 | nonce, nonce_length, |
| 494 | NULL, 0, |
| 495 | plaintext, sizeof( plaintext ), |
| 496 | ciphertext, sizeof( ciphertext ), |
| 497 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 501 | { |
| 502 | psa_status_t verify_status = |
| 503 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 504 | PSA_SUCCESS : |
| 505 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 506 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 507 | nonce, nonce_length, |
| 508 | NULL, 0, |
| 509 | ciphertext, ciphertext_length, |
| 510 | plaintext, sizeof( plaintext ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 511 | &plaintext_length ), |
| 512 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | return( 1 ); |
| 516 | |
| 517 | exit: |
| 518 | return( 0 ); |
| 519 | } |
| 520 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 521 | static int exercise_signature_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 522 | psa_key_usage_t usage, |
| 523 | psa_algorithm_t alg ) |
| 524 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 525 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 526 | size_t payload_length = 16; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 527 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 528 | size_t signature_length = sizeof( signature ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 529 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 530 | |
| 531 | /* If the policy allows signing with any hash, just pick one. */ |
| 532 | if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH ) |
| 533 | { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 534 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 535 | hash_alg = KNOWN_SUPPORTED_HASH_ALG; |
| 536 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 537 | #else |
| 538 | test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 539 | return( 1 ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 540 | #endif |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 541 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 542 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 543 | if( usage & PSA_KEY_USAGE_SIGN_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 544 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 545 | /* Some algorithms require the payload to have the size of |
| 546 | * the hash encoded in the algorithm. Use this input size |
| 547 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 548 | if( hash_alg != 0 ) |
| 549 | payload_length = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 550 | PSA_ASSERT( psa_sign_hash( handle, alg, |
| 551 | payload, payload_length, |
| 552 | signature, sizeof( signature ), |
| 553 | &signature_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 556 | if( usage & PSA_KEY_USAGE_VERIFY_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 557 | { |
| 558 | psa_status_t verify_status = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 559 | ( usage & PSA_KEY_USAGE_SIGN_HASH ? |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 560 | PSA_SUCCESS : |
| 561 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 562 | TEST_EQUAL( psa_verify_hash( handle, alg, |
| 563 | payload, payload_length, |
| 564 | signature, signature_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 565 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | return( 1 ); |
| 569 | |
| 570 | exit: |
| 571 | return( 0 ); |
| 572 | } |
| 573 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 574 | static int exercise_asymmetric_encryption_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 575 | psa_key_usage_t usage, |
| 576 | psa_algorithm_t alg ) |
| 577 | { |
| 578 | unsigned char plaintext[256] = "Hello, world..."; |
| 579 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 580 | size_t ciphertext_length = sizeof( ciphertext ); |
| 581 | size_t plaintext_length = 16; |
| 582 | |
| 583 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 584 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 585 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 586 | plaintext, plaintext_length, |
| 587 | NULL, 0, |
| 588 | ciphertext, sizeof( ciphertext ), |
| 589 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 593 | { |
| 594 | psa_status_t status = |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 595 | psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 596 | ciphertext, ciphertext_length, |
| 597 | NULL, 0, |
| 598 | plaintext, sizeof( plaintext ), |
| 599 | &plaintext_length ); |
| 600 | TEST_ASSERT( status == PSA_SUCCESS || |
| 601 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 602 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 603 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 604 | } |
| 605 | |
| 606 | return( 1 ); |
| 607 | |
| 608 | exit: |
| 609 | return( 0 ); |
| 610 | } |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 611 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 612 | static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation, |
| 613 | psa_key_handle_t handle, |
| 614 | psa_algorithm_t alg, |
| 615 | unsigned char* input1, size_t input1_length, |
| 616 | unsigned char* input2, size_t input2_length, |
| 617 | size_t capacity ) |
| 618 | { |
| 619 | PSA_ASSERT( psa_key_derivation_setup( operation, alg ) ); |
| 620 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 621 | { |
| 622 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 623 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 624 | input1, input1_length ) ); |
| 625 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 626 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 627 | handle ) ); |
| 628 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 629 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 630 | input2, |
| 631 | input2_length ) ); |
| 632 | } |
| 633 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 634 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 635 | { |
| 636 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 637 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 638 | input1, input1_length ) ); |
| 639 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 640 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 641 | handle ) ); |
| 642 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 643 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 644 | input2, input2_length ) ); |
| 645 | } |
| 646 | else |
| 647 | { |
| 648 | TEST_ASSERT( ! "Key derivation algorithm not supported" ); |
| 649 | } |
| 650 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 651 | if( capacity != SIZE_MAX ) |
| 652 | PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) ); |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 653 | |
| 654 | return( 1 ); |
| 655 | |
| 656 | exit: |
| 657 | return( 0 ); |
| 658 | } |
| 659 | |
| 660 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 661 | static int exercise_key_derivation_key( psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 662 | psa_key_usage_t usage, |
| 663 | psa_algorithm_t alg ) |
| 664 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 665 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 666 | unsigned char input1[] = "Input 1"; |
| 667 | size_t input1_length = sizeof( input1 ); |
| 668 | unsigned char input2[] = "Input 2"; |
| 669 | size_t input2_length = sizeof( input2 ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 670 | unsigned char output[1]; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 671 | size_t capacity = sizeof( output ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 672 | |
| 673 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 674 | { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 675 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 676 | input1, input1_length, |
| 677 | input2, input2_length, capacity ) ) |
| 678 | goto exit; |
Gilles Peskine | 7607cd6 | 2019-05-29 17:35:00 +0200 | [diff] [blame] | 679 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 680 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 681 | output, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 682 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 683 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | return( 1 ); |
| 687 | |
| 688 | exit: |
| 689 | return( 0 ); |
| 690 | } |
| 691 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 692 | /* We need two keys to exercise key agreement. Exercise the |
| 693 | * private key against its own public key. */ |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 694 | static psa_status_t key_agreement_with_self( |
| 695 | psa_key_derivation_operation_t *operation, |
| 696 | psa_key_handle_t handle ) |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 697 | { |
| 698 | psa_key_type_t private_key_type; |
| 699 | psa_key_type_t public_key_type; |
| 700 | size_t key_bits; |
| 701 | uint8_t *public_key = NULL; |
| 702 | size_t public_key_length; |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 703 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 704 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 705 | * 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] | 706 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 707 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 708 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 709 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 710 | private_key_type = psa_get_key_type( &attributes ); |
| 711 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 712 | 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] | 713 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 714 | ASSERT_ALLOC( public_key, public_key_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 715 | PSA_ASSERT( psa_export_public_key( handle, |
| 716 | public_key, public_key_length, |
| 717 | &public_key_length ) ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 718 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 719 | status = psa_key_derivation_key_agreement( |
| 720 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle, |
| 721 | public_key, public_key_length ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 722 | exit: |
| 723 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 724 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 725 | return( status ); |
| 726 | } |
| 727 | |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 728 | /* We need two keys to exercise key agreement. Exercise the |
| 729 | * private key against its own public key. */ |
| 730 | static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg, |
| 731 | psa_key_handle_t handle ) |
| 732 | { |
| 733 | psa_key_type_t private_key_type; |
| 734 | psa_key_type_t public_key_type; |
| 735 | size_t key_bits; |
| 736 | uint8_t *public_key = NULL; |
| 737 | size_t public_key_length; |
| 738 | uint8_t output[1024]; |
| 739 | size_t output_length; |
| 740 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 741 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 742 | * 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] | 743 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 744 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 745 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 746 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 747 | private_key_type = psa_get_key_type( &attributes ); |
| 748 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 749 | 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] | 750 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 751 | ASSERT_ALLOC( public_key, public_key_length ); |
| 752 | PSA_ASSERT( psa_export_public_key( handle, |
| 753 | public_key, public_key_length, |
| 754 | &public_key_length ) ); |
| 755 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 756 | status = psa_raw_key_agreement( alg, handle, |
| 757 | public_key, public_key_length, |
| 758 | output, sizeof( output ), &output_length ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 759 | exit: |
| 760 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 761 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 762 | return( status ); |
| 763 | } |
| 764 | |
| 765 | static int exercise_raw_key_agreement_key( psa_key_handle_t handle, |
| 766 | psa_key_usage_t usage, |
| 767 | psa_algorithm_t alg ) |
| 768 | { |
| 769 | int ok = 0; |
| 770 | |
| 771 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 772 | { |
| 773 | /* We need two keys to exercise key agreement. Exercise the |
| 774 | * private key against its own public key. */ |
| 775 | PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) ); |
| 776 | } |
| 777 | ok = 1; |
| 778 | |
| 779 | exit: |
| 780 | return( ok ); |
| 781 | } |
| 782 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 783 | static int exercise_key_agreement_key( psa_key_handle_t handle, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 784 | psa_key_usage_t usage, |
| 785 | psa_algorithm_t alg ) |
| 786 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 787 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 788 | unsigned char output[1]; |
| 789 | int ok = 0; |
| 790 | |
| 791 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 792 | { |
| 793 | /* We need two keys to exercise key agreement. Exercise the |
| 794 | * private key against its own public key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 795 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 796 | PSA_ASSERT( key_agreement_with_self( &operation, handle ) ); |
| 797 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 798 | output, |
| 799 | sizeof( output ) ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 800 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 801 | } |
| 802 | ok = 1; |
| 803 | |
| 804 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 805 | return( ok ); |
| 806 | } |
| 807 | |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 808 | int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 809 | size_t min_bits, size_t max_bits, |
| 810 | int must_be_odd ) |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 811 | { |
| 812 | size_t len; |
| 813 | size_t actual_bits; |
| 814 | unsigned char msb; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 815 | TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 816 | MBEDTLS_ASN1_INTEGER ), |
| 817 | 0 ); |
k-stachowiak | 9b88efc | 2019-09-13 15:26:53 +0200 | [diff] [blame] | 818 | |
| 819 | /* Check if the retrieved length doesn't extend the actual buffer's size. |
| 820 | * It is assumed here, that end >= p, which validates casting to size_t. */ |
| 821 | TEST_ASSERT( len <= (size_t)( end - *p) ); |
| 822 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 823 | /* Tolerate a slight departure from DER encoding: |
| 824 | * - 0 may be represented by an empty string or a 1-byte string. |
| 825 | * - The sign bit may be used as a value bit. */ |
| 826 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 827 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 828 | { |
| 829 | ++( *p ); |
| 830 | --len; |
| 831 | } |
| 832 | if( min_bits == 0 && len == 0 ) |
| 833 | return( 1 ); |
| 834 | msb = ( *p )[0]; |
| 835 | TEST_ASSERT( msb != 0 ); |
| 836 | actual_bits = 8 * ( len - 1 ); |
| 837 | while( msb != 0 ) |
| 838 | { |
| 839 | msb >>= 1; |
| 840 | ++actual_bits; |
| 841 | } |
| 842 | TEST_ASSERT( actual_bits >= min_bits ); |
| 843 | TEST_ASSERT( actual_bits <= max_bits ); |
| 844 | if( must_be_odd ) |
| 845 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 846 | *p += len; |
| 847 | return( 1 ); |
| 848 | exit: |
| 849 | return( 0 ); |
| 850 | } |
| 851 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 852 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 853 | uint8_t *exported, size_t exported_length ) |
| 854 | { |
| 855 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 856 | TEST_EQUAL( exported_length, ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 857 | else |
| 858 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 859 | |
| 860 | #if defined(MBEDTLS_DES_C) |
| 861 | if( type == PSA_KEY_TYPE_DES ) |
| 862 | { |
| 863 | /* Check the parity bits. */ |
| 864 | unsigned i; |
| 865 | for( i = 0; i < bits / 8; i++ ) |
| 866 | { |
| 867 | unsigned bit_count = 0; |
| 868 | unsigned m; |
| 869 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 870 | { |
| 871 | if( exported[i] & m ) |
| 872 | ++bit_count; |
| 873 | } |
| 874 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 875 | } |
| 876 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 877 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 878 | #endif |
| 879 | |
| 880 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 881 | if( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 882 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 883 | uint8_t *p = exported; |
| 884 | uint8_t *end = exported + exported_length; |
| 885 | size_t len; |
| 886 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 887 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 888 | * modulus INTEGER, -- n |
| 889 | * publicExponent INTEGER, -- e |
| 890 | * privateExponent INTEGER, -- d |
| 891 | * prime1 INTEGER, -- p |
| 892 | * prime2 INTEGER, -- q |
| 893 | * exponent1 INTEGER, -- d mod (p-1) |
| 894 | * exponent2 INTEGER, -- d mod (q-1) |
| 895 | * coefficient INTEGER, -- (inverse of q) mod p |
| 896 | * } |
| 897 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 898 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 899 | MBEDTLS_ASN1_SEQUENCE | |
| 900 | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); |
| 901 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 902 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 903 | goto exit; |
| 904 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 905 | goto exit; |
| 906 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 907 | goto exit; |
| 908 | /* Require d to be at least half the size of n. */ |
| 909 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 910 | goto exit; |
| 911 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 912 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 913 | goto exit; |
| 914 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 915 | goto exit; |
| 916 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 917 | goto exit; |
| 918 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 919 | goto exit; |
| 920 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 921 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 922 | TEST_EQUAL( p, end ); |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 923 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 924 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 925 | #endif /* MBEDTLS_RSA_C */ |
| 926 | |
| 927 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 928 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 929 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 930 | /* Just the secret value */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 931 | TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 932 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 933 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 934 | #endif /* MBEDTLS_ECP_C */ |
| 935 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 936 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 937 | { |
| 938 | uint8_t *p = exported; |
| 939 | uint8_t *end = exported + exported_length; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 940 | #if defined(MBEDTLS_RSA_C) |
| 941 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 942 | { |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 943 | size_t len; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 944 | /* RSAPublicKey ::= SEQUENCE { |
| 945 | * modulus INTEGER, -- n |
| 946 | * publicExponent INTEGER } -- e |
| 947 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 948 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 949 | MBEDTLS_ASN1_SEQUENCE | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 950 | MBEDTLS_ASN1_CONSTRUCTED ), |
| 951 | 0 ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 952 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 953 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 954 | goto exit; |
| 955 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 956 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 957 | TEST_EQUAL( p, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 958 | } |
| 959 | else |
| 960 | #endif /* MBEDTLS_RSA_C */ |
| 961 | #if defined(MBEDTLS_ECP_C) |
| 962 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 963 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 964 | /* The representation of an ECC public key is: |
| 965 | * - The byte 0x04; |
| 966 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 967 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 968 | * - where m is the bit size associated with the curve. |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 969 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 970 | TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end ); |
| 971 | TEST_EQUAL( p[0], 4 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 972 | } |
| 973 | else |
| 974 | #endif /* MBEDTLS_ECP_C */ |
| 975 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 976 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 977 | mbedtls_snprintf( message, sizeof( message ), |
| 978 | "No sanity check for public key type=0x%08lx", |
| 979 | (unsigned long) type ); |
| 980 | test_fail( message, __LINE__, __FILE__ ); |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 981 | (void) p; |
| 982 | (void) end; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 983 | return( 0 ); |
| 984 | } |
| 985 | } |
| 986 | else |
| 987 | |
| 988 | { |
| 989 | /* No sanity checks for other types */ |
| 990 | } |
| 991 | |
| 992 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 993 | |
| 994 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 995 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 998 | static int exercise_export_key( psa_key_handle_t handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 999 | psa_key_usage_t usage ) |
| 1000 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1001 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1002 | uint8_t *exported = NULL; |
| 1003 | size_t exported_size = 0; |
| 1004 | size_t exported_length = 0; |
| 1005 | int ok = 0; |
| 1006 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1007 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 1008 | |
| 1009 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1010 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1011 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1012 | TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ), |
| 1013 | PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1014 | ok = 1; |
| 1015 | goto exit; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1018 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ), |
| 1019 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1020 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1021 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1022 | PSA_ASSERT( psa_export_key( handle, |
| 1023 | exported, exported_size, |
| 1024 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1025 | ok = exported_key_sanity_check( psa_get_key_type( &attributes ), |
| 1026 | psa_get_key_bits( &attributes ), |
| 1027 | exported, exported_length ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1028 | |
| 1029 | exit: |
| 1030 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1031 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1032 | return( ok ); |
| 1033 | } |
| 1034 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1035 | static int exercise_export_public_key( psa_key_handle_t handle ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1036 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1037 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1038 | psa_key_type_t public_type; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1039 | uint8_t *exported = NULL; |
| 1040 | size_t exported_size = 0; |
| 1041 | size_t exported_length = 0; |
| 1042 | int ok = 0; |
| 1043 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1044 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1045 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1046 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1047 | TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1048 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1049 | return( 1 ); |
| 1050 | } |
| 1051 | |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1052 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1053 | psa_get_key_type( &attributes ) ); |
| 1054 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, |
| 1055 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1056 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1057 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1058 | PSA_ASSERT( psa_export_public_key( handle, |
| 1059 | exported, exported_size, |
| 1060 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1061 | ok = exported_key_sanity_check( public_type, |
| 1062 | psa_get_key_bits( &attributes ), |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1063 | exported, exported_length ); |
| 1064 | |
| 1065 | exit: |
| 1066 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1067 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1068 | return( ok ); |
| 1069 | } |
| 1070 | |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 1071 | /** Do smoke tests on a key. |
| 1072 | * |
| 1073 | * Perform one of each operation indicated by \p alg (decrypt/encrypt, |
| 1074 | * sign/verify, or derivation) that is permitted according to \p usage. |
| 1075 | * \p usage and \p alg should correspond to the expected policy on the |
| 1076 | * key. |
| 1077 | * |
| 1078 | * Export the key if permitted by \p usage, and check that the output |
| 1079 | * looks sensible. If \p usage forbids export, check that |
| 1080 | * \p psa_export_key correctly rejects the attempt. If the key is |
| 1081 | * asymmetric, also check \p psa_export_public_key. |
| 1082 | * |
| 1083 | * If the key fails the tests, this function calls the test framework's |
| 1084 | * `test_fail` function and returns false. Otherwise this function returns |
| 1085 | * true. Therefore it should be used as follows: |
| 1086 | * ``` |
| 1087 | * if( ! exercise_key( ... ) ) goto exit; |
| 1088 | * ``` |
| 1089 | * |
| 1090 | * \param handle The key to exercise. It should be capable of performing |
| 1091 | * \p alg. |
| 1092 | * \param usage The usage flags to assume. |
| 1093 | * \param alg The algorithm to exercise. |
| 1094 | * |
| 1095 | * \retval 0 The key failed the smoke tests. |
| 1096 | * \retval 1 The key passed the smoke tests. |
| 1097 | */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1098 | static int exercise_key( psa_key_handle_t handle, |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1099 | psa_key_usage_t usage, |
| 1100 | psa_algorithm_t alg ) |
| 1101 | { |
| 1102 | int ok; |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 1103 | |
| 1104 | if( ! check_key_attributes_sanity( handle ) ) |
| 1105 | return( 0 ); |
| 1106 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1107 | if( alg == 0 ) |
| 1108 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 1109 | else if( PSA_ALG_IS_MAC( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1110 | ok = exercise_mac_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1111 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1112 | ok = exercise_cipher_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1113 | else if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1114 | ok = exercise_aead_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1115 | else if( PSA_ALG_IS_SIGN( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1116 | ok = exercise_signature_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1117 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1118 | ok = exercise_asymmetric_encryption_key( handle, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1119 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1120 | ok = exercise_key_derivation_key( handle, usage, alg ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 1121 | else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 1122 | ok = exercise_raw_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1123 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1124 | ok = exercise_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1125 | else |
| 1126 | { |
| 1127 | char message[40]; |
| 1128 | mbedtls_snprintf( message, sizeof( message ), |
| 1129 | "No code to exercise alg=0x%08lx", |
| 1130 | (unsigned long) alg ); |
| 1131 | test_fail( message, __LINE__, __FILE__ ); |
| 1132 | ok = 0; |
| 1133 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1134 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1135 | ok = ok && exercise_export_key( handle, usage ); |
| 1136 | ok = ok && exercise_export_public_key( handle ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1137 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1138 | return( ok ); |
| 1139 | } |
| 1140 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1141 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 1142 | psa_algorithm_t alg ) |
| 1143 | { |
| 1144 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 1145 | { |
| 1146 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1147 | PSA_KEY_USAGE_VERIFY_HASH : |
| 1148 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1149 | } |
| 1150 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 1151 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 1152 | { |
| 1153 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1154 | PSA_KEY_USAGE_ENCRYPT : |
| 1155 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 1156 | } |
| 1157 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 1158 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 1159 | { |
| 1160 | return( PSA_KEY_USAGE_DERIVE ); |
| 1161 | } |
| 1162 | else |
| 1163 | { |
| 1164 | return( 0 ); |
| 1165 | } |
| 1166 | |
| 1167 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1168 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1169 | static int test_operations_on_invalid_handle( psa_key_handle_t handle ) |
| 1170 | { |
| 1171 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1172 | uint8_t buffer[1]; |
| 1173 | size_t length; |
| 1174 | int ok = 0; |
| 1175 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1176 | psa_set_key_id( &attributes, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1177 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 1178 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 1179 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
| 1180 | TEST_EQUAL( psa_get_key_attributes( handle, &attributes ), |
| 1181 | PSA_ERROR_INVALID_HANDLE ); |
| 1182 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 1183 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1184 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1185 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1186 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 1187 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 1188 | |
| 1189 | TEST_EQUAL( psa_export_key( handle, |
| 1190 | buffer, sizeof( buffer ), &length ), |
| 1191 | PSA_ERROR_INVALID_HANDLE ); |
| 1192 | TEST_EQUAL( psa_export_public_key( handle, |
| 1193 | buffer, sizeof( buffer ), &length ), |
| 1194 | PSA_ERROR_INVALID_HANDLE ); |
| 1195 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1196 | ok = 1; |
| 1197 | |
| 1198 | exit: |
| 1199 | psa_reset_key_attributes( &attributes ); |
| 1200 | return( ok ); |
| 1201 | } |
| 1202 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1203 | /* Assert that a key isn't reported as having a slot number. */ |
| 1204 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1205 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1206 | do \ |
| 1207 | { \ |
| 1208 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 1209 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 1210 | attributes, \ |
| 1211 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 1212 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 1213 | } \ |
| 1214 | while( 0 ) |
| 1215 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1216 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1217 | ( (void) 0 ) |
| 1218 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1219 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1220 | /* An overapproximation of the amount of storage needed for a key of the |
| 1221 | * given type and with the given content. The API doesn't make it easy |
| 1222 | * to find a good value for the size. The current implementation doesn't |
| 1223 | * care about the value anyway. */ |
| 1224 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 1225 | ( data )->len |
| 1226 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1227 | typedef enum { |
| 1228 | IMPORT_KEY = 0, |
| 1229 | GENERATE_KEY = 1, |
| 1230 | DERIVE_KEY = 2 |
| 1231 | } generate_method; |
| 1232 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1233 | /* END_HEADER */ |
| 1234 | |
| 1235 | /* BEGIN_DEPENDENCIES |
| 1236 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1237 | * END_DEPENDENCIES |
| 1238 | */ |
| 1239 | |
| 1240 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1241 | void static_checks( ) |
| 1242 | { |
| 1243 | size_t max_truncated_mac_size = |
| 1244 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1245 | |
| 1246 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1247 | * encoding. The shifted mask is the maximum truncated value. The |
| 1248 | * untruncated algorithm may be one byte larger. */ |
| 1249 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 1250 | |
| 1251 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 1252 | /* Check deprecated constants. */ |
| 1253 | TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR ); |
| 1254 | TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS ); |
| 1255 | TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST ); |
| 1256 | TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA ); |
| 1257 | TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED ); |
| 1258 | TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH ); |
| 1259 | TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH ); |
| 1260 | TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 1261 | |
| 1262 | TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_CURVE_SECP_K1 ); |
| 1263 | TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_CURVE_SECP_K1 ); |
| 1264 | TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_CURVE_SECP_K1 ); |
| 1265 | TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_CURVE_SECP_K1 ); |
| 1266 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_CURVE_SECP_R1 ); |
| 1267 | TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_CURVE_SECP_R1 ); |
| 1268 | TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_CURVE_SECP_R1 ); |
| 1269 | TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_CURVE_SECP_R1 ); |
| 1270 | TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_CURVE_SECP_R1 ); |
| 1271 | TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_CURVE_SECP_R1 ); |
| 1272 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_CURVE_SECP_R2 ); |
| 1273 | TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_CURVE_SECT_K1 ); |
| 1274 | TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_CURVE_SECT_K1 ); |
| 1275 | TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_CURVE_SECT_K1 ); |
| 1276 | TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_CURVE_SECT_K1 ); |
| 1277 | TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_CURVE_SECT_K1 ); |
| 1278 | TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_CURVE_SECT_K1 ); |
| 1279 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_CURVE_SECT_R1 ); |
| 1280 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_CURVE_SECT_R1 ); |
| 1281 | TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_CURVE_SECT_R1 ); |
| 1282 | TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_CURVE_SECT_R1 ); |
| 1283 | TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_CURVE_SECT_R1 ); |
| 1284 | TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_CURVE_SECT_R1 ); |
| 1285 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_CURVE_SECT_R2 ); |
| 1286 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_CURVE_SECT_R2 ); |
| 1287 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_CURVE_BRAINPOOL_P_R1 ); |
| 1288 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_CURVE_BRAINPOOL_P_R1 ); |
| 1289 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_CURVE_BRAINPOOL_P_R1 ); |
| 1290 | TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_CURVE_MONTGOMERY ); |
| 1291 | TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_CURVE_MONTGOMERY ); |
| 1292 | |
| 1293 | TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_GROUP_RFC7919 ); |
| 1294 | TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_GROUP_RFC7919 ); |
| 1295 | TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_GROUP_RFC7919 ); |
| 1296 | TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_GROUP_RFC7919 ); |
| 1297 | TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_GROUP_RFC7919 ); |
| 1298 | #endif |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1299 | } |
| 1300 | /* END_CASE */ |
| 1301 | |
| 1302 | /* BEGIN_CASE */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1303 | void attributes_set_get( int id_arg, int lifetime_arg, |
| 1304 | int usage_flags_arg, int alg_arg, |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1305 | int type_arg, int bits_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1306 | { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1307 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1308 | psa_key_id_t id = id_arg; |
| 1309 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1310 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 1311 | psa_algorithm_t alg = alg_arg; |
| 1312 | psa_key_type_t type = type_arg; |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1313 | size_t bits = bits_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1314 | |
| 1315 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1316 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1317 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1318 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1319 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1320 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1321 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1322 | psa_set_key_id( &attributes, id ); |
| 1323 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1324 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 1325 | psa_set_key_algorithm( &attributes, alg ); |
| 1326 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1327 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1328 | |
| 1329 | TEST_EQUAL( psa_get_key_id( &attributes ), id ); |
| 1330 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime ); |
| 1331 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 1332 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
| 1333 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1334 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1335 | |
| 1336 | psa_reset_key_attributes( &attributes ); |
| 1337 | |
| 1338 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1339 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1340 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1341 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1342 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1343 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1344 | } |
| 1345 | /* END_CASE */ |
| 1346 | |
| 1347 | /* BEGIN_CASE */ |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1348 | void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg, |
| 1349 | int expected_id_arg, int expected_lifetime_arg ) |
| 1350 | { |
| 1351 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1352 | psa_key_id_t id1 = id1_arg; |
| 1353 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1354 | psa_key_id_t id2 = id2_arg; |
| 1355 | psa_key_id_t expected_id = expected_id_arg; |
| 1356 | psa_key_lifetime_t expected_lifetime = expected_lifetime_arg; |
| 1357 | |
| 1358 | if( id1_arg != -1 ) |
| 1359 | psa_set_key_id( &attributes, id1 ); |
| 1360 | if( lifetime_arg != -1 ) |
| 1361 | psa_set_key_lifetime( &attributes, lifetime ); |
| 1362 | if( id2_arg != -1 ) |
| 1363 | psa_set_key_id( &attributes, id2 ); |
| 1364 | |
| 1365 | TEST_EQUAL( psa_get_key_id( &attributes ), expected_id ); |
| 1366 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime ); |
| 1367 | } |
| 1368 | /* END_CASE */ |
| 1369 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1370 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1371 | void slot_number_attribute( ) |
| 1372 | { |
| 1373 | psa_key_slot_number_t slot_number = 0xdeadbeef; |
| 1374 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1375 | |
| 1376 | /* Initially, there is no slot number. */ |
| 1377 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1378 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1379 | |
| 1380 | /* Test setting a slot number. */ |
| 1381 | psa_set_key_slot_number( &attributes, 0 ); |
| 1382 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1383 | TEST_EQUAL( slot_number, 0 ); |
| 1384 | |
| 1385 | /* Test changing the slot number. */ |
| 1386 | psa_set_key_slot_number( &attributes, 42 ); |
| 1387 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1388 | TEST_EQUAL( slot_number, 42 ); |
| 1389 | |
| 1390 | /* Test clearing the slot number. */ |
| 1391 | psa_clear_key_slot_number( &attributes ); |
| 1392 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1393 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1394 | |
| 1395 | /* Clearing again should have no effect. */ |
| 1396 | psa_clear_key_slot_number( &attributes ); |
| 1397 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1398 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1399 | |
| 1400 | /* Test that reset clears the slot number. */ |
| 1401 | psa_set_key_slot_number( &attributes, 42 ); |
| 1402 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1403 | TEST_EQUAL( slot_number, 42 ); |
| 1404 | psa_reset_key_attributes( &attributes ); |
| 1405 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1406 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1407 | } |
| 1408 | /* END_CASE */ |
| 1409 | |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1410 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1411 | void import_with_policy( int type_arg, |
| 1412 | int usage_arg, int alg_arg, |
| 1413 | int expected_status_arg ) |
| 1414 | { |
| 1415 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1416 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1417 | psa_key_handle_t handle = 0; |
| 1418 | psa_key_type_t type = type_arg; |
| 1419 | psa_key_usage_t usage = usage_arg; |
| 1420 | psa_algorithm_t alg = alg_arg; |
| 1421 | psa_status_t expected_status = expected_status_arg; |
| 1422 | const uint8_t key_material[16] = {0}; |
| 1423 | psa_status_t status; |
| 1424 | |
| 1425 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1426 | |
| 1427 | psa_set_key_type( &attributes, type ); |
| 1428 | psa_set_key_usage_flags( &attributes, usage ); |
| 1429 | psa_set_key_algorithm( &attributes, alg ); |
| 1430 | |
| 1431 | status = psa_import_key( &attributes, |
| 1432 | key_material, sizeof( key_material ), |
| 1433 | &handle ); |
| 1434 | TEST_EQUAL( status, expected_status ); |
| 1435 | if( status != PSA_SUCCESS ) |
| 1436 | goto exit; |
| 1437 | |
| 1438 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1439 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1440 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1441 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1442 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1443 | |
| 1444 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1445 | test_operations_on_invalid_handle( handle ); |
| 1446 | |
| 1447 | exit: |
| 1448 | psa_destroy_key( handle ); |
| 1449 | psa_reset_key_attributes( &got_attributes ); |
| 1450 | PSA_DONE( ); |
| 1451 | } |
| 1452 | /* END_CASE */ |
| 1453 | |
| 1454 | /* BEGIN_CASE */ |
| 1455 | void import_with_data( data_t *data, int type_arg, |
| 1456 | int attr_bits_arg, |
| 1457 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1458 | { |
| 1459 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1460 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1461 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1462 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1463 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1464 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1465 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1466 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1467 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1468 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1469 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1470 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1471 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1472 | status = psa_import_key( &attributes, data->x, data->len, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1473 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1474 | if( status != PSA_SUCCESS ) |
| 1475 | goto exit; |
| 1476 | |
| 1477 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1478 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1479 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1480 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1481 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1482 | |
| 1483 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1484 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1485 | |
| 1486 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1487 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1488 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1489 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1490 | } |
| 1491 | /* END_CASE */ |
| 1492 | |
| 1493 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1494 | void import_large_key( int type_arg, int byte_size_arg, |
| 1495 | int expected_status_arg ) |
| 1496 | { |
| 1497 | psa_key_type_t type = type_arg; |
| 1498 | size_t byte_size = byte_size_arg; |
| 1499 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1500 | psa_status_t expected_status = expected_status_arg; |
| 1501 | psa_key_handle_t handle = 0; |
| 1502 | psa_status_t status; |
| 1503 | uint8_t *buffer = NULL; |
| 1504 | size_t buffer_size = byte_size + 1; |
| 1505 | size_t n; |
| 1506 | |
| 1507 | /* It would be better to skip the test than fail it if the allocation |
| 1508 | * fails, but the test framework doesn't support this yet. */ |
| 1509 | ASSERT_ALLOC( buffer, buffer_size ); |
| 1510 | memset( buffer, 'K', byte_size ); |
| 1511 | |
| 1512 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1513 | |
| 1514 | /* Try importing the key */ |
| 1515 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1516 | psa_set_key_type( &attributes, type ); |
| 1517 | status = psa_import_key( &attributes, buffer, byte_size, &handle ); |
| 1518 | TEST_EQUAL( status, expected_status ); |
| 1519 | |
| 1520 | if( status == PSA_SUCCESS ) |
| 1521 | { |
| 1522 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1523 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1524 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1525 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1526 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1527 | memset( buffer, 0, byte_size + 1 ); |
| 1528 | PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) ); |
| 1529 | for( n = 0; n < byte_size; n++ ) |
| 1530 | TEST_EQUAL( buffer[n], 'K' ); |
| 1531 | for( n = byte_size; n < buffer_size; n++ ) |
| 1532 | TEST_EQUAL( buffer[n], 0 ); |
| 1533 | } |
| 1534 | |
| 1535 | exit: |
| 1536 | psa_destroy_key( handle ); |
| 1537 | PSA_DONE( ); |
| 1538 | mbedtls_free( buffer ); |
| 1539 | } |
| 1540 | /* END_CASE */ |
| 1541 | |
| 1542 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1543 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1544 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1545 | psa_key_handle_t handle = 0; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1546 | size_t bits = bits_arg; |
| 1547 | psa_status_t expected_status = expected_status_arg; |
| 1548 | psa_status_t status; |
| 1549 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1550 | 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] | 1551 | size_t buffer_size = /* Slight overapproximations */ |
| 1552 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1553 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1554 | unsigned char *p; |
| 1555 | int ret; |
| 1556 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1557 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1558 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1559 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1560 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1561 | |
| 1562 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1563 | bits, keypair ) ) >= 0 ); |
| 1564 | length = ret; |
| 1565 | |
| 1566 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1567 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1568 | status = psa_import_key( &attributes, p, length, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1569 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1570 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1571 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1572 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1573 | |
| 1574 | exit: |
| 1575 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1576 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1577 | } |
| 1578 | /* END_CASE */ |
| 1579 | |
| 1580 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1581 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1582 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1583 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1584 | int expected_bits, |
| 1585 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1586 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1587 | int canonical_input ) |
| 1588 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1589 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1590 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1591 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1592 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1593 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1594 | unsigned char *exported = NULL; |
| 1595 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1596 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1597 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1598 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1599 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1600 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1601 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1602 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1603 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1604 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1605 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1606 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1607 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1608 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1609 | psa_set_key_algorithm( &attributes, alg ); |
| 1610 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1611 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1612 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1613 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1614 | |
| 1615 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1616 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1617 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1618 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1619 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1620 | |
| 1621 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1622 | status = psa_export_key( handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1623 | exported, export_size, |
| 1624 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1625 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1626 | |
| 1627 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1628 | * and export_size. On errors, the exported length must be 0. */ |
| 1629 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1630 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1631 | TEST_ASSERT( exported_length <= export_size ); |
| 1632 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1633 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1634 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1635 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1636 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1637 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1638 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1639 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1640 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1641 | if( ! exercise_export_key( handle, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1642 | goto exit; |
| 1643 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1644 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1645 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1646 | else |
| 1647 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1648 | psa_key_handle_t handle2; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1649 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
| 1650 | &handle2 ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1651 | PSA_ASSERT( psa_export_key( handle2, |
| 1652 | reexported, |
| 1653 | export_size, |
| 1654 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1655 | ASSERT_COMPARE( exported, exported_length, |
| 1656 | reexported, reexported_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1657 | PSA_ASSERT( psa_close_key( handle2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1658 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1659 | 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] | 1660 | |
| 1661 | destroy: |
| 1662 | /* Destroy the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1663 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1664 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1665 | |
| 1666 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1667 | mbedtls_free( exported ); |
| 1668 | mbedtls_free( reexported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1669 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1670 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1671 | } |
| 1672 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1673 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1674 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1675 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1676 | int type_arg, |
| 1677 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1678 | int export_size_delta, |
| 1679 | int expected_export_status_arg, |
| 1680 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1681 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1682 | psa_key_handle_t handle = 0; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1683 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1684 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1685 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1686 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1687 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1688 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1689 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1690 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1691 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1692 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1693 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1694 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1695 | psa_set_key_algorithm( &attributes, alg ); |
| 1696 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1697 | |
| 1698 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1699 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1700 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1701 | /* Export the public key */ |
| 1702 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1703 | status = psa_export_public_key( handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1704 | exported, export_size, |
| 1705 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1706 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1707 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1708 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1709 | 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] | 1710 | size_t bits; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1711 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1712 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1713 | TEST_ASSERT( expected_public_key->len <= |
| 1714 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1715 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1716 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1717 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1718 | |
| 1719 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1720 | mbedtls_free( exported ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1721 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1722 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1723 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1724 | } |
| 1725 | /* END_CASE */ |
| 1726 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1727 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1728 | void import_and_exercise_key( data_t *data, |
| 1729 | int type_arg, |
| 1730 | int bits_arg, |
| 1731 | int alg_arg ) |
| 1732 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1733 | psa_key_handle_t handle = 0; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1734 | psa_key_type_t type = type_arg; |
| 1735 | size_t bits = bits_arg; |
| 1736 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1737 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1738 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1739 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1740 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1741 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1742 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1743 | psa_set_key_usage_flags( &attributes, usage ); |
| 1744 | psa_set_key_algorithm( &attributes, alg ); |
| 1745 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1746 | |
| 1747 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1748 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1749 | |
| 1750 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1751 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1752 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1753 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1754 | |
| 1755 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1756 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1757 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1758 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1759 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1760 | test_operations_on_invalid_handle( handle ); |
| 1761 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1762 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1763 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1764 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1765 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1766 | } |
| 1767 | /* END_CASE */ |
| 1768 | |
| 1769 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1770 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1771 | int bits_arg, int expected_bits_arg, |
| 1772 | int usage_arg, int expected_usage_arg, |
| 1773 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1774 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1775 | psa_key_handle_t handle = 0; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1776 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1777 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1778 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1779 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1780 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1781 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1782 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1783 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1784 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1785 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1786 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1787 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1788 | psa_set_key_usage_flags( &attributes, usage ); |
| 1789 | psa_set_key_algorithm( &attributes, alg ); |
| 1790 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1791 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1792 | |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1793 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
| 1794 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1795 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1796 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1797 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1798 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1799 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1800 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1801 | |
| 1802 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1803 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1804 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1805 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1806 | } |
| 1807 | /* END_CASE */ |
| 1808 | |
| 1809 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1810 | void check_key_policy( int type_arg, int bits_arg, |
| 1811 | int usage_arg, int alg_arg ) |
| 1812 | { |
| 1813 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
| 1814 | usage_arg, usage_arg, alg_arg, alg_arg ); |
| 1815 | goto exit; |
| 1816 | } |
| 1817 | /* END_CASE */ |
| 1818 | |
| 1819 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1820 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1821 | { |
| 1822 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1823 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1824 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1825 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1826 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1827 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1828 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1829 | |
| 1830 | memset( &zero, 0, sizeof( zero ) ); |
| 1831 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1832 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1833 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1834 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1835 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1836 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1837 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1838 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1839 | |
| 1840 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1841 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1842 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1843 | |
| 1844 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1845 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1846 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1847 | |
| 1848 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1849 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1850 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1851 | } |
| 1852 | /* END_CASE */ |
| 1853 | |
| 1854 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1855 | void mac_key_policy( int policy_usage, |
| 1856 | int policy_alg, |
| 1857 | int key_type, |
| 1858 | data_t *key_data, |
| 1859 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1860 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1861 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1862 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1863 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1864 | psa_status_t status; |
| 1865 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1866 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1867 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1868 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1869 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1870 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1871 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1872 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1873 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1874 | &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1875 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1876 | status = psa_mac_sign_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1877 | if( policy_alg == exercise_alg && |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1878 | ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1879 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1880 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1881 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1882 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1883 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1884 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1885 | status = psa_mac_verify_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1886 | if( policy_alg == exercise_alg && |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1887 | ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1888 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1889 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1890 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1891 | |
| 1892 | exit: |
| 1893 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1894 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1895 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1896 | } |
| 1897 | /* END_CASE */ |
| 1898 | |
| 1899 | /* BEGIN_CASE */ |
| 1900 | void cipher_key_policy( int policy_usage, |
| 1901 | int policy_alg, |
| 1902 | int key_type, |
| 1903 | data_t *key_data, |
| 1904 | int exercise_alg ) |
| 1905 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1906 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1907 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1908 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1909 | psa_status_t status; |
| 1910 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1911 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1912 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1913 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1914 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1915 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1916 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1917 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1918 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1919 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1920 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1921 | if( policy_alg == exercise_alg && |
| 1922 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1923 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1924 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1925 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1926 | psa_cipher_abort( &operation ); |
| 1927 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1928 | status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1929 | if( policy_alg == exercise_alg && |
| 1930 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1931 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1932 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1933 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1934 | |
| 1935 | exit: |
| 1936 | psa_cipher_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1937 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1938 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1939 | } |
| 1940 | /* END_CASE */ |
| 1941 | |
| 1942 | /* BEGIN_CASE */ |
| 1943 | void aead_key_policy( int policy_usage, |
| 1944 | int policy_alg, |
| 1945 | int key_type, |
| 1946 | data_t *key_data, |
| 1947 | int nonce_length_arg, |
| 1948 | int tag_length_arg, |
| 1949 | int exercise_alg ) |
| 1950 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1951 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1952 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1953 | psa_status_t status; |
| 1954 | unsigned char nonce[16] = {0}; |
| 1955 | size_t nonce_length = nonce_length_arg; |
| 1956 | unsigned char tag[16]; |
| 1957 | size_t tag_length = tag_length_arg; |
| 1958 | size_t output_length; |
| 1959 | |
| 1960 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1961 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1962 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1963 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1964 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1965 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1966 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1967 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1968 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1969 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1970 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1971 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1972 | status = psa_aead_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1973 | nonce, nonce_length, |
| 1974 | NULL, 0, |
| 1975 | NULL, 0, |
| 1976 | tag, tag_length, |
| 1977 | &output_length ); |
| 1978 | if( policy_alg == exercise_alg && |
| 1979 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1980 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1981 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1982 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1983 | |
| 1984 | memset( tag, 0, sizeof( tag ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1985 | status = psa_aead_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1986 | nonce, nonce_length, |
| 1987 | NULL, 0, |
| 1988 | tag, tag_length, |
| 1989 | NULL, 0, |
| 1990 | &output_length ); |
| 1991 | if( policy_alg == exercise_alg && |
| 1992 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1993 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1994 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1995 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1996 | |
| 1997 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1998 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1999 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2000 | } |
| 2001 | /* END_CASE */ |
| 2002 | |
| 2003 | /* BEGIN_CASE */ |
| 2004 | void asymmetric_encryption_key_policy( int policy_usage, |
| 2005 | int policy_alg, |
| 2006 | int key_type, |
| 2007 | data_t *key_data, |
| 2008 | int exercise_alg ) |
| 2009 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2010 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2011 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2012 | psa_status_t status; |
| 2013 | size_t key_bits; |
| 2014 | size_t buffer_length; |
| 2015 | unsigned char *buffer = NULL; |
| 2016 | size_t output_length; |
| 2017 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2018 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2019 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2020 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2021 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2022 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2023 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2024 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2025 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2026 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2027 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 2028 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2029 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2030 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2031 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2032 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2033 | status = psa_asymmetric_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2034 | NULL, 0, |
| 2035 | NULL, 0, |
| 2036 | buffer, buffer_length, |
| 2037 | &output_length ); |
| 2038 | if( policy_alg == exercise_alg && |
| 2039 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2040 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2041 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2042 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2043 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2044 | if( buffer_length != 0 ) |
| 2045 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2046 | status = psa_asymmetric_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2047 | buffer, buffer_length, |
| 2048 | NULL, 0, |
| 2049 | buffer, buffer_length, |
| 2050 | &output_length ); |
| 2051 | if( policy_alg == exercise_alg && |
| 2052 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2053 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2054 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2055 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2056 | |
| 2057 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2058 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2059 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2060 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2061 | mbedtls_free( buffer ); |
| 2062 | } |
| 2063 | /* END_CASE */ |
| 2064 | |
| 2065 | /* BEGIN_CASE */ |
| 2066 | void asymmetric_signature_key_policy( int policy_usage, |
| 2067 | int policy_alg, |
| 2068 | int key_type, |
| 2069 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2070 | int exercise_alg, |
| 2071 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2072 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2073 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2074 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2075 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2076 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2077 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2078 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2079 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2080 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2081 | int compatible_alg = payload_length_arg > 0; |
| 2082 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2083 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2084 | size_t signature_length; |
| 2085 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2086 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2087 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2088 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2089 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2090 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2091 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2092 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2093 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2094 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2095 | status = psa_sign_hash( handle, exercise_alg, |
| 2096 | payload, payload_length, |
| 2097 | signature, sizeof( signature ), |
| 2098 | &signature_length ); |
| 2099 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2100 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2101 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2102 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2103 | |
| 2104 | memset( signature, 0, sizeof( signature ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2105 | status = psa_verify_hash( handle, exercise_alg, |
| 2106 | payload, payload_length, |
| 2107 | signature, sizeof( signature ) ); |
| 2108 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2109 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2110 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2111 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2112 | |
| 2113 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2114 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2115 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2116 | } |
| 2117 | /* END_CASE */ |
| 2118 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2119 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2120 | void derive_key_policy( int policy_usage, |
| 2121 | int policy_alg, |
| 2122 | int key_type, |
| 2123 | data_t *key_data, |
| 2124 | int exercise_alg ) |
| 2125 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2126 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2127 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2128 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2129 | psa_status_t status; |
| 2130 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2131 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2132 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2133 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2134 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2135 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2136 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2137 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2138 | &handle ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2139 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2140 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2141 | |
| 2142 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2143 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2144 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2145 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2146 | &operation, |
| 2147 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2148 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2149 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2150 | |
| 2151 | status = psa_key_derivation_input_key( &operation, |
| 2152 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2153 | handle ); |
| 2154 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2155 | if( policy_alg == exercise_alg && |
| 2156 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2157 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2158 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2159 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2160 | |
| 2161 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2162 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2163 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2164 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2165 | } |
| 2166 | /* END_CASE */ |
| 2167 | |
| 2168 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2169 | void agreement_key_policy( int policy_usage, |
| 2170 | int policy_alg, |
| 2171 | int key_type_arg, |
| 2172 | data_t *key_data, |
| 2173 | int exercise_alg ) |
| 2174 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2175 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2176 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2177 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2178 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2179 | psa_status_t status; |
| 2180 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2181 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2182 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2183 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2184 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2185 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2186 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2187 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2188 | &handle ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2189 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2190 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2191 | status = key_agreement_with_self( &operation, handle ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2192 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2193 | if( policy_alg == exercise_alg && |
| 2194 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2195 | PSA_ASSERT( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2196 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2197 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2198 | |
| 2199 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2200 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2201 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2202 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2203 | } |
| 2204 | /* END_CASE */ |
| 2205 | |
| 2206 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2207 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2208 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2209 | { |
| 2210 | psa_key_handle_t handle = 0; |
| 2211 | psa_key_type_t key_type = key_type_arg; |
| 2212 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2213 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2214 | psa_key_usage_t usage = usage_arg; |
| 2215 | psa_algorithm_t alg = alg_arg; |
| 2216 | psa_algorithm_t alg2 = alg2_arg; |
| 2217 | |
| 2218 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2219 | |
| 2220 | psa_set_key_usage_flags( &attributes, usage ); |
| 2221 | psa_set_key_algorithm( &attributes, alg ); |
| 2222 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2223 | psa_set_key_type( &attributes, key_type ); |
| 2224 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2225 | &handle ) ); |
| 2226 | |
| 2227 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 2228 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2229 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2230 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2231 | |
| 2232 | if( ! exercise_key( handle, usage, alg ) ) |
| 2233 | goto exit; |
| 2234 | if( ! exercise_key( handle, usage, alg2 ) ) |
| 2235 | goto exit; |
| 2236 | |
| 2237 | exit: |
| 2238 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2239 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2240 | } |
| 2241 | /* END_CASE */ |
| 2242 | |
| 2243 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2244 | void raw_agreement_key_policy( int policy_usage, |
| 2245 | int policy_alg, |
| 2246 | int key_type_arg, |
| 2247 | data_t *key_data, |
| 2248 | int exercise_alg ) |
| 2249 | { |
| 2250 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2251 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2252 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2253 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2254 | psa_status_t status; |
| 2255 | |
| 2256 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2257 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2258 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2259 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2260 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2261 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2262 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2263 | &handle ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2264 | |
| 2265 | status = raw_key_agreement_with_self( exercise_alg, handle ); |
| 2266 | |
| 2267 | if( policy_alg == exercise_alg && |
| 2268 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 2269 | PSA_ASSERT( status ); |
| 2270 | else |
| 2271 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2272 | |
| 2273 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2274 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2275 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2276 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2277 | } |
| 2278 | /* END_CASE */ |
| 2279 | |
| 2280 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2281 | void copy_success( int source_usage_arg, |
| 2282 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2283 | int type_arg, data_t *material, |
| 2284 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2285 | int target_usage_arg, |
| 2286 | int target_alg_arg, int target_alg2_arg, |
| 2287 | int expected_usage_arg, |
| 2288 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2289 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2290 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2291 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2292 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2293 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2294 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2295 | psa_key_handle_t source_handle = 0; |
| 2296 | psa_key_handle_t target_handle = 0; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2297 | uint8_t *export_buffer = NULL; |
| 2298 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2299 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2300 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2301 | /* Prepare the source key. */ |
| 2302 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2303 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2304 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2305 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2306 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2307 | material->x, material->len, |
| 2308 | &source_handle ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2309 | PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2310 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2311 | /* Prepare the target attributes. */ |
| 2312 | if( copy_attributes ) |
| 2313 | target_attributes = source_attributes; |
| 2314 | if( target_usage_arg != -1 ) |
| 2315 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2316 | if( target_alg_arg != -1 ) |
| 2317 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2318 | if( target_alg2_arg != -1 ) |
| 2319 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2320 | |
| 2321 | /* Copy the key. */ |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2322 | PSA_ASSERT( psa_copy_key( source_handle, |
| 2323 | &target_attributes, &target_handle ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2324 | |
| 2325 | /* Destroy the source to ensure that this doesn't affect the target. */ |
| 2326 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2327 | |
| 2328 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2329 | PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) ); |
| 2330 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2331 | psa_get_key_type( &target_attributes ) ); |
| 2332 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2333 | psa_get_key_bits( &target_attributes ) ); |
| 2334 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2335 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2336 | TEST_EQUAL( expected_alg2, |
| 2337 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2338 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2339 | { |
| 2340 | size_t length; |
| 2341 | ASSERT_ALLOC( export_buffer, material->len ); |
| 2342 | PSA_ASSERT( psa_export_key( target_handle, export_buffer, |
| 2343 | material->len, &length ) ); |
| 2344 | ASSERT_COMPARE( material->x, material->len, |
| 2345 | export_buffer, length ); |
| 2346 | } |
| 2347 | if( ! exercise_key( target_handle, expected_usage, expected_alg ) ) |
| 2348 | goto exit; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2349 | if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) ) |
| 2350 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2351 | |
| 2352 | PSA_ASSERT( psa_close_key( target_handle ) ); |
| 2353 | |
| 2354 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2355 | psa_reset_key_attributes( &source_attributes ); |
| 2356 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2357 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2358 | mbedtls_free( export_buffer ); |
| 2359 | } |
| 2360 | /* END_CASE */ |
| 2361 | |
| 2362 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2363 | void copy_fail( int source_usage_arg, |
| 2364 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2365 | int type_arg, data_t *material, |
| 2366 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2367 | int target_usage_arg, |
| 2368 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2369 | int expected_status_arg ) |
| 2370 | { |
| 2371 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2372 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2373 | psa_key_handle_t source_handle = 0; |
| 2374 | psa_key_handle_t target_handle = 0; |
| 2375 | |
| 2376 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2377 | |
| 2378 | /* Prepare the source key. */ |
| 2379 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2380 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2381 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2382 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2383 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2384 | material->x, material->len, |
| 2385 | &source_handle ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2386 | |
| 2387 | /* Prepare the target attributes. */ |
| 2388 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2389 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2390 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2391 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2392 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2393 | |
| 2394 | /* Try to copy the key. */ |
| 2395 | TEST_EQUAL( psa_copy_key( source_handle, |
| 2396 | &target_attributes, &target_handle ), |
| 2397 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2398 | |
| 2399 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2400 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2401 | exit: |
| 2402 | psa_reset_key_attributes( &source_attributes ); |
| 2403 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2404 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2405 | } |
| 2406 | /* END_CASE */ |
| 2407 | |
| 2408 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2409 | void hash_operation_init( ) |
| 2410 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2411 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2412 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2413 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2414 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2415 | * to supress the Clang warning for the test. */ |
| 2416 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2417 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2418 | psa_hash_operation_t zero; |
| 2419 | |
| 2420 | memset( &zero, 0, sizeof( zero ) ); |
| 2421 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2422 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2423 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2424 | PSA_ERROR_BAD_STATE ); |
| 2425 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2426 | PSA_ERROR_BAD_STATE ); |
| 2427 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2428 | PSA_ERROR_BAD_STATE ); |
| 2429 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2430 | /* A default hash operation should be abortable without error. */ |
| 2431 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2432 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2433 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2434 | } |
| 2435 | /* END_CASE */ |
| 2436 | |
| 2437 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2438 | void hash_setup( int alg_arg, |
| 2439 | int expected_status_arg ) |
| 2440 | { |
| 2441 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2442 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2443 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2444 | psa_status_t status; |
| 2445 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2446 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2447 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2448 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2449 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2450 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2451 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2452 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2453 | |
| 2454 | /* If setup failed, reproduce the failure, so as to |
| 2455 | * test the resulting state of the operation object. */ |
| 2456 | if( status != PSA_SUCCESS ) |
| 2457 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2458 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2459 | /* Now the operation object should be reusable. */ |
| 2460 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2461 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2462 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2463 | #endif |
| 2464 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2465 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2466 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2467 | } |
| 2468 | /* END_CASE */ |
| 2469 | |
| 2470 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2471 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2472 | int output_size_arg, int expected_status_arg ) |
| 2473 | { |
| 2474 | psa_algorithm_t alg = alg_arg; |
| 2475 | uint8_t *output = NULL; |
| 2476 | size_t output_size = output_size_arg; |
| 2477 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 2478 | psa_status_t expected_status = expected_status_arg; |
| 2479 | psa_status_t status; |
| 2480 | |
| 2481 | ASSERT_ALLOC( output, output_size ); |
| 2482 | |
| 2483 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2484 | |
| 2485 | status = psa_hash_compute( alg, input->x, input->len, |
| 2486 | output, output_size, &output_length ); |
| 2487 | TEST_EQUAL( status, expected_status ); |
| 2488 | TEST_ASSERT( output_length <= output_size ); |
| 2489 | |
| 2490 | exit: |
| 2491 | mbedtls_free( output ); |
| 2492 | PSA_DONE( ); |
| 2493 | } |
| 2494 | /* END_CASE */ |
| 2495 | |
| 2496 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2497 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2498 | data_t *reference_hash, |
| 2499 | int expected_status_arg ) |
| 2500 | { |
| 2501 | psa_algorithm_t alg = alg_arg; |
| 2502 | psa_status_t expected_status = expected_status_arg; |
| 2503 | psa_status_t status; |
| 2504 | |
| 2505 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2506 | |
| 2507 | status = psa_hash_compare( alg, input->x, input->len, |
| 2508 | reference_hash->x, reference_hash->len ); |
| 2509 | TEST_EQUAL( status, expected_status ); |
| 2510 | |
| 2511 | exit: |
| 2512 | PSA_DONE( ); |
| 2513 | } |
| 2514 | /* END_CASE */ |
| 2515 | |
| 2516 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2517 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2518 | data_t *expected_output ) |
| 2519 | { |
| 2520 | psa_algorithm_t alg = alg_arg; |
| 2521 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2522 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 2523 | size_t i; |
| 2524 | |
| 2525 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2526 | |
| 2527 | /* Compute with tight buffer */ |
| 2528 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2529 | output, PSA_HASH_SIZE( alg ), |
| 2530 | &output_length ) ); |
| 2531 | TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) ); |
| 2532 | ASSERT_COMPARE( output, output_length, |
| 2533 | expected_output->x, expected_output->len ); |
| 2534 | |
| 2535 | /* Compute with larger buffer */ |
| 2536 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2537 | output, sizeof( output ), |
| 2538 | &output_length ) ); |
| 2539 | TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) ); |
| 2540 | ASSERT_COMPARE( output, output_length, |
| 2541 | expected_output->x, expected_output->len ); |
| 2542 | |
| 2543 | /* Compare with correct hash */ |
| 2544 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2545 | output, output_length ) ); |
| 2546 | |
| 2547 | /* Compare with trailing garbage */ |
| 2548 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2549 | output, output_length + 1 ), |
| 2550 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2551 | |
| 2552 | /* Compare with truncated hash */ |
| 2553 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2554 | output, output_length - 1 ), |
| 2555 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2556 | |
| 2557 | /* Compare with corrupted value */ |
| 2558 | for( i = 0; i < output_length; i++ ) |
| 2559 | { |
| 2560 | test_set_step( i ); |
| 2561 | output[i] ^= 1; |
| 2562 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2563 | output, output_length ), |
| 2564 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2565 | output[i] ^= 1; |
| 2566 | } |
| 2567 | |
| 2568 | exit: |
| 2569 | PSA_DONE( ); |
| 2570 | } |
| 2571 | /* END_CASE */ |
| 2572 | |
| 2573 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2574 | void hash_bad_order( ) |
| 2575 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2576 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2577 | unsigned char input[] = ""; |
| 2578 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2579 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2580 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2581 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2582 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2583 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2584 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2585 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2586 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2587 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2588 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2589 | /* Call setup twice in a row. */ |
| 2590 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2591 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2592 | PSA_ERROR_BAD_STATE ); |
| 2593 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2594 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2595 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2596 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2597 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2598 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2599 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2600 | /* Call update after finish. */ |
| 2601 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2602 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2603 | hash, sizeof( hash ), &hash_len ) ); |
| 2604 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2605 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2606 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2607 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2608 | /* Call verify without calling setup beforehand. */ |
| 2609 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2610 | valid_hash, sizeof( valid_hash ) ), |
| 2611 | PSA_ERROR_BAD_STATE ); |
| 2612 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2613 | |
| 2614 | /* Call verify after finish. */ |
| 2615 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2616 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2617 | hash, sizeof( hash ), &hash_len ) ); |
| 2618 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2619 | valid_hash, sizeof( valid_hash ) ), |
| 2620 | PSA_ERROR_BAD_STATE ); |
| 2621 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2622 | |
| 2623 | /* Call verify twice in a row. */ |
| 2624 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2625 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2626 | valid_hash, sizeof( valid_hash ) ) ); |
| 2627 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2628 | valid_hash, sizeof( valid_hash ) ), |
| 2629 | PSA_ERROR_BAD_STATE ); |
| 2630 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2631 | |
| 2632 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2633 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2634 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2635 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2636 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2637 | |
| 2638 | /* Call finish twice in a row. */ |
| 2639 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2640 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2641 | hash, sizeof( hash ), &hash_len ) ); |
| 2642 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2643 | hash, sizeof( hash ), &hash_len ), |
| 2644 | PSA_ERROR_BAD_STATE ); |
| 2645 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2646 | |
| 2647 | /* Call finish after calling verify. */ |
| 2648 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2649 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2650 | valid_hash, sizeof( valid_hash ) ) ); |
| 2651 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2652 | hash, sizeof( hash ), &hash_len ), |
| 2653 | PSA_ERROR_BAD_STATE ); |
| 2654 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2655 | |
| 2656 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2657 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2658 | } |
| 2659 | /* END_CASE */ |
| 2660 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2661 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2662 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2663 | { |
| 2664 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2665 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2666 | * appended to it */ |
| 2667 | unsigned char hash[] = { |
| 2668 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2669 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2670 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2671 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2672 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2673 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2674 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2675 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2676 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2677 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2678 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2679 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2680 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2681 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2682 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2683 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2684 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2685 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2686 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2687 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2688 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2689 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2690 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2691 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2692 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2693 | } |
| 2694 | /* END_CASE */ |
| 2695 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2696 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2697 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2698 | { |
| 2699 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2700 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2701 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2702 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2703 | size_t hash_len; |
| 2704 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2705 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2706 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2707 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2708 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2709 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2710 | hash, expected_size - 1, &hash_len ), |
| 2711 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2712 | |
| 2713 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2714 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2715 | } |
| 2716 | /* END_CASE */ |
| 2717 | |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2718 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2719 | void hash_clone_source_state( ) |
| 2720 | { |
| 2721 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2722 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2723 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2724 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2725 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2726 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2727 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2728 | size_t hash_len; |
| 2729 | |
| 2730 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2731 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2732 | |
| 2733 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2734 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2735 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2736 | hash, sizeof( hash ), &hash_len ) ); |
| 2737 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2738 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2739 | |
| 2740 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2741 | PSA_ERROR_BAD_STATE ); |
| 2742 | |
| 2743 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2744 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2745 | hash, sizeof( hash ), &hash_len ) ); |
| 2746 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2747 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2748 | hash, sizeof( hash ), &hash_len ) ); |
| 2749 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2750 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2751 | hash, sizeof( hash ), &hash_len ) ); |
| 2752 | |
| 2753 | exit: |
| 2754 | psa_hash_abort( &op_source ); |
| 2755 | psa_hash_abort( &op_init ); |
| 2756 | psa_hash_abort( &op_setup ); |
| 2757 | psa_hash_abort( &op_finished ); |
| 2758 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2759 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2760 | } |
| 2761 | /* END_CASE */ |
| 2762 | |
| 2763 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2764 | void hash_clone_target_state( ) |
| 2765 | { |
| 2766 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2767 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2768 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2769 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2770 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2771 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2772 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2773 | size_t hash_len; |
| 2774 | |
| 2775 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2776 | |
| 2777 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2778 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2779 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2780 | hash, sizeof( hash ), &hash_len ) ); |
| 2781 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2782 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2783 | |
| 2784 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2785 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2786 | hash, sizeof( hash ), &hash_len ) ); |
| 2787 | |
| 2788 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2789 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2790 | PSA_ERROR_BAD_STATE ); |
| 2791 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2792 | PSA_ERROR_BAD_STATE ); |
| 2793 | |
| 2794 | exit: |
| 2795 | psa_hash_abort( &op_target ); |
| 2796 | psa_hash_abort( &op_init ); |
| 2797 | psa_hash_abort( &op_setup ); |
| 2798 | psa_hash_abort( &op_finished ); |
| 2799 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2800 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2801 | } |
| 2802 | /* END_CASE */ |
| 2803 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2804 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2805 | void mac_operation_init( ) |
| 2806 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2807 | const uint8_t input[1] = { 0 }; |
| 2808 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2809 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2810 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2811 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2812 | * to supress the Clang warning for the test. */ |
| 2813 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2814 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2815 | psa_mac_operation_t zero; |
| 2816 | |
| 2817 | memset( &zero, 0, sizeof( zero ) ); |
| 2818 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2819 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2820 | TEST_EQUAL( psa_mac_update( &func, |
| 2821 | input, sizeof( input ) ), |
| 2822 | PSA_ERROR_BAD_STATE ); |
| 2823 | TEST_EQUAL( psa_mac_update( &init, |
| 2824 | input, sizeof( input ) ), |
| 2825 | PSA_ERROR_BAD_STATE ); |
| 2826 | TEST_EQUAL( psa_mac_update( &zero, |
| 2827 | input, sizeof( input ) ), |
| 2828 | PSA_ERROR_BAD_STATE ); |
| 2829 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2830 | /* A default MAC operation should be abortable without error. */ |
| 2831 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2832 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2833 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2834 | } |
| 2835 | /* END_CASE */ |
| 2836 | |
| 2837 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2838 | void mac_setup( int key_type_arg, |
| 2839 | data_t *key, |
| 2840 | int alg_arg, |
| 2841 | int expected_status_arg ) |
| 2842 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2843 | psa_key_type_t key_type = key_type_arg; |
| 2844 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2845 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2846 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2847 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2848 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2849 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2850 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2851 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2852 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2853 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2854 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2855 | &operation, &status ) ) |
| 2856 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2857 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2858 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2859 | /* The operation object should be reusable. */ |
| 2860 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2861 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2862 | smoke_test_key_data, |
| 2863 | sizeof( smoke_test_key_data ), |
| 2864 | KNOWN_SUPPORTED_MAC_ALG, |
| 2865 | &operation, &status ) ) |
| 2866 | goto exit; |
| 2867 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2868 | #endif |
| 2869 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2870 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2871 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2872 | } |
| 2873 | /* END_CASE */ |
| 2874 | |
| 2875 | /* BEGIN_CASE */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2876 | void mac_bad_order( ) |
| 2877 | { |
| 2878 | psa_key_handle_t handle = 0; |
| 2879 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2880 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
| 2881 | const uint8_t key[] = { |
| 2882 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2883 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2884 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2885 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2886 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2887 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2888 | size_t sign_mac_length = 0; |
| 2889 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2890 | const uint8_t verify_mac[] = { |
| 2891 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2892 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2893 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2894 | |
| 2895 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2896 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2897 | psa_set_key_algorithm( &attributes, alg ); |
| 2898 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2899 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2900 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2901 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2902 | /* Call update without calling setup beforehand. */ |
| 2903 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2904 | PSA_ERROR_BAD_STATE ); |
| 2905 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2906 | |
| 2907 | /* Call sign finish without calling setup beforehand. */ |
| 2908 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2909 | &sign_mac_length), |
| 2910 | PSA_ERROR_BAD_STATE ); |
| 2911 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2912 | |
| 2913 | /* Call verify finish without calling setup beforehand. */ |
| 2914 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2915 | verify_mac, sizeof( verify_mac ) ), |
| 2916 | PSA_ERROR_BAD_STATE ); |
| 2917 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2918 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2919 | /* Call setup twice in a row. */ |
| 2920 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2921 | handle, alg ) ); |
| 2922 | TEST_EQUAL( psa_mac_sign_setup( &operation, |
| 2923 | handle, alg ), |
| 2924 | PSA_ERROR_BAD_STATE ); |
| 2925 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2926 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2927 | /* Call update after sign finish. */ |
| 2928 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2929 | handle, alg ) ); |
| 2930 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2931 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2932 | sign_mac, sizeof( sign_mac ), |
| 2933 | &sign_mac_length ) ); |
| 2934 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2935 | PSA_ERROR_BAD_STATE ); |
| 2936 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2937 | |
| 2938 | /* Call update after verify finish. */ |
| 2939 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2940 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2941 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2942 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2943 | verify_mac, sizeof( verify_mac ) ) ); |
| 2944 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2945 | PSA_ERROR_BAD_STATE ); |
| 2946 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2947 | |
| 2948 | /* Call sign finish twice in a row. */ |
| 2949 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2950 | handle, alg ) ); |
| 2951 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2952 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2953 | sign_mac, sizeof( sign_mac ), |
| 2954 | &sign_mac_length ) ); |
| 2955 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2956 | sign_mac, sizeof( sign_mac ), |
| 2957 | &sign_mac_length ), |
| 2958 | PSA_ERROR_BAD_STATE ); |
| 2959 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2960 | |
| 2961 | /* Call verify finish twice in a row. */ |
| 2962 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2963 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2964 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2965 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2966 | verify_mac, sizeof( verify_mac ) ) ); |
| 2967 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2968 | verify_mac, sizeof( verify_mac ) ), |
| 2969 | PSA_ERROR_BAD_STATE ); |
| 2970 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2971 | |
| 2972 | /* Setup sign but try verify. */ |
| 2973 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2974 | handle, alg ) ); |
| 2975 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2976 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2977 | verify_mac, sizeof( verify_mac ) ), |
| 2978 | PSA_ERROR_BAD_STATE ); |
| 2979 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2980 | |
| 2981 | /* Setup verify but try sign. */ |
| 2982 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2983 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2984 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2985 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2986 | sign_mac, sizeof( sign_mac ), |
| 2987 | &sign_mac_length ), |
| 2988 | PSA_ERROR_BAD_STATE ); |
| 2989 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2990 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2991 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2992 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2993 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2994 | PSA_DONE( ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2995 | } |
| 2996 | /* END_CASE */ |
| 2997 | |
| 2998 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2999 | void mac_sign( int key_type_arg, |
| 3000 | data_t *key, |
| 3001 | int alg_arg, |
| 3002 | data_t *input, |
| 3003 | data_t *expected_mac ) |
| 3004 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3005 | psa_key_handle_t handle = 0; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3006 | psa_key_type_t key_type = key_type_arg; |
| 3007 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3008 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3009 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3010 | /* Leave a little extra room in the output buffer. At the end of the |
| 3011 | * test, we'll check that the implementation didn't overwrite onto |
| 3012 | * this extra room. */ |
| 3013 | uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10]; |
| 3014 | size_t mac_buffer_size = |
| 3015 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 3016 | size_t mac_length = 0; |
| 3017 | |
| 3018 | memset( actual_mac, '+', sizeof( actual_mac ) ); |
| 3019 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
| 3020 | TEST_ASSERT( expected_mac->len <= mac_buffer_size ); |
| 3021 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3022 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3023 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3024 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3025 | psa_set_key_algorithm( &attributes, alg ); |
| 3026 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3027 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3028 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3029 | |
| 3030 | /* Calculate the MAC. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3031 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 3032 | handle, alg ) ); |
| 3033 | PSA_ASSERT( psa_mac_update( &operation, |
| 3034 | input->x, input->len ) ); |
| 3035 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3036 | actual_mac, mac_buffer_size, |
| 3037 | &mac_length ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3038 | |
| 3039 | /* Compare with the expected value. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 3040 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3041 | actual_mac, mac_length ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3042 | |
| 3043 | /* Verify that the end of the buffer is untouched. */ |
| 3044 | TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+', |
| 3045 | sizeof( actual_mac ) - mac_length ) ); |
| 3046 | |
| 3047 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3048 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3049 | PSA_DONE( ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3050 | } |
| 3051 | /* END_CASE */ |
| 3052 | |
| 3053 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3054 | void mac_verify( int key_type_arg, |
| 3055 | data_t *key, |
| 3056 | int alg_arg, |
| 3057 | data_t *input, |
| 3058 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3059 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3060 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3061 | psa_key_type_t key_type = key_type_arg; |
| 3062 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3063 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3064 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3065 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3066 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 3067 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3068 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3069 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3070 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3071 | psa_set_key_algorithm( &attributes, alg ); |
| 3072 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3073 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3074 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3075 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3076 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 3077 | handle, alg ) ); |
| 3078 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 3079 | PSA_ASSERT( psa_mac_update( &operation, |
| 3080 | input->x, input->len ) ); |
| 3081 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3082 | expected_mac->x, |
| 3083 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3084 | |
| 3085 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3086 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3087 | PSA_DONE( ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3088 | } |
| 3089 | /* END_CASE */ |
| 3090 | |
| 3091 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3092 | void cipher_operation_init( ) |
| 3093 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3094 | const uint8_t input[1] = { 0 }; |
| 3095 | unsigned char output[1] = { 0 }; |
| 3096 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3097 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3098 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3099 | * though it's OK by the C standard. We could test for this, but we'd need |
| 3100 | * to supress the Clang warning for the test. */ |
| 3101 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3102 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3103 | psa_cipher_operation_t zero; |
| 3104 | |
| 3105 | memset( &zero, 0, sizeof( zero ) ); |
| 3106 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3107 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3108 | TEST_EQUAL( psa_cipher_update( &func, |
| 3109 | input, sizeof( input ), |
| 3110 | output, sizeof( output ), |
| 3111 | &output_length ), |
| 3112 | PSA_ERROR_BAD_STATE ); |
| 3113 | TEST_EQUAL( psa_cipher_update( &init, |
| 3114 | input, sizeof( input ), |
| 3115 | output, sizeof( output ), |
| 3116 | &output_length ), |
| 3117 | PSA_ERROR_BAD_STATE ); |
| 3118 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3119 | input, sizeof( input ), |
| 3120 | output, sizeof( output ), |
| 3121 | &output_length ), |
| 3122 | PSA_ERROR_BAD_STATE ); |
| 3123 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3124 | /* A default cipher operation should be abortable without error. */ |
| 3125 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3126 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3127 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3128 | } |
| 3129 | /* END_CASE */ |
| 3130 | |
| 3131 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3132 | void cipher_setup( int key_type_arg, |
| 3133 | data_t *key, |
| 3134 | int alg_arg, |
| 3135 | int expected_status_arg ) |
| 3136 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3137 | psa_key_type_t key_type = key_type_arg; |
| 3138 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3139 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3140 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3141 | psa_status_t status; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3142 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3143 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3144 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3145 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3146 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3147 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3148 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3149 | &operation, &status ) ) |
| 3150 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3151 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3152 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3153 | /* The operation object should be reusable. */ |
| 3154 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3155 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3156 | smoke_test_key_data, |
| 3157 | sizeof( smoke_test_key_data ), |
| 3158 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3159 | &operation, &status ) ) |
| 3160 | goto exit; |
| 3161 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3162 | #endif |
| 3163 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3164 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3165 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3166 | } |
| 3167 | /* END_CASE */ |
| 3168 | |
| 3169 | /* BEGIN_CASE */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3170 | void cipher_bad_order( ) |
| 3171 | { |
| 3172 | psa_key_handle_t handle = 0; |
| 3173 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3174 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3175 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3176 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3177 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 3178 | const uint8_t key[] = { |
| 3179 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3180 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3181 | const uint8_t text[] = { |
| 3182 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3183 | 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3184 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 3185 | size_t length = 0; |
| 3186 | |
| 3187 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3188 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3189 | psa_set_key_algorithm( &attributes, alg ); |
| 3190 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3191 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3192 | |
| 3193 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3194 | /* Call encrypt setup twice in a row. */ |
| 3195 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3196 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ), |
| 3197 | PSA_ERROR_BAD_STATE ); |
| 3198 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3199 | |
| 3200 | /* Call decrypt setup twice in a row. */ |
| 3201 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) ); |
| 3202 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ), |
| 3203 | PSA_ERROR_BAD_STATE ); |
| 3204 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3205 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3206 | /* Generate an IV without calling setup beforehand. */ |
| 3207 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3208 | buffer, sizeof( buffer ), |
| 3209 | &length ), |
| 3210 | PSA_ERROR_BAD_STATE ); |
| 3211 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3212 | |
| 3213 | /* Generate an IV twice in a row. */ |
| 3214 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3215 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3216 | buffer, sizeof( buffer ), |
| 3217 | &length ) ); |
| 3218 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3219 | buffer, sizeof( buffer ), |
| 3220 | &length ), |
| 3221 | PSA_ERROR_BAD_STATE ); |
| 3222 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3223 | |
| 3224 | /* Generate an IV after it's already set. */ |
| 3225 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3226 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3227 | iv, sizeof( iv ) ) ); |
| 3228 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3229 | buffer, sizeof( buffer ), |
| 3230 | &length ), |
| 3231 | PSA_ERROR_BAD_STATE ); |
| 3232 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3233 | |
| 3234 | /* Set an IV without calling setup beforehand. */ |
| 3235 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3236 | iv, sizeof( iv ) ), |
| 3237 | PSA_ERROR_BAD_STATE ); |
| 3238 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3239 | |
| 3240 | /* Set an IV after it's already set. */ |
| 3241 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3242 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3243 | iv, sizeof( iv ) ) ); |
| 3244 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3245 | iv, sizeof( iv ) ), |
| 3246 | PSA_ERROR_BAD_STATE ); |
| 3247 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3248 | |
| 3249 | /* Set an IV after it's already generated. */ |
| 3250 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3251 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3252 | buffer, sizeof( buffer ), |
| 3253 | &length ) ); |
| 3254 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3255 | iv, sizeof( iv ) ), |
| 3256 | PSA_ERROR_BAD_STATE ); |
| 3257 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3258 | |
| 3259 | /* Call update without calling setup beforehand. */ |
| 3260 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3261 | text, sizeof( text ), |
| 3262 | buffer, sizeof( buffer ), |
| 3263 | &length ), |
| 3264 | PSA_ERROR_BAD_STATE ); |
| 3265 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3266 | |
| 3267 | /* Call update without an IV where an IV is required. */ |
| 3268 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3269 | text, sizeof( text ), |
| 3270 | buffer, sizeof( buffer ), |
| 3271 | &length ), |
| 3272 | PSA_ERROR_BAD_STATE ); |
| 3273 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3274 | |
| 3275 | /* Call update after finish. */ |
| 3276 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3277 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3278 | iv, sizeof( iv ) ) ); |
| 3279 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3280 | buffer, sizeof( buffer ), &length ) ); |
| 3281 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3282 | text, sizeof( text ), |
| 3283 | buffer, sizeof( buffer ), |
| 3284 | &length ), |
| 3285 | PSA_ERROR_BAD_STATE ); |
| 3286 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3287 | |
| 3288 | /* Call finish without calling setup beforehand. */ |
| 3289 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3290 | buffer, sizeof( buffer ), &length ), |
| 3291 | PSA_ERROR_BAD_STATE ); |
| 3292 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3293 | |
| 3294 | /* Call finish without an IV where an IV is required. */ |
| 3295 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3296 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3297 | * for cipher modes with padding. */ |
| 3298 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3299 | buffer, sizeof( buffer ), &length ), |
| 3300 | PSA_ERROR_BAD_STATE ); |
| 3301 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3302 | |
| 3303 | /* Call finish twice in a row. */ |
| 3304 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3305 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3306 | iv, sizeof( iv ) ) ); |
| 3307 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3308 | buffer, sizeof( buffer ), &length ) ); |
| 3309 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3310 | buffer, sizeof( buffer ), &length ), |
| 3311 | PSA_ERROR_BAD_STATE ); |
| 3312 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3313 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3314 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 3315 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3316 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3317 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3318 | } |
| 3319 | /* END_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3320 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3321 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3322 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3323 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3324 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3325 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3326 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3327 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3328 | psa_status_t status; |
| 3329 | psa_key_type_t key_type = key_type_arg; |
| 3330 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3331 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3332 | unsigned char *output = NULL; |
| 3333 | size_t output_buffer_size = 0; |
| 3334 | size_t function_output_length = 0; |
| 3335 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3336 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3337 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3338 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3339 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3340 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3341 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3342 | psa_set_key_algorithm( &attributes, alg ); |
| 3343 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3344 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3345 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3346 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3347 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3348 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3349 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3350 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3351 | output_buffer_size = ( (size_t) input->len + |
| 3352 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3353 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3354 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3355 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3356 | input->x, input->len, |
| 3357 | output, output_buffer_size, |
| 3358 | &function_output_length ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3359 | total_output_length += function_output_length; |
| 3360 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3361 | output + total_output_length, |
| 3362 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3363 | &function_output_length ); |
| 3364 | total_output_length += function_output_length; |
| 3365 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3366 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3367 | if( expected_status == PSA_SUCCESS ) |
| 3368 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3369 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3370 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3371 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | exit: |
| 3375 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3376 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3377 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3378 | } |
| 3379 | /* END_CASE */ |
| 3380 | |
| 3381 | /* BEGIN_CASE */ |
| 3382 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3383 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3384 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3385 | int first_part_size_arg, |
| 3386 | int output1_length_arg, int output2_length_arg, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3387 | data_t *expected_output ) |
| 3388 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3389 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3390 | psa_key_type_t key_type = key_type_arg; |
| 3391 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3392 | size_t first_part_size = first_part_size_arg; |
| 3393 | size_t output1_length = output1_length_arg; |
| 3394 | size_t output2_length = output2_length_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3395 | unsigned char *output = NULL; |
| 3396 | size_t output_buffer_size = 0; |
| 3397 | size_t function_output_length = 0; |
| 3398 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3399 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3400 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3401 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3402 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3403 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3404 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3405 | psa_set_key_algorithm( &attributes, alg ); |
| 3406 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3407 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3408 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3409 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3410 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3411 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3412 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3413 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3414 | output_buffer_size = ( (size_t) input->len + |
| 3415 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3416 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3417 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3418 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3419 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3420 | output, output_buffer_size, |
| 3421 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3422 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3423 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3424 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3425 | input->x + first_part_size, |
| 3426 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3427 | output + total_output_length, |
| 3428 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3429 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3430 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3431 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3432 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3433 | output + total_output_length, |
| 3434 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3435 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3436 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3437 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3438 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3439 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3440 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3441 | |
| 3442 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3443 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3444 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3445 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3446 | } |
| 3447 | /* END_CASE */ |
| 3448 | |
| 3449 | /* BEGIN_CASE */ |
| 3450 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3451 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3452 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3453 | int first_part_size_arg, |
| 3454 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3455 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3456 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3457 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3458 | |
| 3459 | psa_key_type_t key_type = key_type_arg; |
| 3460 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3461 | size_t first_part_size = first_part_size_arg; |
| 3462 | size_t output1_length = output1_length_arg; |
| 3463 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3464 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3465 | size_t output_buffer_size = 0; |
| 3466 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3467 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3468 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3469 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3470 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3471 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3472 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3473 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3474 | psa_set_key_algorithm( &attributes, alg ); |
| 3475 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3476 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3477 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3478 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3479 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3480 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3481 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3482 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3483 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3484 | output_buffer_size = ( (size_t) input->len + |
| 3485 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3486 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3487 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3488 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3489 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3490 | input->x, first_part_size, |
| 3491 | output, output_buffer_size, |
| 3492 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3493 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3494 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3495 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3496 | input->x + first_part_size, |
| 3497 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3498 | output + total_output_length, |
| 3499 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3500 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3501 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3502 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3503 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3504 | output + total_output_length, |
| 3505 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3506 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3507 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3508 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3509 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3510 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3511 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3512 | |
| 3513 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3514 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3515 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3516 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3517 | } |
| 3518 | /* END_CASE */ |
| 3519 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3520 | /* BEGIN_CASE */ |
| 3521 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3522 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3523 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3524 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3525 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3526 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3527 | psa_status_t status; |
| 3528 | psa_key_type_t key_type = key_type_arg; |
| 3529 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3530 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3531 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3532 | size_t output_buffer_size = 0; |
| 3533 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3534 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3535 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3536 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3537 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3538 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3539 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3540 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3541 | psa_set_key_algorithm( &attributes, alg ); |
| 3542 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3543 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3544 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3545 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3546 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3547 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3548 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3549 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3550 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3551 | output_buffer_size = ( (size_t) input->len + |
| 3552 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3553 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3554 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3555 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3556 | input->x, input->len, |
| 3557 | output, output_buffer_size, |
| 3558 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3559 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3560 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3561 | output + total_output_length, |
| 3562 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3563 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3564 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3565 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3566 | |
| 3567 | if( expected_status == PSA_SUCCESS ) |
| 3568 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3569 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3570 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3571 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3572 | } |
| 3573 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3574 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3575 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3576 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3577 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3578 | } |
| 3579 | /* END_CASE */ |
| 3580 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3581 | /* BEGIN_CASE */ |
| 3582 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3583 | data_t *key, |
| 3584 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3585 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3586 | psa_key_handle_t handle = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3587 | psa_key_type_t key_type = key_type_arg; |
| 3588 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 3589 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3590 | size_t iv_size = 16; |
| 3591 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3592 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3593 | size_t output1_size = 0; |
| 3594 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3595 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3596 | size_t output2_size = 0; |
| 3597 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3598 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3599 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3600 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3601 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3602 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3603 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3604 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3605 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3606 | psa_set_key_algorithm( &attributes, alg ); |
| 3607 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3608 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3609 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3610 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3611 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3612 | handle, alg ) ); |
| 3613 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3614 | handle, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3615 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3616 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3617 | iv, iv_size, |
| 3618 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3619 | output1_size = ( (size_t) input->len + |
| 3620 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3621 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3622 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3623 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3624 | output1, output1_size, |
| 3625 | &output1_length ) ); |
| 3626 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3627 | output1 + output1_length, |
| 3628 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3629 | &function_output_length ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3630 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3631 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3632 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3633 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3634 | |
| 3635 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3636 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3637 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3638 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3639 | iv, iv_length ) ); |
| 3640 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3641 | output2, output2_size, |
| 3642 | &output2_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3643 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3644 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3645 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3646 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3647 | &function_output_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3648 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3649 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3650 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3651 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3652 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3653 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3654 | |
| 3655 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3656 | mbedtls_free( output1 ); |
| 3657 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3658 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3659 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3660 | } |
| 3661 | /* END_CASE */ |
| 3662 | |
| 3663 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3664 | void cipher_verify_output_multipart( int alg_arg, |
| 3665 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3666 | data_t *key, |
| 3667 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3668 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3669 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3670 | psa_key_handle_t handle = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3671 | psa_key_type_t key_type = key_type_arg; |
| 3672 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3673 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3674 | unsigned char iv[16] = {0}; |
| 3675 | size_t iv_size = 16; |
| 3676 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3677 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3678 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3679 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3680 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3681 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3682 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3683 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3684 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3685 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3686 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3687 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3688 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3689 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3690 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3691 | psa_set_key_algorithm( &attributes, alg ); |
| 3692 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3693 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3694 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3695 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3696 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3697 | handle, alg ) ); |
| 3698 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3699 | handle, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3700 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3701 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3702 | iv, iv_size, |
| 3703 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3704 | output1_buffer_size = ( (size_t) input->len + |
| 3705 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3706 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3707 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3708 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3709 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3710 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3711 | output1, output1_buffer_size, |
| 3712 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3713 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3714 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3715 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3716 | input->x + first_part_size, |
| 3717 | input->len - first_part_size, |
| 3718 | output1, output1_buffer_size, |
| 3719 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3720 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3721 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3722 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3723 | output1 + output1_length, |
| 3724 | output1_buffer_size - output1_length, |
| 3725 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3726 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3727 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3728 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3729 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3730 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3731 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3732 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3733 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3734 | iv, iv_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3735 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3736 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3737 | output2, output2_buffer_size, |
| 3738 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3739 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3740 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3741 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3742 | output1 + first_part_size, |
| 3743 | output1_length - first_part_size, |
| 3744 | output2, output2_buffer_size, |
| 3745 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3746 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3747 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3748 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3749 | output2 + output2_length, |
| 3750 | output2_buffer_size - output2_length, |
| 3751 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3752 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3753 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3754 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3755 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3756 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3757 | |
| 3758 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3759 | mbedtls_free( output1 ); |
| 3760 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3761 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3762 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3763 | } |
| 3764 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3765 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3766 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3767 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3768 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3769 | data_t *nonce, |
| 3770 | data_t *additional_data, |
| 3771 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3772 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3773 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3774 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3775 | psa_key_type_t key_type = key_type_arg; |
| 3776 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3777 | unsigned char *output_data = NULL; |
| 3778 | size_t output_size = 0; |
| 3779 | size_t output_length = 0; |
| 3780 | unsigned char *output_data2 = NULL; |
| 3781 | size_t output_length2 = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3782 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3783 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3784 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3785 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3786 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3787 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3788 | * should be exact. */ |
| 3789 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3790 | TEST_EQUAL( output_size, |
| 3791 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3792 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3793 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3794 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3795 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3796 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3797 | psa_set_key_algorithm( &attributes, alg ); |
| 3798 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3799 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3800 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3801 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3802 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3803 | TEST_EQUAL( psa_aead_encrypt( handle, alg, |
| 3804 | nonce->x, nonce->len, |
| 3805 | additional_data->x, |
| 3806 | additional_data->len, |
| 3807 | input_data->x, input_data->len, |
| 3808 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3809 | &output_length ), |
| 3810 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3811 | |
| 3812 | if( PSA_SUCCESS == expected_result ) |
| 3813 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3814 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3815 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3816 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3817 | * should be exact. */ |
| 3818 | TEST_EQUAL( input_data->len, |
| 3819 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) ); |
| 3820 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3821 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3822 | nonce->x, nonce->len, |
| 3823 | additional_data->x, |
| 3824 | additional_data->len, |
| 3825 | output_data, output_length, |
| 3826 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3827 | &output_length2 ), |
| 3828 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3829 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3830 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3831 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3832 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3833 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3834 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3835 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3836 | mbedtls_free( output_data ); |
| 3837 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3838 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3839 | } |
| 3840 | /* END_CASE */ |
| 3841 | |
| 3842 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3843 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3844 | int alg_arg, |
| 3845 | data_t *nonce, |
| 3846 | data_t *additional_data, |
| 3847 | data_t *input_data, |
| 3848 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3849 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3850 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3851 | psa_key_type_t key_type = key_type_arg; |
| 3852 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3853 | unsigned char *output_data = NULL; |
| 3854 | size_t output_size = 0; |
| 3855 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3856 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3857 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3858 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3859 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3860 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3861 | * should be exact. */ |
| 3862 | TEST_EQUAL( output_size, |
| 3863 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3864 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3865 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3866 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3867 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3868 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3869 | psa_set_key_algorithm( &attributes, alg ); |
| 3870 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3871 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3872 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3873 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3874 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3875 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 3876 | nonce->x, nonce->len, |
| 3877 | additional_data->x, additional_data->len, |
| 3878 | input_data->x, input_data->len, |
| 3879 | output_data, output_size, |
| 3880 | &output_length ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3881 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3882 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3883 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3884 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3885 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3886 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3887 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3888 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3889 | } |
| 3890 | /* END_CASE */ |
| 3891 | |
| 3892 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3893 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3894 | int alg_arg, |
| 3895 | data_t *nonce, |
| 3896 | data_t *additional_data, |
| 3897 | data_t *input_data, |
| 3898 | data_t *expected_data, |
| 3899 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3900 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3901 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3902 | psa_key_type_t key_type = key_type_arg; |
| 3903 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3904 | unsigned char *output_data = NULL; |
| 3905 | size_t output_size = 0; |
| 3906 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3907 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3908 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3909 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3910 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3911 | output_size = input_data->len - tag_length; |
| 3912 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3913 | * should be exact. */ |
| 3914 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3915 | TEST_EQUAL( output_size, |
| 3916 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3917 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3918 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3919 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3920 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3921 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3922 | psa_set_key_algorithm( &attributes, alg ); |
| 3923 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3924 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3925 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3926 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3927 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3928 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3929 | nonce->x, nonce->len, |
| 3930 | additional_data->x, |
| 3931 | additional_data->len, |
| 3932 | input_data->x, input_data->len, |
| 3933 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3934 | &output_length ), |
| 3935 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3936 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3937 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3938 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3939 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3940 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3941 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3942 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3943 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3944 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3945 | } |
| 3946 | /* END_CASE */ |
| 3947 | |
| 3948 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3949 | void signature_size( int type_arg, |
| 3950 | int bits, |
| 3951 | int alg_arg, |
| 3952 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3953 | { |
| 3954 | psa_key_type_t type = type_arg; |
| 3955 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3956 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3957 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3958 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3959 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3960 | TEST_EQUAL( actual_size, |
| 3961 | PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) ); |
| 3962 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3963 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3964 | exit: |
| 3965 | ; |
| 3966 | } |
| 3967 | /* END_CASE */ |
| 3968 | |
| 3969 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3970 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 3971 | int alg_arg, data_t *input_data, |
| 3972 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3973 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3974 | psa_key_handle_t handle = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3975 | psa_key_type_t key_type = key_type_arg; |
| 3976 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3977 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3978 | unsigned char *signature = NULL; |
| 3979 | size_t signature_size; |
| 3980 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3981 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3982 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3983 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3984 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3985 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3986 | psa_set_key_algorithm( &attributes, alg ); |
| 3987 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3988 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3989 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3990 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3991 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3992 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3993 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3994 | /* Allocate a buffer which has the size advertized by the |
| 3995 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3996 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3997 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3998 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3999 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4000 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4001 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4002 | /* Perform the signature. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4003 | PSA_ASSERT( psa_sign_hash( handle, alg, |
| 4004 | input_data->x, input_data->len, |
| 4005 | signature, signature_size, |
| 4006 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4007 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4008 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4009 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4010 | |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4011 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4012 | memset( signature, 0, signature_size ); |
| 4013 | signature_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4014 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 4015 | input_data->x, input_data->len, |
| 4016 | signature, signature_size, |
| 4017 | &signature_length ) ); |
| 4018 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4019 | signature, signature_length ); |
| 4020 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4021 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4022 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4023 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4024 | psa_destroy_key( handle ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 4025 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4026 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4027 | } |
| 4028 | /* END_CASE */ |
| 4029 | |
| 4030 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4031 | void sign_fail( int key_type_arg, data_t *key_data, |
| 4032 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4033 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4034 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4035 | psa_key_handle_t handle = 0; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4036 | psa_key_type_t key_type = key_type_arg; |
| 4037 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4038 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4039 | psa_status_t actual_status; |
| 4040 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 4041 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 4042 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4043 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4044 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4045 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4046 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4047 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4048 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4049 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4050 | psa_set_key_algorithm( &attributes, alg ); |
| 4051 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4052 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4053 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4054 | &handle ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4055 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4056 | actual_status = psa_sign_hash( handle, alg, |
| 4057 | input_data->x, input_data->len, |
| 4058 | signature, signature_size, |
| 4059 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4060 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4061 | /* The value of *signature_length is unspecified on error, but |
| 4062 | * whatever it is, it should be less than signature_size, so that |
| 4063 | * if the caller tries to read *signature_length bytes without |
| 4064 | * checking the error code then they don't overflow a buffer. */ |
| 4065 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4066 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4067 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4068 | signature_length = INVALID_EXPORT_LENGTH; |
| 4069 | TEST_EQUAL( psa_asymmetric_sign( handle, alg, |
| 4070 | input_data->x, input_data->len, |
| 4071 | signature, signature_size, |
| 4072 | &signature_length ), |
| 4073 | expected_status ); |
| 4074 | TEST_ASSERT( signature_length <= signature_size ); |
| 4075 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4076 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4077 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4078 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4079 | psa_destroy_key( handle ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4080 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4081 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4082 | } |
| 4083 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 4084 | |
| 4085 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4086 | void sign_verify( int key_type_arg, data_t *key_data, |
| 4087 | int alg_arg, data_t *input_data ) |
| 4088 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4089 | psa_key_handle_t handle = 0; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4090 | psa_key_type_t key_type = key_type_arg; |
| 4091 | psa_algorithm_t alg = alg_arg; |
| 4092 | size_t key_bits; |
| 4093 | unsigned char *signature = NULL; |
| 4094 | size_t signature_size; |
| 4095 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4096 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4097 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4098 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4099 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4100 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4101 | psa_set_key_algorithm( &attributes, alg ); |
| 4102 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4103 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4104 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4105 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4106 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4107 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4108 | |
| 4109 | /* Allocate a buffer which has the size advertized by the |
| 4110 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4111 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4112 | key_bits, alg ); |
| 4113 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4114 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4115 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4116 | |
| 4117 | /* Perform the signature. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4118 | PSA_ASSERT( psa_sign_hash( handle, alg, |
| 4119 | input_data->x, input_data->len, |
| 4120 | signature, signature_size, |
| 4121 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4122 | /* Check that the signature length looks sensible. */ |
| 4123 | TEST_ASSERT( signature_length <= signature_size ); |
| 4124 | TEST_ASSERT( signature_length > 0 ); |
| 4125 | |
| 4126 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4127 | PSA_ASSERT( psa_verify_hash( handle, alg, |
| 4128 | input_data->x, input_data->len, |
| 4129 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4130 | |
| 4131 | if( input_data->len != 0 ) |
| 4132 | { |
| 4133 | /* Flip a bit in the input and verify that the signature is now |
| 4134 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4135 | * because ECDSA may ignore the last few bits of the input. */ |
| 4136 | input_data->x[0] ^= 1; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4137 | TEST_EQUAL( psa_verify_hash( handle, alg, |
| 4138 | input_data->x, input_data->len, |
| 4139 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4140 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4144 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4145 | psa_destroy_key( handle ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4146 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4147 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4148 | } |
| 4149 | /* END_CASE */ |
| 4150 | |
| 4151 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4152 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 4153 | int alg_arg, data_t *hash_data, |
| 4154 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4155 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4156 | psa_key_handle_t handle = 0; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4157 | psa_key_type_t key_type = key_type_arg; |
| 4158 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4159 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4160 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4161 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 4162 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4163 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4164 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4165 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4166 | psa_set_key_algorithm( &attributes, alg ); |
| 4167 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4168 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4169 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4170 | &handle ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4171 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4172 | PSA_ASSERT( psa_verify_hash( handle, alg, |
| 4173 | hash_data->x, hash_data->len, |
| 4174 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4175 | |
| 4176 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4177 | PSA_ASSERT( psa_asymmetric_verify( handle, alg, |
| 4178 | hash_data->x, hash_data->len, |
| 4179 | signature_data->x, |
| 4180 | signature_data->len ) ); |
| 4181 | |
| 4182 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4183 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4184 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4185 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4186 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4187 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4188 | } |
| 4189 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4190 | |
| 4191 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4192 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 4193 | int alg_arg, data_t *hash_data, |
| 4194 | data_t *signature_data, |
| 4195 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4196 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4197 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4198 | psa_key_type_t key_type = key_type_arg; |
| 4199 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4200 | psa_status_t actual_status; |
| 4201 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4202 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4203 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4204 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4205 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4206 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4207 | psa_set_key_algorithm( &attributes, alg ); |
| 4208 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4209 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4210 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4211 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4212 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4213 | actual_status = psa_verify_hash( handle, alg, |
| 4214 | hash_data->x, hash_data->len, |
| 4215 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4216 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4217 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4218 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4219 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 4220 | hash_data->x, hash_data->len, |
| 4221 | signature_data->x, signature_data->len ), |
| 4222 | expected_status ); |
| 4223 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4224 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4225 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4226 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4227 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4228 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4229 | } |
| 4230 | /* END_CASE */ |
| 4231 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4232 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4233 | void asymmetric_encrypt( int key_type_arg, |
| 4234 | data_t *key_data, |
| 4235 | int alg_arg, |
| 4236 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4237 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4238 | int expected_output_length_arg, |
| 4239 | int expected_status_arg ) |
| 4240 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4241 | psa_key_handle_t handle = 0; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4242 | psa_key_type_t key_type = key_type_arg; |
| 4243 | psa_algorithm_t alg = alg_arg; |
| 4244 | size_t expected_output_length = expected_output_length_arg; |
| 4245 | size_t key_bits; |
| 4246 | unsigned char *output = NULL; |
| 4247 | size_t output_size; |
| 4248 | size_t output_length = ~0; |
| 4249 | psa_status_t actual_status; |
| 4250 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4251 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4252 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4253 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4254 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4255 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4256 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4257 | psa_set_key_algorithm( &attributes, alg ); |
| 4258 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4259 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4260 | &handle ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4261 | |
| 4262 | /* Determine the maximum output length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4263 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4264 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4265 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4266 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4267 | |
| 4268 | /* Encrypt the input */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4269 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4270 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4271 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4272 | output, output_size, |
| 4273 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4274 | TEST_EQUAL( actual_status, expected_status ); |
| 4275 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4276 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4277 | /* If the label is empty, the test framework puts a non-null pointer |
| 4278 | * in label->x. Test that a null pointer works as well. */ |
| 4279 | if( label->len == 0 ) |
| 4280 | { |
| 4281 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4282 | if( output_size != 0 ) |
| 4283 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4284 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4285 | input_data->x, input_data->len, |
| 4286 | NULL, label->len, |
| 4287 | output, output_size, |
| 4288 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4289 | TEST_EQUAL( actual_status, expected_status ); |
| 4290 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4291 | } |
| 4292 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4293 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4294 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4295 | psa_destroy_key( handle ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4296 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4297 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4298 | } |
| 4299 | /* END_CASE */ |
| 4300 | |
| 4301 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4302 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4303 | data_t *key_data, |
| 4304 | int alg_arg, |
| 4305 | data_t *input_data, |
| 4306 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4307 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4308 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4309 | psa_key_type_t key_type = key_type_arg; |
| 4310 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4311 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4312 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4313 | size_t output_size; |
| 4314 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4315 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4316 | size_t output2_size; |
| 4317 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4318 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4319 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4320 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4321 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4322 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4323 | psa_set_key_algorithm( &attributes, alg ); |
| 4324 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4325 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4326 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4327 | &handle ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4328 | |
| 4329 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4330 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4331 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4332 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4333 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4334 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4335 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4336 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4337 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4338 | * the original plaintext because of the non-optional random |
| 4339 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4340 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 4341 | input_data->x, input_data->len, |
| 4342 | label->x, label->len, |
| 4343 | output, output_size, |
| 4344 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4345 | /* We don't know what ciphertext length to expect, but check that |
| 4346 | * it looks sensible. */ |
| 4347 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4348 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4349 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4350 | output, output_length, |
| 4351 | label->x, label->len, |
| 4352 | output2, output2_size, |
| 4353 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4354 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4355 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4356 | |
| 4357 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4358 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4359 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4360 | mbedtls_free( output ); |
| 4361 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4362 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4363 | } |
| 4364 | /* END_CASE */ |
| 4365 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4366 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4367 | void asymmetric_decrypt( int key_type_arg, |
| 4368 | data_t *key_data, |
| 4369 | int alg_arg, |
| 4370 | data_t *input_data, |
| 4371 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4372 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4373 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4374 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4375 | psa_key_type_t key_type = key_type_arg; |
| 4376 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4377 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4378 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4379 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4380 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4381 | |
Jaeden Amero | 412654a | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4382 | output_size = expected_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4383 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4384 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4385 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4386 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4387 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4388 | psa_set_key_algorithm( &attributes, alg ); |
| 4389 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4390 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4391 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4392 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4393 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4394 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4395 | input_data->x, input_data->len, |
| 4396 | label->x, label->len, |
| 4397 | output, |
| 4398 | output_size, |
| 4399 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4400 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4401 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4402 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4403 | /* If the label is empty, the test framework puts a non-null pointer |
| 4404 | * in label->x. Test that a null pointer works as well. */ |
| 4405 | if( label->len == 0 ) |
| 4406 | { |
| 4407 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4408 | if( output_size != 0 ) |
| 4409 | memset( output, 0, output_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4410 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4411 | input_data->x, input_data->len, |
| 4412 | NULL, label->len, |
| 4413 | output, |
| 4414 | output_size, |
| 4415 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4416 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4417 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4418 | } |
| 4419 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4420 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4421 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4422 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4423 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4424 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4425 | } |
| 4426 | /* END_CASE */ |
| 4427 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4428 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4429 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4430 | data_t *key_data, |
| 4431 | int alg_arg, |
| 4432 | data_t *input_data, |
| 4433 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4434 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4435 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4436 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4437 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4438 | psa_key_type_t key_type = key_type_arg; |
| 4439 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4440 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4441 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4442 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4443 | psa_status_t actual_status; |
| 4444 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4445 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4446 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4447 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4448 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4449 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4450 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4451 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4452 | psa_set_key_algorithm( &attributes, alg ); |
| 4453 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4454 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4455 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4456 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4457 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4458 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4459 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4460 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4461 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4462 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4463 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4464 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4465 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4466 | /* If the label is empty, the test framework puts a non-null pointer |
| 4467 | * in label->x. Test that a null pointer works as well. */ |
| 4468 | if( label->len == 0 ) |
| 4469 | { |
| 4470 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4471 | if( output_size != 0 ) |
| 4472 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4473 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4474 | input_data->x, input_data->len, |
| 4475 | NULL, label->len, |
| 4476 | output, output_size, |
| 4477 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4478 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4479 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4480 | } |
| 4481 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4482 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4483 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4484 | psa_destroy_key( handle ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4485 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4486 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4487 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4488 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4489 | |
| 4490 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4491 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4492 | { |
| 4493 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4494 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4495 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4496 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4497 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4498 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4499 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4500 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4501 | |
| 4502 | memset( &zero, 0, sizeof( zero ) ); |
| 4503 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4504 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4505 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4506 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4507 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4508 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4509 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4510 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4511 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4512 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4513 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4514 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4515 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4516 | } |
| 4517 | /* END_CASE */ |
| 4518 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4519 | /* BEGIN_CASE */ |
| 4520 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4521 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4522 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4523 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4524 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4525 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4526 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4527 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4528 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4529 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4530 | |
| 4531 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4532 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4533 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4534 | } |
| 4535 | /* END_CASE */ |
| 4536 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4537 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4538 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4539 | int expected_status_arg ) |
| 4540 | { |
| 4541 | psa_algorithm_t alg = alg_arg; |
| 4542 | size_t capacity = capacity_arg; |
| 4543 | psa_status_t expected_status = expected_status_arg; |
| 4544 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4545 | |
| 4546 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4547 | |
| 4548 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4549 | |
| 4550 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4551 | expected_status ); |
| 4552 | |
| 4553 | exit: |
| 4554 | psa_key_derivation_abort( &operation ); |
| 4555 | PSA_DONE( ); |
| 4556 | } |
| 4557 | /* END_CASE */ |
| 4558 | |
| 4559 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4560 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4561 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4562 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4563 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4564 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4565 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4566 | int expected_status_arg3, |
| 4567 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4568 | { |
| 4569 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4570 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 4571 | 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] | 4572 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4573 | expected_status_arg2, |
| 4574 | expected_status_arg3}; |
| 4575 | data_t *inputs[] = {input1, input2, input3}; |
| 4576 | psa_key_handle_t handles[] = {0, 0, 0}; |
| 4577 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4578 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4579 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4580 | psa_key_type_t output_key_type = output_key_type_arg; |
| 4581 | psa_key_handle_t output_handle = 0; |
| 4582 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4583 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4584 | |
| 4585 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4586 | |
| 4587 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4588 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4589 | |
| 4590 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4591 | |
| 4592 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4593 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4594 | if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4595 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4596 | psa_set_key_type( &attributes, key_types[i] ); |
| 4597 | PSA_ASSERT( psa_import_key( &attributes, |
| 4598 | inputs[i]->x, inputs[i]->len, |
| 4599 | &handles[i] ) ); |
| 4600 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
| 4601 | handles[i] ), |
| 4602 | expected_statuses[i] ); |
| 4603 | } |
| 4604 | else |
| 4605 | { |
| 4606 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4607 | &operation, steps[i], |
| 4608 | inputs[i]->x, inputs[i]->len ), |
| 4609 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4610 | } |
| 4611 | } |
| 4612 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4613 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 4614 | { |
| 4615 | psa_reset_key_attributes( &attributes ); |
| 4616 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 4617 | psa_set_key_bits( &attributes, 8 ); |
| 4618 | actual_output_status = |
| 4619 | psa_key_derivation_output_key( &attributes, &operation, |
| 4620 | &output_handle ); |
| 4621 | } |
| 4622 | else |
| 4623 | { |
| 4624 | uint8_t buffer[1]; |
| 4625 | actual_output_status = |
| 4626 | psa_key_derivation_output_bytes( &operation, |
| 4627 | buffer, sizeof( buffer ) ); |
| 4628 | } |
| 4629 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 4630 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4631 | exit: |
| 4632 | psa_key_derivation_abort( &operation ); |
| 4633 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4634 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4635 | psa_destroy_key( output_handle ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4636 | PSA_DONE( ); |
| 4637 | } |
| 4638 | /* END_CASE */ |
| 4639 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4640 | /* BEGIN_CASE */ |
| 4641 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4642 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4643 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4644 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4645 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4646 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4647 | unsigned char input1[] = "Input 1"; |
| 4648 | size_t input1_length = sizeof( input1 ); |
| 4649 | unsigned char input2[] = "Input 2"; |
| 4650 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4651 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4652 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4653 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4654 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4655 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4656 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4657 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4658 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4659 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4660 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4661 | psa_set_key_algorithm( &attributes, alg ); |
| 4662 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4663 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4664 | PSA_ASSERT( psa_import_key( &attributes, |
| 4665 | key_data, sizeof( key_data ), |
| 4666 | &handle ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4667 | |
| 4668 | /* valid key derivation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4669 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4670 | input1, input1_length, |
| 4671 | input2, input2_length, |
| 4672 | capacity ) ) |
| 4673 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4674 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4675 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4676 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4677 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4678 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4679 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4680 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4681 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4682 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4683 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4684 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4685 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4686 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4687 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4688 | } |
| 4689 | /* END_CASE */ |
| 4690 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4691 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4692 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4693 | { |
| 4694 | uint8_t output_buffer[16]; |
| 4695 | size_t buffer_size = 16; |
| 4696 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4697 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4698 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4699 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4700 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4701 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4702 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4703 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4704 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4705 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4706 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4707 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4708 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4709 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4710 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4711 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4712 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4713 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4714 | |
| 4715 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4716 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4717 | } |
| 4718 | /* END_CASE */ |
| 4719 | |
| 4720 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4721 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4722 | int step1_arg, data_t *input1, |
| 4723 | int step2_arg, data_t *input2, |
| 4724 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4725 | int requested_capacity_arg, |
| 4726 | data_t *expected_output1, |
| 4727 | data_t *expected_output2 ) |
| 4728 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4729 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4730 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4731 | data_t *inputs[] = {input1, input2, input3}; |
| 4732 | psa_key_handle_t handles[] = {0, 0, 0}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4733 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4734 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4735 | uint8_t *expected_outputs[2] = |
| 4736 | {expected_output1->x, expected_output2->x}; |
| 4737 | size_t output_sizes[2] = |
| 4738 | {expected_output1->len, expected_output2->len}; |
| 4739 | size_t output_buffer_size = 0; |
| 4740 | uint8_t *output_buffer = NULL; |
| 4741 | size_t expected_capacity; |
| 4742 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4743 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4744 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4745 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4746 | |
| 4747 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4748 | { |
| 4749 | if( output_sizes[i] > output_buffer_size ) |
| 4750 | output_buffer_size = output_sizes[i]; |
| 4751 | if( output_sizes[i] == 0 ) |
| 4752 | expected_outputs[i] = NULL; |
| 4753 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4754 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4755 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4756 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4757 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4758 | psa_set_key_algorithm( &attributes, alg ); |
| 4759 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4760 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4761 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4762 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4763 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4764 | requested_capacity ) ); |
| 4765 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4766 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4767 | switch( steps[i] ) |
| 4768 | { |
| 4769 | case 0: |
| 4770 | break; |
| 4771 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4772 | PSA_ASSERT( psa_import_key( &attributes, |
| 4773 | inputs[i]->x, inputs[i]->len, |
| 4774 | &handles[i] ) ); |
| 4775 | PSA_ASSERT( psa_key_derivation_input_key( |
| 4776 | &operation, steps[i], |
| 4777 | handles[i] ) ); |
| 4778 | break; |
| 4779 | default: |
| 4780 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4781 | &operation, steps[i], |
| 4782 | inputs[i]->x, inputs[i]->len ) ); |
| 4783 | break; |
| 4784 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4785 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4786 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4787 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4788 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4789 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4790 | expected_capacity = requested_capacity; |
| 4791 | |
| 4792 | /* Expansion phase. */ |
| 4793 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4794 | { |
| 4795 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4796 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4797 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4798 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4799 | { |
| 4800 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4801 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4802 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4803 | continue; |
| 4804 | } |
| 4805 | else if( expected_capacity == 0 || |
| 4806 | output_sizes[i] > expected_capacity ) |
| 4807 | { |
| 4808 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4809 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4810 | expected_capacity = 0; |
| 4811 | continue; |
| 4812 | } |
| 4813 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4814 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4815 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4816 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4817 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4818 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4819 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4820 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4821 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4822 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4823 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4824 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4825 | |
| 4826 | exit: |
| 4827 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4828 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4829 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4830 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4831 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4832 | } |
| 4833 | /* END_CASE */ |
| 4834 | |
| 4835 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4836 | void derive_full( int alg_arg, |
| 4837 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4838 | data_t *input1, |
| 4839 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4840 | int requested_capacity_arg ) |
| 4841 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4842 | psa_key_handle_t handle = 0; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4843 | psa_algorithm_t alg = alg_arg; |
| 4844 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4845 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4846 | unsigned char output_buffer[16]; |
| 4847 | size_t expected_capacity = requested_capacity; |
| 4848 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4849 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4850 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4851 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4852 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4853 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4854 | psa_set_key_algorithm( &attributes, alg ); |
| 4855 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4856 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4857 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4858 | &handle ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4859 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4860 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4861 | input1->x, input1->len, |
| 4862 | input2->x, input2->len, |
| 4863 | requested_capacity ) ) |
| 4864 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4865 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4866 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4867 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4868 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4869 | |
| 4870 | /* Expansion phase. */ |
| 4871 | while( current_capacity > 0 ) |
| 4872 | { |
| 4873 | size_t read_size = sizeof( output_buffer ); |
| 4874 | if( read_size > current_capacity ) |
| 4875 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4876 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4877 | output_buffer, |
| 4878 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4879 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4880 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4881 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4882 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4883 | } |
| 4884 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4885 | /* Check that the operation refuses to go over capacity. */ |
| 4886 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4887 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4888 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4889 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4890 | |
| 4891 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4892 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4893 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4894 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4895 | } |
| 4896 | /* END_CASE */ |
| 4897 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4898 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4899 | void derive_key_exercise( int alg_arg, |
| 4900 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4901 | data_t *input1, |
| 4902 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4903 | int derived_type_arg, |
| 4904 | int derived_bits_arg, |
| 4905 | int derived_usage_arg, |
| 4906 | int derived_alg_arg ) |
| 4907 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4908 | psa_key_handle_t base_handle = 0; |
| 4909 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4910 | psa_algorithm_t alg = alg_arg; |
| 4911 | psa_key_type_t derived_type = derived_type_arg; |
| 4912 | size_t derived_bits = derived_bits_arg; |
| 4913 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4914 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4915 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4916 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4917 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4918 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4919 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4920 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4921 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4922 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4923 | psa_set_key_algorithm( &attributes, alg ); |
| 4924 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4925 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4926 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4927 | |
| 4928 | /* Derive a key. */ |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4929 | if ( setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4930 | input1->x, input1->len, |
| 4931 | input2->x, input2->len, capacity ) ) |
| 4932 | goto exit; |
| 4933 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4934 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4935 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4936 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4937 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4938 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4939 | &derived_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4940 | |
| 4941 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4942 | PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) ); |
| 4943 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4944 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4945 | |
| 4946 | /* Exercise the derived key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4947 | if( ! exercise_key( derived_handle, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4948 | goto exit; |
| 4949 | |
| 4950 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4951 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4952 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4953 | psa_destroy_key( base_handle ); |
| 4954 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4955 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4956 | } |
| 4957 | /* END_CASE */ |
| 4958 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4959 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4960 | void derive_key_export( int alg_arg, |
| 4961 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4962 | data_t *input1, |
| 4963 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4964 | int bytes1_arg, |
| 4965 | int bytes2_arg ) |
| 4966 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4967 | psa_key_handle_t base_handle = 0; |
| 4968 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4969 | psa_algorithm_t alg = alg_arg; |
| 4970 | size_t bytes1 = bytes1_arg; |
| 4971 | size_t bytes2 = bytes2_arg; |
| 4972 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4973 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4974 | uint8_t *output_buffer = NULL; |
| 4975 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4976 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4977 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4978 | size_t length; |
| 4979 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4980 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4981 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4982 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4983 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4984 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4985 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4986 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4987 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 4988 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4989 | |
| 4990 | /* Derive some material and output it. */ |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4991 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4992 | input1->x, input1->len, |
| 4993 | input2->x, input2->len, capacity ) ) |
| 4994 | goto exit; |
| 4995 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4996 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4997 | output_buffer, |
| 4998 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4999 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5000 | |
| 5001 | /* 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] | 5002 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 5003 | input1->x, input1->len, |
| 5004 | input2->x, input2->len, capacity ) ) |
| 5005 | goto exit; |
| 5006 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5007 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5008 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 5009 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5010 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5011 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5012 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5013 | PSA_ASSERT( psa_export_key( derived_handle, |
| 5014 | export_buffer, bytes1, |
| 5015 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5016 | TEST_EQUAL( length, bytes1 ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5017 | PSA_ASSERT( psa_destroy_key( derived_handle ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5018 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5019 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5020 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5021 | PSA_ASSERT( psa_export_key( derived_handle, |
| 5022 | export_buffer + bytes1, bytes2, |
| 5023 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5024 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5025 | |
| 5026 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5027 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 5028 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5029 | |
| 5030 | exit: |
| 5031 | mbedtls_free( output_buffer ); |
| 5032 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5033 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5034 | psa_destroy_key( base_handle ); |
| 5035 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5036 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5037 | } |
| 5038 | /* END_CASE */ |
| 5039 | |
| 5040 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5041 | void derive_key( int alg_arg, |
| 5042 | data_t *key_data, data_t *input1, data_t *input2, |
| 5043 | int type_arg, int bits_arg, |
| 5044 | int expected_status_arg ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5045 | { |
| 5046 | psa_key_handle_t base_handle = 0; |
| 5047 | psa_key_handle_t derived_handle = 0; |
| 5048 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5049 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5050 | size_t bits = bits_arg; |
| 5051 | psa_status_t expected_status = expected_status_arg; |
| 5052 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5053 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5054 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5055 | |
| 5056 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5057 | |
| 5058 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5059 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5060 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5061 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 5062 | &base_handle ) ); |
| 5063 | |
| 5064 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 5065 | input1->x, input1->len, |
| 5066 | input2->x, input2->len, SIZE_MAX ) ) |
| 5067 | goto exit; |
| 5068 | |
| 5069 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5070 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5071 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5072 | psa_set_key_bits( &derived_attributes, bits ); |
| 5073 | TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 5074 | &derived_handle ), |
| 5075 | expected_status ); |
| 5076 | |
| 5077 | exit: |
| 5078 | psa_key_derivation_abort( &operation ); |
| 5079 | psa_destroy_key( base_handle ); |
| 5080 | psa_destroy_key( derived_handle ); |
| 5081 | PSA_DONE( ); |
| 5082 | } |
| 5083 | /* END_CASE */ |
| 5084 | |
| 5085 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5086 | void key_agreement_setup( int alg_arg, |
| 5087 | int our_key_type_arg, data_t *our_key_data, |
| 5088 | data_t *peer_key_data, |
| 5089 | int expected_status_arg ) |
| 5090 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5091 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5092 | psa_algorithm_t alg = alg_arg; |
| 5093 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5094 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5095 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5096 | psa_status_t expected_status = expected_status_arg; |
| 5097 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5098 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5099 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5100 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5101 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5102 | psa_set_key_algorithm( &attributes, alg ); |
| 5103 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5104 | PSA_ASSERT( psa_import_key( &attributes, |
| 5105 | our_key_data->x, our_key_data->len, |
| 5106 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5107 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5108 | /* The tests currently include inputs that should fail at either step. |
| 5109 | * Test cases that fail at the setup step should be changed to call |
| 5110 | * key_derivation_setup instead, and this function should be renamed |
| 5111 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5112 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5113 | if( status == PSA_SUCCESS ) |
| 5114 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5115 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 5116 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5117 | our_key, |
| 5118 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5119 | expected_status ); |
| 5120 | } |
| 5121 | else |
| 5122 | { |
| 5123 | TEST_ASSERT( status == expected_status ); |
| 5124 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5125 | |
| 5126 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5127 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5128 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5129 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5130 | } |
| 5131 | /* END_CASE */ |
| 5132 | |
| 5133 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5134 | void raw_key_agreement( int alg_arg, |
| 5135 | int our_key_type_arg, data_t *our_key_data, |
| 5136 | data_t *peer_key_data, |
| 5137 | data_t *expected_output ) |
| 5138 | { |
| 5139 | psa_key_handle_t our_key = 0; |
| 5140 | psa_algorithm_t alg = alg_arg; |
| 5141 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5142 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5143 | unsigned char *output = NULL; |
| 5144 | size_t output_length = ~0; |
| 5145 | |
| 5146 | ASSERT_ALLOC( output, expected_output->len ); |
| 5147 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5148 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5149 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5150 | psa_set_key_algorithm( &attributes, alg ); |
| 5151 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5152 | PSA_ASSERT( psa_import_key( &attributes, |
| 5153 | our_key_data->x, our_key_data->len, |
| 5154 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5155 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5156 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 5157 | peer_key_data->x, peer_key_data->len, |
| 5158 | output, expected_output->len, |
| 5159 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5160 | ASSERT_COMPARE( output, output_length, |
| 5161 | expected_output->x, expected_output->len ); |
| 5162 | |
| 5163 | exit: |
| 5164 | mbedtls_free( output ); |
| 5165 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5166 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5167 | } |
| 5168 | /* END_CASE */ |
| 5169 | |
| 5170 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5171 | void key_agreement_capacity( int alg_arg, |
| 5172 | int our_key_type_arg, data_t *our_key_data, |
| 5173 | data_t *peer_key_data, |
| 5174 | int expected_capacity_arg ) |
| 5175 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5176 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5177 | psa_algorithm_t alg = alg_arg; |
| 5178 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5179 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5180 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5181 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5182 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5183 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5184 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5185 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5186 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5187 | psa_set_key_algorithm( &attributes, alg ); |
| 5188 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5189 | PSA_ASSERT( psa_import_key( &attributes, |
| 5190 | our_key_data->x, our_key_data->len, |
| 5191 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5192 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5193 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5194 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5195 | &operation, |
| 5196 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5197 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5198 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5199 | { |
| 5200 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5201 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5202 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5203 | NULL, 0 ) ); |
| 5204 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5205 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5206 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5207 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5208 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5209 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5210 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5211 | /* Test the actual capacity by reading the output. */ |
| 5212 | while( actual_capacity > sizeof( output ) ) |
| 5213 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5214 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5215 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5216 | actual_capacity -= sizeof( output ); |
| 5217 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5218 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5219 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5220 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5221 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5222 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5223 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5224 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5225 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5226 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5227 | } |
| 5228 | /* END_CASE */ |
| 5229 | |
| 5230 | /* BEGIN_CASE */ |
| 5231 | void key_agreement_output( int alg_arg, |
| 5232 | int our_key_type_arg, data_t *our_key_data, |
| 5233 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5234 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5235 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5236 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5237 | psa_algorithm_t alg = alg_arg; |
| 5238 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5239 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5240 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5241 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5242 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5243 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 5244 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5245 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5246 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5247 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5248 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5249 | psa_set_key_algorithm( &attributes, alg ); |
| 5250 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5251 | PSA_ASSERT( psa_import_key( &attributes, |
| 5252 | our_key_data->x, our_key_data->len, |
| 5253 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5254 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5255 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5256 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5257 | &operation, |
| 5258 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5259 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5260 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5261 | { |
| 5262 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5263 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5264 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5265 | NULL, 0 ) ); |
| 5266 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5267 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5268 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5269 | actual_output, |
| 5270 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5271 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 5272 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5273 | if( expected_output2->len != 0 ) |
| 5274 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5275 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5276 | actual_output, |
| 5277 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5278 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 5279 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5280 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5281 | |
| 5282 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5283 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5284 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5285 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5286 | mbedtls_free( actual_output ); |
| 5287 | } |
| 5288 | /* END_CASE */ |
| 5289 | |
| 5290 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5291 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5292 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5293 | size_t bytes = bytes_arg; |
| 5294 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5295 | unsigned char *output = NULL; |
| 5296 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5297 | size_t i; |
| 5298 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5299 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5300 | TEST_ASSERT( bytes_arg >= 0 ); |
| 5301 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5302 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 5303 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5304 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5305 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5306 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5307 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5308 | /* Run several times, to ensure that every output byte will be |
| 5309 | * nonzero at least once with overwhelming probability |
| 5310 | * (2^(-8*number_of_runs)). */ |
| 5311 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5312 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5313 | if( bytes != 0 ) |
| 5314 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5315 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5316 | |
| 5317 | /* Check that no more than bytes have been overwritten */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5318 | ASSERT_COMPARE( output + bytes, sizeof( trail ), |
| 5319 | trail, sizeof( trail ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5320 | |
| 5321 | for( i = 0; i < bytes; i++ ) |
| 5322 | { |
| 5323 | if( output[i] != 0 ) |
| 5324 | ++changed[i]; |
| 5325 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5326 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5327 | |
| 5328 | /* Check that every byte was changed to nonzero at least once. This |
| 5329 | * validates that psa_generate_random is overwriting every byte of |
| 5330 | * the output buffer. */ |
| 5331 | for( i = 0; i < bytes; i++ ) |
| 5332 | { |
| 5333 | TEST_ASSERT( changed[i] != 0 ); |
| 5334 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5335 | |
| 5336 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5337 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5338 | mbedtls_free( output ); |
| 5339 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5340 | } |
| 5341 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5342 | |
| 5343 | /* BEGIN_CASE */ |
| 5344 | void generate_key( int type_arg, |
| 5345 | int bits_arg, |
| 5346 | int usage_arg, |
| 5347 | int alg_arg, |
| 5348 | int expected_status_arg ) |
| 5349 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5350 | psa_key_handle_t handle = 0; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5351 | psa_key_type_t type = type_arg; |
| 5352 | psa_key_usage_t usage = usage_arg; |
| 5353 | size_t bits = bits_arg; |
| 5354 | psa_algorithm_t alg = alg_arg; |
| 5355 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5356 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5357 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5358 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5359 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5360 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5361 | psa_set_key_usage_flags( &attributes, usage ); |
| 5362 | psa_set_key_algorithm( &attributes, alg ); |
| 5363 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5364 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5365 | |
| 5366 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5367 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5368 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5369 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5370 | |
| 5371 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5372 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 5373 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5374 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5375 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5376 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5377 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5378 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5379 | |
| 5380 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5381 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5382 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5383 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5384 | } |
| 5385 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5386 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5387 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */ |
| 5388 | void generate_key_rsa( int bits_arg, |
| 5389 | data_t *e_arg, |
| 5390 | int expected_status_arg ) |
| 5391 | { |
| 5392 | psa_key_handle_t handle = 0; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5393 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5394 | size_t bits = bits_arg; |
| 5395 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5396 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5397 | psa_status_t expected_status = expected_status_arg; |
| 5398 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5399 | uint8_t *exported = NULL; |
| 5400 | size_t exported_size = |
| 5401 | PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
| 5402 | size_t exported_length = SIZE_MAX; |
| 5403 | uint8_t *e_read_buffer = NULL; |
| 5404 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5405 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5406 | size_t e_read_length = SIZE_MAX; |
| 5407 | |
| 5408 | if( e_arg->len == 0 || |
| 5409 | ( e_arg->len == 3 && |
| 5410 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5411 | { |
| 5412 | is_default_public_exponent = 1; |
| 5413 | e_read_size = 0; |
| 5414 | } |
| 5415 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5416 | ASSERT_ALLOC( exported, exported_size ); |
| 5417 | |
| 5418 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5419 | |
| 5420 | psa_set_key_usage_flags( &attributes, usage ); |
| 5421 | psa_set_key_algorithm( &attributes, alg ); |
| 5422 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5423 | e_arg->x, e_arg->len ) ); |
| 5424 | psa_set_key_bits( &attributes, bits ); |
| 5425 | |
| 5426 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5427 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5428 | if( expected_status != PSA_SUCCESS ) |
| 5429 | goto exit; |
| 5430 | |
| 5431 | /* Test the key information */ |
| 5432 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5433 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5434 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5435 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5436 | e_read_buffer, e_read_size, |
| 5437 | &e_read_length ) ); |
| 5438 | if( is_default_public_exponent ) |
| 5439 | TEST_EQUAL( e_read_length, 0 ); |
| 5440 | else |
| 5441 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5442 | |
| 5443 | /* Do something with the key according to its type and permitted usage. */ |
| 5444 | if( ! exercise_key( handle, usage, alg ) ) |
| 5445 | goto exit; |
| 5446 | |
| 5447 | /* Export the key and check the public exponent. */ |
| 5448 | PSA_ASSERT( psa_export_public_key( handle, |
| 5449 | exported, exported_size, |
| 5450 | &exported_length ) ); |
| 5451 | { |
| 5452 | uint8_t *p = exported; |
| 5453 | uint8_t *end = exported + exported_length; |
| 5454 | size_t len; |
| 5455 | /* RSAPublicKey ::= SEQUENCE { |
| 5456 | * modulus INTEGER, -- n |
| 5457 | * publicExponent INTEGER } -- e |
| 5458 | */ |
| 5459 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5460 | MBEDTLS_ASN1_SEQUENCE | |
| 5461 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5462 | TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
| 5463 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5464 | MBEDTLS_ASN1_INTEGER ) ); |
| 5465 | if( len >= 1 && p[0] == 0 ) |
| 5466 | { |
| 5467 | ++p; |
| 5468 | --len; |
| 5469 | } |
| 5470 | if( e_arg->len == 0 ) |
| 5471 | { |
| 5472 | TEST_EQUAL( len, 3 ); |
| 5473 | TEST_EQUAL( p[0], 1 ); |
| 5474 | TEST_EQUAL( p[1], 0 ); |
| 5475 | TEST_EQUAL( p[2], 1 ); |
| 5476 | } |
| 5477 | else |
| 5478 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5479 | } |
| 5480 | |
| 5481 | exit: |
| 5482 | psa_reset_key_attributes( &attributes ); |
| 5483 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5484 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5485 | mbedtls_free( e_read_buffer ); |
| 5486 | mbedtls_free( exported ); |
| 5487 | } |
| 5488 | /* END_CASE */ |
| 5489 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5490 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5491 | void persistent_key_load_key_from_storage( data_t *data, |
| 5492 | int type_arg, int bits_arg, |
| 5493 | int usage_flags_arg, int alg_arg, |
| 5494 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5495 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5496 | psa_key_id_t key_id = 1; |
| 5497 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5498 | psa_key_handle_t handle = 0; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5499 | psa_key_handle_t base_key = 0; |
| 5500 | psa_key_type_t type = type_arg; |
| 5501 | size_t bits = bits_arg; |
| 5502 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5503 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5504 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5505 | unsigned char *first_export = NULL; |
| 5506 | unsigned char *second_export = NULL; |
| 5507 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 5508 | size_t first_exported_length; |
| 5509 | size_t second_exported_length; |
| 5510 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5511 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5512 | { |
| 5513 | ASSERT_ALLOC( first_export, export_size ); |
| 5514 | ASSERT_ALLOC( second_export, export_size ); |
| 5515 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5516 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5517 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5518 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5519 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5520 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5521 | psa_set_key_algorithm( &attributes, alg ); |
| 5522 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5523 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5524 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5525 | switch( generation_method ) |
| 5526 | { |
| 5527 | case IMPORT_KEY: |
| 5528 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5529 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
| 5530 | &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5531 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5532 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5533 | case GENERATE_KEY: |
| 5534 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5535 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5536 | break; |
| 5537 | |
| 5538 | case DERIVE_KEY: |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5539 | { |
| 5540 | /* Create base key */ |
| 5541 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5542 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5543 | psa_set_key_usage_flags( &base_attributes, |
| 5544 | PSA_KEY_USAGE_DERIVE ); |
| 5545 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5546 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5547 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5548 | data->x, data->len, |
| 5549 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5550 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5551 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5552 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5553 | &operation, |
| 5554 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5555 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5556 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5557 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5558 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5559 | &operation, |
| 5560 | &handle ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5561 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5562 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
| 5563 | base_key = 0; |
| 5564 | } |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5565 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5566 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5567 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5568 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5569 | /* Export the key if permitted by the key policy. */ |
| 5570 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5571 | { |
| 5572 | PSA_ASSERT( psa_export_key( handle, |
| 5573 | first_export, export_size, |
| 5574 | &first_exported_length ) ); |
| 5575 | if( generation_method == IMPORT_KEY ) |
| 5576 | ASSERT_COMPARE( data->x, data->len, |
| 5577 | first_export, first_exported_length ); |
| 5578 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5579 | |
| 5580 | /* Shutdown and restart */ |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 5581 | PSA_ASSERT( psa_close_key( handle ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5582 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5583 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5584 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5585 | /* Check key slot still contains key data */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5586 | PSA_ASSERT( psa_open_key( key_id, &handle ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5587 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5588 | TEST_EQUAL( psa_get_key_id( &attributes ), key_id ); |
| 5589 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5590 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5591 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5592 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5593 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 5594 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5595 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5596 | /* Export the key again if permitted by the key policy. */ |
| 5597 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5598 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5599 | PSA_ASSERT( psa_export_key( handle, |
| 5600 | second_export, export_size, |
| 5601 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5602 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5603 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
| 5606 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5607 | if( ! exercise_key( handle, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5608 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5609 | |
| 5610 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5611 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5612 | mbedtls_free( first_export ); |
| 5613 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5614 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5615 | psa_destroy_key( base_key ); |
| 5616 | if( handle == 0 ) |
| 5617 | { |
| 5618 | /* In case there was a test failure after creating the persistent key |
| 5619 | * but while it was not open, try to re-open the persistent key |
| 5620 | * to delete it. */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5621 | psa_open_key( key_id, &handle ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5622 | } |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5623 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5624 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5625 | } |
| 5626 | /* END_CASE */ |