Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
| 7 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 8 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 9 | * uses mbedtls_ctr_drbg internally. */ |
| 10 | #include "mbedtls/ctr_drbg.h" |
| 11 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 12 | #include "psa_crypto_helpers.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 13 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 14 | /* Tests that require more than 128kB of RAM plus change have this symbol |
| 15 | * as a dependency. Currently we always define this symbol, so the tests |
| 16 | * are always executed. In the future we should make this conditional |
| 17 | * so that tests that require a lot of memory are skipped on constrained |
| 18 | * platforms. */ |
Gilles Peskine | 49232e8 | 2019-08-07 11:01:30 +0200 | [diff] [blame] | 19 | #define HAVE_RAM_AVAILABLE_128K |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 20 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 21 | #include "psa/crypto.h" |
| 22 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 23 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 24 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 25 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 26 | /* A hash algorithm that is known to be supported. |
| 27 | * |
| 28 | * This is used in some smoke tests. |
| 29 | */ |
| 30 | #if defined(MBEDTLS_MD2_C) |
| 31 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2 |
| 32 | #elif defined(MBEDTLS_MD4_C) |
| 33 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4 |
| 34 | #elif defined(MBEDTLS_MD5_C) |
| 35 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5 |
| 36 | /* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of |
| 37 | * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160 |
| 38 | * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be |
| 39 | * implausible anyway. */ |
| 40 | #elif defined(MBEDTLS_SHA1_C) |
| 41 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1 |
| 42 | #elif defined(MBEDTLS_SHA256_C) |
| 43 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256 |
| 44 | #elif defined(MBEDTLS_SHA512_C) |
| 45 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384 |
| 46 | #elif defined(MBEDTLS_SHA3_C) |
| 47 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256 |
| 48 | #else |
| 49 | #undef KNOWN_SUPPORTED_HASH_ALG |
| 50 | #endif |
| 51 | |
| 52 | /* A block cipher that is known to be supported. |
| 53 | * |
| 54 | * For simplicity's sake, stick to block ciphers with 16-byte blocks. |
| 55 | */ |
| 56 | #if defined(MBEDTLS_AES_C) |
| 57 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES |
| 58 | #elif defined(MBEDTLS_ARIA_C) |
| 59 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA |
| 60 | #elif defined(MBEDTLS_CAMELLIA_C) |
| 61 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA |
| 62 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER |
| 63 | #endif |
| 64 | |
| 65 | /* A MAC mode that is known to be supported. |
| 66 | * |
| 67 | * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or |
| 68 | * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER. |
| 69 | * |
| 70 | * This is used in some smoke tests. |
| 71 | */ |
| 72 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 73 | #define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) ) |
| 74 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC |
| 75 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C) |
| 76 | #define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC |
| 77 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 78 | #else |
| 79 | #undef KNOWN_SUPPORTED_MAC_ALG |
| 80 | #undef KNOWN_SUPPORTED_MAC_KEY_TYPE |
| 81 | #endif |
| 82 | |
| 83 | /* A cipher algorithm and key type that are known to be supported. |
| 84 | * |
| 85 | * This is used in some smoke tests. |
| 86 | */ |
| 87 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR) |
| 88 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR |
| 89 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 90 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING |
| 91 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB) |
| 92 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB |
| 93 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB) |
| 94 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB |
| 95 | #else |
| 96 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 97 | #endif |
| 98 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG) |
| 99 | #define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 100 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 101 | #elif defined(MBEDTLS_RC4_C) |
| 102 | #define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4 |
| 103 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4 |
| 104 | #else |
| 105 | #undef KNOWN_SUPPORTED_CIPHER_ALG |
| 106 | #undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE |
| 107 | #endif |
| 108 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 109 | /** Test if a buffer contains a constant byte value. |
| 110 | * |
| 111 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 112 | * |
| 113 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 114 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 115 | * \param size Size of the buffer in bytes. |
| 116 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 117 | * \return 1 if the buffer is all-bits-zero. |
| 118 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 119 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 120 | static int mem_is_char( void *buffer, unsigned char c, size_t size ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 121 | { |
| 122 | size_t i; |
| 123 | for( i = 0; i < size; i++ ) |
| 124 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 125 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 126 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 127 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 128 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 129 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 130 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 131 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 132 | static int asn1_write_10x( unsigned char **p, |
| 133 | unsigned char *start, |
| 134 | size_t bits, |
| 135 | unsigned char x ) |
| 136 | { |
| 137 | int ret; |
| 138 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 139 | if( bits == 0 ) |
| 140 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 141 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 142 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 143 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 144 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 145 | *p -= len; |
| 146 | ( *p )[len-1] = x; |
| 147 | if( bits % 8 == 0 ) |
| 148 | ( *p )[1] |= 1; |
| 149 | else |
| 150 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 151 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 152 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 153 | MBEDTLS_ASN1_INTEGER ) ); |
| 154 | return( len ); |
| 155 | } |
| 156 | |
| 157 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 158 | size_t buffer_size, |
| 159 | unsigned char **p, |
| 160 | size_t bits, |
| 161 | int keypair ) |
| 162 | { |
| 163 | size_t half_bits = ( bits + 1 ) / 2; |
| 164 | int ret; |
| 165 | int len = 0; |
| 166 | /* Construct something that looks like a DER encoding of |
| 167 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 168 | * RSAPrivateKey ::= SEQUENCE { |
| 169 | * version Version, |
| 170 | * modulus INTEGER, -- n |
| 171 | * publicExponent INTEGER, -- e |
| 172 | * privateExponent INTEGER, -- d |
| 173 | * prime1 INTEGER, -- p |
| 174 | * prime2 INTEGER, -- q |
| 175 | * exponent1 INTEGER, -- d mod (p-1) |
| 176 | * exponent2 INTEGER, -- d mod (q-1) |
| 177 | * coefficient INTEGER, -- (inverse of q) mod p |
| 178 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 179 | * } |
| 180 | * Or, for a public key, the same structure with only |
| 181 | * version, modulus and publicExponent. |
| 182 | */ |
| 183 | *p = buffer + buffer_size; |
| 184 | if( keypair ) |
| 185 | { |
| 186 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 187 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 188 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 189 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 190 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 191 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 192 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 193 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 194 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 195 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 196 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 197 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 198 | } |
| 199 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 200 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 201 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 202 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 203 | if( keypair ) |
| 204 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 205 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 206 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 207 | { |
| 208 | const unsigned char tag = |
| 209 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 210 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 211 | } |
| 212 | return( len ); |
| 213 | } |
| 214 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 215 | int exercise_mac_setup( psa_key_type_t key_type, |
| 216 | const unsigned char *key_bytes, |
| 217 | size_t key_length, |
| 218 | psa_algorithm_t alg, |
| 219 | psa_mac_operation_t *operation, |
| 220 | psa_status_t *status ) |
| 221 | { |
| 222 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 223 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 224 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 225 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 226 | psa_set_key_algorithm( &attributes, alg ); |
| 227 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 228 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 229 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 230 | |
| 231 | *status = psa_mac_sign_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 232 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 233 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 234 | /* If setup failed, reproduce the failure, so that the caller can |
| 235 | * test the resulting state of the operation object. */ |
| 236 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 237 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 238 | TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ), |
| 239 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | psa_destroy_key( handle ); |
| 243 | return( 1 ); |
| 244 | |
| 245 | exit: |
| 246 | psa_destroy_key( handle ); |
| 247 | return( 0 ); |
| 248 | } |
| 249 | |
| 250 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 251 | const unsigned char *key_bytes, |
| 252 | size_t key_length, |
| 253 | psa_algorithm_t alg, |
| 254 | psa_cipher_operation_t *operation, |
| 255 | psa_status_t *status ) |
| 256 | { |
| 257 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 258 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 259 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 260 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 261 | psa_set_key_algorithm( &attributes, alg ); |
| 262 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 263 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 264 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 265 | |
| 266 | *status = psa_cipher_encrypt_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 267 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 268 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 269 | /* If setup failed, reproduce the failure, so that the caller can |
| 270 | * test the resulting state of the operation object. */ |
| 271 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 272 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 273 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ), |
| 274 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | psa_destroy_key( handle ); |
| 278 | return( 1 ); |
| 279 | |
| 280 | exit: |
| 281 | psa_destroy_key( handle ); |
| 282 | return( 0 ); |
| 283 | } |
| 284 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 285 | static int exercise_mac_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 286 | psa_key_usage_t usage, |
| 287 | psa_algorithm_t alg ) |
| 288 | { |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 289 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 290 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 291 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 292 | size_t mac_length = sizeof( mac ); |
| 293 | |
| 294 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 295 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 296 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 297 | handle, alg ) ); |
| 298 | PSA_ASSERT( psa_mac_update( &operation, |
| 299 | input, sizeof( input ) ) ); |
| 300 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 301 | mac, sizeof( mac ), |
| 302 | &mac_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 306 | { |
| 307 | psa_status_t verify_status = |
| 308 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 309 | PSA_SUCCESS : |
| 310 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 311 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 312 | handle, alg ) ); |
| 313 | PSA_ASSERT( psa_mac_update( &operation, |
| 314 | input, sizeof( input ) ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 315 | TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ), |
| 316 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | return( 1 ); |
| 320 | |
| 321 | exit: |
| 322 | psa_mac_abort( &operation ); |
| 323 | return( 0 ); |
| 324 | } |
| 325 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 326 | static int exercise_cipher_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 327 | psa_key_usage_t usage, |
| 328 | psa_algorithm_t alg ) |
| 329 | { |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 330 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 331 | unsigned char iv[16] = {0}; |
| 332 | size_t iv_length = sizeof( iv ); |
| 333 | const unsigned char plaintext[16] = "Hello, world..."; |
| 334 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 335 | size_t ciphertext_length = sizeof( ciphertext ); |
| 336 | unsigned char decrypted[sizeof( ciphertext )]; |
| 337 | size_t part_length; |
| 338 | |
| 339 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 340 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 341 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 342 | handle, alg ) ); |
| 343 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 344 | iv, sizeof( iv ), |
| 345 | &iv_length ) ); |
| 346 | PSA_ASSERT( psa_cipher_update( &operation, |
| 347 | plaintext, sizeof( plaintext ), |
| 348 | ciphertext, sizeof( ciphertext ), |
| 349 | &ciphertext_length ) ); |
| 350 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 351 | ciphertext + ciphertext_length, |
| 352 | sizeof( ciphertext ) - ciphertext_length, |
| 353 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 354 | ciphertext_length += part_length; |
| 355 | } |
| 356 | |
| 357 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 358 | { |
| 359 | psa_status_t status; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 360 | int maybe_invalid_padding = 0; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 361 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 362 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 363 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 364 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 365 | /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't |
| 366 | * have this macro yet. */ |
| 367 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( |
| 368 | psa_get_key_type( &attributes ) ); |
| 369 | maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 370 | } |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 371 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 372 | handle, alg ) ); |
| 373 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 374 | iv, iv_length ) ); |
| 375 | PSA_ASSERT( psa_cipher_update( &operation, |
| 376 | ciphertext, ciphertext_length, |
| 377 | decrypted, sizeof( decrypted ), |
| 378 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 379 | status = psa_cipher_finish( &operation, |
| 380 | decrypted + part_length, |
| 381 | sizeof( decrypted ) - part_length, |
| 382 | &part_length ); |
| 383 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 384 | * if the input is some aribtrary data rather than an actual |
| 385 | ciphertext, a padding error is likely. */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 386 | if( maybe_invalid_padding ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 387 | TEST_ASSERT( status == PSA_SUCCESS || |
| 388 | status == PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 389 | else |
| 390 | PSA_ASSERT( status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return( 1 ); |
| 394 | |
| 395 | exit: |
| 396 | psa_cipher_abort( &operation ); |
| 397 | return( 0 ); |
| 398 | } |
| 399 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 400 | static int exercise_aead_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 401 | psa_key_usage_t usage, |
| 402 | psa_algorithm_t alg ) |
| 403 | { |
| 404 | unsigned char nonce[16] = {0}; |
| 405 | size_t nonce_length = sizeof( nonce ); |
| 406 | unsigned char plaintext[16] = "Hello, world..."; |
| 407 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 408 | size_t ciphertext_length = sizeof( ciphertext ); |
| 409 | size_t plaintext_length = sizeof( ciphertext ); |
| 410 | |
| 411 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 412 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 413 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 414 | nonce, nonce_length, |
| 415 | NULL, 0, |
| 416 | plaintext, sizeof( plaintext ), |
| 417 | ciphertext, sizeof( ciphertext ), |
| 418 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 422 | { |
| 423 | psa_status_t verify_status = |
| 424 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 425 | PSA_SUCCESS : |
| 426 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 427 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 428 | nonce, nonce_length, |
| 429 | NULL, 0, |
| 430 | ciphertext, ciphertext_length, |
| 431 | plaintext, sizeof( plaintext ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 432 | &plaintext_length ), |
| 433 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | return( 1 ); |
| 437 | |
| 438 | exit: |
| 439 | return( 0 ); |
| 440 | } |
| 441 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 442 | static int exercise_signature_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 443 | psa_key_usage_t usage, |
| 444 | psa_algorithm_t alg ) |
| 445 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 446 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 447 | size_t payload_length = 16; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 448 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 449 | size_t signature_length = sizeof( signature ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 450 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 451 | |
| 452 | /* If the policy allows signing with any hash, just pick one. */ |
| 453 | if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH ) |
| 454 | { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 455 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 456 | hash_alg = KNOWN_SUPPORTED_HASH_ALG; |
| 457 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 458 | #else |
| 459 | test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 460 | return( 1 ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 461 | #endif |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 462 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 463 | |
| 464 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 465 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 466 | /* Some algorithms require the payload to have the size of |
| 467 | * the hash encoded in the algorithm. Use this input size |
| 468 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 469 | if( hash_alg != 0 ) |
| 470 | payload_length = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 471 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 472 | payload, payload_length, |
| 473 | signature, sizeof( signature ), |
| 474 | &signature_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 478 | { |
| 479 | psa_status_t verify_status = |
| 480 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 481 | PSA_SUCCESS : |
| 482 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 483 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 484 | payload, payload_length, |
| 485 | signature, signature_length ), |
| 486 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | return( 1 ); |
| 490 | |
| 491 | exit: |
| 492 | return( 0 ); |
| 493 | } |
| 494 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 495 | static int exercise_asymmetric_encryption_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 496 | psa_key_usage_t usage, |
| 497 | psa_algorithm_t alg ) |
| 498 | { |
| 499 | unsigned char plaintext[256] = "Hello, world..."; |
| 500 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 501 | size_t ciphertext_length = sizeof( ciphertext ); |
| 502 | size_t plaintext_length = 16; |
| 503 | |
| 504 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 505 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 506 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 507 | plaintext, plaintext_length, |
| 508 | NULL, 0, |
| 509 | ciphertext, sizeof( ciphertext ), |
| 510 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 514 | { |
| 515 | psa_status_t status = |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 516 | psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 517 | ciphertext, ciphertext_length, |
| 518 | NULL, 0, |
| 519 | plaintext, sizeof( plaintext ), |
| 520 | &plaintext_length ); |
| 521 | TEST_ASSERT( status == PSA_SUCCESS || |
| 522 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 523 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 524 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 525 | } |
| 526 | |
| 527 | return( 1 ); |
| 528 | |
| 529 | exit: |
| 530 | return( 0 ); |
| 531 | } |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 532 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 533 | static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation, |
| 534 | psa_key_handle_t handle, |
| 535 | psa_algorithm_t alg, |
| 536 | unsigned char* input1, size_t input1_length, |
| 537 | unsigned char* input2, size_t input2_length, |
| 538 | size_t capacity ) |
| 539 | { |
| 540 | PSA_ASSERT( psa_key_derivation_setup( operation, alg ) ); |
| 541 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 542 | { |
| 543 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 544 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 545 | input1, input1_length ) ); |
| 546 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 547 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 548 | handle ) ); |
| 549 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 550 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 551 | input2, |
| 552 | input2_length ) ); |
| 553 | } |
| 554 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 555 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 556 | { |
| 557 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 558 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 559 | input1, input1_length ) ); |
| 560 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 561 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 562 | handle ) ); |
| 563 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 564 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 565 | input2, input2_length ) ); |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | TEST_ASSERT( ! "Key derivation algorithm not supported" ); |
| 570 | } |
| 571 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 572 | if( capacity != SIZE_MAX ) |
| 573 | PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) ); |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 574 | |
| 575 | return( 1 ); |
| 576 | |
| 577 | exit: |
| 578 | return( 0 ); |
| 579 | } |
| 580 | |
| 581 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 582 | static int exercise_key_derivation_key( psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 583 | psa_key_usage_t usage, |
| 584 | psa_algorithm_t alg ) |
| 585 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 586 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 587 | unsigned char input1[] = "Input 1"; |
| 588 | size_t input1_length = sizeof( input1 ); |
| 589 | unsigned char input2[] = "Input 2"; |
| 590 | size_t input2_length = sizeof( input2 ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 591 | unsigned char output[1]; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 592 | size_t capacity = sizeof( output ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 593 | |
| 594 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 595 | { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 596 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 597 | input1, input1_length, |
| 598 | input2, input2_length, capacity ) ) |
| 599 | goto exit; |
Gilles Peskine | 7607cd6 | 2019-05-29 17:35:00 +0200 | [diff] [blame] | 600 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 601 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 602 | output, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 603 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 604 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | return( 1 ); |
| 608 | |
| 609 | exit: |
| 610 | return( 0 ); |
| 611 | } |
| 612 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 613 | /* We need two keys to exercise key agreement. Exercise the |
| 614 | * private key against its own public key. */ |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 615 | static psa_status_t key_agreement_with_self( |
| 616 | psa_key_derivation_operation_t *operation, |
| 617 | psa_key_handle_t handle ) |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 618 | { |
| 619 | psa_key_type_t private_key_type; |
| 620 | psa_key_type_t public_key_type; |
| 621 | size_t key_bits; |
| 622 | uint8_t *public_key = NULL; |
| 623 | size_t public_key_length; |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 624 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 625 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 626 | * but it's good enough: callers will report it as a failed test anyway. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 627 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 628 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 629 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 630 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 631 | private_key_type = psa_get_key_type( &attributes ); |
| 632 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 633 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 634 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 635 | ASSERT_ALLOC( public_key, public_key_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 636 | PSA_ASSERT( psa_export_public_key( handle, |
| 637 | public_key, public_key_length, |
| 638 | &public_key_length ) ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 639 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 640 | status = psa_key_derivation_key_agreement( |
| 641 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle, |
| 642 | public_key, public_key_length ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 643 | exit: |
| 644 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 645 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 646 | return( status ); |
| 647 | } |
| 648 | |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 649 | /* We need two keys to exercise key agreement. Exercise the |
| 650 | * private key against its own public key. */ |
| 651 | static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg, |
| 652 | psa_key_handle_t handle ) |
| 653 | { |
| 654 | psa_key_type_t private_key_type; |
| 655 | psa_key_type_t public_key_type; |
| 656 | size_t key_bits; |
| 657 | uint8_t *public_key = NULL; |
| 658 | size_t public_key_length; |
| 659 | uint8_t output[1024]; |
| 660 | size_t output_length; |
| 661 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 662 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 663 | * but it's good enough: callers will report it as a failed test anyway. */ |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 664 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 665 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 666 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 667 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 668 | private_key_type = psa_get_key_type( &attributes ); |
| 669 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 670 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 671 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 672 | ASSERT_ALLOC( public_key, public_key_length ); |
| 673 | PSA_ASSERT( psa_export_public_key( handle, |
| 674 | public_key, public_key_length, |
| 675 | &public_key_length ) ); |
| 676 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 677 | status = psa_raw_key_agreement( alg, handle, |
| 678 | public_key, public_key_length, |
| 679 | output, sizeof( output ), &output_length ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 680 | exit: |
| 681 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 682 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 683 | return( status ); |
| 684 | } |
| 685 | |
| 686 | static int exercise_raw_key_agreement_key( psa_key_handle_t handle, |
| 687 | psa_key_usage_t usage, |
| 688 | psa_algorithm_t alg ) |
| 689 | { |
| 690 | int ok = 0; |
| 691 | |
| 692 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 693 | { |
| 694 | /* We need two keys to exercise key agreement. Exercise the |
| 695 | * private key against its own public key. */ |
| 696 | PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) ); |
| 697 | } |
| 698 | ok = 1; |
| 699 | |
| 700 | exit: |
| 701 | return( ok ); |
| 702 | } |
| 703 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 704 | static int exercise_key_agreement_key( psa_key_handle_t handle, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 705 | psa_key_usage_t usage, |
| 706 | psa_algorithm_t alg ) |
| 707 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 708 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 709 | unsigned char output[1]; |
| 710 | int ok = 0; |
| 711 | |
| 712 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 713 | { |
| 714 | /* We need two keys to exercise key agreement. Exercise the |
| 715 | * private key against its own public key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 716 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 717 | PSA_ASSERT( key_agreement_with_self( &operation, handle ) ); |
| 718 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 719 | output, |
| 720 | sizeof( output ) ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 721 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 722 | } |
| 723 | ok = 1; |
| 724 | |
| 725 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 726 | return( ok ); |
| 727 | } |
| 728 | |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 729 | int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 730 | size_t min_bits, size_t max_bits, |
| 731 | int must_be_odd ) |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 732 | { |
| 733 | size_t len; |
| 734 | size_t actual_bits; |
| 735 | unsigned char msb; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 736 | TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 737 | MBEDTLS_ASN1_INTEGER ), |
| 738 | 0 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 739 | /* Tolerate a slight departure from DER encoding: |
| 740 | * - 0 may be represented by an empty string or a 1-byte string. |
| 741 | * - The sign bit may be used as a value bit. */ |
| 742 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 743 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 744 | { |
| 745 | ++( *p ); |
| 746 | --len; |
| 747 | } |
| 748 | if( min_bits == 0 && len == 0 ) |
| 749 | return( 1 ); |
| 750 | msb = ( *p )[0]; |
| 751 | TEST_ASSERT( msb != 0 ); |
| 752 | actual_bits = 8 * ( len - 1 ); |
| 753 | while( msb != 0 ) |
| 754 | { |
| 755 | msb >>= 1; |
| 756 | ++actual_bits; |
| 757 | } |
| 758 | TEST_ASSERT( actual_bits >= min_bits ); |
| 759 | TEST_ASSERT( actual_bits <= max_bits ); |
| 760 | if( must_be_odd ) |
| 761 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 762 | *p += len; |
| 763 | return( 1 ); |
| 764 | exit: |
| 765 | return( 0 ); |
| 766 | } |
| 767 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 768 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 769 | uint8_t *exported, size_t exported_length ) |
| 770 | { |
| 771 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 772 | TEST_EQUAL( exported_length, ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 773 | else |
| 774 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 775 | |
| 776 | #if defined(MBEDTLS_DES_C) |
| 777 | if( type == PSA_KEY_TYPE_DES ) |
| 778 | { |
| 779 | /* Check the parity bits. */ |
| 780 | unsigned i; |
| 781 | for( i = 0; i < bits / 8; i++ ) |
| 782 | { |
| 783 | unsigned bit_count = 0; |
| 784 | unsigned m; |
| 785 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 786 | { |
| 787 | if( exported[i] & m ) |
| 788 | ++bit_count; |
| 789 | } |
| 790 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 791 | } |
| 792 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 793 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 794 | #endif |
| 795 | |
| 796 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 797 | if( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 798 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 799 | uint8_t *p = exported; |
| 800 | uint8_t *end = exported + exported_length; |
| 801 | size_t len; |
| 802 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 803 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 804 | * modulus INTEGER, -- n |
| 805 | * publicExponent INTEGER, -- e |
| 806 | * privateExponent INTEGER, -- d |
| 807 | * prime1 INTEGER, -- p |
| 808 | * prime2 INTEGER, -- q |
| 809 | * exponent1 INTEGER, -- d mod (p-1) |
| 810 | * exponent2 INTEGER, -- d mod (q-1) |
| 811 | * coefficient INTEGER, -- (inverse of q) mod p |
| 812 | * } |
| 813 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 814 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 815 | MBEDTLS_ASN1_SEQUENCE | |
| 816 | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); |
| 817 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 818 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 819 | goto exit; |
| 820 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 821 | goto exit; |
| 822 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 823 | goto exit; |
| 824 | /* Require d to be at least half the size of n. */ |
| 825 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 826 | goto exit; |
| 827 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 828 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 829 | goto exit; |
| 830 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 831 | goto exit; |
| 832 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 833 | goto exit; |
| 834 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 835 | goto exit; |
| 836 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 837 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 838 | TEST_EQUAL( p, end ); |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 839 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 840 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 841 | #endif /* MBEDTLS_RSA_C */ |
| 842 | |
| 843 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 844 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 845 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 846 | /* Just the secret value */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 847 | TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 848 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 849 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 850 | #endif /* MBEDTLS_ECP_C */ |
| 851 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 852 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 853 | { |
| 854 | uint8_t *p = exported; |
| 855 | uint8_t *end = exported + exported_length; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 856 | #if defined(MBEDTLS_RSA_C) |
| 857 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 858 | { |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 859 | size_t len; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 860 | /* RSAPublicKey ::= SEQUENCE { |
| 861 | * modulus INTEGER, -- n |
| 862 | * publicExponent INTEGER } -- e |
| 863 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 864 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 865 | MBEDTLS_ASN1_SEQUENCE | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 866 | MBEDTLS_ASN1_CONSTRUCTED ), |
| 867 | 0 ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 868 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 869 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 870 | goto exit; |
| 871 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 872 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 873 | TEST_EQUAL( p, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 874 | } |
| 875 | else |
| 876 | #endif /* MBEDTLS_RSA_C */ |
| 877 | #if defined(MBEDTLS_ECP_C) |
| 878 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 879 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 880 | /* The representation of an ECC public key is: |
| 881 | * - The byte 0x04; |
| 882 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 883 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 884 | * - where m is the bit size associated with the curve. |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 885 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 886 | TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end ); |
| 887 | TEST_EQUAL( p[0], 4 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 888 | } |
| 889 | else |
| 890 | #endif /* MBEDTLS_ECP_C */ |
| 891 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 892 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 893 | mbedtls_snprintf( message, sizeof( message ), |
| 894 | "No sanity check for public key type=0x%08lx", |
| 895 | (unsigned long) type ); |
| 896 | test_fail( message, __LINE__, __FILE__ ); |
| 897 | return( 0 ); |
| 898 | } |
| 899 | } |
| 900 | else |
| 901 | |
| 902 | { |
| 903 | /* No sanity checks for other types */ |
| 904 | } |
| 905 | |
| 906 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 907 | |
| 908 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 909 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 912 | static int exercise_export_key( psa_key_handle_t handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 913 | psa_key_usage_t usage ) |
| 914 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 915 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 916 | uint8_t *exported = NULL; |
| 917 | size_t exported_size = 0; |
| 918 | size_t exported_length = 0; |
| 919 | int ok = 0; |
| 920 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 921 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 922 | |
| 923 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 924 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 925 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 926 | TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ), |
| 927 | PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 928 | ok = 1; |
| 929 | goto exit; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 932 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ), |
| 933 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 934 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 935 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 936 | PSA_ASSERT( psa_export_key( handle, |
| 937 | exported, exported_size, |
| 938 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 939 | ok = exported_key_sanity_check( psa_get_key_type( &attributes ), |
| 940 | psa_get_key_bits( &attributes ), |
| 941 | exported, exported_length ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 942 | |
| 943 | exit: |
| 944 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 945 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 946 | return( ok ); |
| 947 | } |
| 948 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 949 | static int exercise_export_public_key( psa_key_handle_t handle ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 950 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 951 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 952 | psa_key_type_t public_type; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 953 | uint8_t *exported = NULL; |
| 954 | size_t exported_size = 0; |
| 955 | size_t exported_length = 0; |
| 956 | int ok = 0; |
| 957 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 958 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 959 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 960 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 961 | TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 962 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 963 | return( 1 ); |
| 964 | } |
| 965 | |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 966 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 967 | psa_get_key_type( &attributes ) ); |
| 968 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, |
| 969 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 970 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 971 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 972 | PSA_ASSERT( psa_export_public_key( handle, |
| 973 | exported, exported_size, |
| 974 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 975 | ok = exported_key_sanity_check( public_type, |
| 976 | psa_get_key_bits( &attributes ), |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 977 | exported, exported_length ); |
| 978 | |
| 979 | exit: |
| 980 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 981 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 982 | return( ok ); |
| 983 | } |
| 984 | |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 985 | /** Do smoke tests on a key. |
| 986 | * |
| 987 | * Perform one of each operation indicated by \p alg (decrypt/encrypt, |
| 988 | * sign/verify, or derivation) that is permitted according to \p usage. |
| 989 | * \p usage and \p alg should correspond to the expected policy on the |
| 990 | * key. |
| 991 | * |
| 992 | * Export the key if permitted by \p usage, and check that the output |
| 993 | * looks sensible. If \p usage forbids export, check that |
| 994 | * \p psa_export_key correctly rejects the attempt. If the key is |
| 995 | * asymmetric, also check \p psa_export_public_key. |
| 996 | * |
| 997 | * If the key fails the tests, this function calls the test framework's |
| 998 | * `test_fail` function and returns false. Otherwise this function returns |
| 999 | * true. Therefore it should be used as follows: |
| 1000 | * ``` |
| 1001 | * if( ! exercise_key( ... ) ) goto exit; |
| 1002 | * ``` |
| 1003 | * |
| 1004 | * \param handle The key to exercise. It should be capable of performing |
| 1005 | * \p alg. |
| 1006 | * \param usage The usage flags to assume. |
| 1007 | * \param alg The algorithm to exercise. |
| 1008 | * |
| 1009 | * \retval 0 The key failed the smoke tests. |
| 1010 | * \retval 1 The key passed the smoke tests. |
| 1011 | */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1012 | static int exercise_key( psa_key_handle_t handle, |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1013 | psa_key_usage_t usage, |
| 1014 | psa_algorithm_t alg ) |
| 1015 | { |
| 1016 | int ok; |
| 1017 | if( alg == 0 ) |
| 1018 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 1019 | else if( PSA_ALG_IS_MAC( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1020 | ok = exercise_mac_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1021 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1022 | ok = exercise_cipher_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1023 | else if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1024 | ok = exercise_aead_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1025 | else if( PSA_ALG_IS_SIGN( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1026 | ok = exercise_signature_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1027 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1028 | ok = exercise_asymmetric_encryption_key( handle, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1029 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1030 | ok = exercise_key_derivation_key( handle, usage, alg ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 1031 | else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 1032 | ok = exercise_raw_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1033 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1034 | ok = exercise_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1035 | else |
| 1036 | { |
| 1037 | char message[40]; |
| 1038 | mbedtls_snprintf( message, sizeof( message ), |
| 1039 | "No code to exercise alg=0x%08lx", |
| 1040 | (unsigned long) alg ); |
| 1041 | test_fail( message, __LINE__, __FILE__ ); |
| 1042 | ok = 0; |
| 1043 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1044 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1045 | ok = ok && exercise_export_key( handle, usage ); |
| 1046 | ok = ok && exercise_export_public_key( handle ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1047 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1048 | return( ok ); |
| 1049 | } |
| 1050 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1051 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 1052 | psa_algorithm_t alg ) |
| 1053 | { |
| 1054 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 1055 | { |
| 1056 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1057 | PSA_KEY_USAGE_VERIFY : |
| 1058 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 1059 | } |
| 1060 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 1061 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 1062 | { |
| 1063 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1064 | PSA_KEY_USAGE_ENCRYPT : |
| 1065 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 1066 | } |
| 1067 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 1068 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 1069 | { |
| 1070 | return( PSA_KEY_USAGE_DERIVE ); |
| 1071 | } |
| 1072 | else |
| 1073 | { |
| 1074 | return( 0 ); |
| 1075 | } |
| 1076 | |
| 1077 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1078 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1079 | static int test_operations_on_invalid_handle( psa_key_handle_t handle ) |
| 1080 | { |
| 1081 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1082 | uint8_t buffer[1]; |
| 1083 | size_t length; |
| 1084 | int ok = 0; |
| 1085 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1086 | psa_set_key_id( &attributes, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1087 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 1088 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 1089 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
| 1090 | TEST_EQUAL( psa_get_key_attributes( handle, &attributes ), |
| 1091 | PSA_ERROR_INVALID_HANDLE ); |
| 1092 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 1093 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1094 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1095 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1096 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 1097 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 1098 | |
| 1099 | TEST_EQUAL( psa_export_key( handle, |
| 1100 | buffer, sizeof( buffer ), &length ), |
| 1101 | PSA_ERROR_INVALID_HANDLE ); |
| 1102 | TEST_EQUAL( psa_export_public_key( handle, |
| 1103 | buffer, sizeof( buffer ), &length ), |
| 1104 | PSA_ERROR_INVALID_HANDLE ); |
| 1105 | |
| 1106 | TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE ); |
| 1107 | TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE ); |
| 1108 | |
| 1109 | ok = 1; |
| 1110 | |
| 1111 | exit: |
| 1112 | psa_reset_key_attributes( &attributes ); |
| 1113 | return( ok ); |
| 1114 | } |
| 1115 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1116 | /* Assert that a key isn't reported as having a slot number. */ |
| 1117 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1118 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1119 | do \ |
| 1120 | { \ |
| 1121 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 1122 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 1123 | attributes, \ |
| 1124 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 1125 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 1126 | } \ |
| 1127 | while( 0 ) |
| 1128 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1129 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1130 | ( (void) 0 ) |
| 1131 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1132 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1133 | /* An overapproximation of the amount of storage needed for a key of the |
| 1134 | * given type and with the given content. The API doesn't make it easy |
| 1135 | * to find a good value for the size. The current implementation doesn't |
| 1136 | * care about the value anyway. */ |
| 1137 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 1138 | ( data )->len |
| 1139 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1140 | typedef enum { |
| 1141 | IMPORT_KEY = 0, |
| 1142 | GENERATE_KEY = 1, |
| 1143 | DERIVE_KEY = 2 |
| 1144 | } generate_method; |
| 1145 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1146 | /* END_HEADER */ |
| 1147 | |
| 1148 | /* BEGIN_DEPENDENCIES |
| 1149 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1150 | * END_DEPENDENCIES |
| 1151 | */ |
| 1152 | |
| 1153 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1154 | void static_checks( ) |
| 1155 | { |
| 1156 | size_t max_truncated_mac_size = |
| 1157 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1158 | |
| 1159 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1160 | * encoding. The shifted mask is the maximum truncated value. The |
| 1161 | * untruncated algorithm may be one byte larger. */ |
| 1162 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 1163 | } |
| 1164 | /* END_CASE */ |
| 1165 | |
| 1166 | /* BEGIN_CASE */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1167 | void attributes_set_get( int id_arg, int lifetime_arg, |
| 1168 | int usage_flags_arg, int alg_arg, |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1169 | int type_arg, int bits_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1170 | { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1171 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1172 | psa_key_id_t id = id_arg; |
| 1173 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1174 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 1175 | psa_algorithm_t alg = alg_arg; |
| 1176 | psa_key_type_t type = type_arg; |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1177 | size_t bits = bits_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1178 | |
| 1179 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1180 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1181 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1182 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1183 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1184 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1185 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1186 | psa_set_key_id( &attributes, id ); |
| 1187 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1188 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 1189 | psa_set_key_algorithm( &attributes, alg ); |
| 1190 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1191 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1192 | |
| 1193 | TEST_EQUAL( psa_get_key_id( &attributes ), id ); |
| 1194 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime ); |
| 1195 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 1196 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
| 1197 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1198 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1199 | |
| 1200 | psa_reset_key_attributes( &attributes ); |
| 1201 | |
| 1202 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1203 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1204 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1205 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1206 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1207 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1208 | } |
| 1209 | /* END_CASE */ |
| 1210 | |
| 1211 | /* BEGIN_CASE */ |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1212 | void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg, |
| 1213 | int expected_id_arg, int expected_lifetime_arg ) |
| 1214 | { |
| 1215 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1216 | psa_key_id_t id1 = id1_arg; |
| 1217 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1218 | psa_key_id_t id2 = id2_arg; |
| 1219 | psa_key_id_t expected_id = expected_id_arg; |
| 1220 | psa_key_lifetime_t expected_lifetime = expected_lifetime_arg; |
| 1221 | |
| 1222 | if( id1_arg != -1 ) |
| 1223 | psa_set_key_id( &attributes, id1 ); |
| 1224 | if( lifetime_arg != -1 ) |
| 1225 | psa_set_key_lifetime( &attributes, lifetime ); |
| 1226 | if( id2_arg != -1 ) |
| 1227 | psa_set_key_id( &attributes, id2 ); |
| 1228 | |
| 1229 | TEST_EQUAL( psa_get_key_id( &attributes ), expected_id ); |
| 1230 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime ); |
| 1231 | } |
| 1232 | /* END_CASE */ |
| 1233 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1234 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1235 | void slot_number_attribute( ) |
| 1236 | { |
| 1237 | psa_key_slot_number_t slot_number = 0xdeadbeef; |
| 1238 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1239 | |
| 1240 | /* Initially, there is no slot number. */ |
| 1241 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1242 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1243 | |
| 1244 | /* Test setting a slot number. */ |
| 1245 | psa_set_key_slot_number( &attributes, 0 ); |
| 1246 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1247 | TEST_EQUAL( slot_number, 0 ); |
| 1248 | |
| 1249 | /* Test changing the slot number. */ |
| 1250 | psa_set_key_slot_number( &attributes, 42 ); |
| 1251 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1252 | TEST_EQUAL( slot_number, 42 ); |
| 1253 | |
| 1254 | /* Test clearing the slot number. */ |
| 1255 | psa_clear_key_slot_number( &attributes ); |
| 1256 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1257 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1258 | |
| 1259 | /* Clearing again should have no effect. */ |
| 1260 | psa_clear_key_slot_number( &attributes ); |
| 1261 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1262 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1263 | |
| 1264 | /* Test that reset clears the slot number. */ |
| 1265 | psa_set_key_slot_number( &attributes, 42 ); |
| 1266 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1267 | TEST_EQUAL( slot_number, 42 ); |
| 1268 | psa_reset_key_attributes( &attributes ); |
| 1269 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1270 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1271 | } |
| 1272 | /* END_CASE */ |
| 1273 | |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1274 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1275 | void import_with_policy( int type_arg, |
| 1276 | int usage_arg, int alg_arg, |
| 1277 | int expected_status_arg ) |
| 1278 | { |
| 1279 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1280 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1281 | psa_key_handle_t handle = 0; |
| 1282 | psa_key_type_t type = type_arg; |
| 1283 | psa_key_usage_t usage = usage_arg; |
| 1284 | psa_algorithm_t alg = alg_arg; |
| 1285 | psa_status_t expected_status = expected_status_arg; |
| 1286 | const uint8_t key_material[16] = {0}; |
| 1287 | psa_status_t status; |
| 1288 | |
| 1289 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1290 | |
| 1291 | psa_set_key_type( &attributes, type ); |
| 1292 | psa_set_key_usage_flags( &attributes, usage ); |
| 1293 | psa_set_key_algorithm( &attributes, alg ); |
| 1294 | |
| 1295 | status = psa_import_key( &attributes, |
| 1296 | key_material, sizeof( key_material ), |
| 1297 | &handle ); |
| 1298 | TEST_EQUAL( status, expected_status ); |
| 1299 | if( status != PSA_SUCCESS ) |
| 1300 | goto exit; |
| 1301 | |
| 1302 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1303 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1304 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1305 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1306 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1307 | |
| 1308 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1309 | test_operations_on_invalid_handle( handle ); |
| 1310 | |
| 1311 | exit: |
| 1312 | psa_destroy_key( handle ); |
| 1313 | psa_reset_key_attributes( &got_attributes ); |
| 1314 | PSA_DONE( ); |
| 1315 | } |
| 1316 | /* END_CASE */ |
| 1317 | |
| 1318 | /* BEGIN_CASE */ |
| 1319 | void import_with_data( data_t *data, int type_arg, |
| 1320 | int attr_bits_arg, |
| 1321 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1322 | { |
| 1323 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1324 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1325 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1326 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1327 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1328 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1329 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1330 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1331 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1332 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1333 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1334 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1335 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1336 | status = psa_import_key( &attributes, data->x, data->len, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1337 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1338 | if( status != PSA_SUCCESS ) |
| 1339 | goto exit; |
| 1340 | |
| 1341 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1342 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1343 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1344 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1345 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1346 | |
| 1347 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1348 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1349 | |
| 1350 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1351 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1352 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1353 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1354 | } |
| 1355 | /* END_CASE */ |
| 1356 | |
| 1357 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1358 | void import_large_key( int type_arg, int byte_size_arg, |
| 1359 | int expected_status_arg ) |
| 1360 | { |
| 1361 | psa_key_type_t type = type_arg; |
| 1362 | size_t byte_size = byte_size_arg; |
| 1363 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1364 | psa_status_t expected_status = expected_status_arg; |
| 1365 | psa_key_handle_t handle = 0; |
| 1366 | psa_status_t status; |
| 1367 | uint8_t *buffer = NULL; |
| 1368 | size_t buffer_size = byte_size + 1; |
| 1369 | size_t n; |
| 1370 | |
| 1371 | /* It would be better to skip the test than fail it if the allocation |
| 1372 | * fails, but the test framework doesn't support this yet. */ |
| 1373 | ASSERT_ALLOC( buffer, buffer_size ); |
| 1374 | memset( buffer, 'K', byte_size ); |
| 1375 | |
| 1376 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1377 | |
| 1378 | /* Try importing the key */ |
| 1379 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1380 | psa_set_key_type( &attributes, type ); |
| 1381 | status = psa_import_key( &attributes, buffer, byte_size, &handle ); |
| 1382 | TEST_EQUAL( status, expected_status ); |
| 1383 | |
| 1384 | if( status == PSA_SUCCESS ) |
| 1385 | { |
| 1386 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1387 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1388 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1389 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1390 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1391 | memset( buffer, 0, byte_size + 1 ); |
| 1392 | PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) ); |
| 1393 | for( n = 0; n < byte_size; n++ ) |
| 1394 | TEST_EQUAL( buffer[n], 'K' ); |
| 1395 | for( n = byte_size; n < buffer_size; n++ ) |
| 1396 | TEST_EQUAL( buffer[n], 0 ); |
| 1397 | } |
| 1398 | |
| 1399 | exit: |
| 1400 | psa_destroy_key( handle ); |
| 1401 | PSA_DONE( ); |
| 1402 | mbedtls_free( buffer ); |
| 1403 | } |
| 1404 | /* END_CASE */ |
| 1405 | |
| 1406 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1407 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1408 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1409 | psa_key_handle_t handle = 0; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1410 | size_t bits = bits_arg; |
| 1411 | psa_status_t expected_status = expected_status_arg; |
| 1412 | psa_status_t status; |
| 1413 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1414 | 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] | 1415 | size_t buffer_size = /* Slight overapproximations */ |
| 1416 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1417 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1418 | unsigned char *p; |
| 1419 | int ret; |
| 1420 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1421 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1422 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1423 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1424 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1425 | |
| 1426 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1427 | bits, keypair ) ) >= 0 ); |
| 1428 | length = ret; |
| 1429 | |
| 1430 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1431 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1432 | status = psa_import_key( &attributes, p, length, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1433 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1434 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1435 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1436 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1437 | |
| 1438 | exit: |
| 1439 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1440 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1441 | } |
| 1442 | /* END_CASE */ |
| 1443 | |
| 1444 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1445 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1446 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1447 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1448 | int expected_bits, |
| 1449 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1450 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1451 | int canonical_input ) |
| 1452 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1453 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1454 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1455 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1456 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1457 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1458 | unsigned char *exported = NULL; |
| 1459 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1460 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1461 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1462 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1463 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1464 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1465 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1466 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1467 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1468 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1469 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1470 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1471 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1472 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1473 | psa_set_key_algorithm( &attributes, alg ); |
| 1474 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1475 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1476 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1477 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1478 | |
| 1479 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1480 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1481 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1482 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1483 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1484 | |
| 1485 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1486 | status = psa_export_key( handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1487 | exported, export_size, |
| 1488 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1489 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1490 | |
| 1491 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1492 | * and export_size. On errors, the exported length must be 0. */ |
| 1493 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1494 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1495 | TEST_ASSERT( exported_length <= export_size ); |
| 1496 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1497 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1498 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1499 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1500 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1501 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1502 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1503 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1504 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1505 | if( ! exercise_export_key( handle, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1506 | goto exit; |
| 1507 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1508 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1509 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1510 | else |
| 1511 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1512 | psa_key_handle_t handle2; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1513 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
| 1514 | &handle2 ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1515 | PSA_ASSERT( psa_export_key( handle2, |
| 1516 | reexported, |
| 1517 | export_size, |
| 1518 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1519 | ASSERT_COMPARE( exported, exported_length, |
| 1520 | reexported, reexported_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1521 | PSA_ASSERT( psa_close_key( handle2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1522 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1523 | 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] | 1524 | |
| 1525 | destroy: |
| 1526 | /* Destroy the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1527 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1528 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1529 | |
| 1530 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1531 | mbedtls_free( exported ); |
| 1532 | mbedtls_free( reexported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1533 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1534 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1535 | } |
| 1536 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1537 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1538 | /* BEGIN_CASE */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1539 | void invalid_handle( int handle ) |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1540 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1541 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1542 | test_operations_on_invalid_handle( handle ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1543 | |
| 1544 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1545 | PSA_DONE( ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1546 | } |
| 1547 | /* END_CASE */ |
| 1548 | |
| 1549 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1550 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1551 | int type_arg, |
| 1552 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1553 | int export_size_delta, |
| 1554 | int expected_export_status_arg, |
| 1555 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1556 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1557 | psa_key_handle_t handle = 0; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1558 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1559 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1560 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1561 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1562 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1563 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1564 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1565 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1566 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1567 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1568 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1569 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1570 | psa_set_key_algorithm( &attributes, alg ); |
| 1571 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1572 | |
| 1573 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1574 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1575 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1576 | /* Export the public key */ |
| 1577 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1578 | status = psa_export_public_key( handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1579 | exported, export_size, |
| 1580 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1581 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1582 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1583 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1584 | 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] | 1585 | size_t bits; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1586 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1587 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1588 | TEST_ASSERT( expected_public_key->len <= |
| 1589 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1590 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1591 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1592 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1593 | |
| 1594 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1595 | mbedtls_free( exported ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1596 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1597 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1598 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1599 | } |
| 1600 | /* END_CASE */ |
| 1601 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1602 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1603 | void import_and_exercise_key( data_t *data, |
| 1604 | int type_arg, |
| 1605 | int bits_arg, |
| 1606 | int alg_arg ) |
| 1607 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1608 | psa_key_handle_t handle = 0; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1609 | psa_key_type_t type = type_arg; |
| 1610 | size_t bits = bits_arg; |
| 1611 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1612 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1613 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1614 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1615 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1616 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1617 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1618 | psa_set_key_usage_flags( &attributes, usage ); |
| 1619 | psa_set_key_algorithm( &attributes, alg ); |
| 1620 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1621 | |
| 1622 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1623 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1624 | |
| 1625 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1626 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1627 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1628 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1629 | |
| 1630 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1631 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1632 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1633 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1634 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1635 | test_operations_on_invalid_handle( handle ); |
| 1636 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1637 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1638 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1639 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1640 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1641 | } |
| 1642 | /* END_CASE */ |
| 1643 | |
| 1644 | /* BEGIN_CASE */ |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1645 | void key_policy( int usage_arg, int alg_arg ) |
| 1646 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1647 | psa_key_handle_t handle = 0; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1648 | psa_algorithm_t alg = alg_arg; |
| 1649 | psa_key_usage_t usage = usage_arg; |
| 1650 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 1651 | unsigned char key[32] = {0}; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1652 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1653 | |
| 1654 | memset( key, 0x2a, sizeof( key ) ); |
| 1655 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1656 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1657 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1658 | psa_set_key_usage_flags( &attributes, usage ); |
| 1659 | psa_set_key_algorithm( &attributes, alg ); |
| 1660 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1661 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1662 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1663 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1664 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1665 | TEST_EQUAL( psa_get_key_type( &attributes ), key_type ); |
| 1666 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage ); |
| 1667 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1668 | |
| 1669 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1670 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1671 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1672 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1673 | } |
| 1674 | /* END_CASE */ |
| 1675 | |
| 1676 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1677 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1678 | { |
| 1679 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1680 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1681 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1682 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1683 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1684 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1685 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1686 | |
| 1687 | memset( &zero, 0, sizeof( zero ) ); |
| 1688 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1689 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1690 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1691 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1692 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1693 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1694 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1695 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1696 | |
| 1697 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1698 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1699 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1700 | |
| 1701 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1702 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1703 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1704 | |
| 1705 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1706 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1707 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1708 | } |
| 1709 | /* END_CASE */ |
| 1710 | |
| 1711 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1712 | void mac_key_policy( int policy_usage, |
| 1713 | int policy_alg, |
| 1714 | int key_type, |
| 1715 | data_t *key_data, |
| 1716 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1717 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1718 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1719 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1720 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1721 | psa_status_t status; |
| 1722 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1723 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1724 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1725 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1726 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1727 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1728 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1729 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1730 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1731 | &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1732 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1733 | status = psa_mac_sign_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1734 | if( policy_alg == exercise_alg && |
| 1735 | ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1736 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1737 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1738 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1739 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1740 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1741 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1742 | status = psa_mac_verify_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1743 | if( policy_alg == exercise_alg && |
| 1744 | ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1745 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1746 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1747 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1748 | |
| 1749 | exit: |
| 1750 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1751 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1752 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1753 | } |
| 1754 | /* END_CASE */ |
| 1755 | |
| 1756 | /* BEGIN_CASE */ |
| 1757 | void cipher_key_policy( int policy_usage, |
| 1758 | int policy_alg, |
| 1759 | int key_type, |
| 1760 | data_t *key_data, |
| 1761 | int exercise_alg ) |
| 1762 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1763 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1764 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1765 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1766 | psa_status_t status; |
| 1767 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1768 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1769 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1770 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1771 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1772 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1773 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1774 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1775 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1776 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1777 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1778 | if( policy_alg == exercise_alg && |
| 1779 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1780 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1781 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1782 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1783 | psa_cipher_abort( &operation ); |
| 1784 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1785 | status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1786 | if( policy_alg == exercise_alg && |
| 1787 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1788 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1789 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1790 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1791 | |
| 1792 | exit: |
| 1793 | psa_cipher_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1794 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1795 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1796 | } |
| 1797 | /* END_CASE */ |
| 1798 | |
| 1799 | /* BEGIN_CASE */ |
| 1800 | void aead_key_policy( int policy_usage, |
| 1801 | int policy_alg, |
| 1802 | int key_type, |
| 1803 | data_t *key_data, |
| 1804 | int nonce_length_arg, |
| 1805 | int tag_length_arg, |
| 1806 | int exercise_alg ) |
| 1807 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1808 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1809 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1810 | psa_status_t status; |
| 1811 | unsigned char nonce[16] = {0}; |
| 1812 | size_t nonce_length = nonce_length_arg; |
| 1813 | unsigned char tag[16]; |
| 1814 | size_t tag_length = tag_length_arg; |
| 1815 | size_t output_length; |
| 1816 | |
| 1817 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1818 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1819 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1820 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1821 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1822 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1823 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1824 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1825 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1826 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1827 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1828 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1829 | status = psa_aead_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1830 | nonce, nonce_length, |
| 1831 | NULL, 0, |
| 1832 | NULL, 0, |
| 1833 | tag, tag_length, |
| 1834 | &output_length ); |
| 1835 | if( policy_alg == exercise_alg && |
| 1836 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1837 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1838 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1839 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1840 | |
| 1841 | memset( tag, 0, sizeof( tag ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1842 | status = psa_aead_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1843 | nonce, nonce_length, |
| 1844 | NULL, 0, |
| 1845 | tag, tag_length, |
| 1846 | NULL, 0, |
| 1847 | &output_length ); |
| 1848 | if( policy_alg == exercise_alg && |
| 1849 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1850 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1851 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1852 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1853 | |
| 1854 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1855 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1856 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1857 | } |
| 1858 | /* END_CASE */ |
| 1859 | |
| 1860 | /* BEGIN_CASE */ |
| 1861 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1862 | int policy_alg, |
| 1863 | int key_type, |
| 1864 | data_t *key_data, |
| 1865 | int exercise_alg ) |
| 1866 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1867 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1868 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1869 | psa_status_t status; |
| 1870 | size_t key_bits; |
| 1871 | size_t buffer_length; |
| 1872 | unsigned char *buffer = NULL; |
| 1873 | size_t output_length; |
| 1874 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1875 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1876 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1877 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1878 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1879 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1880 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1881 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1882 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1883 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1884 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1885 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1886 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1887 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1888 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1889 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1890 | status = psa_asymmetric_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1891 | NULL, 0, |
| 1892 | NULL, 0, |
| 1893 | buffer, buffer_length, |
| 1894 | &output_length ); |
| 1895 | if( policy_alg == exercise_alg && |
| 1896 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1897 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1898 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1899 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1900 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1901 | if( buffer_length != 0 ) |
| 1902 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1903 | status = psa_asymmetric_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1904 | buffer, buffer_length, |
| 1905 | NULL, 0, |
| 1906 | buffer, buffer_length, |
| 1907 | &output_length ); |
| 1908 | if( policy_alg == exercise_alg && |
| 1909 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1910 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1911 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1912 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1913 | |
| 1914 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1915 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1916 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1917 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1918 | mbedtls_free( buffer ); |
| 1919 | } |
| 1920 | /* END_CASE */ |
| 1921 | |
| 1922 | /* BEGIN_CASE */ |
| 1923 | void asymmetric_signature_key_policy( int policy_usage, |
| 1924 | int policy_alg, |
| 1925 | int key_type, |
| 1926 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1927 | int exercise_alg, |
| 1928 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1929 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1930 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1931 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1932 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1933 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1934 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1935 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1936 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1937 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1938 | int compatible_alg = payload_length_arg > 0; |
| 1939 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1940 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
| 1941 | size_t signature_length; |
| 1942 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1943 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1944 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1945 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1946 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1947 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1948 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1949 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1950 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1951 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1952 | status = psa_asymmetric_sign( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1953 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1954 | signature, sizeof( signature ), |
| 1955 | &signature_length ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1956 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1957 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1958 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1959 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1960 | |
| 1961 | memset( signature, 0, sizeof( signature ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1962 | status = psa_asymmetric_verify( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1963 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1964 | signature, sizeof( signature ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1965 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1966 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1967 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1968 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1969 | |
| 1970 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1971 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1972 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1973 | } |
| 1974 | /* END_CASE */ |
| 1975 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1976 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1977 | void derive_key_policy( int policy_usage, |
| 1978 | int policy_alg, |
| 1979 | int key_type, |
| 1980 | data_t *key_data, |
| 1981 | int exercise_alg ) |
| 1982 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1983 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1984 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1985 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1986 | psa_status_t status; |
| 1987 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1988 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1989 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1990 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1991 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1992 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1993 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1994 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1995 | &handle ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1996 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1997 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1998 | |
| 1999 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2000 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2001 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2002 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2003 | &operation, |
| 2004 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2005 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2006 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2007 | |
| 2008 | status = psa_key_derivation_input_key( &operation, |
| 2009 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2010 | handle ); |
| 2011 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2012 | if( policy_alg == exercise_alg && |
| 2013 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2014 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +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 | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2017 | |
| 2018 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2019 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2020 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2021 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2022 | } |
| 2023 | /* END_CASE */ |
| 2024 | |
| 2025 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2026 | void agreement_key_policy( int policy_usage, |
| 2027 | int policy_alg, |
| 2028 | int key_type_arg, |
| 2029 | data_t *key_data, |
| 2030 | int exercise_alg ) |
| 2031 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2032 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2033 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2034 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2035 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2036 | psa_status_t status; |
| 2037 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2038 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2039 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2040 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2041 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2042 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2043 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2044 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2045 | &handle ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2046 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2047 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2048 | status = key_agreement_with_self( &operation, handle ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2049 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2050 | if( policy_alg == exercise_alg && |
| 2051 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2052 | PSA_ASSERT( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2053 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2054 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2055 | |
| 2056 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2057 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2058 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2059 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2060 | } |
| 2061 | /* END_CASE */ |
| 2062 | |
| 2063 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2064 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2065 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2066 | { |
| 2067 | psa_key_handle_t handle = 0; |
| 2068 | psa_key_type_t key_type = key_type_arg; |
| 2069 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2070 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2071 | psa_key_usage_t usage = usage_arg; |
| 2072 | psa_algorithm_t alg = alg_arg; |
| 2073 | psa_algorithm_t alg2 = alg2_arg; |
| 2074 | |
| 2075 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2076 | |
| 2077 | psa_set_key_usage_flags( &attributes, usage ); |
| 2078 | psa_set_key_algorithm( &attributes, alg ); |
| 2079 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2080 | psa_set_key_type( &attributes, key_type ); |
| 2081 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2082 | &handle ) ); |
| 2083 | |
| 2084 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 2085 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2086 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2087 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2088 | |
| 2089 | if( ! exercise_key( handle, usage, alg ) ) |
| 2090 | goto exit; |
| 2091 | if( ! exercise_key( handle, usage, alg2 ) ) |
| 2092 | goto exit; |
| 2093 | |
| 2094 | exit: |
| 2095 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2096 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2097 | } |
| 2098 | /* END_CASE */ |
| 2099 | |
| 2100 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2101 | void raw_agreement_key_policy( int policy_usage, |
| 2102 | int policy_alg, |
| 2103 | int key_type_arg, |
| 2104 | data_t *key_data, |
| 2105 | int exercise_alg ) |
| 2106 | { |
| 2107 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2108 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2109 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2110 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2111 | psa_status_t status; |
| 2112 | |
| 2113 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2114 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2115 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2116 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2117 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2118 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2119 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2120 | &handle ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2121 | |
| 2122 | status = raw_key_agreement_with_self( exercise_alg, handle ); |
| 2123 | |
| 2124 | if( policy_alg == exercise_alg && |
| 2125 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 2126 | PSA_ASSERT( status ); |
| 2127 | else |
| 2128 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2129 | |
| 2130 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2131 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2132 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2133 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2134 | } |
| 2135 | /* END_CASE */ |
| 2136 | |
| 2137 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2138 | void copy_success( int source_usage_arg, |
| 2139 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2140 | int type_arg, data_t *material, |
| 2141 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2142 | int target_usage_arg, |
| 2143 | int target_alg_arg, int target_alg2_arg, |
| 2144 | int expected_usage_arg, |
| 2145 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2146 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2147 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2148 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2149 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2150 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2151 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2152 | psa_key_handle_t source_handle = 0; |
| 2153 | psa_key_handle_t target_handle = 0; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2154 | uint8_t *export_buffer = NULL; |
| 2155 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2156 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2157 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2158 | /* Prepare the source key. */ |
| 2159 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2160 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2161 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2162 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2163 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2164 | material->x, material->len, |
| 2165 | &source_handle ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2166 | PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2167 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2168 | /* Prepare the target attributes. */ |
| 2169 | if( copy_attributes ) |
| 2170 | target_attributes = source_attributes; |
| 2171 | if( target_usage_arg != -1 ) |
| 2172 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2173 | if( target_alg_arg != -1 ) |
| 2174 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2175 | if( target_alg2_arg != -1 ) |
| 2176 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2177 | |
| 2178 | /* Copy the key. */ |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2179 | PSA_ASSERT( psa_copy_key( source_handle, |
| 2180 | &target_attributes, &target_handle ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2181 | |
| 2182 | /* Destroy the source to ensure that this doesn't affect the target. */ |
| 2183 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2184 | |
| 2185 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2186 | PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) ); |
| 2187 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2188 | psa_get_key_type( &target_attributes ) ); |
| 2189 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2190 | psa_get_key_bits( &target_attributes ) ); |
| 2191 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2192 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2193 | TEST_EQUAL( expected_alg2, |
| 2194 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2195 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2196 | { |
| 2197 | size_t length; |
| 2198 | ASSERT_ALLOC( export_buffer, material->len ); |
| 2199 | PSA_ASSERT( psa_export_key( target_handle, export_buffer, |
| 2200 | material->len, &length ) ); |
| 2201 | ASSERT_COMPARE( material->x, material->len, |
| 2202 | export_buffer, length ); |
| 2203 | } |
| 2204 | if( ! exercise_key( target_handle, expected_usage, expected_alg ) ) |
| 2205 | goto exit; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2206 | if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) ) |
| 2207 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2208 | |
| 2209 | PSA_ASSERT( psa_close_key( target_handle ) ); |
| 2210 | |
| 2211 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2212 | psa_reset_key_attributes( &source_attributes ); |
| 2213 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2214 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2215 | mbedtls_free( export_buffer ); |
| 2216 | } |
| 2217 | /* END_CASE */ |
| 2218 | |
| 2219 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2220 | void copy_fail( int source_usage_arg, |
| 2221 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2222 | int type_arg, data_t *material, |
| 2223 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2224 | int target_usage_arg, |
| 2225 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2226 | int expected_status_arg ) |
| 2227 | { |
| 2228 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2229 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2230 | psa_key_handle_t source_handle = 0; |
| 2231 | psa_key_handle_t target_handle = 0; |
| 2232 | |
| 2233 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2234 | |
| 2235 | /* Prepare the source key. */ |
| 2236 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2237 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2238 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2239 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2240 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2241 | material->x, material->len, |
| 2242 | &source_handle ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2243 | |
| 2244 | /* Prepare the target attributes. */ |
| 2245 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2246 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2247 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2248 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2249 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2250 | |
| 2251 | /* Try to copy the key. */ |
| 2252 | TEST_EQUAL( psa_copy_key( source_handle, |
| 2253 | &target_attributes, &target_handle ), |
| 2254 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2255 | |
| 2256 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2257 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2258 | exit: |
| 2259 | psa_reset_key_attributes( &source_attributes ); |
| 2260 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2261 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2262 | } |
| 2263 | /* END_CASE */ |
| 2264 | |
| 2265 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2266 | void hash_operation_init( ) |
| 2267 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2268 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2269 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2270 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2271 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2272 | * to supress the Clang warning for the test. */ |
| 2273 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2274 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2275 | psa_hash_operation_t zero; |
| 2276 | |
| 2277 | memset( &zero, 0, sizeof( zero ) ); |
| 2278 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2279 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2280 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2281 | PSA_ERROR_BAD_STATE ); |
| 2282 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2283 | PSA_ERROR_BAD_STATE ); |
| 2284 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2285 | PSA_ERROR_BAD_STATE ); |
| 2286 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2287 | /* A default hash operation should be abortable without error. */ |
| 2288 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2289 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2290 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2291 | } |
| 2292 | /* END_CASE */ |
| 2293 | |
| 2294 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2295 | void hash_setup( int alg_arg, |
| 2296 | int expected_status_arg ) |
| 2297 | { |
| 2298 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2299 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2300 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2301 | psa_status_t status; |
| 2302 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2303 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2304 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2305 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2306 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2307 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2308 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2309 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2310 | |
| 2311 | /* If setup failed, reproduce the failure, so as to |
| 2312 | * test the resulting state of the operation object. */ |
| 2313 | if( status != PSA_SUCCESS ) |
| 2314 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2315 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2316 | /* Now the operation object should be reusable. */ |
| 2317 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2318 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2319 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2320 | #endif |
| 2321 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2322 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2323 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2324 | } |
| 2325 | /* END_CASE */ |
| 2326 | |
| 2327 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2328 | void hash_bad_order( ) |
| 2329 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2330 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2331 | unsigned char input[] = ""; |
| 2332 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2333 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2334 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2335 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2336 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2337 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2338 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2339 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2340 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2341 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2342 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2343 | /* Call setup twice in a row. */ |
| 2344 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2345 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2346 | PSA_ERROR_BAD_STATE ); |
| 2347 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2348 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2349 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2350 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2351 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2352 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2353 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2354 | /* Call update after finish. */ |
| 2355 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2356 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2357 | hash, sizeof( hash ), &hash_len ) ); |
| 2358 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2359 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2360 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2361 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2362 | /* Call verify without calling setup beforehand. */ |
| 2363 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2364 | valid_hash, sizeof( valid_hash ) ), |
| 2365 | PSA_ERROR_BAD_STATE ); |
| 2366 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2367 | |
| 2368 | /* Call verify after finish. */ |
| 2369 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2370 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2371 | hash, sizeof( hash ), &hash_len ) ); |
| 2372 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2373 | valid_hash, sizeof( valid_hash ) ), |
| 2374 | PSA_ERROR_BAD_STATE ); |
| 2375 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2376 | |
| 2377 | /* Call verify twice in a row. */ |
| 2378 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2379 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2380 | valid_hash, sizeof( valid_hash ) ) ); |
| 2381 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2382 | valid_hash, sizeof( valid_hash ) ), |
| 2383 | PSA_ERROR_BAD_STATE ); |
| 2384 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2385 | |
| 2386 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2387 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2388 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2389 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2390 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2391 | |
| 2392 | /* Call finish twice in a row. */ |
| 2393 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2394 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2395 | hash, sizeof( hash ), &hash_len ) ); |
| 2396 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2397 | hash, sizeof( hash ), &hash_len ), |
| 2398 | PSA_ERROR_BAD_STATE ); |
| 2399 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2400 | |
| 2401 | /* Call finish after calling verify. */ |
| 2402 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2403 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2404 | valid_hash, sizeof( valid_hash ) ) ); |
| 2405 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2406 | hash, sizeof( hash ), &hash_len ), |
| 2407 | PSA_ERROR_BAD_STATE ); |
| 2408 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2409 | |
| 2410 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2411 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2412 | } |
| 2413 | /* END_CASE */ |
| 2414 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2415 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2416 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2417 | { |
| 2418 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2419 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2420 | * appended to it */ |
| 2421 | unsigned char hash[] = { |
| 2422 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2423 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2424 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2425 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2426 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2427 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2428 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2429 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2430 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2431 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2432 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2433 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2434 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2435 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2436 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2437 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2438 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2439 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2440 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2441 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2442 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2443 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2444 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2445 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2446 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2447 | } |
| 2448 | /* END_CASE */ |
| 2449 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2450 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2451 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2452 | { |
| 2453 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2454 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2455 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2456 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2457 | size_t hash_len; |
| 2458 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2459 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2460 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2461 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2462 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2463 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2464 | hash, expected_size - 1, &hash_len ), |
| 2465 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2466 | |
| 2467 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2468 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2469 | } |
| 2470 | /* END_CASE */ |
| 2471 | |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2472 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2473 | void hash_clone_source_state( ) |
| 2474 | { |
| 2475 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2476 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2477 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2478 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2479 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2480 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2481 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2482 | size_t hash_len; |
| 2483 | |
| 2484 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2485 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2486 | |
| 2487 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2488 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2489 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2490 | hash, sizeof( hash ), &hash_len ) ); |
| 2491 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2492 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2493 | |
| 2494 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2495 | PSA_ERROR_BAD_STATE ); |
| 2496 | |
| 2497 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2498 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2499 | hash, sizeof( hash ), &hash_len ) ); |
| 2500 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2501 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2502 | hash, sizeof( hash ), &hash_len ) ); |
| 2503 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2504 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2505 | hash, sizeof( hash ), &hash_len ) ); |
| 2506 | |
| 2507 | exit: |
| 2508 | psa_hash_abort( &op_source ); |
| 2509 | psa_hash_abort( &op_init ); |
| 2510 | psa_hash_abort( &op_setup ); |
| 2511 | psa_hash_abort( &op_finished ); |
| 2512 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2513 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2514 | } |
| 2515 | /* END_CASE */ |
| 2516 | |
| 2517 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2518 | void hash_clone_target_state( ) |
| 2519 | { |
| 2520 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2521 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2522 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2523 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2524 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2525 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2526 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2527 | size_t hash_len; |
| 2528 | |
| 2529 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2530 | |
| 2531 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2532 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2533 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2534 | hash, sizeof( hash ), &hash_len ) ); |
| 2535 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2536 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2537 | |
| 2538 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2539 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2540 | hash, sizeof( hash ), &hash_len ) ); |
| 2541 | |
| 2542 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2543 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2544 | PSA_ERROR_BAD_STATE ); |
| 2545 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2546 | PSA_ERROR_BAD_STATE ); |
| 2547 | |
| 2548 | exit: |
| 2549 | psa_hash_abort( &op_target ); |
| 2550 | psa_hash_abort( &op_init ); |
| 2551 | psa_hash_abort( &op_setup ); |
| 2552 | psa_hash_abort( &op_finished ); |
| 2553 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2554 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2555 | } |
| 2556 | /* END_CASE */ |
| 2557 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2558 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2559 | void mac_operation_init( ) |
| 2560 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2561 | const uint8_t input[1] = { 0 }; |
| 2562 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2563 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2564 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2565 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2566 | * to supress the Clang warning for the test. */ |
| 2567 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2568 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2569 | psa_mac_operation_t zero; |
| 2570 | |
| 2571 | memset( &zero, 0, sizeof( zero ) ); |
| 2572 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2573 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2574 | TEST_EQUAL( psa_mac_update( &func, |
| 2575 | input, sizeof( input ) ), |
| 2576 | PSA_ERROR_BAD_STATE ); |
| 2577 | TEST_EQUAL( psa_mac_update( &init, |
| 2578 | input, sizeof( input ) ), |
| 2579 | PSA_ERROR_BAD_STATE ); |
| 2580 | TEST_EQUAL( psa_mac_update( &zero, |
| 2581 | input, sizeof( input ) ), |
| 2582 | PSA_ERROR_BAD_STATE ); |
| 2583 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2584 | /* A default MAC operation should be abortable without error. */ |
| 2585 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2586 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2587 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2588 | } |
| 2589 | /* END_CASE */ |
| 2590 | |
| 2591 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2592 | void mac_setup( int key_type_arg, |
| 2593 | data_t *key, |
| 2594 | int alg_arg, |
| 2595 | int expected_status_arg ) |
| 2596 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2597 | psa_key_type_t key_type = key_type_arg; |
| 2598 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2599 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2600 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2601 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2602 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2603 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2604 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2605 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2606 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2607 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2608 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2609 | &operation, &status ) ) |
| 2610 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2611 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2612 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2613 | /* The operation object should be reusable. */ |
| 2614 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2615 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2616 | smoke_test_key_data, |
| 2617 | sizeof( smoke_test_key_data ), |
| 2618 | KNOWN_SUPPORTED_MAC_ALG, |
| 2619 | &operation, &status ) ) |
| 2620 | goto exit; |
| 2621 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2622 | #endif |
| 2623 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2624 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2625 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2626 | } |
| 2627 | /* END_CASE */ |
| 2628 | |
| 2629 | /* BEGIN_CASE */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2630 | void mac_bad_order( ) |
| 2631 | { |
| 2632 | psa_key_handle_t handle = 0; |
| 2633 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2634 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
| 2635 | const uint8_t key[] = { |
| 2636 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2637 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2638 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2639 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2640 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2641 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2642 | size_t sign_mac_length = 0; |
| 2643 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2644 | const uint8_t verify_mac[] = { |
| 2645 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2646 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2647 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2648 | |
| 2649 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2650 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 2651 | psa_set_key_algorithm( &attributes, alg ); |
| 2652 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2653 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2654 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2655 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2656 | /* Call update without calling setup beforehand. */ |
| 2657 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2658 | PSA_ERROR_BAD_STATE ); |
| 2659 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2660 | |
| 2661 | /* Call sign finish without calling setup beforehand. */ |
| 2662 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2663 | &sign_mac_length), |
| 2664 | PSA_ERROR_BAD_STATE ); |
| 2665 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2666 | |
| 2667 | /* Call verify finish without calling setup beforehand. */ |
| 2668 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2669 | verify_mac, sizeof( verify_mac ) ), |
| 2670 | PSA_ERROR_BAD_STATE ); |
| 2671 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2672 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2673 | /* Call setup twice in a row. */ |
| 2674 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2675 | handle, alg ) ); |
| 2676 | TEST_EQUAL( psa_mac_sign_setup( &operation, |
| 2677 | handle, alg ), |
| 2678 | PSA_ERROR_BAD_STATE ); |
| 2679 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2680 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2681 | /* Call update after sign finish. */ |
| 2682 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2683 | handle, alg ) ); |
| 2684 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2685 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2686 | sign_mac, sizeof( sign_mac ), |
| 2687 | &sign_mac_length ) ); |
| 2688 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2689 | PSA_ERROR_BAD_STATE ); |
| 2690 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2691 | |
| 2692 | /* Call update after verify finish. */ |
| 2693 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2694 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2695 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2696 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2697 | verify_mac, sizeof( verify_mac ) ) ); |
| 2698 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2699 | PSA_ERROR_BAD_STATE ); |
| 2700 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2701 | |
| 2702 | /* Call sign finish twice in a row. */ |
| 2703 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2704 | handle, alg ) ); |
| 2705 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2706 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2707 | sign_mac, sizeof( sign_mac ), |
| 2708 | &sign_mac_length ) ); |
| 2709 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2710 | sign_mac, sizeof( sign_mac ), |
| 2711 | &sign_mac_length ), |
| 2712 | PSA_ERROR_BAD_STATE ); |
| 2713 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2714 | |
| 2715 | /* Call verify finish twice in a row. */ |
| 2716 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2717 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2718 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2719 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2720 | verify_mac, sizeof( verify_mac ) ) ); |
| 2721 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2722 | verify_mac, sizeof( verify_mac ) ), |
| 2723 | PSA_ERROR_BAD_STATE ); |
| 2724 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2725 | |
| 2726 | /* Setup sign but try verify. */ |
| 2727 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2728 | handle, alg ) ); |
| 2729 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2730 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2731 | verify_mac, sizeof( verify_mac ) ), |
| 2732 | PSA_ERROR_BAD_STATE ); |
| 2733 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2734 | |
| 2735 | /* Setup verify but try sign. */ |
| 2736 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2737 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2738 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2739 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2740 | sign_mac, sizeof( sign_mac ), |
| 2741 | &sign_mac_length ), |
| 2742 | PSA_ERROR_BAD_STATE ); |
| 2743 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2744 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2745 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2746 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2747 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2748 | PSA_DONE( ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2749 | } |
| 2750 | /* END_CASE */ |
| 2751 | |
| 2752 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2753 | void mac_sign( int key_type_arg, |
| 2754 | data_t *key, |
| 2755 | int alg_arg, |
| 2756 | data_t *input, |
| 2757 | data_t *expected_mac ) |
| 2758 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2759 | psa_key_handle_t handle = 0; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2760 | psa_key_type_t key_type = key_type_arg; |
| 2761 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2762 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2763 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2764 | /* Leave a little extra room in the output buffer. At the end of the |
| 2765 | * test, we'll check that the implementation didn't overwrite onto |
| 2766 | * this extra room. */ |
| 2767 | uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10]; |
| 2768 | size_t mac_buffer_size = |
| 2769 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 2770 | size_t mac_length = 0; |
| 2771 | |
| 2772 | memset( actual_mac, '+', sizeof( actual_mac ) ); |
| 2773 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
| 2774 | TEST_ASSERT( expected_mac->len <= mac_buffer_size ); |
| 2775 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2776 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2777 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2778 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 2779 | psa_set_key_algorithm( &attributes, alg ); |
| 2780 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2781 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2782 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2783 | |
| 2784 | /* Calculate the MAC. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2785 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2786 | handle, alg ) ); |
| 2787 | PSA_ASSERT( psa_mac_update( &operation, |
| 2788 | input->x, input->len ) ); |
| 2789 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2790 | actual_mac, mac_buffer_size, |
| 2791 | &mac_length ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2792 | |
| 2793 | /* Compare with the expected value. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 2794 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2795 | actual_mac, mac_length ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2796 | |
| 2797 | /* Verify that the end of the buffer is untouched. */ |
| 2798 | TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+', |
| 2799 | sizeof( actual_mac ) - mac_length ) ); |
| 2800 | |
| 2801 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2802 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2803 | PSA_DONE( ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2804 | } |
| 2805 | /* END_CASE */ |
| 2806 | |
| 2807 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2808 | void mac_verify( int key_type_arg, |
| 2809 | data_t *key, |
| 2810 | int alg_arg, |
| 2811 | data_t *input, |
| 2812 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2813 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2814 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2815 | psa_key_type_t key_type = key_type_arg; |
| 2816 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2817 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2818 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2819 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2820 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2821 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2822 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2823 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2824 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 2825 | psa_set_key_algorithm( &attributes, alg ); |
| 2826 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2827 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2828 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2829 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2830 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 2831 | handle, alg ) ); |
| 2832 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2833 | PSA_ASSERT( psa_mac_update( &operation, |
| 2834 | input->x, input->len ) ); |
| 2835 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2836 | expected_mac->x, |
| 2837 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2838 | |
| 2839 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2840 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2841 | PSA_DONE( ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2842 | } |
| 2843 | /* END_CASE */ |
| 2844 | |
| 2845 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2846 | void cipher_operation_init( ) |
| 2847 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2848 | const uint8_t input[1] = { 0 }; |
| 2849 | unsigned char output[1] = { 0 }; |
| 2850 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2851 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2852 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2853 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2854 | * to supress the Clang warning for the test. */ |
| 2855 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2856 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2857 | psa_cipher_operation_t zero; |
| 2858 | |
| 2859 | memset( &zero, 0, sizeof( zero ) ); |
| 2860 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2861 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2862 | TEST_EQUAL( psa_cipher_update( &func, |
| 2863 | input, sizeof( input ), |
| 2864 | output, sizeof( output ), |
| 2865 | &output_length ), |
| 2866 | PSA_ERROR_BAD_STATE ); |
| 2867 | TEST_EQUAL( psa_cipher_update( &init, |
| 2868 | input, sizeof( input ), |
| 2869 | output, sizeof( output ), |
| 2870 | &output_length ), |
| 2871 | PSA_ERROR_BAD_STATE ); |
| 2872 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2873 | input, sizeof( input ), |
| 2874 | output, sizeof( output ), |
| 2875 | &output_length ), |
| 2876 | PSA_ERROR_BAD_STATE ); |
| 2877 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2878 | /* A default cipher operation should be abortable without error. */ |
| 2879 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2880 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2881 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2882 | } |
| 2883 | /* END_CASE */ |
| 2884 | |
| 2885 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2886 | void cipher_setup( int key_type_arg, |
| 2887 | data_t *key, |
| 2888 | int alg_arg, |
| 2889 | int expected_status_arg ) |
| 2890 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2891 | psa_key_type_t key_type = key_type_arg; |
| 2892 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2893 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2894 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2895 | psa_status_t status; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2896 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2897 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2898 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2899 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2900 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2901 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2902 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2903 | &operation, &status ) ) |
| 2904 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2905 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2906 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2907 | /* The operation object should be reusable. */ |
| 2908 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2909 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2910 | smoke_test_key_data, |
| 2911 | sizeof( smoke_test_key_data ), |
| 2912 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2913 | &operation, &status ) ) |
| 2914 | goto exit; |
| 2915 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2916 | #endif |
| 2917 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2918 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2919 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2920 | } |
| 2921 | /* END_CASE */ |
| 2922 | |
| 2923 | /* BEGIN_CASE */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2924 | void cipher_bad_order( ) |
| 2925 | { |
| 2926 | psa_key_handle_t handle = 0; |
| 2927 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2928 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2929 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2930 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2931 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2932 | const uint8_t key[] = { |
| 2933 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2934 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2935 | const uint8_t text[] = { |
| 2936 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2937 | 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2938 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2939 | size_t length = 0; |
| 2940 | |
| 2941 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2942 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2943 | psa_set_key_algorithm( &attributes, alg ); |
| 2944 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2945 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2946 | |
| 2947 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2948 | /* Call encrypt setup twice in a row. */ |
| 2949 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2950 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ), |
| 2951 | PSA_ERROR_BAD_STATE ); |
| 2952 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2953 | |
| 2954 | /* Call decrypt setup twice in a row. */ |
| 2955 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) ); |
| 2956 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ), |
| 2957 | PSA_ERROR_BAD_STATE ); |
| 2958 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2959 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2960 | /* Generate an IV without calling setup beforehand. */ |
| 2961 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2962 | buffer, sizeof( buffer ), |
| 2963 | &length ), |
| 2964 | PSA_ERROR_BAD_STATE ); |
| 2965 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2966 | |
| 2967 | /* Generate an IV twice in a row. */ |
| 2968 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2969 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2970 | buffer, sizeof( buffer ), |
| 2971 | &length ) ); |
| 2972 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2973 | buffer, sizeof( buffer ), |
| 2974 | &length ), |
| 2975 | PSA_ERROR_BAD_STATE ); |
| 2976 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2977 | |
| 2978 | /* Generate an IV after it's already set. */ |
| 2979 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2980 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2981 | iv, sizeof( iv ) ) ); |
| 2982 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2983 | buffer, sizeof( buffer ), |
| 2984 | &length ), |
| 2985 | PSA_ERROR_BAD_STATE ); |
| 2986 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2987 | |
| 2988 | /* Set an IV without calling setup beforehand. */ |
| 2989 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2990 | iv, sizeof( iv ) ), |
| 2991 | PSA_ERROR_BAD_STATE ); |
| 2992 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2993 | |
| 2994 | /* Set an IV after it's already set. */ |
| 2995 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2996 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2997 | iv, sizeof( iv ) ) ); |
| 2998 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2999 | iv, sizeof( iv ) ), |
| 3000 | PSA_ERROR_BAD_STATE ); |
| 3001 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3002 | |
| 3003 | /* Set an IV after it's already generated. */ |
| 3004 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3005 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3006 | buffer, sizeof( buffer ), |
| 3007 | &length ) ); |
| 3008 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3009 | iv, sizeof( iv ) ), |
| 3010 | PSA_ERROR_BAD_STATE ); |
| 3011 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3012 | |
| 3013 | /* Call update without calling setup beforehand. */ |
| 3014 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3015 | text, sizeof( text ), |
| 3016 | buffer, sizeof( buffer ), |
| 3017 | &length ), |
| 3018 | PSA_ERROR_BAD_STATE ); |
| 3019 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3020 | |
| 3021 | /* Call update without an IV where an IV is required. */ |
| 3022 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3023 | text, sizeof( text ), |
| 3024 | buffer, sizeof( buffer ), |
| 3025 | &length ), |
| 3026 | PSA_ERROR_BAD_STATE ); |
| 3027 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3028 | |
| 3029 | /* Call update after finish. */ |
| 3030 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3031 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3032 | iv, sizeof( iv ) ) ); |
| 3033 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3034 | buffer, sizeof( buffer ), &length ) ); |
| 3035 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3036 | text, sizeof( text ), |
| 3037 | buffer, sizeof( buffer ), |
| 3038 | &length ), |
| 3039 | PSA_ERROR_BAD_STATE ); |
| 3040 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3041 | |
| 3042 | /* Call finish without calling setup beforehand. */ |
| 3043 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3044 | buffer, sizeof( buffer ), &length ), |
| 3045 | PSA_ERROR_BAD_STATE ); |
| 3046 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3047 | |
| 3048 | /* Call finish without an IV where an IV is required. */ |
| 3049 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3050 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3051 | * for cipher modes with padding. */ |
| 3052 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3053 | buffer, sizeof( buffer ), &length ), |
| 3054 | PSA_ERROR_BAD_STATE ); |
| 3055 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3056 | |
| 3057 | /* Call finish twice in a row. */ |
| 3058 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3059 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3060 | iv, sizeof( iv ) ) ); |
| 3061 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3062 | buffer, sizeof( buffer ), &length ) ); |
| 3063 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3064 | buffer, sizeof( buffer ), &length ), |
| 3065 | PSA_ERROR_BAD_STATE ); |
| 3066 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3067 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3068 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 3069 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3070 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3071 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3072 | } |
| 3073 | /* END_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3074 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3075 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3076 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3077 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3078 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3079 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3080 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3081 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3082 | psa_status_t status; |
| 3083 | psa_key_type_t key_type = key_type_arg; |
| 3084 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3085 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3086 | unsigned char *output = NULL; |
| 3087 | size_t output_buffer_size = 0; |
| 3088 | size_t function_output_length = 0; |
| 3089 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3090 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3091 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3092 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3093 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3094 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3095 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3096 | psa_set_key_algorithm( &attributes, alg ); |
| 3097 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3098 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3099 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3100 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3101 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3102 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3103 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3104 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3105 | output_buffer_size = ( (size_t) input->len + |
| 3106 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3107 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3108 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3109 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3110 | input->x, input->len, |
| 3111 | output, output_buffer_size, |
| 3112 | &function_output_length ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3113 | total_output_length += function_output_length; |
| 3114 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3115 | output + total_output_length, |
| 3116 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3117 | &function_output_length ); |
| 3118 | total_output_length += function_output_length; |
| 3119 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3120 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3121 | if( expected_status == PSA_SUCCESS ) |
| 3122 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3123 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3124 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3125 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | exit: |
| 3129 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3130 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3131 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3132 | } |
| 3133 | /* END_CASE */ |
| 3134 | |
| 3135 | /* BEGIN_CASE */ |
| 3136 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3137 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3138 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3139 | int first_part_size_arg, |
| 3140 | int output1_length_arg, int output2_length_arg, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3141 | data_t *expected_output ) |
| 3142 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3143 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3144 | psa_key_type_t key_type = key_type_arg; |
| 3145 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3146 | size_t first_part_size = first_part_size_arg; |
| 3147 | size_t output1_length = output1_length_arg; |
| 3148 | size_t output2_length = output2_length_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3149 | unsigned char *output = NULL; |
| 3150 | size_t output_buffer_size = 0; |
| 3151 | size_t function_output_length = 0; |
| 3152 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3153 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3154 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3155 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3156 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3157 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3158 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3159 | psa_set_key_algorithm( &attributes, alg ); |
| 3160 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3161 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3162 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3163 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3164 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3165 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3166 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3167 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3168 | output_buffer_size = ( (size_t) input->len + |
| 3169 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3170 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3171 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3172 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3173 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3174 | output, output_buffer_size, |
| 3175 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3176 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3177 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3178 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3179 | input->x + first_part_size, |
| 3180 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3181 | output + total_output_length, |
| 3182 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3183 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3184 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3185 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3186 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3187 | output + total_output_length, |
| 3188 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3189 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3190 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3191 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3192 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3193 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3194 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3195 | |
| 3196 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3197 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3198 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3199 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3200 | } |
| 3201 | /* END_CASE */ |
| 3202 | |
| 3203 | /* BEGIN_CASE */ |
| 3204 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3205 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3206 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3207 | int first_part_size_arg, |
| 3208 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3209 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3210 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3211 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3212 | |
| 3213 | psa_key_type_t key_type = key_type_arg; |
| 3214 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3215 | size_t first_part_size = first_part_size_arg; |
| 3216 | size_t output1_length = output1_length_arg; |
| 3217 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3218 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3219 | size_t output_buffer_size = 0; |
| 3220 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3221 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3222 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3223 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3224 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3225 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3226 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3227 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3228 | psa_set_key_algorithm( &attributes, alg ); |
| 3229 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3230 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3231 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3232 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3233 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3234 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3235 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3236 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3237 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3238 | output_buffer_size = ( (size_t) input->len + |
| 3239 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3240 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3241 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3242 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3243 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3244 | input->x, first_part_size, |
| 3245 | output, output_buffer_size, |
| 3246 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3247 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3248 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3249 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3250 | input->x + first_part_size, |
| 3251 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3252 | output + total_output_length, |
| 3253 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3254 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3255 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3256 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3257 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3258 | output + total_output_length, |
| 3259 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3260 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3261 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3262 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3263 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3264 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3265 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3266 | |
| 3267 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3268 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3269 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3270 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3271 | } |
| 3272 | /* END_CASE */ |
| 3273 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3274 | /* BEGIN_CASE */ |
| 3275 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3276 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3277 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3278 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3279 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3280 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3281 | psa_status_t status; |
| 3282 | psa_key_type_t key_type = key_type_arg; |
| 3283 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3284 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3285 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3286 | size_t output_buffer_size = 0; |
| 3287 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3288 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3289 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3290 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3291 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3292 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3293 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3294 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3295 | psa_set_key_algorithm( &attributes, alg ); |
| 3296 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3297 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3298 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3299 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3300 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3301 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3302 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3303 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3304 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3305 | output_buffer_size = ( (size_t) input->len + |
| 3306 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3307 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3308 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3309 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3310 | input->x, input->len, |
| 3311 | output, output_buffer_size, |
| 3312 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3313 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3314 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3315 | output + total_output_length, |
| 3316 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3317 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3318 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3319 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3320 | |
| 3321 | if( expected_status == PSA_SUCCESS ) |
| 3322 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3323 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3324 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3325 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3326 | } |
| 3327 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3328 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3329 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3330 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3331 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3332 | } |
| 3333 | /* END_CASE */ |
| 3334 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3335 | /* BEGIN_CASE */ |
| 3336 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3337 | data_t *key, |
| 3338 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3339 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3340 | psa_key_handle_t handle = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3341 | psa_key_type_t key_type = key_type_arg; |
| 3342 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 3343 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3344 | size_t iv_size = 16; |
| 3345 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3346 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3347 | size_t output1_size = 0; |
| 3348 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3349 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3350 | size_t output2_size = 0; |
| 3351 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3352 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3353 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3354 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3355 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3356 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3357 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3358 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3359 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3360 | psa_set_key_algorithm( &attributes, alg ); |
| 3361 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3362 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3363 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3364 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3365 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3366 | handle, alg ) ); |
| 3367 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3368 | handle, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3369 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3370 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3371 | iv, iv_size, |
| 3372 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3373 | output1_size = ( (size_t) input->len + |
| 3374 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3375 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3376 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3377 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3378 | output1, output1_size, |
| 3379 | &output1_length ) ); |
| 3380 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3381 | output1 + output1_length, |
| 3382 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3383 | &function_output_length ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3384 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3385 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3386 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3387 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3388 | |
| 3389 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3390 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3391 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3392 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3393 | iv, iv_length ) ); |
| 3394 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3395 | output2, output2_size, |
| 3396 | &output2_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3397 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3398 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3399 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3400 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3401 | &function_output_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3402 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3403 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3404 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3405 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3406 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3407 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3408 | |
| 3409 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3410 | mbedtls_free( output1 ); |
| 3411 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3412 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3413 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3414 | } |
| 3415 | /* END_CASE */ |
| 3416 | |
| 3417 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3418 | void cipher_verify_output_multipart( int alg_arg, |
| 3419 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3420 | data_t *key, |
| 3421 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3422 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3423 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3424 | psa_key_handle_t handle = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3425 | psa_key_type_t key_type = key_type_arg; |
| 3426 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3427 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3428 | unsigned char iv[16] = {0}; |
| 3429 | size_t iv_size = 16; |
| 3430 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3431 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3432 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3433 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3434 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3435 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3436 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3437 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3438 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3439 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3440 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3441 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3442 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3443 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3444 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3445 | psa_set_key_algorithm( &attributes, alg ); |
| 3446 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3447 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3448 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3449 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3450 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3451 | handle, alg ) ); |
| 3452 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3453 | handle, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3454 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3455 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3456 | iv, iv_size, |
| 3457 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3458 | output1_buffer_size = ( (size_t) input->len + |
| 3459 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3460 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3461 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3462 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3463 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3464 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3465 | output1, output1_buffer_size, |
| 3466 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3467 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3468 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3469 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3470 | input->x + first_part_size, |
| 3471 | input->len - first_part_size, |
| 3472 | output1, output1_buffer_size, |
| 3473 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3474 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3475 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3476 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3477 | output1 + output1_length, |
| 3478 | output1_buffer_size - output1_length, |
| 3479 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3480 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3481 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3482 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3483 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3484 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3485 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3486 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3487 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3488 | iv, iv_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3489 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3490 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3491 | output2, output2_buffer_size, |
| 3492 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3493 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3494 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3495 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3496 | output1 + first_part_size, |
| 3497 | output1_length - first_part_size, |
| 3498 | output2, output2_buffer_size, |
| 3499 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3500 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3501 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3502 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3503 | output2 + output2_length, |
| 3504 | output2_buffer_size - output2_length, |
| 3505 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3506 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3507 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3508 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3509 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3510 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3511 | |
| 3512 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3513 | mbedtls_free( output1 ); |
| 3514 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3515 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3516 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3517 | } |
| 3518 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3519 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3520 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3521 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3522 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3523 | data_t *nonce, |
| 3524 | data_t *additional_data, |
| 3525 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3526 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3527 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3528 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3529 | psa_key_type_t key_type = key_type_arg; |
| 3530 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3531 | unsigned char *output_data = NULL; |
| 3532 | size_t output_size = 0; |
| 3533 | size_t output_length = 0; |
| 3534 | unsigned char *output_data2 = NULL; |
| 3535 | size_t output_length2 = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3536 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3537 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3538 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3539 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3540 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3541 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3542 | * should be exact. */ |
| 3543 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3544 | TEST_EQUAL( output_size, |
| 3545 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3546 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3547 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3548 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3549 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3550 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3551 | psa_set_key_algorithm( &attributes, alg ); |
| 3552 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3553 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3554 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3555 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3556 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3557 | TEST_EQUAL( psa_aead_encrypt( handle, alg, |
| 3558 | nonce->x, nonce->len, |
| 3559 | additional_data->x, |
| 3560 | additional_data->len, |
| 3561 | input_data->x, input_data->len, |
| 3562 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3563 | &output_length ), |
| 3564 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3565 | |
| 3566 | if( PSA_SUCCESS == expected_result ) |
| 3567 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3568 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3569 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3570 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3571 | * should be exact. */ |
| 3572 | TEST_EQUAL( input_data->len, |
| 3573 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) ); |
| 3574 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3575 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3576 | nonce->x, nonce->len, |
| 3577 | additional_data->x, |
| 3578 | additional_data->len, |
| 3579 | output_data, output_length, |
| 3580 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3581 | &output_length2 ), |
| 3582 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3583 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3584 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3585 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3586 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3587 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3588 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3589 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3590 | mbedtls_free( output_data ); |
| 3591 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3592 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3593 | } |
| 3594 | /* END_CASE */ |
| 3595 | |
| 3596 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3597 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3598 | int alg_arg, |
| 3599 | data_t *nonce, |
| 3600 | data_t *additional_data, |
| 3601 | data_t *input_data, |
| 3602 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3603 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3604 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3605 | psa_key_type_t key_type = key_type_arg; |
| 3606 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3607 | unsigned char *output_data = NULL; |
| 3608 | size_t output_size = 0; |
| 3609 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3610 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3611 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3612 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3613 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3614 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3615 | * should be exact. */ |
| 3616 | TEST_EQUAL( output_size, |
| 3617 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3618 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3619 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3620 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3621 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3622 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3623 | psa_set_key_algorithm( &attributes, alg ); |
| 3624 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3625 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3626 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3627 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3628 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3629 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 3630 | nonce->x, nonce->len, |
| 3631 | additional_data->x, additional_data->len, |
| 3632 | input_data->x, input_data->len, |
| 3633 | output_data, output_size, |
| 3634 | &output_length ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3635 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3636 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3637 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3638 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3639 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3640 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3641 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3642 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3643 | } |
| 3644 | /* END_CASE */ |
| 3645 | |
| 3646 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3647 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3648 | int alg_arg, |
| 3649 | data_t *nonce, |
| 3650 | data_t *additional_data, |
| 3651 | data_t *input_data, |
| 3652 | data_t *expected_data, |
| 3653 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3654 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3655 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3656 | psa_key_type_t key_type = key_type_arg; |
| 3657 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3658 | unsigned char *output_data = NULL; |
| 3659 | size_t output_size = 0; |
| 3660 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3661 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3662 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3663 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3664 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3665 | output_size = input_data->len - tag_length; |
| 3666 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3667 | * should be exact. */ |
| 3668 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3669 | TEST_EQUAL( output_size, |
| 3670 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3671 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3672 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3673 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3674 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3675 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3676 | psa_set_key_algorithm( &attributes, alg ); |
| 3677 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3678 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3679 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3680 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3681 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3682 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3683 | nonce->x, nonce->len, |
| 3684 | additional_data->x, |
| 3685 | additional_data->len, |
| 3686 | input_data->x, input_data->len, |
| 3687 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3688 | &output_length ), |
| 3689 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3690 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3691 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3692 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3693 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3694 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3695 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3696 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3697 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3698 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3699 | } |
| 3700 | /* END_CASE */ |
| 3701 | |
| 3702 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3703 | void signature_size( int type_arg, |
| 3704 | int bits, |
| 3705 | int alg_arg, |
| 3706 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3707 | { |
| 3708 | psa_key_type_t type = type_arg; |
| 3709 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3710 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3711 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3712 | exit: |
| 3713 | ; |
| 3714 | } |
| 3715 | /* END_CASE */ |
| 3716 | |
| 3717 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3718 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 3719 | int alg_arg, data_t *input_data, |
| 3720 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3721 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3722 | psa_key_handle_t handle = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3723 | psa_key_type_t key_type = key_type_arg; |
| 3724 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3725 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3726 | unsigned char *signature = NULL; |
| 3727 | size_t signature_size; |
| 3728 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3729 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3730 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3731 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3732 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3733 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 3734 | psa_set_key_algorithm( &attributes, alg ); |
| 3735 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3736 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3737 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3738 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3739 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3740 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3741 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3742 | /* Allocate a buffer which has the size advertized by the |
| 3743 | * library. */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3744 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3745 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3746 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3747 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3748 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3749 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3750 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3751 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3752 | input_data->x, input_data->len, |
| 3753 | signature, signature_size, |
| 3754 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3755 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3756 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3757 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3758 | |
| 3759 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3760 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3761 | psa_destroy_key( handle ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3762 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3763 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3764 | } |
| 3765 | /* END_CASE */ |
| 3766 | |
| 3767 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3768 | void sign_fail( int key_type_arg, data_t *key_data, |
| 3769 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3770 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3771 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3772 | psa_key_handle_t handle = 0; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3773 | psa_key_type_t key_type = key_type_arg; |
| 3774 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3775 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3776 | psa_status_t actual_status; |
| 3777 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3778 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3779 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3780 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3781 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3782 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3783 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3784 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3785 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3786 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 3787 | psa_set_key_algorithm( &attributes, alg ); |
| 3788 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3789 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3790 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3791 | &handle ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3792 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3793 | actual_status = psa_asymmetric_sign( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3794 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3795 | signature, signature_size, |
| 3796 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3797 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3798 | /* The value of *signature_length is unspecified on error, but |
| 3799 | * whatever it is, it should be less than signature_size, so that |
| 3800 | * if the caller tries to read *signature_length bytes without |
| 3801 | * checking the error code then they don't overflow a buffer. */ |
| 3802 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3803 | |
| 3804 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3805 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3806 | psa_destroy_key( handle ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3807 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3808 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3809 | } |
| 3810 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3811 | |
| 3812 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3813 | void sign_verify( int key_type_arg, data_t *key_data, |
| 3814 | int alg_arg, data_t *input_data ) |
| 3815 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3816 | psa_key_handle_t handle = 0; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3817 | psa_key_type_t key_type = key_type_arg; |
| 3818 | psa_algorithm_t alg = alg_arg; |
| 3819 | size_t key_bits; |
| 3820 | unsigned char *signature = NULL; |
| 3821 | size_t signature_size; |
| 3822 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3823 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3824 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3825 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3826 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3827 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 3828 | psa_set_key_algorithm( &attributes, alg ); |
| 3829 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3830 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3831 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3832 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3833 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3834 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3835 | |
| 3836 | /* Allocate a buffer which has the size advertized by the |
| 3837 | * library. */ |
| 3838 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3839 | key_bits, alg ); |
| 3840 | TEST_ASSERT( signature_size != 0 ); |
| 3841 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3842 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3843 | |
| 3844 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3845 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3846 | input_data->x, input_data->len, |
| 3847 | signature, signature_size, |
| 3848 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3849 | /* Check that the signature length looks sensible. */ |
| 3850 | TEST_ASSERT( signature_length <= signature_size ); |
| 3851 | TEST_ASSERT( signature_length > 0 ); |
| 3852 | |
| 3853 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3854 | PSA_ASSERT( psa_asymmetric_verify( |
| 3855 | handle, alg, |
| 3856 | input_data->x, input_data->len, |
| 3857 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3858 | |
| 3859 | if( input_data->len != 0 ) |
| 3860 | { |
| 3861 | /* Flip a bit in the input and verify that the signature is now |
| 3862 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3863 | * because ECDSA may ignore the last few bits of the input. */ |
| 3864 | input_data->x[0] ^= 1; |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3865 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 3866 | input_data->x, input_data->len, |
| 3867 | signature, signature_length ), |
| 3868 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3872 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3873 | psa_destroy_key( handle ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3874 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3875 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3876 | } |
| 3877 | /* END_CASE */ |
| 3878 | |
| 3879 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3880 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 3881 | int alg_arg, data_t *hash_data, |
| 3882 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3883 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3884 | psa_key_handle_t handle = 0; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3885 | psa_key_type_t key_type = key_type_arg; |
| 3886 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3887 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3888 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3889 | TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
| 3890 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3891 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3892 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3893 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 3894 | psa_set_key_algorithm( &attributes, alg ); |
| 3895 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3896 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3897 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3898 | &handle ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3899 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3900 | PSA_ASSERT( psa_asymmetric_verify( handle, alg, |
| 3901 | hash_data->x, hash_data->len, |
| 3902 | signature_data->x, |
| 3903 | signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3904 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3905 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3906 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3907 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3908 | } |
| 3909 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3910 | |
| 3911 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3912 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 3913 | int alg_arg, data_t *hash_data, |
| 3914 | data_t *signature_data, |
| 3915 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3916 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3917 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3918 | psa_key_type_t key_type = key_type_arg; |
| 3919 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3920 | psa_status_t actual_status; |
| 3921 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3922 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3923 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3924 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3925 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3926 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 3927 | psa_set_key_algorithm( &attributes, alg ); |
| 3928 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3929 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3930 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3931 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3932 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3933 | actual_status = psa_asymmetric_verify( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3934 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3935 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3936 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3937 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3938 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3939 | |
| 3940 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3941 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3942 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3943 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3944 | } |
| 3945 | /* END_CASE */ |
| 3946 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3947 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3948 | void asymmetric_encrypt( int key_type_arg, |
| 3949 | data_t *key_data, |
| 3950 | int alg_arg, |
| 3951 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3952 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3953 | int expected_output_length_arg, |
| 3954 | int expected_status_arg ) |
| 3955 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3956 | psa_key_handle_t handle = 0; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3957 | psa_key_type_t key_type = key_type_arg; |
| 3958 | psa_algorithm_t alg = alg_arg; |
| 3959 | size_t expected_output_length = expected_output_length_arg; |
| 3960 | size_t key_bits; |
| 3961 | unsigned char *output = NULL; |
| 3962 | size_t output_size; |
| 3963 | size_t output_length = ~0; |
| 3964 | psa_status_t actual_status; |
| 3965 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3966 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3967 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3968 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3969 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3970 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3971 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3972 | psa_set_key_algorithm( &attributes, alg ); |
| 3973 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3974 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3975 | &handle ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3976 | |
| 3977 | /* Determine the maximum output length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3978 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3979 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3980 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3981 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3982 | |
| 3983 | /* Encrypt the input */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3984 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3985 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3986 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3987 | output, output_size, |
| 3988 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3989 | TEST_EQUAL( actual_status, expected_status ); |
| 3990 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3991 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3992 | /* If the label is empty, the test framework puts a non-null pointer |
| 3993 | * in label->x. Test that a null pointer works as well. */ |
| 3994 | if( label->len == 0 ) |
| 3995 | { |
| 3996 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3997 | if( output_size != 0 ) |
| 3998 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3999 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4000 | input_data->x, input_data->len, |
| 4001 | NULL, label->len, |
| 4002 | output, output_size, |
| 4003 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4004 | TEST_EQUAL( actual_status, expected_status ); |
| 4005 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4006 | } |
| 4007 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4008 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4009 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4010 | psa_destroy_key( handle ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4011 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4012 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4013 | } |
| 4014 | /* END_CASE */ |
| 4015 | |
| 4016 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4017 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4018 | data_t *key_data, |
| 4019 | int alg_arg, |
| 4020 | data_t *input_data, |
| 4021 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4022 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4023 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4024 | psa_key_type_t key_type = key_type_arg; |
| 4025 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4026 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4027 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4028 | size_t output_size; |
| 4029 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4030 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4031 | size_t output2_size; |
| 4032 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4033 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4034 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4035 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4036 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4037 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4038 | psa_set_key_algorithm( &attributes, alg ); |
| 4039 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4040 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4041 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4042 | &handle ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4043 | |
| 4044 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4045 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4046 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4047 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4048 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4049 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4050 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4051 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4052 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4053 | * the original plaintext because of the non-optional random |
| 4054 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4055 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 4056 | input_data->x, input_data->len, |
| 4057 | label->x, label->len, |
| 4058 | output, output_size, |
| 4059 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4060 | /* We don't know what ciphertext length to expect, but check that |
| 4061 | * it looks sensible. */ |
| 4062 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4063 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4064 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4065 | output, output_length, |
| 4066 | label->x, label->len, |
| 4067 | output2, output2_size, |
| 4068 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4069 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4070 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4071 | |
| 4072 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4073 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4074 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4075 | mbedtls_free( output ); |
| 4076 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4077 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4078 | } |
| 4079 | /* END_CASE */ |
| 4080 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4081 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4082 | void asymmetric_decrypt( int key_type_arg, |
| 4083 | data_t *key_data, |
| 4084 | int alg_arg, |
| 4085 | data_t *input_data, |
| 4086 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4087 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4088 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4089 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4090 | psa_key_type_t key_type = key_type_arg; |
| 4091 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4092 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4093 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4094 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4095 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4096 | |
Jaeden Amero | 412654a | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4097 | output_size = expected_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4098 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4099 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4100 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4101 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4102 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4103 | psa_set_key_algorithm( &attributes, alg ); |
| 4104 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4105 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4106 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4107 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4108 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4109 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4110 | input_data->x, input_data->len, |
| 4111 | label->x, label->len, |
| 4112 | output, |
| 4113 | output_size, |
| 4114 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4115 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4116 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4117 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4118 | /* If the label is empty, the test framework puts a non-null pointer |
| 4119 | * in label->x. Test that a null pointer works as well. */ |
| 4120 | if( label->len == 0 ) |
| 4121 | { |
| 4122 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4123 | if( output_size != 0 ) |
| 4124 | memset( output, 0, output_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4125 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4126 | input_data->x, input_data->len, |
| 4127 | NULL, label->len, |
| 4128 | output, |
| 4129 | output_size, |
| 4130 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4131 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4132 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4133 | } |
| 4134 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4135 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4136 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4137 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4138 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4139 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4140 | } |
| 4141 | /* END_CASE */ |
| 4142 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4143 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4144 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4145 | data_t *key_data, |
| 4146 | int alg_arg, |
| 4147 | data_t *input_data, |
| 4148 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4149 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4150 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4151 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4152 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4153 | psa_key_type_t key_type = key_type_arg; |
| 4154 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4155 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4156 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4157 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4158 | psa_status_t actual_status; |
| 4159 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4160 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4161 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4162 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4163 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4164 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4165 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4166 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4167 | psa_set_key_algorithm( &attributes, alg ); |
| 4168 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4169 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4170 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4171 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4172 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4173 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4174 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4175 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4176 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4177 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4178 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4179 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4180 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4181 | /* If the label is empty, the test framework puts a non-null pointer |
| 4182 | * in label->x. Test that a null pointer works as well. */ |
| 4183 | if( label->len == 0 ) |
| 4184 | { |
| 4185 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4186 | if( output_size != 0 ) |
| 4187 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4188 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4189 | input_data->x, input_data->len, |
| 4190 | NULL, label->len, |
| 4191 | output, output_size, |
| 4192 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4193 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4194 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4195 | } |
| 4196 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4197 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4198 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4199 | psa_destroy_key( handle ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4200 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4201 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4202 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4203 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4204 | |
| 4205 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4206 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4207 | { |
| 4208 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4209 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4210 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4211 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4212 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4213 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4214 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4215 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4216 | |
| 4217 | memset( &zero, 0, sizeof( zero ) ); |
| 4218 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4219 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4220 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4221 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4222 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4223 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4224 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4225 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4226 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4227 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4228 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4229 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4230 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4231 | } |
| 4232 | /* END_CASE */ |
| 4233 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4234 | /* BEGIN_CASE */ |
| 4235 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4236 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4237 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4238 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4239 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4240 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4241 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4242 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4243 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4244 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4245 | |
| 4246 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4247 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4248 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4249 | } |
| 4250 | /* END_CASE */ |
| 4251 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4252 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4253 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4254 | int expected_status_arg ) |
| 4255 | { |
| 4256 | psa_algorithm_t alg = alg_arg; |
| 4257 | size_t capacity = capacity_arg; |
| 4258 | psa_status_t expected_status = expected_status_arg; |
| 4259 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4260 | |
| 4261 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4262 | |
| 4263 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4264 | |
| 4265 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4266 | expected_status ); |
| 4267 | |
| 4268 | exit: |
| 4269 | psa_key_derivation_abort( &operation ); |
| 4270 | PSA_DONE( ); |
| 4271 | } |
| 4272 | /* END_CASE */ |
| 4273 | |
| 4274 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4275 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4276 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4277 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4278 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4279 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4280 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame^] | 4281 | int expected_status_arg3, |
| 4282 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4283 | { |
| 4284 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4285 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 4286 | 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] | 4287 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4288 | expected_status_arg2, |
| 4289 | expected_status_arg3}; |
| 4290 | data_t *inputs[] = {input1, input2, input3}; |
| 4291 | psa_key_handle_t handles[] = {0, 0, 0}; |
| 4292 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4293 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4294 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame^] | 4295 | psa_key_type_t output_key_type = output_key_type_arg; |
| 4296 | psa_key_handle_t output_handle = 0; |
| 4297 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4298 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4299 | |
| 4300 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4301 | |
| 4302 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4303 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4304 | |
| 4305 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4306 | |
| 4307 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4308 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4309 | if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4310 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4311 | psa_set_key_type( &attributes, key_types[i] ); |
| 4312 | PSA_ASSERT( psa_import_key( &attributes, |
| 4313 | inputs[i]->x, inputs[i]->len, |
| 4314 | &handles[i] ) ); |
| 4315 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
| 4316 | handles[i] ), |
| 4317 | expected_statuses[i] ); |
| 4318 | } |
| 4319 | else |
| 4320 | { |
| 4321 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4322 | &operation, steps[i], |
| 4323 | inputs[i]->x, inputs[i]->len ), |
| 4324 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4325 | } |
| 4326 | } |
| 4327 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame^] | 4328 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 4329 | { |
| 4330 | psa_reset_key_attributes( &attributes ); |
| 4331 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 4332 | psa_set_key_bits( &attributes, 8 ); |
| 4333 | actual_output_status = |
| 4334 | psa_key_derivation_output_key( &attributes, &operation, |
| 4335 | &output_handle ); |
| 4336 | } |
| 4337 | else |
| 4338 | { |
| 4339 | uint8_t buffer[1]; |
| 4340 | actual_output_status = |
| 4341 | psa_key_derivation_output_bytes( &operation, |
| 4342 | buffer, sizeof( buffer ) ); |
| 4343 | } |
| 4344 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 4345 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4346 | exit: |
| 4347 | psa_key_derivation_abort( &operation ); |
| 4348 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4349 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame^] | 4350 | psa_destroy_key( output_handle ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4351 | PSA_DONE( ); |
| 4352 | } |
| 4353 | /* END_CASE */ |
| 4354 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4355 | /* BEGIN_CASE */ |
| 4356 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4357 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4358 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4359 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4360 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4361 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4362 | unsigned char input1[] = "Input 1"; |
| 4363 | size_t input1_length = sizeof( input1 ); |
| 4364 | unsigned char input2[] = "Input 2"; |
| 4365 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4366 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4367 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4368 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4369 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4370 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4371 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4372 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4373 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4374 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4375 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4376 | psa_set_key_algorithm( &attributes, alg ); |
| 4377 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4378 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4379 | PSA_ASSERT( psa_import_key( &attributes, |
| 4380 | key_data, sizeof( key_data ), |
| 4381 | &handle ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4382 | |
| 4383 | /* valid key derivation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4384 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4385 | input1, input1_length, |
| 4386 | input2, input2_length, |
| 4387 | capacity ) ) |
| 4388 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4389 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4390 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4391 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4392 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4393 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4394 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4395 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4396 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4397 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4398 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4399 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4400 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4401 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4402 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4403 | } |
| 4404 | /* END_CASE */ |
| 4405 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4406 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4407 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4408 | { |
| 4409 | uint8_t output_buffer[16]; |
| 4410 | size_t buffer_size = 16; |
| 4411 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4412 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4413 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4414 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4415 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4416 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4417 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4418 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4419 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4420 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4421 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4422 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4423 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4424 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4425 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4426 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4427 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4428 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4429 | |
| 4430 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4431 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4432 | } |
| 4433 | /* END_CASE */ |
| 4434 | |
| 4435 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4436 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4437 | int step1_arg, data_t *input1, |
| 4438 | int step2_arg, data_t *input2, |
| 4439 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4440 | int requested_capacity_arg, |
| 4441 | data_t *expected_output1, |
| 4442 | data_t *expected_output2 ) |
| 4443 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4444 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4445 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4446 | data_t *inputs[] = {input1, input2, input3}; |
| 4447 | psa_key_handle_t handles[] = {0, 0, 0}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4448 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4449 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4450 | uint8_t *expected_outputs[2] = |
| 4451 | {expected_output1->x, expected_output2->x}; |
| 4452 | size_t output_sizes[2] = |
| 4453 | {expected_output1->len, expected_output2->len}; |
| 4454 | size_t output_buffer_size = 0; |
| 4455 | uint8_t *output_buffer = NULL; |
| 4456 | size_t expected_capacity; |
| 4457 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4458 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4459 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4460 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4461 | |
| 4462 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4463 | { |
| 4464 | if( output_sizes[i] > output_buffer_size ) |
| 4465 | output_buffer_size = output_sizes[i]; |
| 4466 | if( output_sizes[i] == 0 ) |
| 4467 | expected_outputs[i] = NULL; |
| 4468 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4469 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4470 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4471 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4472 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4473 | psa_set_key_algorithm( &attributes, alg ); |
| 4474 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4475 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4476 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4477 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4478 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4479 | requested_capacity ) ); |
| 4480 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4481 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4482 | switch( steps[i] ) |
| 4483 | { |
| 4484 | case 0: |
| 4485 | break; |
| 4486 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4487 | PSA_ASSERT( psa_import_key( &attributes, |
| 4488 | inputs[i]->x, inputs[i]->len, |
| 4489 | &handles[i] ) ); |
| 4490 | PSA_ASSERT( psa_key_derivation_input_key( |
| 4491 | &operation, steps[i], |
| 4492 | handles[i] ) ); |
| 4493 | break; |
| 4494 | default: |
| 4495 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4496 | &operation, steps[i], |
| 4497 | inputs[i]->x, inputs[i]->len ) ); |
| 4498 | break; |
| 4499 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4500 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4501 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4502 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4503 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4504 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4505 | expected_capacity = requested_capacity; |
| 4506 | |
| 4507 | /* Expansion phase. */ |
| 4508 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4509 | { |
| 4510 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4511 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4512 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4513 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4514 | { |
| 4515 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4516 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4517 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4518 | continue; |
| 4519 | } |
| 4520 | else if( expected_capacity == 0 || |
| 4521 | output_sizes[i] > expected_capacity ) |
| 4522 | { |
| 4523 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4524 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4525 | expected_capacity = 0; |
| 4526 | continue; |
| 4527 | } |
| 4528 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4529 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4530 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4531 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4532 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4533 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4534 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4535 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4536 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4537 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4538 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4539 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4540 | |
| 4541 | exit: |
| 4542 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4543 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4544 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4545 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4546 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4547 | } |
| 4548 | /* END_CASE */ |
| 4549 | |
| 4550 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4551 | void derive_full( int alg_arg, |
| 4552 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4553 | data_t *input1, |
| 4554 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4555 | int requested_capacity_arg ) |
| 4556 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4557 | psa_key_handle_t handle = 0; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4558 | psa_algorithm_t alg = alg_arg; |
| 4559 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4560 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4561 | unsigned char output_buffer[16]; |
| 4562 | size_t expected_capacity = requested_capacity; |
| 4563 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4564 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4565 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4566 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4567 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4568 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4569 | psa_set_key_algorithm( &attributes, alg ); |
| 4570 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4571 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4572 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4573 | &handle ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4574 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4575 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4576 | input1->x, input1->len, |
| 4577 | input2->x, input2->len, |
| 4578 | requested_capacity ) ) |
| 4579 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4580 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4581 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4582 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4583 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4584 | |
| 4585 | /* Expansion phase. */ |
| 4586 | while( current_capacity > 0 ) |
| 4587 | { |
| 4588 | size_t read_size = sizeof( output_buffer ); |
| 4589 | if( read_size > current_capacity ) |
| 4590 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4591 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4592 | output_buffer, |
| 4593 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4594 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4595 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4596 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4597 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4598 | } |
| 4599 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4600 | /* Check that the operation refuses to go over capacity. */ |
| 4601 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4602 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4603 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4604 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4605 | |
| 4606 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4607 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4608 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4609 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4610 | } |
| 4611 | /* END_CASE */ |
| 4612 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4613 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4614 | void derive_key_exercise( int alg_arg, |
| 4615 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4616 | data_t *input1, |
| 4617 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4618 | int derived_type_arg, |
| 4619 | int derived_bits_arg, |
| 4620 | int derived_usage_arg, |
| 4621 | int derived_alg_arg ) |
| 4622 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4623 | psa_key_handle_t base_handle = 0; |
| 4624 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4625 | psa_algorithm_t alg = alg_arg; |
| 4626 | psa_key_type_t derived_type = derived_type_arg; |
| 4627 | size_t derived_bits = derived_bits_arg; |
| 4628 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4629 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4630 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4631 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4632 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4633 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4634 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4635 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4636 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4637 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4638 | psa_set_key_algorithm( &attributes, alg ); |
| 4639 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4640 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4641 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4642 | |
| 4643 | /* Derive a key. */ |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4644 | if ( setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4645 | input1->x, input1->len, |
| 4646 | input2->x, input2->len, capacity ) ) |
| 4647 | goto exit; |
| 4648 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4649 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4650 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4651 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4652 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4653 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4654 | &derived_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4655 | |
| 4656 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4657 | PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) ); |
| 4658 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4659 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4660 | |
| 4661 | /* Exercise the derived key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4662 | if( ! exercise_key( derived_handle, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4663 | goto exit; |
| 4664 | |
| 4665 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4666 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4667 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4668 | psa_destroy_key( base_handle ); |
| 4669 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4670 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4671 | } |
| 4672 | /* END_CASE */ |
| 4673 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4674 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4675 | void derive_key_export( int alg_arg, |
| 4676 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4677 | data_t *input1, |
| 4678 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4679 | int bytes1_arg, |
| 4680 | int bytes2_arg ) |
| 4681 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4682 | psa_key_handle_t base_handle = 0; |
| 4683 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4684 | psa_algorithm_t alg = alg_arg; |
| 4685 | size_t bytes1 = bytes1_arg; |
| 4686 | size_t bytes2 = bytes2_arg; |
| 4687 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4688 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4689 | uint8_t *output_buffer = NULL; |
| 4690 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4691 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4692 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4693 | size_t length; |
| 4694 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4695 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4696 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4697 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4698 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4699 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4700 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4701 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4702 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 4703 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4704 | |
| 4705 | /* Derive some material and output it. */ |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4706 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4707 | input1->x, input1->len, |
| 4708 | input2->x, input2->len, capacity ) ) |
| 4709 | goto exit; |
| 4710 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4711 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4712 | output_buffer, |
| 4713 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4714 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4715 | |
| 4716 | /* 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] | 4717 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4718 | input1->x, input1->len, |
| 4719 | input2->x, input2->len, capacity ) ) |
| 4720 | goto exit; |
| 4721 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4722 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4723 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 4724 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4725 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4726 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4727 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4728 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4729 | export_buffer, bytes1, |
| 4730 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4731 | TEST_EQUAL( length, bytes1 ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4732 | PSA_ASSERT( psa_destroy_key( derived_handle ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4733 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4734 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4735 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4736 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4737 | export_buffer + bytes1, bytes2, |
| 4738 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4739 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4740 | |
| 4741 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4742 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 4743 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4744 | |
| 4745 | exit: |
| 4746 | mbedtls_free( output_buffer ); |
| 4747 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4748 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4749 | psa_destroy_key( base_handle ); |
| 4750 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4751 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4752 | } |
| 4753 | /* END_CASE */ |
| 4754 | |
| 4755 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4756 | void derive_key( int alg_arg, |
| 4757 | data_t *key_data, data_t *input1, data_t *input2, |
| 4758 | int type_arg, int bits_arg, |
| 4759 | int expected_status_arg ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4760 | { |
| 4761 | psa_key_handle_t base_handle = 0; |
| 4762 | psa_key_handle_t derived_handle = 0; |
| 4763 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4764 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4765 | size_t bits = bits_arg; |
| 4766 | psa_status_t expected_status = expected_status_arg; |
| 4767 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4768 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4769 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4770 | |
| 4771 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4772 | |
| 4773 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4774 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4775 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 4776 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 4777 | &base_handle ) ); |
| 4778 | |
| 4779 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4780 | input1->x, input1->len, |
| 4781 | input2->x, input2->len, SIZE_MAX ) ) |
| 4782 | goto exit; |
| 4783 | |
| 4784 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4785 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4786 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4787 | psa_set_key_bits( &derived_attributes, bits ); |
| 4788 | TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 4789 | &derived_handle ), |
| 4790 | expected_status ); |
| 4791 | |
| 4792 | exit: |
| 4793 | psa_key_derivation_abort( &operation ); |
| 4794 | psa_destroy_key( base_handle ); |
| 4795 | psa_destroy_key( derived_handle ); |
| 4796 | PSA_DONE( ); |
| 4797 | } |
| 4798 | /* END_CASE */ |
| 4799 | |
| 4800 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4801 | void key_agreement_setup( int alg_arg, |
| 4802 | int our_key_type_arg, data_t *our_key_data, |
| 4803 | data_t *peer_key_data, |
| 4804 | int expected_status_arg ) |
| 4805 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4806 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4807 | psa_algorithm_t alg = alg_arg; |
| 4808 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4809 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4810 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4811 | psa_status_t expected_status = expected_status_arg; |
| 4812 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4813 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4814 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4815 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4816 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4817 | psa_set_key_algorithm( &attributes, alg ); |
| 4818 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4819 | PSA_ASSERT( psa_import_key( &attributes, |
| 4820 | our_key_data->x, our_key_data->len, |
| 4821 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4822 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4823 | /* The tests currently include inputs that should fail at either step. |
| 4824 | * Test cases that fail at the setup step should be changed to call |
| 4825 | * key_derivation_setup instead, and this function should be renamed |
| 4826 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4827 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4828 | if( status == PSA_SUCCESS ) |
| 4829 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4830 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 4831 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 4832 | our_key, |
| 4833 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4834 | expected_status ); |
| 4835 | } |
| 4836 | else |
| 4837 | { |
| 4838 | TEST_ASSERT( status == expected_status ); |
| 4839 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4840 | |
| 4841 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4842 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4843 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4844 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4845 | } |
| 4846 | /* END_CASE */ |
| 4847 | |
| 4848 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4849 | void raw_key_agreement( int alg_arg, |
| 4850 | int our_key_type_arg, data_t *our_key_data, |
| 4851 | data_t *peer_key_data, |
| 4852 | data_t *expected_output ) |
| 4853 | { |
| 4854 | psa_key_handle_t our_key = 0; |
| 4855 | psa_algorithm_t alg = alg_arg; |
| 4856 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4857 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4858 | unsigned char *output = NULL; |
| 4859 | size_t output_length = ~0; |
| 4860 | |
| 4861 | ASSERT_ALLOC( output, expected_output->len ); |
| 4862 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4863 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4864 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4865 | psa_set_key_algorithm( &attributes, alg ); |
| 4866 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4867 | PSA_ASSERT( psa_import_key( &attributes, |
| 4868 | our_key_data->x, our_key_data->len, |
| 4869 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4870 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4871 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 4872 | peer_key_data->x, peer_key_data->len, |
| 4873 | output, expected_output->len, |
| 4874 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4875 | ASSERT_COMPARE( output, output_length, |
| 4876 | expected_output->x, expected_output->len ); |
| 4877 | |
| 4878 | exit: |
| 4879 | mbedtls_free( output ); |
| 4880 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4881 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4882 | } |
| 4883 | /* END_CASE */ |
| 4884 | |
| 4885 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4886 | void key_agreement_capacity( int alg_arg, |
| 4887 | int our_key_type_arg, data_t *our_key_data, |
| 4888 | data_t *peer_key_data, |
| 4889 | int expected_capacity_arg ) |
| 4890 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4891 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4892 | psa_algorithm_t alg = alg_arg; |
| 4893 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4894 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4895 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4896 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4897 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4898 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4899 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4900 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4901 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4902 | psa_set_key_algorithm( &attributes, alg ); |
| 4903 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4904 | PSA_ASSERT( psa_import_key( &attributes, |
| 4905 | our_key_data->x, our_key_data->len, |
| 4906 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4907 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4908 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4909 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4910 | &operation, |
| 4911 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4912 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4913 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4914 | { |
| 4915 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4916 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4917 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4918 | NULL, 0 ) ); |
| 4919 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4920 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4921 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4922 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4923 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4924 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4925 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4926 | /* Test the actual capacity by reading the output. */ |
| 4927 | while( actual_capacity > sizeof( output ) ) |
| 4928 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4929 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4930 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4931 | actual_capacity -= sizeof( output ); |
| 4932 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4933 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4934 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4935 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4936 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4937 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4938 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4939 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4940 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4941 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4942 | } |
| 4943 | /* END_CASE */ |
| 4944 | |
| 4945 | /* BEGIN_CASE */ |
| 4946 | void key_agreement_output( int alg_arg, |
| 4947 | int our_key_type_arg, data_t *our_key_data, |
| 4948 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4949 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4950 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4951 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4952 | psa_algorithm_t alg = alg_arg; |
| 4953 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4954 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4955 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4956 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4957 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4958 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 4959 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4960 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4961 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4962 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4963 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4964 | psa_set_key_algorithm( &attributes, alg ); |
| 4965 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4966 | PSA_ASSERT( psa_import_key( &attributes, |
| 4967 | our_key_data->x, our_key_data->len, |
| 4968 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4969 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4970 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4971 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4972 | &operation, |
| 4973 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4974 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4975 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4976 | { |
| 4977 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4978 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4979 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4980 | NULL, 0 ) ); |
| 4981 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4982 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4983 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4984 | actual_output, |
| 4985 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4986 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 4987 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4988 | if( expected_output2->len != 0 ) |
| 4989 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4990 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4991 | actual_output, |
| 4992 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4993 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 4994 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4995 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4996 | |
| 4997 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4998 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4999 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5000 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5001 | mbedtls_free( actual_output ); |
| 5002 | } |
| 5003 | /* END_CASE */ |
| 5004 | |
| 5005 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5006 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5007 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5008 | size_t bytes = bytes_arg; |
| 5009 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5010 | unsigned char *output = NULL; |
| 5011 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5012 | size_t i; |
| 5013 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5014 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5015 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 5016 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5017 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5018 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5019 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5020 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5021 | /* Run several times, to ensure that every output byte will be |
| 5022 | * nonzero at least once with overwhelming probability |
| 5023 | * (2^(-8*number_of_runs)). */ |
| 5024 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5025 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5026 | if( bytes != 0 ) |
| 5027 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5028 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5029 | |
| 5030 | /* Check that no more than bytes have been overwritten */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5031 | ASSERT_COMPARE( output + bytes, sizeof( trail ), |
| 5032 | trail, sizeof( trail ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5033 | |
| 5034 | for( i = 0; i < bytes; i++ ) |
| 5035 | { |
| 5036 | if( output[i] != 0 ) |
| 5037 | ++changed[i]; |
| 5038 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5039 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5040 | |
| 5041 | /* Check that every byte was changed to nonzero at least once. This |
| 5042 | * validates that psa_generate_random is overwriting every byte of |
| 5043 | * the output buffer. */ |
| 5044 | for( i = 0; i < bytes; i++ ) |
| 5045 | { |
| 5046 | TEST_ASSERT( changed[i] != 0 ); |
| 5047 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5048 | |
| 5049 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5050 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5051 | mbedtls_free( output ); |
| 5052 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5053 | } |
| 5054 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5055 | |
| 5056 | /* BEGIN_CASE */ |
| 5057 | void generate_key( int type_arg, |
| 5058 | int bits_arg, |
| 5059 | int usage_arg, |
| 5060 | int alg_arg, |
| 5061 | int expected_status_arg ) |
| 5062 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5063 | psa_key_handle_t handle = 0; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5064 | psa_key_type_t type = type_arg; |
| 5065 | psa_key_usage_t usage = usage_arg; |
| 5066 | size_t bits = bits_arg; |
| 5067 | psa_algorithm_t alg = alg_arg; |
| 5068 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5069 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5070 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5071 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5072 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5073 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5074 | psa_set_key_usage_flags( &attributes, usage ); |
| 5075 | psa_set_key_algorithm( &attributes, alg ); |
| 5076 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5077 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5078 | |
| 5079 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5080 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5081 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5082 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5083 | |
| 5084 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5085 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 5086 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5087 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5088 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5089 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5090 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5091 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5092 | |
| 5093 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5094 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5095 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5096 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5097 | } |
| 5098 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5099 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5100 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */ |
| 5101 | void generate_key_rsa( int bits_arg, |
| 5102 | data_t *e_arg, |
| 5103 | int expected_status_arg ) |
| 5104 | { |
| 5105 | psa_key_handle_t handle = 0; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5106 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5107 | size_t bits = bits_arg; |
| 5108 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5109 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5110 | psa_status_t expected_status = expected_status_arg; |
| 5111 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5112 | uint8_t *exported = NULL; |
| 5113 | size_t exported_size = |
| 5114 | PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
| 5115 | size_t exported_length = SIZE_MAX; |
| 5116 | uint8_t *e_read_buffer = NULL; |
| 5117 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5118 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5119 | size_t e_read_length = SIZE_MAX; |
| 5120 | |
| 5121 | if( e_arg->len == 0 || |
| 5122 | ( e_arg->len == 3 && |
| 5123 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5124 | { |
| 5125 | is_default_public_exponent = 1; |
| 5126 | e_read_size = 0; |
| 5127 | } |
| 5128 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5129 | ASSERT_ALLOC( exported, exported_size ); |
| 5130 | |
| 5131 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5132 | |
| 5133 | psa_set_key_usage_flags( &attributes, usage ); |
| 5134 | psa_set_key_algorithm( &attributes, alg ); |
| 5135 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5136 | e_arg->x, e_arg->len ) ); |
| 5137 | psa_set_key_bits( &attributes, bits ); |
| 5138 | |
| 5139 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5140 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5141 | if( expected_status != PSA_SUCCESS ) |
| 5142 | goto exit; |
| 5143 | |
| 5144 | /* Test the key information */ |
| 5145 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5146 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5147 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5148 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5149 | e_read_buffer, e_read_size, |
| 5150 | &e_read_length ) ); |
| 5151 | if( is_default_public_exponent ) |
| 5152 | TEST_EQUAL( e_read_length, 0 ); |
| 5153 | else |
| 5154 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5155 | |
| 5156 | /* Do something with the key according to its type and permitted usage. */ |
| 5157 | if( ! exercise_key( handle, usage, alg ) ) |
| 5158 | goto exit; |
| 5159 | |
| 5160 | /* Export the key and check the public exponent. */ |
| 5161 | PSA_ASSERT( psa_export_public_key( handle, |
| 5162 | exported, exported_size, |
| 5163 | &exported_length ) ); |
| 5164 | { |
| 5165 | uint8_t *p = exported; |
| 5166 | uint8_t *end = exported + exported_length; |
| 5167 | size_t len; |
| 5168 | /* RSAPublicKey ::= SEQUENCE { |
| 5169 | * modulus INTEGER, -- n |
| 5170 | * publicExponent INTEGER } -- e |
| 5171 | */ |
| 5172 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5173 | MBEDTLS_ASN1_SEQUENCE | |
| 5174 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5175 | TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
| 5176 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5177 | MBEDTLS_ASN1_INTEGER ) ); |
| 5178 | if( len >= 1 && p[0] == 0 ) |
| 5179 | { |
| 5180 | ++p; |
| 5181 | --len; |
| 5182 | } |
| 5183 | if( e_arg->len == 0 ) |
| 5184 | { |
| 5185 | TEST_EQUAL( len, 3 ); |
| 5186 | TEST_EQUAL( p[0], 1 ); |
| 5187 | TEST_EQUAL( p[1], 0 ); |
| 5188 | TEST_EQUAL( p[2], 1 ); |
| 5189 | } |
| 5190 | else |
| 5191 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5192 | } |
| 5193 | |
| 5194 | exit: |
| 5195 | psa_reset_key_attributes( &attributes ); |
| 5196 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5197 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5198 | mbedtls_free( e_read_buffer ); |
| 5199 | mbedtls_free( exported ); |
| 5200 | } |
| 5201 | /* END_CASE */ |
| 5202 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5203 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5204 | void persistent_key_load_key_from_storage( data_t *data, |
| 5205 | int type_arg, int bits_arg, |
| 5206 | int usage_flags_arg, int alg_arg, |
| 5207 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5208 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5209 | psa_key_id_t key_id = 1; |
| 5210 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5211 | psa_key_handle_t handle = 0; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5212 | psa_key_handle_t base_key = 0; |
| 5213 | psa_key_type_t type = type_arg; |
| 5214 | size_t bits = bits_arg; |
| 5215 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5216 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5217 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5218 | unsigned char *first_export = NULL; |
| 5219 | unsigned char *second_export = NULL; |
| 5220 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 5221 | size_t first_exported_length; |
| 5222 | size_t second_exported_length; |
| 5223 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5224 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5225 | { |
| 5226 | ASSERT_ALLOC( first_export, export_size ); |
| 5227 | ASSERT_ALLOC( second_export, export_size ); |
| 5228 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5229 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5230 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5231 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5232 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5233 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5234 | psa_set_key_algorithm( &attributes, alg ); |
| 5235 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5236 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5237 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5238 | switch( generation_method ) |
| 5239 | { |
| 5240 | case IMPORT_KEY: |
| 5241 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5242 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
| 5243 | &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5244 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5245 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5246 | case GENERATE_KEY: |
| 5247 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5248 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5249 | break; |
| 5250 | |
| 5251 | case DERIVE_KEY: |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5252 | { |
| 5253 | /* Create base key */ |
| 5254 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5255 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5256 | psa_set_key_usage_flags( &base_attributes, |
| 5257 | PSA_KEY_USAGE_DERIVE ); |
| 5258 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5259 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5260 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5261 | data->x, data->len, |
| 5262 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5263 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5264 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5265 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5266 | &operation, |
| 5267 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5268 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5269 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5270 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5271 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5272 | &operation, |
| 5273 | &handle ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5274 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5275 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
| 5276 | base_key = 0; |
| 5277 | } |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5278 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5279 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5280 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5281 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5282 | /* Export the key if permitted by the key policy. */ |
| 5283 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5284 | { |
| 5285 | PSA_ASSERT( psa_export_key( handle, |
| 5286 | first_export, export_size, |
| 5287 | &first_exported_length ) ); |
| 5288 | if( generation_method == IMPORT_KEY ) |
| 5289 | ASSERT_COMPARE( data->x, data->len, |
| 5290 | first_export, first_exported_length ); |
| 5291 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5292 | |
| 5293 | /* Shutdown and restart */ |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 5294 | PSA_ASSERT( psa_close_key( handle ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5295 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5296 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5297 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5298 | /* Check key slot still contains key data */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5299 | PSA_ASSERT( psa_open_key( key_id, &handle ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5300 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5301 | TEST_EQUAL( psa_get_key_id( &attributes ), key_id ); |
| 5302 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5303 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5304 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5305 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5306 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 5307 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5308 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5309 | /* Export the key again if permitted by the key policy. */ |
| 5310 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5311 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5312 | PSA_ASSERT( psa_export_key( handle, |
| 5313 | second_export, export_size, |
| 5314 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5315 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5316 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5320 | if( ! exercise_key( handle, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5321 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5322 | |
| 5323 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5324 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5325 | mbedtls_free( first_export ); |
| 5326 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5327 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5328 | psa_destroy_key( base_key ); |
| 5329 | if( handle == 0 ) |
| 5330 | { |
| 5331 | /* In case there was a test failure after creating the persistent key |
| 5332 | * but while it was not open, try to re-open the persistent key |
| 5333 | * to delete it. */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5334 | psa_open_key( key_id, &handle ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5335 | } |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5336 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5337 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5338 | } |
| 5339 | /* END_CASE */ |