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