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