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 | |
Ronald Cron | 02c78b7 | 2020-05-27 09:22:32 +0200 | [diff] [blame] | 12 | #include "test/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 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 1262 | TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1263 | TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1264 | TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1265 | TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1266 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1267 | TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1268 | TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1269 | TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1270 | TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1271 | TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1272 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 1273 | TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1274 | TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1275 | TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1276 | TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1277 | TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1278 | TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1279 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1280 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1281 | TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1282 | TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1283 | TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1284 | TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1285 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 1286 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 1287 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1288 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1289 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1290 | TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY ); |
| 1291 | TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY ); |
| 1292 | |
| 1293 | TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1294 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1295 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 1296 | TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1297 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1298 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 1299 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1300 | TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 1301 | |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 1302 | TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 ); |
| 1303 | TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 ); |
| 1304 | TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 ); |
| 1305 | TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 ); |
| 1306 | TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 ); |
| 1307 | |
| 1308 | TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 ); |
| 1309 | TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 1310 | #endif |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1311 | } |
| 1312 | /* END_CASE */ |
| 1313 | |
| 1314 | /* BEGIN_CASE */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1315 | void attributes_set_get( int id_arg, int lifetime_arg, |
| 1316 | int usage_flags_arg, int alg_arg, |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1317 | int type_arg, int bits_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1318 | { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1319 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1320 | psa_key_id_t id = id_arg; |
| 1321 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1322 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 1323 | psa_algorithm_t alg = alg_arg; |
| 1324 | psa_key_type_t type = type_arg; |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1325 | size_t bits = bits_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1326 | |
| 1327 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1328 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1329 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1330 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1331 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1332 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1333 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1334 | psa_set_key_id( &attributes, id ); |
| 1335 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1336 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 1337 | psa_set_key_algorithm( &attributes, alg ); |
| 1338 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1339 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1340 | |
| 1341 | TEST_EQUAL( psa_get_key_id( &attributes ), id ); |
| 1342 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime ); |
| 1343 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 1344 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
| 1345 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1346 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1347 | |
| 1348 | psa_reset_key_attributes( &attributes ); |
| 1349 | |
| 1350 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1351 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1352 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1353 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1354 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1355 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1356 | } |
| 1357 | /* END_CASE */ |
| 1358 | |
| 1359 | /* BEGIN_CASE */ |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1360 | void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg, |
| 1361 | int expected_id_arg, int expected_lifetime_arg ) |
| 1362 | { |
| 1363 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1364 | psa_key_id_t id1 = id1_arg; |
| 1365 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1366 | psa_key_id_t id2 = id2_arg; |
| 1367 | psa_key_id_t expected_id = expected_id_arg; |
| 1368 | psa_key_lifetime_t expected_lifetime = expected_lifetime_arg; |
| 1369 | |
| 1370 | if( id1_arg != -1 ) |
| 1371 | psa_set_key_id( &attributes, id1 ); |
| 1372 | if( lifetime_arg != -1 ) |
| 1373 | psa_set_key_lifetime( &attributes, lifetime ); |
| 1374 | if( id2_arg != -1 ) |
| 1375 | psa_set_key_id( &attributes, id2 ); |
| 1376 | |
| 1377 | TEST_EQUAL( psa_get_key_id( &attributes ), expected_id ); |
| 1378 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime ); |
| 1379 | } |
| 1380 | /* END_CASE */ |
| 1381 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1382 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1383 | void slot_number_attribute( ) |
| 1384 | { |
| 1385 | psa_key_slot_number_t slot_number = 0xdeadbeef; |
| 1386 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1387 | |
| 1388 | /* Initially, there is no slot number. */ |
| 1389 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1390 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1391 | |
| 1392 | /* Test setting a slot number. */ |
| 1393 | psa_set_key_slot_number( &attributes, 0 ); |
| 1394 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1395 | TEST_EQUAL( slot_number, 0 ); |
| 1396 | |
| 1397 | /* Test changing the slot number. */ |
| 1398 | psa_set_key_slot_number( &attributes, 42 ); |
| 1399 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1400 | TEST_EQUAL( slot_number, 42 ); |
| 1401 | |
| 1402 | /* Test clearing the slot number. */ |
| 1403 | psa_clear_key_slot_number( &attributes ); |
| 1404 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1405 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1406 | |
| 1407 | /* Clearing again should have no effect. */ |
| 1408 | psa_clear_key_slot_number( &attributes ); |
| 1409 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1410 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1411 | |
| 1412 | /* Test that reset clears the slot number. */ |
| 1413 | psa_set_key_slot_number( &attributes, 42 ); |
| 1414 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1415 | TEST_EQUAL( slot_number, 42 ); |
| 1416 | psa_reset_key_attributes( &attributes ); |
| 1417 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1418 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1419 | } |
| 1420 | /* END_CASE */ |
| 1421 | |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1422 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1423 | void import_with_policy( int type_arg, |
| 1424 | int usage_arg, int alg_arg, |
| 1425 | int expected_status_arg ) |
| 1426 | { |
| 1427 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1428 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1429 | psa_key_handle_t handle = 0; |
| 1430 | psa_key_type_t type = type_arg; |
| 1431 | psa_key_usage_t usage = usage_arg; |
| 1432 | psa_algorithm_t alg = alg_arg; |
| 1433 | psa_status_t expected_status = expected_status_arg; |
| 1434 | const uint8_t key_material[16] = {0}; |
| 1435 | psa_status_t status; |
| 1436 | |
| 1437 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1438 | |
| 1439 | psa_set_key_type( &attributes, type ); |
| 1440 | psa_set_key_usage_flags( &attributes, usage ); |
| 1441 | psa_set_key_algorithm( &attributes, alg ); |
| 1442 | |
| 1443 | status = psa_import_key( &attributes, |
| 1444 | key_material, sizeof( key_material ), |
| 1445 | &handle ); |
| 1446 | TEST_EQUAL( status, expected_status ); |
| 1447 | if( status != PSA_SUCCESS ) |
| 1448 | goto exit; |
| 1449 | |
| 1450 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1451 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1452 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1453 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1454 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1455 | |
| 1456 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1457 | test_operations_on_invalid_handle( handle ); |
| 1458 | |
| 1459 | exit: |
| 1460 | psa_destroy_key( handle ); |
| 1461 | psa_reset_key_attributes( &got_attributes ); |
| 1462 | PSA_DONE( ); |
| 1463 | } |
| 1464 | /* END_CASE */ |
| 1465 | |
| 1466 | /* BEGIN_CASE */ |
| 1467 | void import_with_data( data_t *data, int type_arg, |
| 1468 | int attr_bits_arg, |
| 1469 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1470 | { |
| 1471 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1472 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1473 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1474 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1475 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1476 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1477 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1478 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1479 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1480 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1481 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1482 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1483 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1484 | status = psa_import_key( &attributes, data->x, data->len, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1485 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1486 | if( status != PSA_SUCCESS ) |
| 1487 | goto exit; |
| 1488 | |
| 1489 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1490 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1491 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1492 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1493 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1494 | |
| 1495 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1496 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1497 | |
| 1498 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1499 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1500 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1501 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1502 | } |
| 1503 | /* END_CASE */ |
| 1504 | |
| 1505 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1506 | void import_large_key( int type_arg, int byte_size_arg, |
| 1507 | int expected_status_arg ) |
| 1508 | { |
| 1509 | psa_key_type_t type = type_arg; |
| 1510 | size_t byte_size = byte_size_arg; |
| 1511 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1512 | psa_status_t expected_status = expected_status_arg; |
| 1513 | psa_key_handle_t handle = 0; |
| 1514 | psa_status_t status; |
| 1515 | uint8_t *buffer = NULL; |
| 1516 | size_t buffer_size = byte_size + 1; |
| 1517 | size_t n; |
| 1518 | |
| 1519 | /* It would be better to skip the test than fail it if the allocation |
| 1520 | * fails, but the test framework doesn't support this yet. */ |
| 1521 | ASSERT_ALLOC( buffer, buffer_size ); |
| 1522 | memset( buffer, 'K', byte_size ); |
| 1523 | |
| 1524 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1525 | |
| 1526 | /* Try importing the key */ |
| 1527 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1528 | psa_set_key_type( &attributes, type ); |
| 1529 | status = psa_import_key( &attributes, buffer, byte_size, &handle ); |
| 1530 | TEST_EQUAL( status, expected_status ); |
| 1531 | |
| 1532 | if( status == PSA_SUCCESS ) |
| 1533 | { |
| 1534 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1535 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1536 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1537 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1538 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1539 | memset( buffer, 0, byte_size + 1 ); |
| 1540 | PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) ); |
| 1541 | for( n = 0; n < byte_size; n++ ) |
| 1542 | TEST_EQUAL( buffer[n], 'K' ); |
| 1543 | for( n = byte_size; n < buffer_size; n++ ) |
| 1544 | TEST_EQUAL( buffer[n], 0 ); |
| 1545 | } |
| 1546 | |
| 1547 | exit: |
| 1548 | psa_destroy_key( handle ); |
| 1549 | PSA_DONE( ); |
| 1550 | mbedtls_free( buffer ); |
| 1551 | } |
| 1552 | /* END_CASE */ |
| 1553 | |
| 1554 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1555 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1556 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1557 | psa_key_handle_t handle = 0; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1558 | size_t bits = bits_arg; |
| 1559 | psa_status_t expected_status = expected_status_arg; |
| 1560 | psa_status_t status; |
| 1561 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1562 | 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] | 1563 | size_t buffer_size = /* Slight overapproximations */ |
| 1564 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1565 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1566 | unsigned char *p; |
| 1567 | int ret; |
| 1568 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1569 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1570 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1571 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1572 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1573 | |
| 1574 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1575 | bits, keypair ) ) >= 0 ); |
| 1576 | length = ret; |
| 1577 | |
| 1578 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1579 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1580 | status = psa_import_key( &attributes, p, length, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1581 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1582 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1583 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1584 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1585 | |
| 1586 | exit: |
| 1587 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1588 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1589 | } |
| 1590 | /* END_CASE */ |
| 1591 | |
| 1592 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1593 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1594 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1595 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1596 | int expected_bits, |
| 1597 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1598 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1599 | int canonical_input ) |
| 1600 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1601 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1602 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1603 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1604 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1605 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1606 | unsigned char *exported = NULL; |
| 1607 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1608 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1609 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1610 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1611 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1612 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1613 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1614 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1615 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1616 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1617 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1618 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1619 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1620 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1621 | psa_set_key_algorithm( &attributes, alg ); |
| 1622 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1623 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1624 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1625 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1626 | |
| 1627 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1628 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1629 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1630 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1631 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1632 | |
| 1633 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1634 | status = psa_export_key( handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1635 | exported, export_size, |
| 1636 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1637 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1638 | |
| 1639 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1640 | * and export_size. On errors, the exported length must be 0. */ |
| 1641 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1642 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1643 | TEST_ASSERT( exported_length <= export_size ); |
| 1644 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1645 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1646 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1647 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1648 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1649 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1650 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1651 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1652 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1653 | if( ! exercise_export_key( handle, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1654 | goto exit; |
| 1655 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1656 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1657 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1658 | else |
| 1659 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1660 | psa_key_handle_t handle2; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1661 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
| 1662 | &handle2 ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1663 | PSA_ASSERT( psa_export_key( handle2, |
| 1664 | reexported, |
| 1665 | export_size, |
| 1666 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1667 | ASSERT_COMPARE( exported, exported_length, |
| 1668 | reexported, reexported_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1669 | PSA_ASSERT( psa_close_key( handle2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1670 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1671 | 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] | 1672 | |
| 1673 | destroy: |
| 1674 | /* Destroy the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1675 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1676 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1677 | |
| 1678 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1679 | mbedtls_free( exported ); |
| 1680 | mbedtls_free( reexported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1681 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1682 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1683 | } |
| 1684 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1685 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1686 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1687 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1688 | int type_arg, |
| 1689 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1690 | int export_size_delta, |
| 1691 | int expected_export_status_arg, |
| 1692 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1693 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1694 | psa_key_handle_t handle = 0; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1695 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1696 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1697 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1698 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1699 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1700 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1701 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1702 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1703 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1704 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1705 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1706 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1707 | psa_set_key_algorithm( &attributes, alg ); |
| 1708 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1709 | |
| 1710 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1711 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1712 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1713 | /* Export the public key */ |
| 1714 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1715 | status = psa_export_public_key( handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1716 | exported, export_size, |
| 1717 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1718 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1719 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1720 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1721 | 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] | 1722 | size_t bits; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1723 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1724 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1725 | TEST_ASSERT( expected_public_key->len <= |
| 1726 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1727 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1728 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1729 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1730 | |
| 1731 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1732 | mbedtls_free( exported ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1733 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1734 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1735 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1736 | } |
| 1737 | /* END_CASE */ |
| 1738 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1739 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1740 | void import_and_exercise_key( data_t *data, |
| 1741 | int type_arg, |
| 1742 | int bits_arg, |
| 1743 | int alg_arg ) |
| 1744 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1745 | psa_key_handle_t handle = 0; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1746 | psa_key_type_t type = type_arg; |
| 1747 | size_t bits = bits_arg; |
| 1748 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1749 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1750 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1751 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1752 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1753 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1754 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1755 | psa_set_key_usage_flags( &attributes, usage ); |
| 1756 | psa_set_key_algorithm( &attributes, alg ); |
| 1757 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1758 | |
| 1759 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1760 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1761 | |
| 1762 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1763 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1764 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1765 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1766 | |
| 1767 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1768 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1769 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1770 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1771 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1772 | test_operations_on_invalid_handle( handle ); |
| 1773 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1774 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1775 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1776 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1777 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1778 | } |
| 1779 | /* END_CASE */ |
| 1780 | |
| 1781 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1782 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1783 | int bits_arg, int expected_bits_arg, |
| 1784 | int usage_arg, int expected_usage_arg, |
| 1785 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1786 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1787 | psa_key_handle_t handle = 0; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1788 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1789 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1790 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1791 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1792 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1793 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1794 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1795 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1796 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1797 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1798 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1799 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1800 | psa_set_key_usage_flags( &attributes, usage ); |
| 1801 | psa_set_key_algorithm( &attributes, alg ); |
| 1802 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1803 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1804 | |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1805 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
| 1806 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1807 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1808 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1809 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1810 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1811 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1812 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1813 | |
| 1814 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1815 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1816 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1817 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1818 | } |
| 1819 | /* END_CASE */ |
| 1820 | |
| 1821 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1822 | void check_key_policy( int type_arg, int bits_arg, |
| 1823 | int usage_arg, int alg_arg ) |
| 1824 | { |
| 1825 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
| 1826 | usage_arg, usage_arg, alg_arg, alg_arg ); |
| 1827 | goto exit; |
| 1828 | } |
| 1829 | /* END_CASE */ |
| 1830 | |
| 1831 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1832 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1833 | { |
| 1834 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1835 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1836 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1837 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1838 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1839 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1840 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1841 | |
| 1842 | memset( &zero, 0, sizeof( zero ) ); |
| 1843 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1844 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1845 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1846 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1847 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1848 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1849 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1850 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1851 | |
| 1852 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1853 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1854 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1855 | |
| 1856 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1857 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1858 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1859 | |
| 1860 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1861 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1862 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1863 | } |
| 1864 | /* END_CASE */ |
| 1865 | |
| 1866 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1867 | void mac_key_policy( int policy_usage, |
| 1868 | int policy_alg, |
| 1869 | int key_type, |
| 1870 | data_t *key_data, |
| 1871 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1872 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1873 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1874 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1875 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1876 | psa_status_t status; |
| 1877 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1878 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1879 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1880 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1881 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1882 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1883 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1884 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1885 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1886 | &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1887 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1888 | status = psa_mac_sign_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1889 | if( policy_alg == exercise_alg && |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1890 | ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1891 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1892 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1893 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1894 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1895 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1896 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1897 | status = psa_mac_verify_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1898 | if( policy_alg == exercise_alg && |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1899 | ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1900 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1901 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1902 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1903 | |
| 1904 | exit: |
| 1905 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1906 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1907 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1908 | } |
| 1909 | /* END_CASE */ |
| 1910 | |
| 1911 | /* BEGIN_CASE */ |
| 1912 | void cipher_key_policy( int policy_usage, |
| 1913 | int policy_alg, |
| 1914 | int key_type, |
| 1915 | data_t *key_data, |
| 1916 | int exercise_alg ) |
| 1917 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1918 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1919 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1920 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1921 | psa_status_t status; |
| 1922 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1923 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1924 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1925 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1926 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1927 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1928 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1929 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1930 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1931 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1932 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1933 | if( policy_alg == exercise_alg && |
| 1934 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1935 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1936 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1937 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1938 | psa_cipher_abort( &operation ); |
| 1939 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1940 | status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1941 | if( policy_alg == exercise_alg && |
| 1942 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1943 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1944 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1945 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1946 | |
| 1947 | exit: |
| 1948 | psa_cipher_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1949 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1950 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1951 | } |
| 1952 | /* END_CASE */ |
| 1953 | |
| 1954 | /* BEGIN_CASE */ |
| 1955 | void aead_key_policy( int policy_usage, |
| 1956 | int policy_alg, |
| 1957 | int key_type, |
| 1958 | data_t *key_data, |
| 1959 | int nonce_length_arg, |
| 1960 | int tag_length_arg, |
| 1961 | int exercise_alg ) |
| 1962 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1963 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1964 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1965 | psa_status_t status; |
| 1966 | unsigned char nonce[16] = {0}; |
| 1967 | size_t nonce_length = nonce_length_arg; |
| 1968 | unsigned char tag[16]; |
| 1969 | size_t tag_length = tag_length_arg; |
| 1970 | size_t output_length; |
| 1971 | |
| 1972 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1973 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1974 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1975 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1976 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1977 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1978 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1979 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1980 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1981 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1982 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1983 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1984 | status = psa_aead_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1985 | nonce, nonce_length, |
| 1986 | NULL, 0, |
| 1987 | NULL, 0, |
| 1988 | tag, tag_length, |
| 1989 | &output_length ); |
| 1990 | if( policy_alg == exercise_alg && |
| 1991 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1992 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1993 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1994 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1995 | |
| 1996 | memset( tag, 0, sizeof( tag ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1997 | status = psa_aead_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1998 | nonce, nonce_length, |
| 1999 | NULL, 0, |
| 2000 | tag, tag_length, |
| 2001 | NULL, 0, |
| 2002 | &output_length ); |
| 2003 | if( policy_alg == exercise_alg && |
| 2004 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2005 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2006 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2007 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2008 | |
| 2009 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2010 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2011 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2012 | } |
| 2013 | /* END_CASE */ |
| 2014 | |
| 2015 | /* BEGIN_CASE */ |
| 2016 | void asymmetric_encryption_key_policy( int policy_usage, |
| 2017 | int policy_alg, |
| 2018 | int key_type, |
| 2019 | data_t *key_data, |
| 2020 | int exercise_alg ) |
| 2021 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2022 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2023 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2024 | psa_status_t status; |
| 2025 | size_t key_bits; |
| 2026 | size_t buffer_length; |
| 2027 | unsigned char *buffer = NULL; |
| 2028 | size_t output_length; |
| 2029 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2030 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2031 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2032 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2033 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2034 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2035 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2036 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2037 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2038 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2039 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 2040 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2041 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2042 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2043 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2044 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2045 | status = psa_asymmetric_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2046 | NULL, 0, |
| 2047 | NULL, 0, |
| 2048 | buffer, buffer_length, |
| 2049 | &output_length ); |
| 2050 | if( policy_alg == exercise_alg && |
| 2051 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2052 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2053 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2054 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2055 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2056 | if( buffer_length != 0 ) |
| 2057 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2058 | status = psa_asymmetric_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2059 | buffer, buffer_length, |
| 2060 | NULL, 0, |
| 2061 | buffer, buffer_length, |
| 2062 | &output_length ); |
| 2063 | if( policy_alg == exercise_alg && |
| 2064 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2065 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2066 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2067 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2068 | |
| 2069 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2070 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2071 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2072 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2073 | mbedtls_free( buffer ); |
| 2074 | } |
| 2075 | /* END_CASE */ |
| 2076 | |
| 2077 | /* BEGIN_CASE */ |
| 2078 | void asymmetric_signature_key_policy( int policy_usage, |
| 2079 | int policy_alg, |
| 2080 | int key_type, |
| 2081 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2082 | int exercise_alg, |
| 2083 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2084 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2085 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2086 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2087 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2088 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2089 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2090 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2091 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2092 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2093 | int compatible_alg = payload_length_arg > 0; |
| 2094 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2095 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2096 | size_t signature_length; |
| 2097 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2098 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2099 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2100 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2101 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2102 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2103 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2104 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2105 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2106 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2107 | status = psa_sign_hash( handle, exercise_alg, |
| 2108 | payload, payload_length, |
| 2109 | signature, sizeof( signature ), |
| 2110 | &signature_length ); |
| 2111 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2112 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2113 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2114 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2115 | |
| 2116 | memset( signature, 0, sizeof( signature ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2117 | status = psa_verify_hash( handle, exercise_alg, |
| 2118 | payload, payload_length, |
| 2119 | signature, sizeof( signature ) ); |
| 2120 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2121 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2122 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2123 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2124 | |
| 2125 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2126 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2127 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2128 | } |
| 2129 | /* END_CASE */ |
| 2130 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2131 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2132 | void derive_key_policy( int policy_usage, |
| 2133 | int policy_alg, |
| 2134 | int key_type, |
| 2135 | data_t *key_data, |
| 2136 | int exercise_alg ) |
| 2137 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2138 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2139 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2140 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2141 | psa_status_t status; |
| 2142 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2143 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2144 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2145 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2146 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2147 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2148 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2149 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2150 | &handle ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2151 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2152 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2153 | |
| 2154 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2155 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2156 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2157 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2158 | &operation, |
| 2159 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2160 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2161 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2162 | |
| 2163 | status = psa_key_derivation_input_key( &operation, |
| 2164 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2165 | handle ); |
| 2166 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2167 | if( policy_alg == exercise_alg && |
| 2168 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2169 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2170 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2171 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2172 | |
| 2173 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2174 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2175 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2176 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2177 | } |
| 2178 | /* END_CASE */ |
| 2179 | |
| 2180 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2181 | void agreement_key_policy( int policy_usage, |
| 2182 | int policy_alg, |
| 2183 | int key_type_arg, |
| 2184 | data_t *key_data, |
| 2185 | int exercise_alg ) |
| 2186 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2187 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2188 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2189 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2190 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2191 | psa_status_t status; |
| 2192 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2193 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2194 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2195 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2196 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2197 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2198 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2199 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2200 | &handle ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2201 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2202 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2203 | status = key_agreement_with_self( &operation, handle ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2204 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2205 | if( policy_alg == exercise_alg && |
| 2206 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2207 | PSA_ASSERT( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2208 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2209 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2210 | |
| 2211 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2212 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2213 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2214 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2215 | } |
| 2216 | /* END_CASE */ |
| 2217 | |
| 2218 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2219 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2220 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2221 | { |
| 2222 | psa_key_handle_t handle = 0; |
| 2223 | psa_key_type_t key_type = key_type_arg; |
| 2224 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2225 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2226 | psa_key_usage_t usage = usage_arg; |
| 2227 | psa_algorithm_t alg = alg_arg; |
| 2228 | psa_algorithm_t alg2 = alg2_arg; |
| 2229 | |
| 2230 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2231 | |
| 2232 | psa_set_key_usage_flags( &attributes, usage ); |
| 2233 | psa_set_key_algorithm( &attributes, alg ); |
| 2234 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2235 | psa_set_key_type( &attributes, key_type ); |
| 2236 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2237 | &handle ) ); |
| 2238 | |
| 2239 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 2240 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2241 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2242 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2243 | |
| 2244 | if( ! exercise_key( handle, usage, alg ) ) |
| 2245 | goto exit; |
| 2246 | if( ! exercise_key( handle, usage, alg2 ) ) |
| 2247 | goto exit; |
| 2248 | |
| 2249 | exit: |
| 2250 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2251 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2252 | } |
| 2253 | /* END_CASE */ |
| 2254 | |
| 2255 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2256 | void raw_agreement_key_policy( int policy_usage, |
| 2257 | int policy_alg, |
| 2258 | int key_type_arg, |
| 2259 | data_t *key_data, |
| 2260 | int exercise_alg ) |
| 2261 | { |
| 2262 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2263 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2264 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2265 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2266 | psa_status_t status; |
| 2267 | |
| 2268 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2269 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2270 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2271 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2272 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2273 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2274 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2275 | &handle ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2276 | |
| 2277 | status = raw_key_agreement_with_self( exercise_alg, handle ); |
| 2278 | |
| 2279 | if( policy_alg == exercise_alg && |
| 2280 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 2281 | PSA_ASSERT( status ); |
| 2282 | else |
| 2283 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2284 | |
| 2285 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2286 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2287 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2288 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2289 | } |
| 2290 | /* END_CASE */ |
| 2291 | |
| 2292 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2293 | void copy_success( int source_usage_arg, |
| 2294 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2295 | int type_arg, data_t *material, |
| 2296 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2297 | int target_usage_arg, |
| 2298 | int target_alg_arg, int target_alg2_arg, |
| 2299 | int expected_usage_arg, |
| 2300 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2301 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2302 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2303 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2304 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2305 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2306 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2307 | psa_key_handle_t source_handle = 0; |
| 2308 | psa_key_handle_t target_handle = 0; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2309 | uint8_t *export_buffer = NULL; |
| 2310 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2311 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2312 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2313 | /* Prepare the source key. */ |
| 2314 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2315 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2316 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2317 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2318 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2319 | material->x, material->len, |
| 2320 | &source_handle ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2321 | PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2322 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2323 | /* Prepare the target attributes. */ |
| 2324 | if( copy_attributes ) |
| 2325 | target_attributes = source_attributes; |
| 2326 | if( target_usage_arg != -1 ) |
| 2327 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2328 | if( target_alg_arg != -1 ) |
| 2329 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2330 | if( target_alg2_arg != -1 ) |
| 2331 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2332 | |
| 2333 | /* Copy the key. */ |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2334 | PSA_ASSERT( psa_copy_key( source_handle, |
| 2335 | &target_attributes, &target_handle ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2336 | |
| 2337 | /* Destroy the source to ensure that this doesn't affect the target. */ |
| 2338 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2339 | |
| 2340 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2341 | PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) ); |
| 2342 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2343 | psa_get_key_type( &target_attributes ) ); |
| 2344 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2345 | psa_get_key_bits( &target_attributes ) ); |
| 2346 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2347 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2348 | TEST_EQUAL( expected_alg2, |
| 2349 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2350 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2351 | { |
| 2352 | size_t length; |
| 2353 | ASSERT_ALLOC( export_buffer, material->len ); |
| 2354 | PSA_ASSERT( psa_export_key( target_handle, export_buffer, |
| 2355 | material->len, &length ) ); |
| 2356 | ASSERT_COMPARE( material->x, material->len, |
| 2357 | export_buffer, length ); |
| 2358 | } |
| 2359 | if( ! exercise_key( target_handle, expected_usage, expected_alg ) ) |
| 2360 | goto exit; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2361 | if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) ) |
| 2362 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2363 | |
| 2364 | PSA_ASSERT( psa_close_key( target_handle ) ); |
| 2365 | |
| 2366 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2367 | psa_reset_key_attributes( &source_attributes ); |
| 2368 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2369 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2370 | mbedtls_free( export_buffer ); |
| 2371 | } |
| 2372 | /* END_CASE */ |
| 2373 | |
| 2374 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2375 | void copy_fail( int source_usage_arg, |
| 2376 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2377 | int type_arg, data_t *material, |
| 2378 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2379 | int target_usage_arg, |
| 2380 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2381 | int expected_status_arg ) |
| 2382 | { |
| 2383 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2384 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2385 | psa_key_handle_t source_handle = 0; |
| 2386 | psa_key_handle_t target_handle = 0; |
| 2387 | |
| 2388 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2389 | |
| 2390 | /* Prepare the source key. */ |
| 2391 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2392 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2393 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2394 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2395 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2396 | material->x, material->len, |
| 2397 | &source_handle ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2398 | |
| 2399 | /* Prepare the target attributes. */ |
| 2400 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2401 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2402 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2403 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2404 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2405 | |
| 2406 | /* Try to copy the key. */ |
| 2407 | TEST_EQUAL( psa_copy_key( source_handle, |
| 2408 | &target_attributes, &target_handle ), |
| 2409 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2410 | |
| 2411 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2412 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2413 | exit: |
| 2414 | psa_reset_key_attributes( &source_attributes ); |
| 2415 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2416 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2417 | } |
| 2418 | /* END_CASE */ |
| 2419 | |
| 2420 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2421 | void hash_operation_init( ) |
| 2422 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2423 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2424 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2425 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2426 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2427 | * to supress the Clang warning for the test. */ |
| 2428 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2429 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2430 | psa_hash_operation_t zero; |
| 2431 | |
| 2432 | memset( &zero, 0, sizeof( zero ) ); |
| 2433 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2434 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2435 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2436 | PSA_ERROR_BAD_STATE ); |
| 2437 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2438 | PSA_ERROR_BAD_STATE ); |
| 2439 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2440 | PSA_ERROR_BAD_STATE ); |
| 2441 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2442 | /* A default hash operation should be abortable without error. */ |
| 2443 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2444 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2445 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2446 | } |
| 2447 | /* END_CASE */ |
| 2448 | |
| 2449 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2450 | void hash_setup( int alg_arg, |
| 2451 | int expected_status_arg ) |
| 2452 | { |
| 2453 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2454 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2455 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2456 | psa_status_t status; |
| 2457 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2458 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2459 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2460 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2461 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2462 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2463 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2464 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2465 | |
| 2466 | /* If setup failed, reproduce the failure, so as to |
| 2467 | * test the resulting state of the operation object. */ |
| 2468 | if( status != PSA_SUCCESS ) |
| 2469 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2470 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2471 | /* Now the operation object should be reusable. */ |
| 2472 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2473 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2474 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2475 | #endif |
| 2476 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2477 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2478 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2479 | } |
| 2480 | /* END_CASE */ |
| 2481 | |
| 2482 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2483 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2484 | int output_size_arg, int expected_status_arg ) |
| 2485 | { |
| 2486 | psa_algorithm_t alg = alg_arg; |
| 2487 | uint8_t *output = NULL; |
| 2488 | size_t output_size = output_size_arg; |
| 2489 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 2490 | psa_status_t expected_status = expected_status_arg; |
| 2491 | psa_status_t status; |
| 2492 | |
| 2493 | ASSERT_ALLOC( output, output_size ); |
| 2494 | |
| 2495 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2496 | |
| 2497 | status = psa_hash_compute( alg, input->x, input->len, |
| 2498 | output, output_size, &output_length ); |
| 2499 | TEST_EQUAL( status, expected_status ); |
| 2500 | TEST_ASSERT( output_length <= output_size ); |
| 2501 | |
| 2502 | exit: |
| 2503 | mbedtls_free( output ); |
| 2504 | PSA_DONE( ); |
| 2505 | } |
| 2506 | /* END_CASE */ |
| 2507 | |
| 2508 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2509 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2510 | data_t *reference_hash, |
| 2511 | int expected_status_arg ) |
| 2512 | { |
| 2513 | psa_algorithm_t alg = alg_arg; |
| 2514 | psa_status_t expected_status = expected_status_arg; |
| 2515 | psa_status_t status; |
| 2516 | |
| 2517 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2518 | |
| 2519 | status = psa_hash_compare( alg, input->x, input->len, |
| 2520 | reference_hash->x, reference_hash->len ); |
| 2521 | TEST_EQUAL( status, expected_status ); |
| 2522 | |
| 2523 | exit: |
| 2524 | PSA_DONE( ); |
| 2525 | } |
| 2526 | /* END_CASE */ |
| 2527 | |
| 2528 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2529 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2530 | data_t *expected_output ) |
| 2531 | { |
| 2532 | psa_algorithm_t alg = alg_arg; |
| 2533 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2534 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 2535 | size_t i; |
| 2536 | |
| 2537 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2538 | |
| 2539 | /* Compute with tight buffer */ |
| 2540 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2541 | output, PSA_HASH_SIZE( alg ), |
| 2542 | &output_length ) ); |
| 2543 | TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) ); |
| 2544 | ASSERT_COMPARE( output, output_length, |
| 2545 | expected_output->x, expected_output->len ); |
| 2546 | |
| 2547 | /* Compute with larger buffer */ |
| 2548 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2549 | output, sizeof( output ), |
| 2550 | &output_length ) ); |
| 2551 | TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) ); |
| 2552 | ASSERT_COMPARE( output, output_length, |
| 2553 | expected_output->x, expected_output->len ); |
| 2554 | |
| 2555 | /* Compare with correct hash */ |
| 2556 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2557 | output, output_length ) ); |
| 2558 | |
| 2559 | /* Compare with trailing garbage */ |
| 2560 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2561 | output, output_length + 1 ), |
| 2562 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2563 | |
| 2564 | /* Compare with truncated hash */ |
| 2565 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2566 | output, output_length - 1 ), |
| 2567 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2568 | |
| 2569 | /* Compare with corrupted value */ |
| 2570 | for( i = 0; i < output_length; i++ ) |
| 2571 | { |
| 2572 | test_set_step( i ); |
| 2573 | output[i] ^= 1; |
| 2574 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2575 | output, output_length ), |
| 2576 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2577 | output[i] ^= 1; |
| 2578 | } |
| 2579 | |
| 2580 | exit: |
| 2581 | PSA_DONE( ); |
| 2582 | } |
| 2583 | /* END_CASE */ |
| 2584 | |
| 2585 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2586 | void hash_bad_order( ) |
| 2587 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2588 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2589 | unsigned char input[] = ""; |
| 2590 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2591 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2592 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2593 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2594 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2595 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2596 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2597 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2598 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2599 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2600 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2601 | /* Call setup twice in a row. */ |
| 2602 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2603 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2604 | PSA_ERROR_BAD_STATE ); |
| 2605 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2606 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2607 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2608 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2609 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2610 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2611 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2612 | /* Call update after finish. */ |
| 2613 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2614 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2615 | hash, sizeof( hash ), &hash_len ) ); |
| 2616 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2617 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2618 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2619 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2620 | /* Call verify without calling setup beforehand. */ |
| 2621 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2622 | valid_hash, sizeof( valid_hash ) ), |
| 2623 | PSA_ERROR_BAD_STATE ); |
| 2624 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2625 | |
| 2626 | /* Call verify after finish. */ |
| 2627 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2628 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2629 | hash, sizeof( hash ), &hash_len ) ); |
| 2630 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2631 | valid_hash, sizeof( valid_hash ) ), |
| 2632 | PSA_ERROR_BAD_STATE ); |
| 2633 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2634 | |
| 2635 | /* Call verify twice in a row. */ |
| 2636 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2637 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2638 | valid_hash, sizeof( valid_hash ) ) ); |
| 2639 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2640 | valid_hash, sizeof( valid_hash ) ), |
| 2641 | PSA_ERROR_BAD_STATE ); |
| 2642 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2643 | |
| 2644 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2645 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2646 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2647 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2648 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2649 | |
| 2650 | /* Call finish twice in a row. */ |
| 2651 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2652 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2653 | hash, sizeof( hash ), &hash_len ) ); |
| 2654 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2655 | hash, sizeof( hash ), &hash_len ), |
| 2656 | PSA_ERROR_BAD_STATE ); |
| 2657 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2658 | |
| 2659 | /* Call finish after calling verify. */ |
| 2660 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2661 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2662 | valid_hash, sizeof( valid_hash ) ) ); |
| 2663 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2664 | hash, sizeof( hash ), &hash_len ), |
| 2665 | PSA_ERROR_BAD_STATE ); |
| 2666 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2667 | |
| 2668 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2669 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2670 | } |
| 2671 | /* END_CASE */ |
| 2672 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2673 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2674 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2675 | { |
| 2676 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2677 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2678 | * appended to it */ |
| 2679 | unsigned char hash[] = { |
| 2680 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2681 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2682 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2683 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2684 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2685 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2686 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2687 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2688 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2689 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2690 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2691 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2692 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2693 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2694 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2695 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2696 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2697 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2698 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2699 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2700 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2701 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2702 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2703 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2704 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2705 | } |
| 2706 | /* END_CASE */ |
| 2707 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2708 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2709 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2710 | { |
| 2711 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2712 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2713 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2714 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2715 | size_t hash_len; |
| 2716 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2717 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2718 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2719 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2720 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2721 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2722 | hash, expected_size - 1, &hash_len ), |
| 2723 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2724 | |
| 2725 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2726 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2727 | } |
| 2728 | /* END_CASE */ |
| 2729 | |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2730 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2731 | void hash_clone_source_state( ) |
| 2732 | { |
| 2733 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2734 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2735 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2736 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2737 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2738 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2739 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2740 | size_t hash_len; |
| 2741 | |
| 2742 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2743 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2744 | |
| 2745 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2746 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2747 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2748 | hash, sizeof( hash ), &hash_len ) ); |
| 2749 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2750 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2751 | |
| 2752 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2753 | PSA_ERROR_BAD_STATE ); |
| 2754 | |
| 2755 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2756 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2757 | hash, sizeof( hash ), &hash_len ) ); |
| 2758 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2759 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2760 | hash, sizeof( hash ), &hash_len ) ); |
| 2761 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2762 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2763 | hash, sizeof( hash ), &hash_len ) ); |
| 2764 | |
| 2765 | exit: |
| 2766 | psa_hash_abort( &op_source ); |
| 2767 | psa_hash_abort( &op_init ); |
| 2768 | psa_hash_abort( &op_setup ); |
| 2769 | psa_hash_abort( &op_finished ); |
| 2770 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2771 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2772 | } |
| 2773 | /* END_CASE */ |
| 2774 | |
| 2775 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2776 | void hash_clone_target_state( ) |
| 2777 | { |
| 2778 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2779 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2780 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2781 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2782 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2783 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2784 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2785 | size_t hash_len; |
| 2786 | |
| 2787 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2788 | |
| 2789 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2790 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2791 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2792 | hash, sizeof( hash ), &hash_len ) ); |
| 2793 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2794 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2795 | |
| 2796 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2797 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2798 | hash, sizeof( hash ), &hash_len ) ); |
| 2799 | |
| 2800 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2801 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2802 | PSA_ERROR_BAD_STATE ); |
| 2803 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2804 | PSA_ERROR_BAD_STATE ); |
| 2805 | |
| 2806 | exit: |
| 2807 | psa_hash_abort( &op_target ); |
| 2808 | psa_hash_abort( &op_init ); |
| 2809 | psa_hash_abort( &op_setup ); |
| 2810 | psa_hash_abort( &op_finished ); |
| 2811 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2812 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2813 | } |
| 2814 | /* END_CASE */ |
| 2815 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2816 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2817 | void mac_operation_init( ) |
| 2818 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2819 | const uint8_t input[1] = { 0 }; |
| 2820 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2821 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2822 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2823 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2824 | * to supress the Clang warning for the test. */ |
| 2825 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2826 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2827 | psa_mac_operation_t zero; |
| 2828 | |
| 2829 | memset( &zero, 0, sizeof( zero ) ); |
| 2830 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2831 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2832 | TEST_EQUAL( psa_mac_update( &func, |
| 2833 | input, sizeof( input ) ), |
| 2834 | PSA_ERROR_BAD_STATE ); |
| 2835 | TEST_EQUAL( psa_mac_update( &init, |
| 2836 | input, sizeof( input ) ), |
| 2837 | PSA_ERROR_BAD_STATE ); |
| 2838 | TEST_EQUAL( psa_mac_update( &zero, |
| 2839 | input, sizeof( input ) ), |
| 2840 | PSA_ERROR_BAD_STATE ); |
| 2841 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2842 | /* A default MAC operation should be abortable without error. */ |
| 2843 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2844 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2845 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2846 | } |
| 2847 | /* END_CASE */ |
| 2848 | |
| 2849 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2850 | void mac_setup( int key_type_arg, |
| 2851 | data_t *key, |
| 2852 | int alg_arg, |
| 2853 | int expected_status_arg ) |
| 2854 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2855 | psa_key_type_t key_type = key_type_arg; |
| 2856 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2857 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2858 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2859 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2860 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2861 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2862 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2863 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2864 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2865 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2866 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2867 | &operation, &status ) ) |
| 2868 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2869 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2870 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2871 | /* The operation object should be reusable. */ |
| 2872 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2873 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2874 | smoke_test_key_data, |
| 2875 | sizeof( smoke_test_key_data ), |
| 2876 | KNOWN_SUPPORTED_MAC_ALG, |
| 2877 | &operation, &status ) ) |
| 2878 | goto exit; |
| 2879 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2880 | #endif |
| 2881 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2882 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2883 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2884 | } |
| 2885 | /* END_CASE */ |
| 2886 | |
| 2887 | /* BEGIN_CASE */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2888 | void mac_bad_order( ) |
| 2889 | { |
| 2890 | psa_key_handle_t handle = 0; |
| 2891 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2892 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
| 2893 | const uint8_t key[] = { |
| 2894 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2895 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2896 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2897 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2898 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2899 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2900 | size_t sign_mac_length = 0; |
| 2901 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2902 | const uint8_t verify_mac[] = { |
| 2903 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2904 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2905 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2906 | |
| 2907 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2908 | 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] | 2909 | psa_set_key_algorithm( &attributes, alg ); |
| 2910 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2911 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2912 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2913 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2914 | /* Call update without calling setup beforehand. */ |
| 2915 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2916 | PSA_ERROR_BAD_STATE ); |
| 2917 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2918 | |
| 2919 | /* Call sign finish without calling setup beforehand. */ |
| 2920 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2921 | &sign_mac_length), |
| 2922 | PSA_ERROR_BAD_STATE ); |
| 2923 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2924 | |
| 2925 | /* Call verify finish without calling setup beforehand. */ |
| 2926 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2927 | verify_mac, sizeof( verify_mac ) ), |
| 2928 | PSA_ERROR_BAD_STATE ); |
| 2929 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2930 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2931 | /* Call setup twice in a row. */ |
| 2932 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2933 | handle, alg ) ); |
| 2934 | TEST_EQUAL( psa_mac_sign_setup( &operation, |
| 2935 | handle, alg ), |
| 2936 | PSA_ERROR_BAD_STATE ); |
| 2937 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2938 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2939 | /* Call update after sign finish. */ |
| 2940 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2941 | handle, alg ) ); |
| 2942 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2943 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2944 | sign_mac, sizeof( sign_mac ), |
| 2945 | &sign_mac_length ) ); |
| 2946 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2947 | PSA_ERROR_BAD_STATE ); |
| 2948 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2949 | |
| 2950 | /* Call update after verify finish. */ |
| 2951 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2952 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2953 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2954 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2955 | verify_mac, sizeof( verify_mac ) ) ); |
| 2956 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2957 | PSA_ERROR_BAD_STATE ); |
| 2958 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2959 | |
| 2960 | /* Call sign finish twice in a row. */ |
| 2961 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2962 | handle, alg ) ); |
| 2963 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2964 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2965 | sign_mac, sizeof( sign_mac ), |
| 2966 | &sign_mac_length ) ); |
| 2967 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2968 | sign_mac, sizeof( sign_mac ), |
| 2969 | &sign_mac_length ), |
| 2970 | PSA_ERROR_BAD_STATE ); |
| 2971 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2972 | |
| 2973 | /* Call verify finish twice in a row. */ |
| 2974 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2975 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2976 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2977 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2978 | verify_mac, sizeof( verify_mac ) ) ); |
| 2979 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2980 | verify_mac, sizeof( verify_mac ) ), |
| 2981 | PSA_ERROR_BAD_STATE ); |
| 2982 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2983 | |
| 2984 | /* Setup sign but try verify. */ |
| 2985 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2986 | handle, alg ) ); |
| 2987 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2988 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2989 | verify_mac, sizeof( verify_mac ) ), |
| 2990 | PSA_ERROR_BAD_STATE ); |
| 2991 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2992 | |
| 2993 | /* Setup verify but try sign. */ |
| 2994 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2995 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2996 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2997 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2998 | sign_mac, sizeof( sign_mac ), |
| 2999 | &sign_mac_length ), |
| 3000 | PSA_ERROR_BAD_STATE ); |
| 3001 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3002 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3003 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 3004 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 3005 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3006 | PSA_DONE( ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 3007 | } |
| 3008 | /* END_CASE */ |
| 3009 | |
| 3010 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3011 | void mac_sign( int key_type_arg, |
| 3012 | data_t *key, |
| 3013 | int alg_arg, |
| 3014 | data_t *input, |
| 3015 | data_t *expected_mac ) |
| 3016 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3017 | psa_key_handle_t handle = 0; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3018 | psa_key_type_t key_type = key_type_arg; |
| 3019 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3020 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3021 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3022 | /* Leave a little extra room in the output buffer. At the end of the |
| 3023 | * test, we'll check that the implementation didn't overwrite onto |
| 3024 | * this extra room. */ |
| 3025 | uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10]; |
| 3026 | size_t mac_buffer_size = |
| 3027 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 3028 | size_t mac_length = 0; |
| 3029 | |
| 3030 | memset( actual_mac, '+', sizeof( actual_mac ) ); |
| 3031 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
| 3032 | TEST_ASSERT( expected_mac->len <= mac_buffer_size ); |
| 3033 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3034 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3035 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3036 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3037 | psa_set_key_algorithm( &attributes, alg ); |
| 3038 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3039 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3040 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3041 | |
| 3042 | /* Calculate the MAC. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3043 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 3044 | handle, alg ) ); |
| 3045 | PSA_ASSERT( psa_mac_update( &operation, |
| 3046 | input->x, input->len ) ); |
| 3047 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3048 | actual_mac, mac_buffer_size, |
| 3049 | &mac_length ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3050 | |
| 3051 | /* Compare with the expected value. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 3052 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3053 | actual_mac, mac_length ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3054 | |
| 3055 | /* Verify that the end of the buffer is untouched. */ |
| 3056 | TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+', |
| 3057 | sizeof( actual_mac ) - mac_length ) ); |
| 3058 | |
| 3059 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3060 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3061 | PSA_DONE( ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3062 | } |
| 3063 | /* END_CASE */ |
| 3064 | |
| 3065 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3066 | void mac_verify( int key_type_arg, |
| 3067 | data_t *key, |
| 3068 | int alg_arg, |
| 3069 | data_t *input, |
| 3070 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3071 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3072 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3073 | psa_key_type_t key_type = key_type_arg; |
| 3074 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3075 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3076 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3077 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3078 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 3079 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3080 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3081 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3082 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3083 | psa_set_key_algorithm( &attributes, alg ); |
| 3084 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3085 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3086 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3087 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3088 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 3089 | handle, alg ) ); |
| 3090 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 3091 | PSA_ASSERT( psa_mac_update( &operation, |
| 3092 | input->x, input->len ) ); |
| 3093 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3094 | expected_mac->x, |
| 3095 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3096 | |
| 3097 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3098 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3099 | PSA_DONE( ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3100 | } |
| 3101 | /* END_CASE */ |
| 3102 | |
| 3103 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3104 | void cipher_operation_init( ) |
| 3105 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3106 | const uint8_t input[1] = { 0 }; |
| 3107 | unsigned char output[1] = { 0 }; |
| 3108 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3109 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3110 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3111 | * though it's OK by the C standard. We could test for this, but we'd need |
| 3112 | * to supress the Clang warning for the test. */ |
| 3113 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3114 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3115 | psa_cipher_operation_t zero; |
| 3116 | |
| 3117 | memset( &zero, 0, sizeof( zero ) ); |
| 3118 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3119 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3120 | TEST_EQUAL( psa_cipher_update( &func, |
| 3121 | input, sizeof( input ), |
| 3122 | output, sizeof( output ), |
| 3123 | &output_length ), |
| 3124 | PSA_ERROR_BAD_STATE ); |
| 3125 | TEST_EQUAL( psa_cipher_update( &init, |
| 3126 | input, sizeof( input ), |
| 3127 | output, sizeof( output ), |
| 3128 | &output_length ), |
| 3129 | PSA_ERROR_BAD_STATE ); |
| 3130 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3131 | input, sizeof( input ), |
| 3132 | output, sizeof( output ), |
| 3133 | &output_length ), |
| 3134 | PSA_ERROR_BAD_STATE ); |
| 3135 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3136 | /* A default cipher operation should be abortable without error. */ |
| 3137 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3138 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3139 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3140 | } |
| 3141 | /* END_CASE */ |
| 3142 | |
| 3143 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3144 | void cipher_setup( int key_type_arg, |
| 3145 | data_t *key, |
| 3146 | int alg_arg, |
| 3147 | int expected_status_arg ) |
| 3148 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3149 | psa_key_type_t key_type = key_type_arg; |
| 3150 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3151 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3152 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3153 | psa_status_t status; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3154 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3155 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3156 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3157 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3158 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3159 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3160 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3161 | &operation, &status ) ) |
| 3162 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3163 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3164 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3165 | /* The operation object should be reusable. */ |
| 3166 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3167 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3168 | smoke_test_key_data, |
| 3169 | sizeof( smoke_test_key_data ), |
| 3170 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3171 | &operation, &status ) ) |
| 3172 | goto exit; |
| 3173 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3174 | #endif |
| 3175 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3176 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3177 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3178 | } |
| 3179 | /* END_CASE */ |
| 3180 | |
| 3181 | /* BEGIN_CASE */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3182 | void cipher_bad_order( ) |
| 3183 | { |
| 3184 | psa_key_handle_t handle = 0; |
| 3185 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3186 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3187 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3188 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3189 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 3190 | const uint8_t key[] = { |
| 3191 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3192 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3193 | const uint8_t text[] = { |
| 3194 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3195 | 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3196 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 3197 | size_t length = 0; |
| 3198 | |
| 3199 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3200 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3201 | psa_set_key_algorithm( &attributes, alg ); |
| 3202 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3203 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3204 | |
| 3205 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3206 | /* Call encrypt setup twice in a row. */ |
| 3207 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3208 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ), |
| 3209 | PSA_ERROR_BAD_STATE ); |
| 3210 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3211 | |
| 3212 | /* Call decrypt setup twice in a row. */ |
| 3213 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) ); |
| 3214 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ), |
| 3215 | PSA_ERROR_BAD_STATE ); |
| 3216 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3217 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3218 | /* Generate an IV without calling setup beforehand. */ |
| 3219 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3220 | buffer, sizeof( buffer ), |
| 3221 | &length ), |
| 3222 | PSA_ERROR_BAD_STATE ); |
| 3223 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3224 | |
| 3225 | /* Generate an IV twice in a row. */ |
| 3226 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3227 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3228 | buffer, sizeof( buffer ), |
| 3229 | &length ) ); |
| 3230 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3231 | buffer, sizeof( buffer ), |
| 3232 | &length ), |
| 3233 | PSA_ERROR_BAD_STATE ); |
| 3234 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3235 | |
| 3236 | /* Generate an IV after it's already set. */ |
| 3237 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3238 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3239 | iv, sizeof( iv ) ) ); |
| 3240 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3241 | buffer, sizeof( buffer ), |
| 3242 | &length ), |
| 3243 | PSA_ERROR_BAD_STATE ); |
| 3244 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3245 | |
| 3246 | /* Set an IV without calling setup beforehand. */ |
| 3247 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3248 | iv, sizeof( iv ) ), |
| 3249 | PSA_ERROR_BAD_STATE ); |
| 3250 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3251 | |
| 3252 | /* Set an IV after it's already set. */ |
| 3253 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3254 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3255 | iv, sizeof( iv ) ) ); |
| 3256 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3257 | iv, sizeof( iv ) ), |
| 3258 | PSA_ERROR_BAD_STATE ); |
| 3259 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3260 | |
| 3261 | /* Set an IV after it's already generated. */ |
| 3262 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3263 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3264 | buffer, sizeof( buffer ), |
| 3265 | &length ) ); |
| 3266 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3267 | iv, sizeof( iv ) ), |
| 3268 | PSA_ERROR_BAD_STATE ); |
| 3269 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3270 | |
| 3271 | /* Call update without calling setup beforehand. */ |
| 3272 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3273 | text, sizeof( text ), |
| 3274 | buffer, sizeof( buffer ), |
| 3275 | &length ), |
| 3276 | PSA_ERROR_BAD_STATE ); |
| 3277 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3278 | |
| 3279 | /* Call update without an IV where an IV is required. */ |
| 3280 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3281 | text, sizeof( text ), |
| 3282 | buffer, sizeof( buffer ), |
| 3283 | &length ), |
| 3284 | PSA_ERROR_BAD_STATE ); |
| 3285 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3286 | |
| 3287 | /* Call update after finish. */ |
| 3288 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3289 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3290 | iv, sizeof( iv ) ) ); |
| 3291 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3292 | buffer, sizeof( buffer ), &length ) ); |
| 3293 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3294 | text, sizeof( text ), |
| 3295 | buffer, sizeof( buffer ), |
| 3296 | &length ), |
| 3297 | PSA_ERROR_BAD_STATE ); |
| 3298 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3299 | |
| 3300 | /* Call finish without calling setup beforehand. */ |
| 3301 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3302 | buffer, sizeof( buffer ), &length ), |
| 3303 | PSA_ERROR_BAD_STATE ); |
| 3304 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3305 | |
| 3306 | /* Call finish without an IV where an IV is required. */ |
| 3307 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3308 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3309 | * for cipher modes with padding. */ |
| 3310 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3311 | buffer, sizeof( buffer ), &length ), |
| 3312 | PSA_ERROR_BAD_STATE ); |
| 3313 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3314 | |
| 3315 | /* Call finish twice in a row. */ |
| 3316 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3317 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3318 | iv, sizeof( iv ) ) ); |
| 3319 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3320 | buffer, sizeof( buffer ), &length ) ); |
| 3321 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3322 | buffer, sizeof( buffer ), &length ), |
| 3323 | PSA_ERROR_BAD_STATE ); |
| 3324 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3325 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3326 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 3327 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3328 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3329 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3330 | } |
| 3331 | /* END_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3332 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3333 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3334 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3335 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3336 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3337 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3338 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3339 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3340 | psa_status_t status; |
| 3341 | psa_key_type_t key_type = key_type_arg; |
| 3342 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3343 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3344 | unsigned char *output = NULL; |
| 3345 | size_t output_buffer_size = 0; |
| 3346 | size_t function_output_length = 0; |
| 3347 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3348 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3349 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3350 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3351 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3352 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3353 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3354 | psa_set_key_algorithm( &attributes, alg ); |
| 3355 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3356 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3357 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3358 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3359 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3360 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3361 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3362 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3363 | output_buffer_size = ( (size_t) input->len + |
| 3364 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3365 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3366 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3367 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3368 | input->x, input->len, |
| 3369 | output, output_buffer_size, |
| 3370 | &function_output_length ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3371 | total_output_length += function_output_length; |
| 3372 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3373 | output + total_output_length, |
| 3374 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3375 | &function_output_length ); |
| 3376 | total_output_length += function_output_length; |
| 3377 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3378 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3379 | if( expected_status == PSA_SUCCESS ) |
| 3380 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3381 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3382 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3383 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | exit: |
| 3387 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3388 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3389 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3390 | } |
| 3391 | /* END_CASE */ |
| 3392 | |
| 3393 | /* BEGIN_CASE */ |
| 3394 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3395 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3396 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3397 | int first_part_size_arg, |
| 3398 | int output1_length_arg, int output2_length_arg, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3399 | data_t *expected_output ) |
| 3400 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3401 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3402 | psa_key_type_t key_type = key_type_arg; |
| 3403 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3404 | size_t first_part_size = first_part_size_arg; |
| 3405 | size_t output1_length = output1_length_arg; |
| 3406 | size_t output2_length = output2_length_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3407 | unsigned char *output = NULL; |
| 3408 | size_t output_buffer_size = 0; |
| 3409 | size_t function_output_length = 0; |
| 3410 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3411 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3412 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3413 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3414 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3415 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3416 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3417 | psa_set_key_algorithm( &attributes, alg ); |
| 3418 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3419 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3420 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3421 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3422 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3423 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3424 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3425 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3426 | output_buffer_size = ( (size_t) input->len + |
| 3427 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3428 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3429 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3430 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3431 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3432 | output, output_buffer_size, |
| 3433 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3434 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3435 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3436 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3437 | input->x + first_part_size, |
| 3438 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3439 | output + total_output_length, |
| 3440 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3441 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3442 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3443 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3444 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3445 | output + total_output_length, |
| 3446 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3447 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3448 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3449 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3450 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3451 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3452 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3453 | |
| 3454 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3455 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3456 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3457 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3458 | } |
| 3459 | /* END_CASE */ |
| 3460 | |
| 3461 | /* BEGIN_CASE */ |
| 3462 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3463 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3464 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3465 | int first_part_size_arg, |
| 3466 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3467 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3468 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3469 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3470 | |
| 3471 | psa_key_type_t key_type = key_type_arg; |
| 3472 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3473 | size_t first_part_size = first_part_size_arg; |
| 3474 | size_t output1_length = output1_length_arg; |
| 3475 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3476 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3477 | size_t output_buffer_size = 0; |
| 3478 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3479 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3480 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3481 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3482 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3483 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3484 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3485 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3486 | psa_set_key_algorithm( &attributes, alg ); |
| 3487 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3488 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3489 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3490 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3491 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3492 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3493 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3494 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3495 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3496 | output_buffer_size = ( (size_t) input->len + |
| 3497 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3498 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3499 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3500 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3501 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3502 | input->x, first_part_size, |
| 3503 | output, output_buffer_size, |
| 3504 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3505 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3506 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3507 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3508 | input->x + first_part_size, |
| 3509 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3510 | output + total_output_length, |
| 3511 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3512 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3513 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3514 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3515 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3516 | output + total_output_length, |
| 3517 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3518 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3519 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3520 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3521 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3522 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3523 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3524 | |
| 3525 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3526 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3527 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3528 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3529 | } |
| 3530 | /* END_CASE */ |
| 3531 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3532 | /* BEGIN_CASE */ |
| 3533 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3534 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3535 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3536 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3537 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3538 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3539 | psa_status_t status; |
| 3540 | psa_key_type_t key_type = key_type_arg; |
| 3541 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3542 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3543 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3544 | size_t output_buffer_size = 0; |
| 3545 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3546 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3547 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3548 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3549 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3550 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3551 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3552 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3553 | psa_set_key_algorithm( &attributes, alg ); |
| 3554 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3555 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3556 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3557 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3558 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3559 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3560 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3561 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3562 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3563 | output_buffer_size = ( (size_t) input->len + |
| 3564 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3565 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3566 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3567 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3568 | input->x, input->len, |
| 3569 | output, output_buffer_size, |
| 3570 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3571 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3572 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3573 | output + total_output_length, |
| 3574 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3575 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3576 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3577 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3578 | |
| 3579 | if( expected_status == PSA_SUCCESS ) |
| 3580 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3581 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3582 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3583 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3584 | } |
| 3585 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3586 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3587 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3588 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3589 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3590 | } |
| 3591 | /* END_CASE */ |
| 3592 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3593 | /* BEGIN_CASE */ |
| 3594 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3595 | data_t *key, |
| 3596 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3597 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3598 | psa_key_handle_t handle = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3599 | psa_key_type_t key_type = key_type_arg; |
| 3600 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 3601 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3602 | size_t iv_size = 16; |
| 3603 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3604 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3605 | size_t output1_size = 0; |
| 3606 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3607 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3608 | size_t output2_size = 0; |
| 3609 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3610 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3611 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3612 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3613 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3614 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3615 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3616 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3617 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3618 | psa_set_key_algorithm( &attributes, alg ); |
| 3619 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3620 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3621 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3622 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3623 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3624 | handle, alg ) ); |
| 3625 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3626 | handle, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3627 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3628 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3629 | iv, iv_size, |
| 3630 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3631 | output1_size = ( (size_t) input->len + |
| 3632 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3633 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3634 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3635 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3636 | output1, output1_size, |
| 3637 | &output1_length ) ); |
| 3638 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3639 | output1 + output1_length, |
| 3640 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3641 | &function_output_length ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3642 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3643 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3644 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3645 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3646 | |
| 3647 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3648 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3649 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3650 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3651 | iv, iv_length ) ); |
| 3652 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3653 | output2, output2_size, |
| 3654 | &output2_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3655 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3656 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3657 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3658 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3659 | &function_output_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3660 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3661 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3662 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3663 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3664 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3665 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3666 | |
| 3667 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3668 | mbedtls_free( output1 ); |
| 3669 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3670 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3671 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3672 | } |
| 3673 | /* END_CASE */ |
| 3674 | |
| 3675 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3676 | void cipher_verify_output_multipart( int alg_arg, |
| 3677 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3678 | data_t *key, |
| 3679 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3680 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3681 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3682 | psa_key_handle_t handle = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3683 | psa_key_type_t key_type = key_type_arg; |
| 3684 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3685 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3686 | unsigned char iv[16] = {0}; |
| 3687 | size_t iv_size = 16; |
| 3688 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3689 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3690 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3691 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3692 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3693 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3694 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3695 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3696 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3697 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3698 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3699 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3700 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3701 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3702 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3703 | psa_set_key_algorithm( &attributes, alg ); |
| 3704 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3705 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3706 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3707 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3708 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3709 | handle, alg ) ); |
| 3710 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3711 | handle, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3712 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3713 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3714 | iv, iv_size, |
| 3715 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3716 | output1_buffer_size = ( (size_t) input->len + |
| 3717 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3718 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3719 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3720 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3721 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3722 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3723 | output1, output1_buffer_size, |
| 3724 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3725 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3726 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3727 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3728 | input->x + first_part_size, |
| 3729 | input->len - first_part_size, |
| 3730 | output1, output1_buffer_size, |
| 3731 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3732 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3733 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3734 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3735 | output1 + output1_length, |
| 3736 | output1_buffer_size - output1_length, |
| 3737 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3738 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3739 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3740 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3741 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3742 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3743 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3744 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3745 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3746 | iv, iv_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_update( &operation2, output1, first_part_size, |
| 3749 | output2, output2_buffer_size, |
| 3750 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3751 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3752 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3753 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3754 | output1 + first_part_size, |
| 3755 | output1_length - first_part_size, |
| 3756 | output2, output2_buffer_size, |
| 3757 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3758 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3759 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3760 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3761 | output2 + output2_length, |
| 3762 | output2_buffer_size - output2_length, |
| 3763 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3764 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3765 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3766 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3767 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3768 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3769 | |
| 3770 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3771 | mbedtls_free( output1 ); |
| 3772 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3773 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3774 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3775 | } |
| 3776 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3777 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3778 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3779 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3780 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3781 | data_t *nonce, |
| 3782 | data_t *additional_data, |
| 3783 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3784 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3785 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3786 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3787 | psa_key_type_t key_type = key_type_arg; |
| 3788 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3789 | unsigned char *output_data = NULL; |
| 3790 | size_t output_size = 0; |
| 3791 | size_t output_length = 0; |
| 3792 | unsigned char *output_data2 = NULL; |
| 3793 | size_t output_length2 = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3794 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3795 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3796 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3797 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3798 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3799 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3800 | * should be exact. */ |
| 3801 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3802 | TEST_EQUAL( output_size, |
| 3803 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3804 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3805 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3806 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3807 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3808 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3809 | psa_set_key_algorithm( &attributes, alg ); |
| 3810 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3811 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3812 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3813 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3814 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3815 | TEST_EQUAL( psa_aead_encrypt( handle, alg, |
| 3816 | nonce->x, nonce->len, |
| 3817 | additional_data->x, |
| 3818 | additional_data->len, |
| 3819 | input_data->x, input_data->len, |
| 3820 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3821 | &output_length ), |
| 3822 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3823 | |
| 3824 | if( PSA_SUCCESS == expected_result ) |
| 3825 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3826 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3827 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3828 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3829 | * should be exact. */ |
| 3830 | TEST_EQUAL( input_data->len, |
| 3831 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) ); |
| 3832 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3833 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3834 | nonce->x, nonce->len, |
| 3835 | additional_data->x, |
| 3836 | additional_data->len, |
| 3837 | output_data, output_length, |
| 3838 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3839 | &output_length2 ), |
| 3840 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3841 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3842 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3843 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3844 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3845 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3846 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3847 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3848 | mbedtls_free( output_data ); |
| 3849 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3850 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3851 | } |
| 3852 | /* END_CASE */ |
| 3853 | |
| 3854 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3855 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3856 | int alg_arg, |
| 3857 | data_t *nonce, |
| 3858 | data_t *additional_data, |
| 3859 | data_t *input_data, |
| 3860 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3861 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3862 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3863 | psa_key_type_t key_type = key_type_arg; |
| 3864 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3865 | unsigned char *output_data = NULL; |
| 3866 | size_t output_size = 0; |
| 3867 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3868 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3869 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3870 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3871 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3872 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3873 | * should be exact. */ |
| 3874 | TEST_EQUAL( output_size, |
| 3875 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3876 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3877 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3878 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3879 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3880 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3881 | psa_set_key_algorithm( &attributes, alg ); |
| 3882 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3883 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3884 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3885 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3886 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3887 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 3888 | nonce->x, nonce->len, |
| 3889 | additional_data->x, additional_data->len, |
| 3890 | input_data->x, input_data->len, |
| 3891 | output_data, output_size, |
| 3892 | &output_length ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3893 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3894 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3895 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3896 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3897 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3898 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3899 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3900 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3901 | } |
| 3902 | /* END_CASE */ |
| 3903 | |
| 3904 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3905 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3906 | int alg_arg, |
| 3907 | data_t *nonce, |
| 3908 | data_t *additional_data, |
| 3909 | data_t *input_data, |
| 3910 | data_t *expected_data, |
| 3911 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3912 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3913 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3914 | psa_key_type_t key_type = key_type_arg; |
| 3915 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3916 | unsigned char *output_data = NULL; |
| 3917 | size_t output_size = 0; |
| 3918 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3919 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3920 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3921 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3922 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3923 | output_size = input_data->len - tag_length; |
| 3924 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3925 | * should be exact. */ |
| 3926 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3927 | TEST_EQUAL( output_size, |
| 3928 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3929 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3930 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3931 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3932 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3933 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3934 | psa_set_key_algorithm( &attributes, alg ); |
| 3935 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3936 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3937 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3938 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3939 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3940 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3941 | nonce->x, nonce->len, |
| 3942 | additional_data->x, |
| 3943 | additional_data->len, |
| 3944 | input_data->x, input_data->len, |
| 3945 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3946 | &output_length ), |
| 3947 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3948 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3949 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3950 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3951 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3952 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3953 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3954 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3955 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3956 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3957 | } |
| 3958 | /* END_CASE */ |
| 3959 | |
| 3960 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3961 | void signature_size( int type_arg, |
| 3962 | int bits, |
| 3963 | int alg_arg, |
| 3964 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3965 | { |
| 3966 | psa_key_type_t type = type_arg; |
| 3967 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3968 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3969 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3970 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3971 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3972 | TEST_EQUAL( actual_size, |
| 3973 | PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) ); |
| 3974 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3975 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3976 | exit: |
| 3977 | ; |
| 3978 | } |
| 3979 | /* END_CASE */ |
| 3980 | |
| 3981 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3982 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 3983 | int alg_arg, data_t *input_data, |
| 3984 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3985 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3986 | psa_key_handle_t handle = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3987 | psa_key_type_t key_type = key_type_arg; |
| 3988 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3989 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3990 | unsigned char *signature = NULL; |
| 3991 | size_t signature_size; |
| 3992 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3993 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3994 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3995 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3996 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3997 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3998 | psa_set_key_algorithm( &attributes, alg ); |
| 3999 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4000 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4001 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4002 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4003 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4004 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4005 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4006 | /* Allocate a buffer which has the size advertized by the |
| 4007 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4008 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4009 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4010 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4011 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4012 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4013 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4014 | /* Perform the signature. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4015 | PSA_ASSERT( psa_sign_hash( handle, alg, |
| 4016 | input_data->x, input_data->len, |
| 4017 | signature, signature_size, |
| 4018 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4019 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4020 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4021 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4022 | |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4023 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4024 | memset( signature, 0, signature_size ); |
| 4025 | signature_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4026 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 4027 | input_data->x, input_data->len, |
| 4028 | signature, signature_size, |
| 4029 | &signature_length ) ); |
| 4030 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4031 | signature, signature_length ); |
| 4032 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4033 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4034 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4035 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4036 | psa_destroy_key( handle ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 4037 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4038 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4039 | } |
| 4040 | /* END_CASE */ |
| 4041 | |
| 4042 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4043 | void sign_fail( int key_type_arg, data_t *key_data, |
| 4044 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4045 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4046 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4047 | psa_key_handle_t handle = 0; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4048 | psa_key_type_t key_type = key_type_arg; |
| 4049 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4050 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4051 | psa_status_t actual_status; |
| 4052 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 4053 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 4054 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4055 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4056 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4057 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4058 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4059 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4060 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4061 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4062 | psa_set_key_algorithm( &attributes, alg ); |
| 4063 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4064 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4065 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4066 | &handle ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4067 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4068 | actual_status = psa_sign_hash( handle, alg, |
| 4069 | input_data->x, input_data->len, |
| 4070 | signature, signature_size, |
| 4071 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4072 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4073 | /* The value of *signature_length is unspecified on error, but |
| 4074 | * whatever it is, it should be less than signature_size, so that |
| 4075 | * if the caller tries to read *signature_length bytes without |
| 4076 | * checking the error code then they don't overflow a buffer. */ |
| 4077 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4078 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4079 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4080 | signature_length = INVALID_EXPORT_LENGTH; |
| 4081 | TEST_EQUAL( psa_asymmetric_sign( handle, alg, |
| 4082 | input_data->x, input_data->len, |
| 4083 | signature, signature_size, |
| 4084 | &signature_length ), |
| 4085 | expected_status ); |
| 4086 | TEST_ASSERT( signature_length <= signature_size ); |
| 4087 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4088 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4089 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4090 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4091 | psa_destroy_key( handle ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4092 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4093 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4094 | } |
| 4095 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 4096 | |
| 4097 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4098 | void sign_verify( int key_type_arg, data_t *key_data, |
| 4099 | int alg_arg, data_t *input_data ) |
| 4100 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4101 | psa_key_handle_t handle = 0; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4102 | psa_key_type_t key_type = key_type_arg; |
| 4103 | psa_algorithm_t alg = alg_arg; |
| 4104 | size_t key_bits; |
| 4105 | unsigned char *signature = NULL; |
| 4106 | size_t signature_size; |
| 4107 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4108 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4109 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4110 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4111 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4112 | 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] | 4113 | psa_set_key_algorithm( &attributes, alg ); |
| 4114 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4115 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4116 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4117 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4118 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4119 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4120 | |
| 4121 | /* Allocate a buffer which has the size advertized by the |
| 4122 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4123 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4124 | key_bits, alg ); |
| 4125 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4126 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4127 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4128 | |
| 4129 | /* Perform the signature. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4130 | PSA_ASSERT( psa_sign_hash( handle, alg, |
| 4131 | input_data->x, input_data->len, |
| 4132 | signature, signature_size, |
| 4133 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4134 | /* Check that the signature length looks sensible. */ |
| 4135 | TEST_ASSERT( signature_length <= signature_size ); |
| 4136 | TEST_ASSERT( signature_length > 0 ); |
| 4137 | |
| 4138 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4139 | PSA_ASSERT( psa_verify_hash( handle, alg, |
| 4140 | input_data->x, input_data->len, |
| 4141 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4142 | |
| 4143 | if( input_data->len != 0 ) |
| 4144 | { |
| 4145 | /* Flip a bit in the input and verify that the signature is now |
| 4146 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4147 | * because ECDSA may ignore the last few bits of the input. */ |
| 4148 | input_data->x[0] ^= 1; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4149 | TEST_EQUAL( psa_verify_hash( handle, alg, |
| 4150 | input_data->x, input_data->len, |
| 4151 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4152 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4156 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4157 | psa_destroy_key( handle ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4158 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4159 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4160 | } |
| 4161 | /* END_CASE */ |
| 4162 | |
| 4163 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4164 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 4165 | int alg_arg, data_t *hash_data, |
| 4166 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4167 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4168 | psa_key_handle_t handle = 0; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4169 | psa_key_type_t key_type = key_type_arg; |
| 4170 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4171 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4172 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4173 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 4174 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4175 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4176 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4177 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4178 | psa_set_key_algorithm( &attributes, alg ); |
| 4179 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4180 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4181 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4182 | &handle ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4183 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4184 | PSA_ASSERT( psa_verify_hash( handle, alg, |
| 4185 | hash_data->x, hash_data->len, |
| 4186 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4187 | |
| 4188 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4189 | PSA_ASSERT( psa_asymmetric_verify( handle, alg, |
| 4190 | hash_data->x, hash_data->len, |
| 4191 | signature_data->x, |
| 4192 | signature_data->len ) ); |
| 4193 | |
| 4194 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4195 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4196 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4197 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4198 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4199 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4200 | } |
| 4201 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4202 | |
| 4203 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4204 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 4205 | int alg_arg, data_t *hash_data, |
| 4206 | data_t *signature_data, |
| 4207 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4208 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4209 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4210 | psa_key_type_t key_type = key_type_arg; |
| 4211 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4212 | psa_status_t actual_status; |
| 4213 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4214 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4215 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4216 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4217 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4218 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4219 | psa_set_key_algorithm( &attributes, alg ); |
| 4220 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4221 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4222 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4223 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4224 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4225 | actual_status = psa_verify_hash( handle, alg, |
| 4226 | hash_data->x, hash_data->len, |
| 4227 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4228 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4229 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4230 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4231 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 4232 | hash_data->x, hash_data->len, |
| 4233 | signature_data->x, signature_data->len ), |
| 4234 | expected_status ); |
| 4235 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4236 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4237 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4238 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4239 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4240 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4241 | } |
| 4242 | /* END_CASE */ |
| 4243 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4244 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4245 | void asymmetric_encrypt( int key_type_arg, |
| 4246 | data_t *key_data, |
| 4247 | int alg_arg, |
| 4248 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4249 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4250 | int expected_output_length_arg, |
| 4251 | int expected_status_arg ) |
| 4252 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4253 | psa_key_handle_t handle = 0; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4254 | psa_key_type_t key_type = key_type_arg; |
| 4255 | psa_algorithm_t alg = alg_arg; |
| 4256 | size_t expected_output_length = expected_output_length_arg; |
| 4257 | size_t key_bits; |
| 4258 | unsigned char *output = NULL; |
| 4259 | size_t output_size; |
| 4260 | size_t output_length = ~0; |
| 4261 | psa_status_t actual_status; |
| 4262 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4263 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4264 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4265 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4266 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4267 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4268 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4269 | psa_set_key_algorithm( &attributes, alg ); |
| 4270 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4271 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4272 | &handle ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4273 | |
| 4274 | /* Determine the maximum output length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4275 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4276 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4277 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4278 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4279 | |
| 4280 | /* Encrypt the input */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4281 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4282 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4283 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4284 | output, output_size, |
| 4285 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4286 | TEST_EQUAL( actual_status, expected_status ); |
| 4287 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4288 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4289 | /* If the label is empty, the test framework puts a non-null pointer |
| 4290 | * in label->x. Test that a null pointer works as well. */ |
| 4291 | if( label->len == 0 ) |
| 4292 | { |
| 4293 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4294 | if( output_size != 0 ) |
| 4295 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4296 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4297 | input_data->x, input_data->len, |
| 4298 | NULL, label->len, |
| 4299 | output, output_size, |
| 4300 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4301 | TEST_EQUAL( actual_status, expected_status ); |
| 4302 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4303 | } |
| 4304 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4305 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4306 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4307 | psa_destroy_key( handle ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4308 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4309 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4310 | } |
| 4311 | /* END_CASE */ |
| 4312 | |
| 4313 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4314 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4315 | data_t *key_data, |
| 4316 | int alg_arg, |
| 4317 | data_t *input_data, |
| 4318 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4319 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4320 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4321 | psa_key_type_t key_type = key_type_arg; |
| 4322 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4323 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4324 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4325 | size_t output_size; |
| 4326 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4327 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4328 | size_t output2_size; |
| 4329 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4330 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4331 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4332 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4333 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4334 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4335 | psa_set_key_algorithm( &attributes, alg ); |
| 4336 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4337 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4338 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4339 | &handle ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4340 | |
| 4341 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4342 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4343 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4344 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4345 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4346 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4347 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4348 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4349 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4350 | * the original plaintext because of the non-optional random |
| 4351 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4352 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 4353 | input_data->x, input_data->len, |
| 4354 | label->x, label->len, |
| 4355 | output, output_size, |
| 4356 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4357 | /* We don't know what ciphertext length to expect, but check that |
| 4358 | * it looks sensible. */ |
| 4359 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4360 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4361 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4362 | output, output_length, |
| 4363 | label->x, label->len, |
| 4364 | output2, output2_size, |
| 4365 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4366 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4367 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4368 | |
| 4369 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4370 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4371 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4372 | mbedtls_free( output ); |
| 4373 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4374 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4375 | } |
| 4376 | /* END_CASE */ |
| 4377 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4378 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4379 | void asymmetric_decrypt( int key_type_arg, |
| 4380 | data_t *key_data, |
| 4381 | int alg_arg, |
| 4382 | data_t *input_data, |
| 4383 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4384 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4385 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4386 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4387 | psa_key_type_t key_type = key_type_arg; |
| 4388 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4389 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4390 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4391 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4392 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4393 | |
Jaeden Amero | 412654a | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4394 | output_size = expected_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4395 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4396 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4397 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4398 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4399 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4400 | psa_set_key_algorithm( &attributes, alg ); |
| 4401 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4402 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4403 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4404 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4405 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4406 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4407 | input_data->x, input_data->len, |
| 4408 | label->x, label->len, |
| 4409 | output, |
| 4410 | output_size, |
| 4411 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4412 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4413 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4414 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4415 | /* If the label is empty, the test framework puts a non-null pointer |
| 4416 | * in label->x. Test that a null pointer works as well. */ |
| 4417 | if( label->len == 0 ) |
| 4418 | { |
| 4419 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4420 | if( output_size != 0 ) |
| 4421 | memset( output, 0, output_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4422 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4423 | input_data->x, input_data->len, |
| 4424 | NULL, label->len, |
| 4425 | output, |
| 4426 | output_size, |
| 4427 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4428 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4429 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4430 | } |
| 4431 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4432 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4433 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4434 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4435 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4436 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4437 | } |
| 4438 | /* END_CASE */ |
| 4439 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4440 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4441 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4442 | data_t *key_data, |
| 4443 | int alg_arg, |
| 4444 | data_t *input_data, |
| 4445 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4446 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4447 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4448 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4449 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4450 | psa_key_type_t key_type = key_type_arg; |
| 4451 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4452 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4453 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4454 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4455 | psa_status_t actual_status; |
| 4456 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4457 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4458 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4459 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4460 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4461 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4462 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4463 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4464 | psa_set_key_algorithm( &attributes, alg ); |
| 4465 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4466 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4467 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4468 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4469 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4470 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4471 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4472 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4473 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4474 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4475 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4476 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4477 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4478 | /* If the label is empty, the test framework puts a non-null pointer |
| 4479 | * in label->x. Test that a null pointer works as well. */ |
| 4480 | if( label->len == 0 ) |
| 4481 | { |
| 4482 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4483 | if( output_size != 0 ) |
| 4484 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4485 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4486 | input_data->x, input_data->len, |
| 4487 | NULL, label->len, |
| 4488 | output, output_size, |
| 4489 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4490 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4491 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4492 | } |
| 4493 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4494 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4495 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4496 | psa_destroy_key( handle ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4497 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4498 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4499 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4500 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4501 | |
| 4502 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4503 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4504 | { |
| 4505 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4506 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4507 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4508 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4509 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4510 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4511 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4512 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4513 | |
| 4514 | memset( &zero, 0, sizeof( zero ) ); |
| 4515 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4516 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4517 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4518 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4519 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4520 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4521 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4522 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4523 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4524 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4525 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4526 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4527 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4528 | } |
| 4529 | /* END_CASE */ |
| 4530 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4531 | /* BEGIN_CASE */ |
| 4532 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4533 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4534 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4535 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4536 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4537 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4538 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4539 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4540 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4541 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4542 | |
| 4543 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4544 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4545 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4546 | } |
| 4547 | /* END_CASE */ |
| 4548 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4549 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4550 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4551 | int expected_status_arg ) |
| 4552 | { |
| 4553 | psa_algorithm_t alg = alg_arg; |
| 4554 | size_t capacity = capacity_arg; |
| 4555 | psa_status_t expected_status = expected_status_arg; |
| 4556 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4557 | |
| 4558 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4559 | |
| 4560 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4561 | |
| 4562 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4563 | expected_status ); |
| 4564 | |
| 4565 | exit: |
| 4566 | psa_key_derivation_abort( &operation ); |
| 4567 | PSA_DONE( ); |
| 4568 | } |
| 4569 | /* END_CASE */ |
| 4570 | |
| 4571 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4572 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4573 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4574 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4575 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4576 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4577 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4578 | int expected_status_arg3, |
| 4579 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4580 | { |
| 4581 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4582 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 4583 | 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] | 4584 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4585 | expected_status_arg2, |
| 4586 | expected_status_arg3}; |
| 4587 | data_t *inputs[] = {input1, input2, input3}; |
| 4588 | psa_key_handle_t handles[] = {0, 0, 0}; |
| 4589 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4590 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4591 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4592 | psa_key_type_t output_key_type = output_key_type_arg; |
| 4593 | psa_key_handle_t output_handle = 0; |
| 4594 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4595 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4596 | |
| 4597 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4598 | |
| 4599 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4600 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4601 | |
| 4602 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4603 | |
| 4604 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4605 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4606 | if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4607 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4608 | psa_set_key_type( &attributes, key_types[i] ); |
| 4609 | PSA_ASSERT( psa_import_key( &attributes, |
| 4610 | inputs[i]->x, inputs[i]->len, |
| 4611 | &handles[i] ) ); |
| 4612 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
| 4613 | handles[i] ), |
| 4614 | expected_statuses[i] ); |
| 4615 | } |
| 4616 | else |
| 4617 | { |
| 4618 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4619 | &operation, steps[i], |
| 4620 | inputs[i]->x, inputs[i]->len ), |
| 4621 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4622 | } |
| 4623 | } |
| 4624 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4625 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 4626 | { |
| 4627 | psa_reset_key_attributes( &attributes ); |
| 4628 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 4629 | psa_set_key_bits( &attributes, 8 ); |
| 4630 | actual_output_status = |
| 4631 | psa_key_derivation_output_key( &attributes, &operation, |
| 4632 | &output_handle ); |
| 4633 | } |
| 4634 | else |
| 4635 | { |
| 4636 | uint8_t buffer[1]; |
| 4637 | actual_output_status = |
| 4638 | psa_key_derivation_output_bytes( &operation, |
| 4639 | buffer, sizeof( buffer ) ); |
| 4640 | } |
| 4641 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 4642 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4643 | exit: |
| 4644 | psa_key_derivation_abort( &operation ); |
| 4645 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4646 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4647 | psa_destroy_key( output_handle ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4648 | PSA_DONE( ); |
| 4649 | } |
| 4650 | /* END_CASE */ |
| 4651 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4652 | /* BEGIN_CASE */ |
| 4653 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4654 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4655 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4656 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4657 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4658 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4659 | unsigned char input1[] = "Input 1"; |
| 4660 | size_t input1_length = sizeof( input1 ); |
| 4661 | unsigned char input2[] = "Input 2"; |
| 4662 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4663 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4664 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4665 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4666 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4667 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4668 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4669 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4670 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4671 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4672 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4673 | psa_set_key_algorithm( &attributes, alg ); |
| 4674 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4675 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4676 | PSA_ASSERT( psa_import_key( &attributes, |
| 4677 | key_data, sizeof( key_data ), |
| 4678 | &handle ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4679 | |
| 4680 | /* valid key derivation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4681 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4682 | input1, input1_length, |
| 4683 | input2, input2_length, |
| 4684 | capacity ) ) |
| 4685 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4686 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4687 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4688 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4689 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4690 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4691 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4692 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4693 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4694 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4695 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4696 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4697 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4698 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4699 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4700 | } |
| 4701 | /* END_CASE */ |
| 4702 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4703 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4704 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4705 | { |
| 4706 | uint8_t output_buffer[16]; |
| 4707 | size_t buffer_size = 16; |
| 4708 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4709 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4710 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4711 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4712 | output_buffer, buffer_size ) |
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 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4715 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4716 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4717 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4718 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4719 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4720 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4721 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4722 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4723 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4724 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4725 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4726 | |
| 4727 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4728 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4729 | } |
| 4730 | /* END_CASE */ |
| 4731 | |
| 4732 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4733 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4734 | int step1_arg, data_t *input1, |
| 4735 | int step2_arg, data_t *input2, |
| 4736 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4737 | int requested_capacity_arg, |
| 4738 | data_t *expected_output1, |
| 4739 | data_t *expected_output2 ) |
| 4740 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4741 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4742 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4743 | data_t *inputs[] = {input1, input2, input3}; |
| 4744 | psa_key_handle_t handles[] = {0, 0, 0}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4745 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4746 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4747 | uint8_t *expected_outputs[2] = |
| 4748 | {expected_output1->x, expected_output2->x}; |
| 4749 | size_t output_sizes[2] = |
| 4750 | {expected_output1->len, expected_output2->len}; |
| 4751 | size_t output_buffer_size = 0; |
| 4752 | uint8_t *output_buffer = NULL; |
| 4753 | size_t expected_capacity; |
| 4754 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4755 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4756 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4757 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4758 | |
| 4759 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4760 | { |
| 4761 | if( output_sizes[i] > output_buffer_size ) |
| 4762 | output_buffer_size = output_sizes[i]; |
| 4763 | if( output_sizes[i] == 0 ) |
| 4764 | expected_outputs[i] = NULL; |
| 4765 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4766 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4767 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4768 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4769 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4770 | psa_set_key_algorithm( &attributes, alg ); |
| 4771 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4772 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4773 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4774 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4775 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4776 | requested_capacity ) ); |
| 4777 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4778 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4779 | switch( steps[i] ) |
| 4780 | { |
| 4781 | case 0: |
| 4782 | break; |
| 4783 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4784 | PSA_ASSERT( psa_import_key( &attributes, |
| 4785 | inputs[i]->x, inputs[i]->len, |
| 4786 | &handles[i] ) ); |
| 4787 | PSA_ASSERT( psa_key_derivation_input_key( |
| 4788 | &operation, steps[i], |
| 4789 | handles[i] ) ); |
| 4790 | break; |
| 4791 | default: |
| 4792 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4793 | &operation, steps[i], |
| 4794 | inputs[i]->x, inputs[i]->len ) ); |
| 4795 | break; |
| 4796 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4797 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4798 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4799 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4800 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4801 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4802 | expected_capacity = requested_capacity; |
| 4803 | |
| 4804 | /* Expansion phase. */ |
| 4805 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4806 | { |
| 4807 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4808 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4809 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4810 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4811 | { |
| 4812 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4813 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4814 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4815 | continue; |
| 4816 | } |
| 4817 | else if( expected_capacity == 0 || |
| 4818 | output_sizes[i] > expected_capacity ) |
| 4819 | { |
| 4820 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4821 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4822 | expected_capacity = 0; |
| 4823 | continue; |
| 4824 | } |
| 4825 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4826 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4827 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4828 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4829 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4830 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4831 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4832 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4833 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4834 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4835 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4836 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4837 | |
| 4838 | exit: |
| 4839 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4840 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4841 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4842 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4843 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4844 | } |
| 4845 | /* END_CASE */ |
| 4846 | |
| 4847 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4848 | void derive_full( int alg_arg, |
| 4849 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4850 | data_t *input1, |
| 4851 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4852 | int requested_capacity_arg ) |
| 4853 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4854 | psa_key_handle_t handle = 0; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4855 | psa_algorithm_t alg = alg_arg; |
| 4856 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4857 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4858 | unsigned char output_buffer[16]; |
| 4859 | size_t expected_capacity = requested_capacity; |
| 4860 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4861 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4862 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4863 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4864 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4865 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4866 | psa_set_key_algorithm( &attributes, alg ); |
| 4867 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4868 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4869 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4870 | &handle ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4871 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4872 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4873 | input1->x, input1->len, |
| 4874 | input2->x, input2->len, |
| 4875 | requested_capacity ) ) |
| 4876 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4877 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4878 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4879 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4880 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4881 | |
| 4882 | /* Expansion phase. */ |
| 4883 | while( current_capacity > 0 ) |
| 4884 | { |
| 4885 | size_t read_size = sizeof( output_buffer ); |
| 4886 | if( read_size > current_capacity ) |
| 4887 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4888 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4889 | output_buffer, |
| 4890 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4891 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4892 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4893 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4894 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4895 | } |
| 4896 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4897 | /* Check that the operation refuses to go over capacity. */ |
| 4898 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4899 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4900 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4901 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4902 | |
| 4903 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4904 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4905 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4906 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4907 | } |
| 4908 | /* END_CASE */ |
| 4909 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4910 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4911 | void derive_key_exercise( int alg_arg, |
| 4912 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4913 | data_t *input1, |
| 4914 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4915 | int derived_type_arg, |
| 4916 | int derived_bits_arg, |
| 4917 | int derived_usage_arg, |
| 4918 | int derived_alg_arg ) |
| 4919 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4920 | psa_key_handle_t base_handle = 0; |
| 4921 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4922 | psa_algorithm_t alg = alg_arg; |
| 4923 | psa_key_type_t derived_type = derived_type_arg; |
| 4924 | size_t derived_bits = derived_bits_arg; |
| 4925 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4926 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4927 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4928 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4929 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4930 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4931 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4932 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4933 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4934 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4935 | psa_set_key_algorithm( &attributes, alg ); |
| 4936 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4937 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4938 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4939 | |
| 4940 | /* Derive a key. */ |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4941 | if ( setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4942 | input1->x, input1->len, |
| 4943 | input2->x, input2->len, capacity ) ) |
| 4944 | goto exit; |
| 4945 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4946 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4947 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4948 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4949 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4950 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4951 | &derived_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4952 | |
| 4953 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4954 | PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) ); |
| 4955 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4956 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4957 | |
| 4958 | /* Exercise the derived key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4959 | if( ! exercise_key( derived_handle, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4960 | goto exit; |
| 4961 | |
| 4962 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4963 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4964 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4965 | psa_destroy_key( base_handle ); |
| 4966 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4967 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4968 | } |
| 4969 | /* END_CASE */ |
| 4970 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4971 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4972 | void derive_key_export( int alg_arg, |
| 4973 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4974 | data_t *input1, |
| 4975 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4976 | int bytes1_arg, |
| 4977 | int bytes2_arg ) |
| 4978 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4979 | psa_key_handle_t base_handle = 0; |
| 4980 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4981 | psa_algorithm_t alg = alg_arg; |
| 4982 | size_t bytes1 = bytes1_arg; |
| 4983 | size_t bytes2 = bytes2_arg; |
| 4984 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4985 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4986 | uint8_t *output_buffer = NULL; |
| 4987 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4988 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4989 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4990 | size_t length; |
| 4991 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4992 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4993 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4994 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4995 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4996 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4997 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4998 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4999 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 5000 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5001 | |
| 5002 | /* Derive some material and output it. */ |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5003 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 5004 | input1->x, input1->len, |
| 5005 | input2->x, input2->len, capacity ) ) |
| 5006 | goto exit; |
| 5007 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5008 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5009 | output_buffer, |
| 5010 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5011 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5012 | |
| 5013 | /* 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] | 5014 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 5015 | input1->x, input1->len, |
| 5016 | input2->x, input2->len, capacity ) ) |
| 5017 | goto exit; |
| 5018 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5019 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5020 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 5021 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5022 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5023 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5024 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5025 | PSA_ASSERT( psa_export_key( derived_handle, |
| 5026 | export_buffer, bytes1, |
| 5027 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5028 | TEST_EQUAL( length, bytes1 ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5029 | PSA_ASSERT( psa_destroy_key( derived_handle ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5030 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5031 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5032 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5033 | PSA_ASSERT( psa_export_key( derived_handle, |
| 5034 | export_buffer + bytes1, bytes2, |
| 5035 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5036 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5037 | |
| 5038 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5039 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 5040 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5041 | |
| 5042 | exit: |
| 5043 | mbedtls_free( output_buffer ); |
| 5044 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5045 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5046 | psa_destroy_key( base_handle ); |
| 5047 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5048 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5049 | } |
| 5050 | /* END_CASE */ |
| 5051 | |
| 5052 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5053 | void derive_key( int alg_arg, |
| 5054 | data_t *key_data, data_t *input1, data_t *input2, |
| 5055 | int type_arg, int bits_arg, |
| 5056 | int expected_status_arg ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5057 | { |
| 5058 | psa_key_handle_t base_handle = 0; |
| 5059 | psa_key_handle_t derived_handle = 0; |
| 5060 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5061 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5062 | size_t bits = bits_arg; |
| 5063 | psa_status_t expected_status = expected_status_arg; |
| 5064 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5065 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5066 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5067 | |
| 5068 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5069 | |
| 5070 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5071 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5072 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5073 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 5074 | &base_handle ) ); |
| 5075 | |
| 5076 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 5077 | input1->x, input1->len, |
| 5078 | input2->x, input2->len, SIZE_MAX ) ) |
| 5079 | goto exit; |
| 5080 | |
| 5081 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5082 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5083 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5084 | psa_set_key_bits( &derived_attributes, bits ); |
| 5085 | TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 5086 | &derived_handle ), |
| 5087 | expected_status ); |
| 5088 | |
| 5089 | exit: |
| 5090 | psa_key_derivation_abort( &operation ); |
| 5091 | psa_destroy_key( base_handle ); |
| 5092 | psa_destroy_key( derived_handle ); |
| 5093 | PSA_DONE( ); |
| 5094 | } |
| 5095 | /* END_CASE */ |
| 5096 | |
| 5097 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5098 | void key_agreement_setup( int alg_arg, |
| 5099 | int our_key_type_arg, data_t *our_key_data, |
| 5100 | data_t *peer_key_data, |
| 5101 | int expected_status_arg ) |
| 5102 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5103 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5104 | psa_algorithm_t alg = alg_arg; |
| 5105 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5106 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5107 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5108 | psa_status_t expected_status = expected_status_arg; |
| 5109 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5110 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5111 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5112 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5113 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5114 | psa_set_key_algorithm( &attributes, alg ); |
| 5115 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5116 | PSA_ASSERT( psa_import_key( &attributes, |
| 5117 | our_key_data->x, our_key_data->len, |
| 5118 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5119 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5120 | /* The tests currently include inputs that should fail at either step. |
| 5121 | * Test cases that fail at the setup step should be changed to call |
| 5122 | * key_derivation_setup instead, and this function should be renamed |
| 5123 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5124 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5125 | if( status == PSA_SUCCESS ) |
| 5126 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5127 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 5128 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5129 | our_key, |
| 5130 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5131 | expected_status ); |
| 5132 | } |
| 5133 | else |
| 5134 | { |
| 5135 | TEST_ASSERT( status == expected_status ); |
| 5136 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5137 | |
| 5138 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5139 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5140 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5141 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5142 | } |
| 5143 | /* END_CASE */ |
| 5144 | |
| 5145 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5146 | void raw_key_agreement( int alg_arg, |
| 5147 | int our_key_type_arg, data_t *our_key_data, |
| 5148 | data_t *peer_key_data, |
| 5149 | data_t *expected_output ) |
| 5150 | { |
| 5151 | psa_key_handle_t our_key = 0; |
| 5152 | psa_algorithm_t alg = alg_arg; |
| 5153 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5154 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5155 | unsigned char *output = NULL; |
| 5156 | size_t output_length = ~0; |
| 5157 | |
| 5158 | ASSERT_ALLOC( output, expected_output->len ); |
| 5159 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5160 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5161 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5162 | psa_set_key_algorithm( &attributes, alg ); |
| 5163 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5164 | PSA_ASSERT( psa_import_key( &attributes, |
| 5165 | our_key_data->x, our_key_data->len, |
| 5166 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5167 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5168 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 5169 | peer_key_data->x, peer_key_data->len, |
| 5170 | output, expected_output->len, |
| 5171 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5172 | ASSERT_COMPARE( output, output_length, |
| 5173 | expected_output->x, expected_output->len ); |
| 5174 | |
| 5175 | exit: |
| 5176 | mbedtls_free( output ); |
| 5177 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5178 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5179 | } |
| 5180 | /* END_CASE */ |
| 5181 | |
| 5182 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5183 | void key_agreement_capacity( int alg_arg, |
| 5184 | int our_key_type_arg, data_t *our_key_data, |
| 5185 | data_t *peer_key_data, |
| 5186 | int expected_capacity_arg ) |
| 5187 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5188 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5189 | psa_algorithm_t alg = alg_arg; |
| 5190 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5191 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5192 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5193 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5194 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5195 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5196 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5197 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5198 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5199 | psa_set_key_algorithm( &attributes, alg ); |
| 5200 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5201 | PSA_ASSERT( psa_import_key( &attributes, |
| 5202 | our_key_data->x, our_key_data->len, |
| 5203 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5204 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5205 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5206 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5207 | &operation, |
| 5208 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5209 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5210 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5211 | { |
| 5212 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5213 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5214 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5215 | NULL, 0 ) ); |
| 5216 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5217 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5218 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5219 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5220 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5221 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5222 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5223 | /* Test the actual capacity by reading the output. */ |
| 5224 | while( actual_capacity > sizeof( output ) ) |
| 5225 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5226 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5227 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5228 | actual_capacity -= sizeof( output ); |
| 5229 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5230 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5231 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5232 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5233 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5234 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5235 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5236 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5237 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5238 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5239 | } |
| 5240 | /* END_CASE */ |
| 5241 | |
| 5242 | /* BEGIN_CASE */ |
| 5243 | void key_agreement_output( int alg_arg, |
| 5244 | int our_key_type_arg, data_t *our_key_data, |
| 5245 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5246 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5247 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5248 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5249 | psa_algorithm_t alg = alg_arg; |
| 5250 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5251 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5252 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5253 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5254 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5255 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 5256 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5257 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5258 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5259 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5260 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5261 | psa_set_key_algorithm( &attributes, alg ); |
| 5262 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5263 | PSA_ASSERT( psa_import_key( &attributes, |
| 5264 | our_key_data->x, our_key_data->len, |
| 5265 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5266 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5267 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5268 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5269 | &operation, |
| 5270 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5271 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5272 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5273 | { |
| 5274 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5275 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5276 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5277 | NULL, 0 ) ); |
| 5278 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5279 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5280 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5281 | actual_output, |
| 5282 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5283 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 5284 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5285 | if( expected_output2->len != 0 ) |
| 5286 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5287 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5288 | actual_output, |
| 5289 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5290 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 5291 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5292 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5293 | |
| 5294 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5295 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5296 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5297 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5298 | mbedtls_free( actual_output ); |
| 5299 | } |
| 5300 | /* END_CASE */ |
| 5301 | |
| 5302 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5303 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5304 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5305 | size_t bytes = bytes_arg; |
| 5306 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5307 | unsigned char *output = NULL; |
| 5308 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5309 | size_t i; |
| 5310 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5311 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5312 | TEST_ASSERT( bytes_arg >= 0 ); |
| 5313 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5314 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 5315 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5316 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5317 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5318 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5319 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5320 | /* Run several times, to ensure that every output byte will be |
| 5321 | * nonzero at least once with overwhelming probability |
| 5322 | * (2^(-8*number_of_runs)). */ |
| 5323 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5324 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5325 | if( bytes != 0 ) |
| 5326 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5327 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5328 | |
| 5329 | /* Check that no more than bytes have been overwritten */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5330 | ASSERT_COMPARE( output + bytes, sizeof( trail ), |
| 5331 | trail, sizeof( trail ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5332 | |
| 5333 | for( i = 0; i < bytes; i++ ) |
| 5334 | { |
| 5335 | if( output[i] != 0 ) |
| 5336 | ++changed[i]; |
| 5337 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5338 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5339 | |
| 5340 | /* Check that every byte was changed to nonzero at least once. This |
| 5341 | * validates that psa_generate_random is overwriting every byte of |
| 5342 | * the output buffer. */ |
| 5343 | for( i = 0; i < bytes; i++ ) |
| 5344 | { |
| 5345 | TEST_ASSERT( changed[i] != 0 ); |
| 5346 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5347 | |
| 5348 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5349 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5350 | mbedtls_free( output ); |
| 5351 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5352 | } |
| 5353 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5354 | |
| 5355 | /* BEGIN_CASE */ |
| 5356 | void generate_key( int type_arg, |
| 5357 | int bits_arg, |
| 5358 | int usage_arg, |
| 5359 | int alg_arg, |
| 5360 | int expected_status_arg ) |
| 5361 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5362 | psa_key_handle_t handle = 0; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5363 | psa_key_type_t type = type_arg; |
| 5364 | psa_key_usage_t usage = usage_arg; |
| 5365 | size_t bits = bits_arg; |
| 5366 | psa_algorithm_t alg = alg_arg; |
| 5367 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5368 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5369 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5370 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5371 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5372 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5373 | psa_set_key_usage_flags( &attributes, usage ); |
| 5374 | psa_set_key_algorithm( &attributes, alg ); |
| 5375 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5376 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5377 | |
| 5378 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5379 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5380 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5381 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5382 | |
| 5383 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5384 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 5385 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5386 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5387 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5388 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5389 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5390 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5391 | |
| 5392 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5393 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5394 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5395 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5396 | } |
| 5397 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5398 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5399 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */ |
| 5400 | void generate_key_rsa( int bits_arg, |
| 5401 | data_t *e_arg, |
| 5402 | int expected_status_arg ) |
| 5403 | { |
| 5404 | psa_key_handle_t handle = 0; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5405 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5406 | size_t bits = bits_arg; |
| 5407 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5408 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5409 | psa_status_t expected_status = expected_status_arg; |
| 5410 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5411 | uint8_t *exported = NULL; |
| 5412 | size_t exported_size = |
| 5413 | PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
| 5414 | size_t exported_length = SIZE_MAX; |
| 5415 | uint8_t *e_read_buffer = NULL; |
| 5416 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5417 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5418 | size_t e_read_length = SIZE_MAX; |
| 5419 | |
| 5420 | if( e_arg->len == 0 || |
| 5421 | ( e_arg->len == 3 && |
| 5422 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5423 | { |
| 5424 | is_default_public_exponent = 1; |
| 5425 | e_read_size = 0; |
| 5426 | } |
| 5427 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5428 | ASSERT_ALLOC( exported, exported_size ); |
| 5429 | |
| 5430 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5431 | |
| 5432 | psa_set_key_usage_flags( &attributes, usage ); |
| 5433 | psa_set_key_algorithm( &attributes, alg ); |
| 5434 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5435 | e_arg->x, e_arg->len ) ); |
| 5436 | psa_set_key_bits( &attributes, bits ); |
| 5437 | |
| 5438 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5439 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5440 | if( expected_status != PSA_SUCCESS ) |
| 5441 | goto exit; |
| 5442 | |
| 5443 | /* Test the key information */ |
| 5444 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5445 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5446 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5447 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5448 | e_read_buffer, e_read_size, |
| 5449 | &e_read_length ) ); |
| 5450 | if( is_default_public_exponent ) |
| 5451 | TEST_EQUAL( e_read_length, 0 ); |
| 5452 | else |
| 5453 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5454 | |
| 5455 | /* Do something with the key according to its type and permitted usage. */ |
| 5456 | if( ! exercise_key( handle, usage, alg ) ) |
| 5457 | goto exit; |
| 5458 | |
| 5459 | /* Export the key and check the public exponent. */ |
| 5460 | PSA_ASSERT( psa_export_public_key( handle, |
| 5461 | exported, exported_size, |
| 5462 | &exported_length ) ); |
| 5463 | { |
| 5464 | uint8_t *p = exported; |
| 5465 | uint8_t *end = exported + exported_length; |
| 5466 | size_t len; |
| 5467 | /* RSAPublicKey ::= SEQUENCE { |
| 5468 | * modulus INTEGER, -- n |
| 5469 | * publicExponent INTEGER } -- e |
| 5470 | */ |
| 5471 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5472 | MBEDTLS_ASN1_SEQUENCE | |
| 5473 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5474 | TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
| 5475 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5476 | MBEDTLS_ASN1_INTEGER ) ); |
| 5477 | if( len >= 1 && p[0] == 0 ) |
| 5478 | { |
| 5479 | ++p; |
| 5480 | --len; |
| 5481 | } |
| 5482 | if( e_arg->len == 0 ) |
| 5483 | { |
| 5484 | TEST_EQUAL( len, 3 ); |
| 5485 | TEST_EQUAL( p[0], 1 ); |
| 5486 | TEST_EQUAL( p[1], 0 ); |
| 5487 | TEST_EQUAL( p[2], 1 ); |
| 5488 | } |
| 5489 | else |
| 5490 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5491 | } |
| 5492 | |
| 5493 | exit: |
| 5494 | psa_reset_key_attributes( &attributes ); |
| 5495 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5496 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5497 | mbedtls_free( e_read_buffer ); |
| 5498 | mbedtls_free( exported ); |
| 5499 | } |
| 5500 | /* END_CASE */ |
| 5501 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5502 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5503 | void persistent_key_load_key_from_storage( data_t *data, |
| 5504 | int type_arg, int bits_arg, |
| 5505 | int usage_flags_arg, int alg_arg, |
| 5506 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5507 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5508 | psa_key_id_t key_id = 1; |
| 5509 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5510 | psa_key_handle_t handle = 0; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5511 | psa_key_handle_t base_key = 0; |
| 5512 | psa_key_type_t type = type_arg; |
| 5513 | size_t bits = bits_arg; |
| 5514 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5515 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5516 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5517 | unsigned char *first_export = NULL; |
| 5518 | unsigned char *second_export = NULL; |
| 5519 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 5520 | size_t first_exported_length; |
| 5521 | size_t second_exported_length; |
| 5522 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5523 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5524 | { |
| 5525 | ASSERT_ALLOC( first_export, export_size ); |
| 5526 | ASSERT_ALLOC( second_export, export_size ); |
| 5527 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5528 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5529 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5530 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5531 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5532 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5533 | psa_set_key_algorithm( &attributes, alg ); |
| 5534 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5535 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5536 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5537 | switch( generation_method ) |
| 5538 | { |
| 5539 | case IMPORT_KEY: |
| 5540 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5541 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
| 5542 | &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5543 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5544 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5545 | case GENERATE_KEY: |
| 5546 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5547 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5548 | break; |
| 5549 | |
| 5550 | case DERIVE_KEY: |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5551 | { |
| 5552 | /* Create base key */ |
| 5553 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5554 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5555 | psa_set_key_usage_flags( &base_attributes, |
| 5556 | PSA_KEY_USAGE_DERIVE ); |
| 5557 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5558 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5559 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5560 | data->x, data->len, |
| 5561 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5562 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5563 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5564 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5565 | &operation, |
| 5566 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5567 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5568 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5569 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5570 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5571 | &operation, |
| 5572 | &handle ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5573 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5574 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
| 5575 | base_key = 0; |
| 5576 | } |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5577 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5578 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5579 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5580 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5581 | /* Export the key if permitted by the key policy. */ |
| 5582 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5583 | { |
| 5584 | PSA_ASSERT( psa_export_key( handle, |
| 5585 | first_export, export_size, |
| 5586 | &first_exported_length ) ); |
| 5587 | if( generation_method == IMPORT_KEY ) |
| 5588 | ASSERT_COMPARE( data->x, data->len, |
| 5589 | first_export, first_exported_length ); |
| 5590 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5591 | |
| 5592 | /* Shutdown and restart */ |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 5593 | PSA_ASSERT( psa_close_key( handle ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5594 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5595 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5596 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5597 | /* Check key slot still contains key data */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5598 | PSA_ASSERT( psa_open_key( key_id, &handle ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5599 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5600 | TEST_EQUAL( psa_get_key_id( &attributes ), key_id ); |
| 5601 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5602 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5603 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5604 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5605 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 5606 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5607 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5608 | /* Export the key again if permitted by the key policy. */ |
| 5609 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5610 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5611 | PSA_ASSERT( psa_export_key( handle, |
| 5612 | second_export, export_size, |
| 5613 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5614 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5615 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5619 | if( ! exercise_key( handle, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5620 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5621 | |
| 5622 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5623 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5624 | mbedtls_free( first_export ); |
| 5625 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5626 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5627 | psa_destroy_key( base_key ); |
| 5628 | if( handle == 0 ) |
| 5629 | { |
| 5630 | /* In case there was a test failure after creating the persistent key |
| 5631 | * but while it was not open, try to re-open the persistent key |
| 5632 | * to delete it. */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5633 | psa_open_key( key_id, &handle ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5634 | } |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5635 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5636 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5637 | } |
| 5638 | /* END_CASE */ |