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 | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 8 | #include "psa_crypto_helpers.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 9 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 10 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 11 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 12 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 13 | /* A hash algorithm that is known to be supported. |
| 14 | * |
| 15 | * This is used in some smoke tests. |
| 16 | */ |
| 17 | #if defined(MBEDTLS_MD2_C) |
| 18 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2 |
| 19 | #elif defined(MBEDTLS_MD4_C) |
| 20 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4 |
| 21 | #elif defined(MBEDTLS_MD5_C) |
| 22 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5 |
| 23 | /* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of |
| 24 | * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160 |
| 25 | * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be |
| 26 | * implausible anyway. */ |
| 27 | #elif defined(MBEDTLS_SHA1_C) |
| 28 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1 |
| 29 | #elif defined(MBEDTLS_SHA256_C) |
| 30 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256 |
| 31 | #elif defined(MBEDTLS_SHA512_C) |
| 32 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384 |
| 33 | #elif defined(MBEDTLS_SHA3_C) |
| 34 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256 |
| 35 | #else |
| 36 | #undef KNOWN_SUPPORTED_HASH_ALG |
| 37 | #endif |
| 38 | |
| 39 | /* A block cipher that is known to be supported. |
| 40 | * |
| 41 | * For simplicity's sake, stick to block ciphers with 16-byte blocks. |
| 42 | */ |
| 43 | #if defined(MBEDTLS_AES_C) |
| 44 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES |
| 45 | #elif defined(MBEDTLS_ARIA_C) |
| 46 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA |
| 47 | #elif defined(MBEDTLS_CAMELLIA_C) |
| 48 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA |
| 49 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER |
| 50 | #endif |
| 51 | |
| 52 | /* A MAC mode that is known to be supported. |
| 53 | * |
| 54 | * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or |
| 55 | * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER. |
| 56 | * |
| 57 | * This is used in some smoke tests. |
| 58 | */ |
| 59 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 60 | #define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) ) |
| 61 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC |
| 62 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C) |
| 63 | #define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC |
| 64 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 65 | #else |
| 66 | #undef KNOWN_SUPPORTED_MAC_ALG |
| 67 | #undef KNOWN_SUPPORTED_MAC_KEY_TYPE |
| 68 | #endif |
| 69 | |
| 70 | /* A cipher algorithm and key type that are known to be supported. |
| 71 | * |
| 72 | * This is used in some smoke tests. |
| 73 | */ |
| 74 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR) |
| 75 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR |
| 76 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 77 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING |
| 78 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB) |
| 79 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB |
| 80 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB) |
| 81 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB |
| 82 | #else |
| 83 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 84 | #endif |
| 85 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG) |
| 86 | #define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 87 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 88 | #elif defined(MBEDTLS_RC4_C) |
| 89 | #define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4 |
| 90 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4 |
| 91 | #else |
| 92 | #undef KNOWN_SUPPORTED_CIPHER_ALG |
| 93 | #undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE |
| 94 | #endif |
| 95 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 96 | /** Test if a buffer contains a constant byte value. |
| 97 | * |
| 98 | * `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] | 99 | * |
| 100 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 101 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 102 | * \param size Size of the buffer in bytes. |
| 103 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 104 | * \return 1 if the buffer is all-bits-zero. |
| 105 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 106 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 107 | 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] | 108 | { |
| 109 | size_t i; |
| 110 | for( i = 0; i < size; i++ ) |
| 111 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 112 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 113 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 114 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 115 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 116 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 117 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 118 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 119 | static int asn1_write_10x( unsigned char **p, |
| 120 | unsigned char *start, |
| 121 | size_t bits, |
| 122 | unsigned char x ) |
| 123 | { |
| 124 | int ret; |
| 125 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 126 | if( bits == 0 ) |
| 127 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 128 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 129 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 130 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 131 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 132 | *p -= len; |
| 133 | ( *p )[len-1] = x; |
| 134 | if( bits % 8 == 0 ) |
| 135 | ( *p )[1] |= 1; |
| 136 | else |
| 137 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 138 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 139 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 140 | MBEDTLS_ASN1_INTEGER ) ); |
| 141 | return( len ); |
| 142 | } |
| 143 | |
| 144 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 145 | size_t buffer_size, |
| 146 | unsigned char **p, |
| 147 | size_t bits, |
| 148 | int keypair ) |
| 149 | { |
| 150 | size_t half_bits = ( bits + 1 ) / 2; |
| 151 | int ret; |
| 152 | int len = 0; |
| 153 | /* Construct something that looks like a DER encoding of |
| 154 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 155 | * RSAPrivateKey ::= SEQUENCE { |
| 156 | * version Version, |
| 157 | * modulus INTEGER, -- n |
| 158 | * publicExponent INTEGER, -- e |
| 159 | * privateExponent INTEGER, -- d |
| 160 | * prime1 INTEGER, -- p |
| 161 | * prime2 INTEGER, -- q |
| 162 | * exponent1 INTEGER, -- d mod (p-1) |
| 163 | * exponent2 INTEGER, -- d mod (q-1) |
| 164 | * coefficient INTEGER, -- (inverse of q) mod p |
| 165 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 166 | * } |
| 167 | * Or, for a public key, the same structure with only |
| 168 | * version, modulus and publicExponent. |
| 169 | */ |
| 170 | *p = buffer + buffer_size; |
| 171 | if( keypair ) |
| 172 | { |
| 173 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 174 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 175 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 176 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 177 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 178 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 179 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 180 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 181 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 182 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 183 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 184 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 185 | } |
| 186 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 187 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 188 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 189 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 190 | if( keypair ) |
| 191 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 192 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 193 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 194 | { |
| 195 | const unsigned char tag = |
| 196 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 197 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 198 | } |
| 199 | return( len ); |
| 200 | } |
| 201 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 202 | int exercise_mac_setup( psa_key_type_t key_type, |
| 203 | const unsigned char *key_bytes, |
| 204 | size_t key_length, |
| 205 | psa_algorithm_t alg, |
| 206 | psa_mac_operation_t *operation, |
| 207 | psa_status_t *status ) |
| 208 | { |
| 209 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 210 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 211 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 212 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 213 | psa_set_key_algorithm( &attributes, alg ); |
| 214 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 215 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 216 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 217 | |
| 218 | *status = psa_mac_sign_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 219 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 220 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 221 | /* If setup failed, reproduce the failure, so that the caller can |
| 222 | * test the resulting state of the operation object. */ |
| 223 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 224 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 225 | TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ), |
| 226 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | psa_destroy_key( handle ); |
| 230 | return( 1 ); |
| 231 | |
| 232 | exit: |
| 233 | psa_destroy_key( handle ); |
| 234 | return( 0 ); |
| 235 | } |
| 236 | |
| 237 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 238 | const unsigned char *key_bytes, |
| 239 | size_t key_length, |
| 240 | psa_algorithm_t alg, |
| 241 | psa_cipher_operation_t *operation, |
| 242 | psa_status_t *status ) |
| 243 | { |
| 244 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 245 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 246 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 247 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 248 | psa_set_key_algorithm( &attributes, alg ); |
| 249 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 250 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 251 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 252 | |
| 253 | *status = psa_cipher_encrypt_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 254 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 255 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 256 | /* If setup failed, reproduce the failure, so that the caller can |
| 257 | * test the resulting state of the operation object. */ |
| 258 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 259 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 260 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ), |
| 261 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | psa_destroy_key( handle ); |
| 265 | return( 1 ); |
| 266 | |
| 267 | exit: |
| 268 | psa_destroy_key( handle ); |
| 269 | return( 0 ); |
| 270 | } |
| 271 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 272 | static int exercise_mac_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 273 | psa_key_usage_t usage, |
| 274 | psa_algorithm_t alg ) |
| 275 | { |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 276 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 277 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 278 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 279 | size_t mac_length = sizeof( mac ); |
| 280 | |
| 281 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 282 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 283 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 284 | handle, alg ) ); |
| 285 | PSA_ASSERT( psa_mac_update( &operation, |
| 286 | input, sizeof( input ) ) ); |
| 287 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 288 | mac, sizeof( mac ), |
| 289 | &mac_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 293 | { |
| 294 | psa_status_t verify_status = |
| 295 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 296 | PSA_SUCCESS : |
| 297 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 298 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 299 | handle, alg ) ); |
| 300 | PSA_ASSERT( psa_mac_update( &operation, |
| 301 | input, sizeof( input ) ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 302 | TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ), |
| 303 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | return( 1 ); |
| 307 | |
| 308 | exit: |
| 309 | psa_mac_abort( &operation ); |
| 310 | return( 0 ); |
| 311 | } |
| 312 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 313 | static int exercise_cipher_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 314 | psa_key_usage_t usage, |
| 315 | psa_algorithm_t alg ) |
| 316 | { |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 317 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 318 | unsigned char iv[16] = {0}; |
| 319 | size_t iv_length = sizeof( iv ); |
| 320 | const unsigned char plaintext[16] = "Hello, world..."; |
| 321 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 322 | size_t ciphertext_length = sizeof( ciphertext ); |
| 323 | unsigned char decrypted[sizeof( ciphertext )]; |
| 324 | size_t part_length; |
| 325 | |
| 326 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 327 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 328 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 329 | handle, alg ) ); |
| 330 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 331 | iv, sizeof( iv ), |
| 332 | &iv_length ) ); |
| 333 | PSA_ASSERT( psa_cipher_update( &operation, |
| 334 | plaintext, sizeof( plaintext ), |
| 335 | ciphertext, sizeof( ciphertext ), |
| 336 | &ciphertext_length ) ); |
| 337 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 338 | ciphertext + ciphertext_length, |
| 339 | sizeof( ciphertext ) - ciphertext_length, |
| 340 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 341 | ciphertext_length += part_length; |
| 342 | } |
| 343 | |
| 344 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 345 | { |
| 346 | psa_status_t status; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 347 | int maybe_invalid_padding = 0; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 348 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 349 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 350 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 351 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 352 | /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't |
| 353 | * have this macro yet. */ |
| 354 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( |
| 355 | psa_get_key_type( &attributes ) ); |
| 356 | maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 357 | } |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 358 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 359 | handle, alg ) ); |
| 360 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 361 | iv, iv_length ) ); |
| 362 | PSA_ASSERT( psa_cipher_update( &operation, |
| 363 | ciphertext, ciphertext_length, |
| 364 | decrypted, sizeof( decrypted ), |
| 365 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 366 | status = psa_cipher_finish( &operation, |
| 367 | decrypted + part_length, |
| 368 | sizeof( decrypted ) - part_length, |
| 369 | &part_length ); |
| 370 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 371 | * if the input is some aribtrary data rather than an actual |
| 372 | ciphertext, a padding error is likely. */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 373 | if( maybe_invalid_padding ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 374 | TEST_ASSERT( status == PSA_SUCCESS || |
| 375 | status == PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 376 | else |
| 377 | PSA_ASSERT( status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | return( 1 ); |
| 381 | |
| 382 | exit: |
| 383 | psa_cipher_abort( &operation ); |
| 384 | return( 0 ); |
| 385 | } |
| 386 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 387 | static int exercise_aead_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 388 | psa_key_usage_t usage, |
| 389 | psa_algorithm_t alg ) |
| 390 | { |
| 391 | unsigned char nonce[16] = {0}; |
| 392 | size_t nonce_length = sizeof( nonce ); |
| 393 | unsigned char plaintext[16] = "Hello, world..."; |
| 394 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 395 | size_t ciphertext_length = sizeof( ciphertext ); |
| 396 | size_t plaintext_length = sizeof( ciphertext ); |
| 397 | |
| 398 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 399 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 400 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 401 | nonce, nonce_length, |
| 402 | NULL, 0, |
| 403 | plaintext, sizeof( plaintext ), |
| 404 | ciphertext, sizeof( ciphertext ), |
| 405 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 409 | { |
| 410 | psa_status_t verify_status = |
| 411 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 412 | PSA_SUCCESS : |
| 413 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 414 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 415 | nonce, nonce_length, |
| 416 | NULL, 0, |
| 417 | ciphertext, ciphertext_length, |
| 418 | plaintext, sizeof( plaintext ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 419 | &plaintext_length ), |
| 420 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | return( 1 ); |
| 424 | |
| 425 | exit: |
| 426 | return( 0 ); |
| 427 | } |
| 428 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 429 | static int exercise_signature_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 430 | psa_key_usage_t usage, |
| 431 | psa_algorithm_t alg ) |
| 432 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 433 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 434 | size_t payload_length = 16; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 435 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 436 | size_t signature_length = sizeof( signature ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 437 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 438 | |
| 439 | /* If the policy allows signing with any hash, just pick one. */ |
| 440 | if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH ) |
| 441 | { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 442 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 443 | hash_alg = KNOWN_SUPPORTED_HASH_ALG; |
| 444 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 445 | #else |
| 446 | test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 447 | return( 1 ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 448 | #endif |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 449 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 450 | |
| 451 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 452 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 453 | /* Some algorithms require the payload to have the size of |
| 454 | * the hash encoded in the algorithm. Use this input size |
| 455 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 456 | if( hash_alg != 0 ) |
| 457 | payload_length = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 458 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 459 | payload, payload_length, |
| 460 | signature, sizeof( signature ), |
| 461 | &signature_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 465 | { |
| 466 | psa_status_t verify_status = |
| 467 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 468 | PSA_SUCCESS : |
| 469 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 470 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 471 | payload, payload_length, |
| 472 | signature, signature_length ), |
| 473 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | return( 1 ); |
| 477 | |
| 478 | exit: |
| 479 | return( 0 ); |
| 480 | } |
| 481 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 482 | static int exercise_asymmetric_encryption_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 483 | psa_key_usage_t usage, |
| 484 | psa_algorithm_t alg ) |
| 485 | { |
| 486 | unsigned char plaintext[256] = "Hello, world..."; |
| 487 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 488 | size_t ciphertext_length = sizeof( ciphertext ); |
| 489 | size_t plaintext_length = 16; |
| 490 | |
| 491 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 492 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 493 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 494 | plaintext, plaintext_length, |
| 495 | NULL, 0, |
| 496 | ciphertext, sizeof( ciphertext ), |
| 497 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 501 | { |
| 502 | psa_status_t status = |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 503 | psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 504 | ciphertext, ciphertext_length, |
| 505 | NULL, 0, |
| 506 | plaintext, sizeof( plaintext ), |
| 507 | &plaintext_length ); |
| 508 | TEST_ASSERT( status == PSA_SUCCESS || |
| 509 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 510 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 511 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 512 | } |
| 513 | |
| 514 | return( 1 ); |
| 515 | |
| 516 | exit: |
| 517 | return( 0 ); |
| 518 | } |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 519 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame^] | 520 | static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation, |
| 521 | psa_key_handle_t handle, |
| 522 | psa_algorithm_t alg, |
| 523 | unsigned char* input1, size_t input1_length, |
| 524 | unsigned char* input2, size_t input2_length, |
| 525 | size_t capacity ) |
| 526 | { |
| 527 | PSA_ASSERT( psa_key_derivation_setup( operation, alg ) ); |
| 528 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 529 | { |
| 530 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 531 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 532 | input1, input1_length ) ); |
| 533 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 534 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 535 | handle ) ); |
| 536 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 537 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 538 | input2, |
| 539 | input2_length ) ); |
| 540 | } |
| 541 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 542 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 543 | { |
| 544 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 545 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 546 | input1, input1_length ) ); |
| 547 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 548 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 549 | handle ) ); |
| 550 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 551 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 552 | input2, input2_length ) ); |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | TEST_ASSERT( ! "Key derivation algorithm not supported" ); |
| 557 | } |
| 558 | |
| 559 | PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) ); |
| 560 | |
| 561 | return( 1 ); |
| 562 | |
| 563 | exit: |
| 564 | return( 0 ); |
| 565 | } |
| 566 | |
| 567 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 568 | static int exercise_key_derivation_key( psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 569 | psa_key_usage_t usage, |
| 570 | psa_algorithm_t alg ) |
| 571 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 572 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame^] | 573 | unsigned char input1[] = "Input 1"; |
| 574 | size_t input1_length = sizeof( input1 ); |
| 575 | unsigned char input2[] = "Input 2"; |
| 576 | size_t input2_length = sizeof( input2 ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 577 | unsigned char output[1]; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame^] | 578 | size_t capacity = sizeof( output ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 579 | |
| 580 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 581 | { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame^] | 582 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 583 | input1, input1_length, |
| 584 | input2, input2_length, capacity ) ) |
| 585 | goto exit; |
Gilles Peskine | 7607cd6 | 2019-05-29 17:35:00 +0200 | [diff] [blame] | 586 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 587 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 588 | output, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame^] | 589 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 590 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | return( 1 ); |
| 594 | |
| 595 | exit: |
| 596 | return( 0 ); |
| 597 | } |
| 598 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 599 | /* We need two keys to exercise key agreement. Exercise the |
| 600 | * private key against its own public key. */ |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 601 | static psa_status_t key_agreement_with_self( |
| 602 | psa_key_derivation_operation_t *operation, |
| 603 | psa_key_handle_t handle ) |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 604 | { |
| 605 | psa_key_type_t private_key_type; |
| 606 | psa_key_type_t public_key_type; |
| 607 | size_t key_bits; |
| 608 | uint8_t *public_key = NULL; |
| 609 | size_t public_key_length; |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 610 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 611 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 612 | * 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] | 613 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 614 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 615 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 616 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 617 | private_key_type = psa_get_key_type( &attributes ); |
| 618 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 619 | 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] | 620 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 621 | ASSERT_ALLOC( public_key, public_key_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 622 | PSA_ASSERT( psa_export_public_key( handle, |
| 623 | public_key, public_key_length, |
| 624 | &public_key_length ) ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 625 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 626 | status = psa_key_derivation_key_agreement( |
| 627 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle, |
| 628 | public_key, public_key_length ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 629 | exit: |
| 630 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 631 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 632 | return( status ); |
| 633 | } |
| 634 | |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 635 | /* We need two keys to exercise key agreement. Exercise the |
| 636 | * private key against its own public key. */ |
| 637 | static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg, |
| 638 | psa_key_handle_t handle ) |
| 639 | { |
| 640 | psa_key_type_t private_key_type; |
| 641 | psa_key_type_t public_key_type; |
| 642 | size_t key_bits; |
| 643 | uint8_t *public_key = NULL; |
| 644 | size_t public_key_length; |
| 645 | uint8_t output[1024]; |
| 646 | size_t output_length; |
| 647 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 648 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 649 | * 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] | 650 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 651 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 652 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 653 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 654 | private_key_type = psa_get_key_type( &attributes ); |
| 655 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 656 | 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] | 657 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 658 | ASSERT_ALLOC( public_key, public_key_length ); |
| 659 | PSA_ASSERT( psa_export_public_key( handle, |
| 660 | public_key, public_key_length, |
| 661 | &public_key_length ) ); |
| 662 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 663 | status = psa_raw_key_agreement( alg, handle, |
| 664 | public_key, public_key_length, |
| 665 | output, sizeof( output ), &output_length ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 666 | exit: |
| 667 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 668 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 669 | return( status ); |
| 670 | } |
| 671 | |
| 672 | static int exercise_raw_key_agreement_key( psa_key_handle_t handle, |
| 673 | psa_key_usage_t usage, |
| 674 | psa_algorithm_t alg ) |
| 675 | { |
| 676 | int ok = 0; |
| 677 | |
| 678 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 679 | { |
| 680 | /* We need two keys to exercise key agreement. Exercise the |
| 681 | * private key against its own public key. */ |
| 682 | PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) ); |
| 683 | } |
| 684 | ok = 1; |
| 685 | |
| 686 | exit: |
| 687 | return( ok ); |
| 688 | } |
| 689 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 690 | static int exercise_key_agreement_key( psa_key_handle_t handle, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 691 | psa_key_usage_t usage, |
| 692 | psa_algorithm_t alg ) |
| 693 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 694 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 695 | unsigned char output[1]; |
| 696 | int ok = 0; |
| 697 | |
| 698 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 699 | { |
| 700 | /* We need two keys to exercise key agreement. Exercise the |
| 701 | * private key against its own public key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 702 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 703 | PSA_ASSERT( key_agreement_with_self( &operation, handle ) ); |
| 704 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 705 | output, |
| 706 | sizeof( output ) ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 707 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 708 | } |
| 709 | ok = 1; |
| 710 | |
| 711 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 712 | return( ok ); |
| 713 | } |
| 714 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 715 | static int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 716 | size_t min_bits, size_t max_bits, |
| 717 | int must_be_odd ) |
| 718 | { |
| 719 | size_t len; |
| 720 | size_t actual_bits; |
| 721 | unsigned char msb; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 722 | TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 723 | MBEDTLS_ASN1_INTEGER ), |
| 724 | 0 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 725 | /* Tolerate a slight departure from DER encoding: |
| 726 | * - 0 may be represented by an empty string or a 1-byte string. |
| 727 | * - The sign bit may be used as a value bit. */ |
| 728 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 729 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 730 | { |
| 731 | ++( *p ); |
| 732 | --len; |
| 733 | } |
| 734 | if( min_bits == 0 && len == 0 ) |
| 735 | return( 1 ); |
| 736 | msb = ( *p )[0]; |
| 737 | TEST_ASSERT( msb != 0 ); |
| 738 | actual_bits = 8 * ( len - 1 ); |
| 739 | while( msb != 0 ) |
| 740 | { |
| 741 | msb >>= 1; |
| 742 | ++actual_bits; |
| 743 | } |
| 744 | TEST_ASSERT( actual_bits >= min_bits ); |
| 745 | TEST_ASSERT( actual_bits <= max_bits ); |
| 746 | if( must_be_odd ) |
| 747 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 748 | *p += len; |
| 749 | return( 1 ); |
| 750 | exit: |
| 751 | return( 0 ); |
| 752 | } |
| 753 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 754 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 755 | uint8_t *exported, size_t exported_length ) |
| 756 | { |
| 757 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 758 | TEST_EQUAL( exported_length, ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 759 | else |
| 760 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 761 | |
| 762 | #if defined(MBEDTLS_DES_C) |
| 763 | if( type == PSA_KEY_TYPE_DES ) |
| 764 | { |
| 765 | /* Check the parity bits. */ |
| 766 | unsigned i; |
| 767 | for( i = 0; i < bits / 8; i++ ) |
| 768 | { |
| 769 | unsigned bit_count = 0; |
| 770 | unsigned m; |
| 771 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 772 | { |
| 773 | if( exported[i] & m ) |
| 774 | ++bit_count; |
| 775 | } |
| 776 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 777 | } |
| 778 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 779 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 780 | #endif |
| 781 | |
| 782 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 783 | if( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 784 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 785 | uint8_t *p = exported; |
| 786 | uint8_t *end = exported + exported_length; |
| 787 | size_t len; |
| 788 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 789 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 790 | * modulus INTEGER, -- n |
| 791 | * publicExponent INTEGER, -- e |
| 792 | * privateExponent INTEGER, -- d |
| 793 | * prime1 INTEGER, -- p |
| 794 | * prime2 INTEGER, -- q |
| 795 | * exponent1 INTEGER, -- d mod (p-1) |
| 796 | * exponent2 INTEGER, -- d mod (q-1) |
| 797 | * coefficient INTEGER, -- (inverse of q) mod p |
| 798 | * } |
| 799 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 800 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 801 | MBEDTLS_ASN1_SEQUENCE | |
| 802 | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); |
| 803 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 804 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 805 | goto exit; |
| 806 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 807 | goto exit; |
| 808 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 809 | goto exit; |
| 810 | /* Require d to be at least half the size of n. */ |
| 811 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 812 | goto exit; |
| 813 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 814 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 815 | goto exit; |
| 816 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 817 | goto exit; |
| 818 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 819 | goto exit; |
| 820 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 821 | goto exit; |
| 822 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 823 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 824 | TEST_EQUAL( p, end ); |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 825 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 826 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 827 | #endif /* MBEDTLS_RSA_C */ |
| 828 | |
| 829 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 830 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 831 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 832 | /* Just the secret value */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 833 | TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 834 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 835 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 836 | #endif /* MBEDTLS_ECP_C */ |
| 837 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 838 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 839 | { |
| 840 | uint8_t *p = exported; |
| 841 | uint8_t *end = exported + exported_length; |
| 842 | size_t len; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 843 | #if defined(MBEDTLS_RSA_C) |
| 844 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 845 | { |
| 846 | /* RSAPublicKey ::= SEQUENCE { |
| 847 | * modulus INTEGER, -- n |
| 848 | * publicExponent INTEGER } -- e |
| 849 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 850 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 851 | MBEDTLS_ASN1_SEQUENCE | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 852 | MBEDTLS_ASN1_CONSTRUCTED ), |
| 853 | 0 ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 854 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 855 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 856 | goto exit; |
| 857 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 858 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 859 | TEST_EQUAL( p, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 860 | } |
| 861 | else |
| 862 | #endif /* MBEDTLS_RSA_C */ |
| 863 | #if defined(MBEDTLS_ECP_C) |
| 864 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 865 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 866 | /* The representation of an ECC public key is: |
| 867 | * - The byte 0x04; |
| 868 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 869 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 870 | * - where m is the bit size associated with the curve. |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 871 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 872 | TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end ); |
| 873 | TEST_EQUAL( p[0], 4 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 874 | } |
| 875 | else |
| 876 | #endif /* MBEDTLS_ECP_C */ |
| 877 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 878 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 879 | mbedtls_snprintf( message, sizeof( message ), |
| 880 | "No sanity check for public key type=0x%08lx", |
| 881 | (unsigned long) type ); |
| 882 | test_fail( message, __LINE__, __FILE__ ); |
| 883 | return( 0 ); |
| 884 | } |
| 885 | } |
| 886 | else |
| 887 | |
| 888 | { |
| 889 | /* No sanity checks for other types */ |
| 890 | } |
| 891 | |
| 892 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 893 | |
| 894 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 895 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 896 | } |
| 897 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 898 | static int exercise_export_key( psa_key_handle_t handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 899 | psa_key_usage_t usage ) |
| 900 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 901 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 902 | uint8_t *exported = NULL; |
| 903 | size_t exported_size = 0; |
| 904 | size_t exported_length = 0; |
| 905 | int ok = 0; |
| 906 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 907 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 908 | |
| 909 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 910 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 911 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 912 | TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ), |
| 913 | PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 914 | ok = 1; |
| 915 | goto exit; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 916 | } |
| 917 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 918 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ), |
| 919 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 920 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 921 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 922 | PSA_ASSERT( psa_export_key( handle, |
| 923 | exported, exported_size, |
| 924 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 925 | ok = exported_key_sanity_check( psa_get_key_type( &attributes ), |
| 926 | psa_get_key_bits( &attributes ), |
| 927 | exported, exported_length ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 928 | |
| 929 | exit: |
| 930 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 931 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 932 | return( ok ); |
| 933 | } |
| 934 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 935 | static int exercise_export_public_key( psa_key_handle_t handle ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 936 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 937 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 938 | psa_key_type_t public_type; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 939 | uint8_t *exported = NULL; |
| 940 | size_t exported_size = 0; |
| 941 | size_t exported_length = 0; |
| 942 | int ok = 0; |
| 943 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 944 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 945 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 946 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 947 | TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 948 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 949 | return( 1 ); |
| 950 | } |
| 951 | |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 952 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 953 | psa_get_key_type( &attributes ) ); |
| 954 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, |
| 955 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 956 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 957 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 958 | PSA_ASSERT( psa_export_public_key( handle, |
| 959 | exported, exported_size, |
| 960 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 961 | ok = exported_key_sanity_check( public_type, |
| 962 | psa_get_key_bits( &attributes ), |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 963 | exported, exported_length ); |
| 964 | |
| 965 | exit: |
| 966 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 967 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 968 | return( ok ); |
| 969 | } |
| 970 | |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 971 | /** Do smoke tests on a key. |
| 972 | * |
| 973 | * Perform one of each operation indicated by \p alg (decrypt/encrypt, |
| 974 | * sign/verify, or derivation) that is permitted according to \p usage. |
| 975 | * \p usage and \p alg should correspond to the expected policy on the |
| 976 | * key. |
| 977 | * |
| 978 | * Export the key if permitted by \p usage, and check that the output |
| 979 | * looks sensible. If \p usage forbids export, check that |
| 980 | * \p psa_export_key correctly rejects the attempt. If the key is |
| 981 | * asymmetric, also check \p psa_export_public_key. |
| 982 | * |
| 983 | * If the key fails the tests, this function calls the test framework's |
| 984 | * `test_fail` function and returns false. Otherwise this function returns |
| 985 | * true. Therefore it should be used as follows: |
| 986 | * ``` |
| 987 | * if( ! exercise_key( ... ) ) goto exit; |
| 988 | * ``` |
| 989 | * |
| 990 | * \param handle The key to exercise. It should be capable of performing |
| 991 | * \p alg. |
| 992 | * \param usage The usage flags to assume. |
| 993 | * \param alg The algorithm to exercise. |
| 994 | * |
| 995 | * \retval 0 The key failed the smoke tests. |
| 996 | * \retval 1 The key passed the smoke tests. |
| 997 | */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 998 | static int exercise_key( psa_key_handle_t handle, |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 999 | psa_key_usage_t usage, |
| 1000 | psa_algorithm_t alg ) |
| 1001 | { |
| 1002 | int ok; |
| 1003 | if( alg == 0 ) |
| 1004 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 1005 | else if( PSA_ALG_IS_MAC( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1006 | ok = exercise_mac_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1007 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1008 | ok = exercise_cipher_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1009 | else if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1010 | ok = exercise_aead_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1011 | else if( PSA_ALG_IS_SIGN( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1012 | ok = exercise_signature_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1013 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1014 | ok = exercise_asymmetric_encryption_key( handle, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1015 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1016 | ok = exercise_key_derivation_key( handle, usage, alg ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 1017 | else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 1018 | ok = exercise_raw_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1019 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1020 | ok = exercise_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1021 | else |
| 1022 | { |
| 1023 | char message[40]; |
| 1024 | mbedtls_snprintf( message, sizeof( message ), |
| 1025 | "No code to exercise alg=0x%08lx", |
| 1026 | (unsigned long) alg ); |
| 1027 | test_fail( message, __LINE__, __FILE__ ); |
| 1028 | ok = 0; |
| 1029 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1030 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1031 | ok = ok && exercise_export_key( handle, usage ); |
| 1032 | ok = ok && exercise_export_public_key( handle ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1033 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1034 | return( ok ); |
| 1035 | } |
| 1036 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1037 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 1038 | psa_algorithm_t alg ) |
| 1039 | { |
| 1040 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 1041 | { |
| 1042 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1043 | PSA_KEY_USAGE_VERIFY : |
| 1044 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 1045 | } |
| 1046 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 1047 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 1048 | { |
| 1049 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1050 | PSA_KEY_USAGE_ENCRYPT : |
| 1051 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 1052 | } |
| 1053 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 1054 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 1055 | { |
| 1056 | return( PSA_KEY_USAGE_DERIVE ); |
| 1057 | } |
| 1058 | else |
| 1059 | { |
| 1060 | return( 0 ); |
| 1061 | } |
| 1062 | |
| 1063 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1064 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1065 | static int test_operations_on_invalid_handle( psa_key_handle_t handle ) |
| 1066 | { |
| 1067 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1068 | uint8_t buffer[1]; |
| 1069 | size_t length; |
| 1070 | int ok = 0; |
| 1071 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1072 | psa_set_key_id( &attributes, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1073 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 1074 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 1075 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
| 1076 | TEST_EQUAL( psa_get_key_attributes( handle, &attributes ), |
| 1077 | PSA_ERROR_INVALID_HANDLE ); |
| 1078 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 1079 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1080 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1081 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1082 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 1083 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 1084 | |
| 1085 | TEST_EQUAL( psa_export_key( handle, |
| 1086 | buffer, sizeof( buffer ), &length ), |
| 1087 | PSA_ERROR_INVALID_HANDLE ); |
| 1088 | TEST_EQUAL( psa_export_public_key( handle, |
| 1089 | buffer, sizeof( buffer ), &length ), |
| 1090 | PSA_ERROR_INVALID_HANDLE ); |
| 1091 | |
| 1092 | TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE ); |
| 1093 | TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE ); |
| 1094 | |
| 1095 | ok = 1; |
| 1096 | |
| 1097 | exit: |
| 1098 | psa_reset_key_attributes( &attributes ); |
| 1099 | return( ok ); |
| 1100 | } |
| 1101 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1102 | /* An overapproximation of the amount of storage needed for a key of the |
| 1103 | * given type and with the given content. The API doesn't make it easy |
| 1104 | * to find a good value for the size. The current implementation doesn't |
| 1105 | * care about the value anyway. */ |
| 1106 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 1107 | ( data )->len |
| 1108 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1109 | typedef enum { |
| 1110 | IMPORT_KEY = 0, |
| 1111 | GENERATE_KEY = 1, |
| 1112 | DERIVE_KEY = 2 |
| 1113 | } generate_method; |
| 1114 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1115 | /* END_HEADER */ |
| 1116 | |
| 1117 | /* BEGIN_DEPENDENCIES |
| 1118 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1119 | * END_DEPENDENCIES |
| 1120 | */ |
| 1121 | |
| 1122 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1123 | void static_checks( ) |
| 1124 | { |
| 1125 | size_t max_truncated_mac_size = |
| 1126 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1127 | |
| 1128 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1129 | * encoding. The shifted mask is the maximum truncated value. The |
| 1130 | * untruncated algorithm may be one byte larger. */ |
| 1131 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 1132 | } |
| 1133 | /* END_CASE */ |
| 1134 | |
| 1135 | /* BEGIN_CASE */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1136 | void attributes_set_get( int id_arg, int lifetime_arg, |
| 1137 | int usage_flags_arg, int alg_arg, |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1138 | int type_arg, int bits_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1139 | { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1140 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1141 | psa_key_id_t id = id_arg; |
| 1142 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1143 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 1144 | psa_algorithm_t alg = alg_arg; |
| 1145 | psa_key_type_t type = type_arg; |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1146 | size_t bits = bits_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1147 | |
| 1148 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1149 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1150 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1151 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1152 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1153 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1154 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1155 | psa_set_key_id( &attributes, id ); |
| 1156 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1157 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 1158 | psa_set_key_algorithm( &attributes, alg ); |
| 1159 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1160 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1161 | |
| 1162 | TEST_EQUAL( psa_get_key_id( &attributes ), id ); |
| 1163 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime ); |
| 1164 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 1165 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
| 1166 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1167 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1168 | |
| 1169 | psa_reset_key_attributes( &attributes ); |
| 1170 | |
| 1171 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1172 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1173 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1174 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1175 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1176 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1177 | } |
| 1178 | /* END_CASE */ |
| 1179 | |
| 1180 | /* BEGIN_CASE */ |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1181 | void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg, |
| 1182 | int expected_id_arg, int expected_lifetime_arg ) |
| 1183 | { |
| 1184 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1185 | psa_key_id_t id1 = id1_arg; |
| 1186 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1187 | psa_key_id_t id2 = id2_arg; |
| 1188 | psa_key_id_t expected_id = expected_id_arg; |
| 1189 | psa_key_lifetime_t expected_lifetime = expected_lifetime_arg; |
| 1190 | |
| 1191 | if( id1_arg != -1 ) |
| 1192 | psa_set_key_id( &attributes, id1 ); |
| 1193 | if( lifetime_arg != -1 ) |
| 1194 | psa_set_key_lifetime( &attributes, lifetime ); |
| 1195 | if( id2_arg != -1 ) |
| 1196 | psa_set_key_id( &attributes, id2 ); |
| 1197 | |
| 1198 | TEST_EQUAL( psa_get_key_id( &attributes ), expected_id ); |
| 1199 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime ); |
| 1200 | } |
| 1201 | /* END_CASE */ |
| 1202 | |
| 1203 | /* BEGIN_CASE */ |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1204 | void import( data_t *data, int type_arg, |
| 1205 | int attr_bits_arg, |
| 1206 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1207 | { |
| 1208 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1209 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1210 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1211 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1212 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1213 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1214 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1215 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1216 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1217 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1218 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1219 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1220 | status = psa_import_key( &attributes, data->x, data->len, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1221 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1222 | if( status != PSA_SUCCESS ) |
| 1223 | goto exit; |
| 1224 | |
| 1225 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1226 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1227 | if( attr_bits != 0 ) |
| 1228 | TEST_EQUAL( attr_bits, got_attributes.bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1229 | |
| 1230 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1231 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1232 | |
| 1233 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1234 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1235 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1236 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1237 | } |
| 1238 | /* END_CASE */ |
| 1239 | |
| 1240 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1241 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1242 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1243 | psa_key_handle_t handle = 0; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1244 | size_t bits = bits_arg; |
| 1245 | psa_status_t expected_status = expected_status_arg; |
| 1246 | psa_status_t status; |
| 1247 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1248 | 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] | 1249 | size_t buffer_size = /* Slight overapproximations */ |
| 1250 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1251 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1252 | unsigned char *p; |
| 1253 | int ret; |
| 1254 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1255 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1256 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1257 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1258 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1259 | |
| 1260 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1261 | bits, keypair ) ) >= 0 ); |
| 1262 | length = ret; |
| 1263 | |
| 1264 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1265 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1266 | status = psa_import_key( &attributes, p, length, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1267 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1268 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1269 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1270 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1271 | |
| 1272 | exit: |
| 1273 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1274 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1275 | } |
| 1276 | /* END_CASE */ |
| 1277 | |
| 1278 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1279 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1280 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1281 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1282 | int expected_bits, |
| 1283 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1284 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1285 | int canonical_input ) |
| 1286 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1287 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1288 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1289 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1290 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1291 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1292 | unsigned char *exported = NULL; |
| 1293 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1294 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1295 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1296 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1297 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1298 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1299 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1300 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1301 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1302 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1303 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1304 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1305 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1306 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1307 | psa_set_key_algorithm( &attributes, alg ); |
| 1308 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1309 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1310 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1311 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1312 | |
| 1313 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1314 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1315 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1316 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1317 | |
| 1318 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1319 | status = psa_export_key( handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1320 | exported, export_size, |
| 1321 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1322 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1323 | |
| 1324 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1325 | * and export_size. On errors, the exported length must be 0. */ |
| 1326 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1327 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1328 | TEST_ASSERT( exported_length <= export_size ); |
| 1329 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1330 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1331 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1332 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1333 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1334 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1335 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1336 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1337 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1338 | if( ! exercise_export_key( handle, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1339 | goto exit; |
| 1340 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1341 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1342 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1343 | else |
| 1344 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1345 | psa_key_handle_t handle2; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1346 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
| 1347 | &handle2 ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1348 | PSA_ASSERT( psa_export_key( handle2, |
| 1349 | reexported, |
| 1350 | export_size, |
| 1351 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1352 | ASSERT_COMPARE( exported, exported_length, |
| 1353 | reexported, reexported_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1354 | PSA_ASSERT( psa_close_key( handle2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1355 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1356 | 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] | 1357 | |
| 1358 | destroy: |
| 1359 | /* Destroy the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1360 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1361 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1362 | |
| 1363 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1364 | mbedtls_free( exported ); |
| 1365 | mbedtls_free( reexported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1366 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1367 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1368 | } |
| 1369 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1370 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1371 | /* BEGIN_CASE */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1372 | void invalid_handle( int handle ) |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1373 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1374 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1375 | test_operations_on_invalid_handle( handle ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1376 | |
| 1377 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1378 | PSA_DONE( ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1379 | } |
| 1380 | /* END_CASE */ |
| 1381 | |
| 1382 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1383 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1384 | int type_arg, |
| 1385 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1386 | int export_size_delta, |
| 1387 | int expected_export_status_arg, |
| 1388 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1389 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1390 | psa_key_handle_t handle = 0; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1391 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1392 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1393 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1394 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1395 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1396 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1397 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1398 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1399 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1400 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1401 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1402 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1403 | psa_set_key_algorithm( &attributes, alg ); |
| 1404 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1405 | |
| 1406 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1407 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1408 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1409 | /* Export the public key */ |
| 1410 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1411 | status = psa_export_public_key( handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1412 | exported, export_size, |
| 1413 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1414 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1415 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1416 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1417 | 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] | 1418 | size_t bits; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1419 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1420 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1421 | TEST_ASSERT( expected_public_key->len <= |
| 1422 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1423 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1424 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1425 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1426 | |
| 1427 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1428 | mbedtls_free( exported ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1429 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1430 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1431 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1432 | } |
| 1433 | /* END_CASE */ |
| 1434 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1435 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1436 | void import_and_exercise_key( data_t *data, |
| 1437 | int type_arg, |
| 1438 | int bits_arg, |
| 1439 | int alg_arg ) |
| 1440 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1441 | psa_key_handle_t handle = 0; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1442 | psa_key_type_t type = type_arg; |
| 1443 | size_t bits = bits_arg; |
| 1444 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1445 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1446 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1447 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1448 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1449 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1450 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1451 | psa_set_key_usage_flags( &attributes, usage ); |
| 1452 | psa_set_key_algorithm( &attributes, alg ); |
| 1453 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1454 | |
| 1455 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1456 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1457 | |
| 1458 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1459 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1460 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1461 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1462 | |
| 1463 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1464 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1465 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1466 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1467 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1468 | test_operations_on_invalid_handle( handle ); |
| 1469 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1470 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1471 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1472 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1473 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1474 | } |
| 1475 | /* END_CASE */ |
| 1476 | |
| 1477 | /* BEGIN_CASE */ |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1478 | void key_policy( int usage_arg, int alg_arg ) |
| 1479 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1480 | psa_key_handle_t handle = 0; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1481 | psa_algorithm_t alg = alg_arg; |
| 1482 | psa_key_usage_t usage = usage_arg; |
| 1483 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 1484 | unsigned char key[32] = {0}; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1485 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1486 | |
| 1487 | memset( key, 0x2a, sizeof( key ) ); |
| 1488 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1489 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1490 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1491 | psa_set_key_usage_flags( &attributes, usage ); |
| 1492 | psa_set_key_algorithm( &attributes, alg ); |
| 1493 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1494 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1495 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1496 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1497 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1498 | TEST_EQUAL( psa_get_key_type( &attributes ), key_type ); |
| 1499 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage ); |
| 1500 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1501 | |
| 1502 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1503 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1504 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1505 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1506 | } |
| 1507 | /* END_CASE */ |
| 1508 | |
| 1509 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1510 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1511 | { |
| 1512 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1513 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1514 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1515 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1516 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1517 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1518 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1519 | |
| 1520 | memset( &zero, 0, sizeof( zero ) ); |
| 1521 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1522 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1523 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1524 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1525 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1526 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1527 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1528 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1529 | |
| 1530 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1531 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1532 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1533 | |
| 1534 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1535 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1536 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1537 | |
| 1538 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1539 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1540 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1541 | } |
| 1542 | /* END_CASE */ |
| 1543 | |
| 1544 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1545 | void mac_key_policy( int policy_usage, |
| 1546 | int policy_alg, |
| 1547 | int key_type, |
| 1548 | data_t *key_data, |
| 1549 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1550 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1551 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1552 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1553 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1554 | psa_status_t status; |
| 1555 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1556 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1557 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1558 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1559 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1560 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1561 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1562 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1563 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1564 | &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1565 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1566 | status = psa_mac_sign_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1567 | if( policy_alg == exercise_alg && |
| 1568 | ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1569 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1570 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1571 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1572 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1573 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1574 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1575 | status = psa_mac_verify_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1576 | if( policy_alg == exercise_alg && |
| 1577 | ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1578 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1579 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1580 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1581 | |
| 1582 | exit: |
| 1583 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1584 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1585 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1586 | } |
| 1587 | /* END_CASE */ |
| 1588 | |
| 1589 | /* BEGIN_CASE */ |
| 1590 | void cipher_key_policy( int policy_usage, |
| 1591 | int policy_alg, |
| 1592 | int key_type, |
| 1593 | data_t *key_data, |
| 1594 | int exercise_alg ) |
| 1595 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1596 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1597 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1598 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1599 | psa_status_t status; |
| 1600 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1601 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1602 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1603 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1604 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1605 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1606 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1607 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1608 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1609 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1610 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1611 | if( policy_alg == exercise_alg && |
| 1612 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1613 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1614 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1615 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1616 | psa_cipher_abort( &operation ); |
| 1617 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1618 | status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1619 | if( policy_alg == exercise_alg && |
| 1620 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1621 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1622 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1623 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1624 | |
| 1625 | exit: |
| 1626 | psa_cipher_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1627 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1628 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1629 | } |
| 1630 | /* END_CASE */ |
| 1631 | |
| 1632 | /* BEGIN_CASE */ |
| 1633 | void aead_key_policy( int policy_usage, |
| 1634 | int policy_alg, |
| 1635 | int key_type, |
| 1636 | data_t *key_data, |
| 1637 | int nonce_length_arg, |
| 1638 | int tag_length_arg, |
| 1639 | int exercise_alg ) |
| 1640 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1641 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1642 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1643 | psa_status_t status; |
| 1644 | unsigned char nonce[16] = {0}; |
| 1645 | size_t nonce_length = nonce_length_arg; |
| 1646 | unsigned char tag[16]; |
| 1647 | size_t tag_length = tag_length_arg; |
| 1648 | size_t output_length; |
| 1649 | |
| 1650 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1651 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1652 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1653 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1654 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1655 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1656 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1657 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1658 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1659 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1660 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1661 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1662 | status = psa_aead_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1663 | nonce, nonce_length, |
| 1664 | NULL, 0, |
| 1665 | NULL, 0, |
| 1666 | tag, tag_length, |
| 1667 | &output_length ); |
| 1668 | if( policy_alg == exercise_alg && |
| 1669 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1670 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1671 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1672 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1673 | |
| 1674 | memset( tag, 0, sizeof( tag ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1675 | status = psa_aead_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1676 | nonce, nonce_length, |
| 1677 | NULL, 0, |
| 1678 | tag, tag_length, |
| 1679 | NULL, 0, |
| 1680 | &output_length ); |
| 1681 | if( policy_alg == exercise_alg && |
| 1682 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1683 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1684 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1685 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1686 | |
| 1687 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1688 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1689 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1690 | } |
| 1691 | /* END_CASE */ |
| 1692 | |
| 1693 | /* BEGIN_CASE */ |
| 1694 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1695 | int policy_alg, |
| 1696 | int key_type, |
| 1697 | data_t *key_data, |
| 1698 | int exercise_alg ) |
| 1699 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1700 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1701 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1702 | psa_status_t status; |
| 1703 | size_t key_bits; |
| 1704 | size_t buffer_length; |
| 1705 | unsigned char *buffer = NULL; |
| 1706 | size_t output_length; |
| 1707 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1708 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1709 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1710 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1711 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1712 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1713 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1714 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1715 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1716 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1717 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1718 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1719 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1720 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1721 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1722 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1723 | status = psa_asymmetric_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1724 | NULL, 0, |
| 1725 | NULL, 0, |
| 1726 | buffer, buffer_length, |
| 1727 | &output_length ); |
| 1728 | if( policy_alg == exercise_alg && |
| 1729 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1730 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1731 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1732 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1733 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1734 | if( buffer_length != 0 ) |
| 1735 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1736 | status = psa_asymmetric_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1737 | buffer, buffer_length, |
| 1738 | NULL, 0, |
| 1739 | buffer, buffer_length, |
| 1740 | &output_length ); |
| 1741 | if( policy_alg == exercise_alg && |
| 1742 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1743 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1744 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1745 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1746 | |
| 1747 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1748 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1749 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1750 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1751 | mbedtls_free( buffer ); |
| 1752 | } |
| 1753 | /* END_CASE */ |
| 1754 | |
| 1755 | /* BEGIN_CASE */ |
| 1756 | void asymmetric_signature_key_policy( int policy_usage, |
| 1757 | int policy_alg, |
| 1758 | int key_type, |
| 1759 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1760 | int exercise_alg, |
| 1761 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1762 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1763 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1764 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1765 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1766 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1767 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1768 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1769 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1770 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1771 | int compatible_alg = payload_length_arg > 0; |
| 1772 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1773 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
| 1774 | size_t signature_length; |
| 1775 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1776 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1777 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1778 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1779 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1780 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1781 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1782 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1783 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1784 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1785 | status = psa_asymmetric_sign( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1786 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1787 | signature, sizeof( signature ), |
| 1788 | &signature_length ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1789 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1790 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1791 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1792 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1793 | |
| 1794 | memset( signature, 0, sizeof( signature ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1795 | status = psa_asymmetric_verify( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1796 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1797 | signature, sizeof( signature ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1798 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1799 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1800 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1801 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1802 | |
| 1803 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1804 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1805 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1806 | } |
| 1807 | /* END_CASE */ |
| 1808 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1809 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1810 | void derive_key_policy( int policy_usage, |
| 1811 | int policy_alg, |
| 1812 | int key_type, |
| 1813 | data_t *key_data, |
| 1814 | int exercise_alg ) |
| 1815 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1816 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1817 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1818 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1819 | psa_status_t status; |
| 1820 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1821 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1822 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1823 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1824 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1825 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1826 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1827 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1828 | &handle ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1829 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1830 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1831 | |
| 1832 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1833 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1834 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1835 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1836 | &operation, |
| 1837 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1838 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1839 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1840 | |
| 1841 | status = psa_key_derivation_input_key( &operation, |
| 1842 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 1843 | handle ); |
| 1844 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1845 | if( policy_alg == exercise_alg && |
| 1846 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1847 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1848 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1849 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1850 | |
| 1851 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1852 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1853 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1854 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1855 | } |
| 1856 | /* END_CASE */ |
| 1857 | |
| 1858 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1859 | void agreement_key_policy( int policy_usage, |
| 1860 | int policy_alg, |
| 1861 | int key_type_arg, |
| 1862 | data_t *key_data, |
| 1863 | int exercise_alg ) |
| 1864 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1865 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1866 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1867 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1868 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1869 | psa_status_t status; |
| 1870 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1871 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1872 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1873 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1874 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1875 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1876 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1877 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1878 | &handle ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1879 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1880 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1881 | status = key_agreement_with_self( &operation, handle ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1882 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1883 | if( policy_alg == exercise_alg && |
| 1884 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1885 | PSA_ASSERT( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1886 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1887 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1888 | |
| 1889 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1890 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1891 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1892 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1893 | } |
| 1894 | /* END_CASE */ |
| 1895 | |
| 1896 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1897 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1898 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1899 | { |
| 1900 | psa_key_handle_t handle = 0; |
| 1901 | psa_key_type_t key_type = key_type_arg; |
| 1902 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1903 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1904 | psa_key_usage_t usage = usage_arg; |
| 1905 | psa_algorithm_t alg = alg_arg; |
| 1906 | psa_algorithm_t alg2 = alg2_arg; |
| 1907 | |
| 1908 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1909 | |
| 1910 | psa_set_key_usage_flags( &attributes, usage ); |
| 1911 | psa_set_key_algorithm( &attributes, alg ); |
| 1912 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1913 | psa_set_key_type( &attributes, key_type ); |
| 1914 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1915 | &handle ) ); |
| 1916 | |
| 1917 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1918 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1919 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1920 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1921 | |
| 1922 | if( ! exercise_key( handle, usage, alg ) ) |
| 1923 | goto exit; |
| 1924 | if( ! exercise_key( handle, usage, alg2 ) ) |
| 1925 | goto exit; |
| 1926 | |
| 1927 | exit: |
| 1928 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1929 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1930 | } |
| 1931 | /* END_CASE */ |
| 1932 | |
| 1933 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1934 | void raw_agreement_key_policy( int policy_usage, |
| 1935 | int policy_alg, |
| 1936 | int key_type_arg, |
| 1937 | data_t *key_data, |
| 1938 | int exercise_alg ) |
| 1939 | { |
| 1940 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1941 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1942 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1943 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1944 | psa_status_t status; |
| 1945 | |
| 1946 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1947 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1948 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1949 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1950 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1951 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1952 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1953 | &handle ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1954 | |
| 1955 | status = raw_key_agreement_with_self( exercise_alg, handle ); |
| 1956 | |
| 1957 | if( policy_alg == exercise_alg && |
| 1958 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 1959 | PSA_ASSERT( status ); |
| 1960 | else |
| 1961 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1962 | |
| 1963 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1964 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1965 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1966 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1967 | } |
| 1968 | /* END_CASE */ |
| 1969 | |
| 1970 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1971 | void copy_success( int source_usage_arg, |
| 1972 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1973 | int type_arg, data_t *material, |
| 1974 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1975 | int target_usage_arg, |
| 1976 | int target_alg_arg, int target_alg2_arg, |
| 1977 | int expected_usage_arg, |
| 1978 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1979 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1980 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1981 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1982 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1983 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1984 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1985 | psa_key_handle_t source_handle = 0; |
| 1986 | psa_key_handle_t target_handle = 0; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1987 | uint8_t *export_buffer = NULL; |
| 1988 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1989 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1990 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1991 | /* Prepare the source key. */ |
| 1992 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1993 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1994 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1995 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1996 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1997 | material->x, material->len, |
| 1998 | &source_handle ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1999 | PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2000 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2001 | /* Prepare the target attributes. */ |
| 2002 | if( copy_attributes ) |
| 2003 | target_attributes = source_attributes; |
| 2004 | if( target_usage_arg != -1 ) |
| 2005 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2006 | if( target_alg_arg != -1 ) |
| 2007 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2008 | if( target_alg2_arg != -1 ) |
| 2009 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2010 | |
| 2011 | /* Copy the key. */ |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2012 | PSA_ASSERT( psa_copy_key( source_handle, |
| 2013 | &target_attributes, &target_handle ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2014 | |
| 2015 | /* Destroy the source to ensure that this doesn't affect the target. */ |
| 2016 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2017 | |
| 2018 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2019 | PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) ); |
| 2020 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2021 | psa_get_key_type( &target_attributes ) ); |
| 2022 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2023 | psa_get_key_bits( &target_attributes ) ); |
| 2024 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2025 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2026 | TEST_EQUAL( expected_alg2, |
| 2027 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2028 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2029 | { |
| 2030 | size_t length; |
| 2031 | ASSERT_ALLOC( export_buffer, material->len ); |
| 2032 | PSA_ASSERT( psa_export_key( target_handle, export_buffer, |
| 2033 | material->len, &length ) ); |
| 2034 | ASSERT_COMPARE( material->x, material->len, |
| 2035 | export_buffer, length ); |
| 2036 | } |
| 2037 | if( ! exercise_key( target_handle, expected_usage, expected_alg ) ) |
| 2038 | goto exit; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2039 | if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) ) |
| 2040 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2041 | |
| 2042 | PSA_ASSERT( psa_close_key( target_handle ) ); |
| 2043 | |
| 2044 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2045 | psa_reset_key_attributes( &source_attributes ); |
| 2046 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2047 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2048 | mbedtls_free( export_buffer ); |
| 2049 | } |
| 2050 | /* END_CASE */ |
| 2051 | |
| 2052 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2053 | void copy_fail( int source_usage_arg, |
| 2054 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2055 | int type_arg, data_t *material, |
| 2056 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2057 | int target_usage_arg, |
| 2058 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2059 | int expected_status_arg ) |
| 2060 | { |
| 2061 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2062 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2063 | psa_key_handle_t source_handle = 0; |
| 2064 | psa_key_handle_t target_handle = 0; |
| 2065 | |
| 2066 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2067 | |
| 2068 | /* Prepare the source key. */ |
| 2069 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2070 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2071 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2072 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2073 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2074 | material->x, material->len, |
| 2075 | &source_handle ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2076 | |
| 2077 | /* Prepare the target attributes. */ |
| 2078 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2079 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2080 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2081 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2082 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2083 | |
| 2084 | /* Try to copy the key. */ |
| 2085 | TEST_EQUAL( psa_copy_key( source_handle, |
| 2086 | &target_attributes, &target_handle ), |
| 2087 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2088 | |
| 2089 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2090 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2091 | exit: |
| 2092 | psa_reset_key_attributes( &source_attributes ); |
| 2093 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2094 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2095 | } |
| 2096 | /* END_CASE */ |
| 2097 | |
| 2098 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2099 | void hash_operation_init( ) |
| 2100 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2101 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2102 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2103 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2104 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2105 | * to supress the Clang warning for the test. */ |
| 2106 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2107 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2108 | psa_hash_operation_t zero; |
| 2109 | |
| 2110 | memset( &zero, 0, sizeof( zero ) ); |
| 2111 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2112 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2113 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2114 | PSA_ERROR_BAD_STATE ); |
| 2115 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2116 | PSA_ERROR_BAD_STATE ); |
| 2117 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2118 | PSA_ERROR_BAD_STATE ); |
| 2119 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2120 | /* A default hash operation should be abortable without error. */ |
| 2121 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2122 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2123 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2124 | } |
| 2125 | /* END_CASE */ |
| 2126 | |
| 2127 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2128 | void hash_setup( int alg_arg, |
| 2129 | int expected_status_arg ) |
| 2130 | { |
| 2131 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2132 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2133 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2134 | psa_status_t status; |
| 2135 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2136 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2137 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2138 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2139 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2140 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2141 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2142 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2143 | |
| 2144 | /* If setup failed, reproduce the failure, so as to |
| 2145 | * test the resulting state of the operation object. */ |
| 2146 | if( status != PSA_SUCCESS ) |
| 2147 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2148 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2149 | /* Now the operation object should be reusable. */ |
| 2150 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2151 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2152 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2153 | #endif |
| 2154 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2155 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2156 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2157 | } |
| 2158 | /* END_CASE */ |
| 2159 | |
| 2160 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2161 | void hash_bad_order( ) |
| 2162 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2163 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2164 | unsigned char input[] = ""; |
| 2165 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2166 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2167 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2168 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2169 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2170 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2171 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2172 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2173 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2174 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2175 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2176 | /* Call setup twice in a row. */ |
| 2177 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2178 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2179 | PSA_ERROR_BAD_STATE ); |
| 2180 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2181 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2182 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2183 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2184 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2185 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2186 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2187 | /* Call update after finish. */ |
| 2188 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2189 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2190 | hash, sizeof( hash ), &hash_len ) ); |
| 2191 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2192 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2193 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2194 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2195 | /* Call verify without calling setup beforehand. */ |
| 2196 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2197 | valid_hash, sizeof( valid_hash ) ), |
| 2198 | PSA_ERROR_BAD_STATE ); |
| 2199 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2200 | |
| 2201 | /* Call verify after finish. */ |
| 2202 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2203 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2204 | hash, sizeof( hash ), &hash_len ) ); |
| 2205 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2206 | valid_hash, sizeof( valid_hash ) ), |
| 2207 | PSA_ERROR_BAD_STATE ); |
| 2208 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2209 | |
| 2210 | /* Call verify twice in a row. */ |
| 2211 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2212 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2213 | valid_hash, sizeof( valid_hash ) ) ); |
| 2214 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2215 | valid_hash, sizeof( valid_hash ) ), |
| 2216 | PSA_ERROR_BAD_STATE ); |
| 2217 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2218 | |
| 2219 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2220 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2221 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2222 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2223 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2224 | |
| 2225 | /* Call finish twice in a row. */ |
| 2226 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2227 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2228 | hash, sizeof( hash ), &hash_len ) ); |
| 2229 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2230 | hash, sizeof( hash ), &hash_len ), |
| 2231 | PSA_ERROR_BAD_STATE ); |
| 2232 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2233 | |
| 2234 | /* Call finish after calling verify. */ |
| 2235 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2236 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2237 | valid_hash, sizeof( valid_hash ) ) ); |
| 2238 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2239 | hash, sizeof( hash ), &hash_len ), |
| 2240 | PSA_ERROR_BAD_STATE ); |
| 2241 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2242 | |
| 2243 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2244 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2245 | } |
| 2246 | /* END_CASE */ |
| 2247 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2248 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2249 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2250 | { |
| 2251 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2252 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2253 | * appended to it */ |
| 2254 | unsigned char hash[] = { |
| 2255 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2256 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2257 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2258 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2259 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2260 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2261 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2262 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2263 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2264 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2265 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2266 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2267 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2268 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2269 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2270 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2271 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2272 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2273 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2274 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2275 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2276 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2277 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2278 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2279 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2280 | } |
| 2281 | /* END_CASE */ |
| 2282 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2283 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2284 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2285 | { |
| 2286 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2287 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2288 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2289 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2290 | size_t hash_len; |
| 2291 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2292 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2293 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2294 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2295 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2296 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2297 | hash, expected_size - 1, &hash_len ), |
| 2298 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2299 | |
| 2300 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2301 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2302 | } |
| 2303 | /* END_CASE */ |
| 2304 | |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2305 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2306 | void hash_clone_source_state( ) |
| 2307 | { |
| 2308 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2309 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2310 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2311 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2312 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2313 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2314 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2315 | size_t hash_len; |
| 2316 | |
| 2317 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2318 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2319 | |
| 2320 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2321 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2322 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2323 | hash, sizeof( hash ), &hash_len ) ); |
| 2324 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2325 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2326 | |
| 2327 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2328 | PSA_ERROR_BAD_STATE ); |
| 2329 | |
| 2330 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2331 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2332 | hash, sizeof( hash ), &hash_len ) ); |
| 2333 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2334 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2335 | hash, sizeof( hash ), &hash_len ) ); |
| 2336 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2337 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2338 | hash, sizeof( hash ), &hash_len ) ); |
| 2339 | |
| 2340 | exit: |
| 2341 | psa_hash_abort( &op_source ); |
| 2342 | psa_hash_abort( &op_init ); |
| 2343 | psa_hash_abort( &op_setup ); |
| 2344 | psa_hash_abort( &op_finished ); |
| 2345 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2346 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2347 | } |
| 2348 | /* END_CASE */ |
| 2349 | |
| 2350 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2351 | void hash_clone_target_state( ) |
| 2352 | { |
| 2353 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2354 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2355 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2356 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2357 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2358 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2359 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2360 | size_t hash_len; |
| 2361 | |
| 2362 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2363 | |
| 2364 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2365 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2366 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2367 | hash, sizeof( hash ), &hash_len ) ); |
| 2368 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2369 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2370 | |
| 2371 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2372 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2373 | hash, sizeof( hash ), &hash_len ) ); |
| 2374 | |
| 2375 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2376 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2377 | PSA_ERROR_BAD_STATE ); |
| 2378 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2379 | PSA_ERROR_BAD_STATE ); |
| 2380 | |
| 2381 | exit: |
| 2382 | psa_hash_abort( &op_target ); |
| 2383 | psa_hash_abort( &op_init ); |
| 2384 | psa_hash_abort( &op_setup ); |
| 2385 | psa_hash_abort( &op_finished ); |
| 2386 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2387 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2388 | } |
| 2389 | /* END_CASE */ |
| 2390 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2391 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2392 | void mac_operation_init( ) |
| 2393 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2394 | const uint8_t input[1] = { 0 }; |
| 2395 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2396 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2397 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2398 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2399 | * to supress the Clang warning for the test. */ |
| 2400 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2401 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2402 | psa_mac_operation_t zero; |
| 2403 | |
| 2404 | memset( &zero, 0, sizeof( zero ) ); |
| 2405 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2406 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2407 | TEST_EQUAL( psa_mac_update( &func, |
| 2408 | input, sizeof( input ) ), |
| 2409 | PSA_ERROR_BAD_STATE ); |
| 2410 | TEST_EQUAL( psa_mac_update( &init, |
| 2411 | input, sizeof( input ) ), |
| 2412 | PSA_ERROR_BAD_STATE ); |
| 2413 | TEST_EQUAL( psa_mac_update( &zero, |
| 2414 | input, sizeof( input ) ), |
| 2415 | PSA_ERROR_BAD_STATE ); |
| 2416 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2417 | /* A default MAC operation should be abortable without error. */ |
| 2418 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2419 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2420 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2421 | } |
| 2422 | /* END_CASE */ |
| 2423 | |
| 2424 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2425 | void mac_setup( int key_type_arg, |
| 2426 | data_t *key, |
| 2427 | int alg_arg, |
| 2428 | int expected_status_arg ) |
| 2429 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2430 | psa_key_type_t key_type = key_type_arg; |
| 2431 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2432 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2433 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2434 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2435 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2436 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2437 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2438 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2439 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2440 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2441 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2442 | &operation, &status ) ) |
| 2443 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2444 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2445 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2446 | /* The operation object should be reusable. */ |
| 2447 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2448 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2449 | smoke_test_key_data, |
| 2450 | sizeof( smoke_test_key_data ), |
| 2451 | KNOWN_SUPPORTED_MAC_ALG, |
| 2452 | &operation, &status ) ) |
| 2453 | goto exit; |
| 2454 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2455 | #endif |
| 2456 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2457 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2458 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2459 | } |
| 2460 | /* END_CASE */ |
| 2461 | |
| 2462 | /* BEGIN_CASE */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2463 | void mac_bad_order( ) |
| 2464 | { |
| 2465 | psa_key_handle_t handle = 0; |
| 2466 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2467 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
| 2468 | const uint8_t key[] = { |
| 2469 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2470 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2471 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2472 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2473 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2474 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2475 | size_t sign_mac_length = 0; |
| 2476 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2477 | const uint8_t verify_mac[] = { |
| 2478 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2479 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2480 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2481 | |
| 2482 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2483 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 2484 | psa_set_key_algorithm( &attributes, alg ); |
| 2485 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2486 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2487 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2488 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2489 | /* Call update without calling setup beforehand. */ |
| 2490 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2491 | PSA_ERROR_BAD_STATE ); |
| 2492 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2493 | |
| 2494 | /* Call sign finish without calling setup beforehand. */ |
| 2495 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2496 | &sign_mac_length), |
| 2497 | PSA_ERROR_BAD_STATE ); |
| 2498 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2499 | |
| 2500 | /* Call verify finish without calling setup beforehand. */ |
| 2501 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2502 | verify_mac, sizeof( verify_mac ) ), |
| 2503 | PSA_ERROR_BAD_STATE ); |
| 2504 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2505 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2506 | /* Call setup twice in a row. */ |
| 2507 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2508 | handle, alg ) ); |
| 2509 | TEST_EQUAL( psa_mac_sign_setup( &operation, |
| 2510 | handle, alg ), |
| 2511 | PSA_ERROR_BAD_STATE ); |
| 2512 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2513 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2514 | /* Call update after sign finish. */ |
| 2515 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2516 | handle, alg ) ); |
| 2517 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2518 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2519 | sign_mac, sizeof( sign_mac ), |
| 2520 | &sign_mac_length ) ); |
| 2521 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2522 | PSA_ERROR_BAD_STATE ); |
| 2523 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2524 | |
| 2525 | /* Call update after verify finish. */ |
| 2526 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2527 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2528 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2529 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2530 | verify_mac, sizeof( verify_mac ) ) ); |
| 2531 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2532 | PSA_ERROR_BAD_STATE ); |
| 2533 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2534 | |
| 2535 | /* Call sign finish twice in a row. */ |
| 2536 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2537 | handle, alg ) ); |
| 2538 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2539 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2540 | sign_mac, sizeof( sign_mac ), |
| 2541 | &sign_mac_length ) ); |
| 2542 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2543 | sign_mac, sizeof( sign_mac ), |
| 2544 | &sign_mac_length ), |
| 2545 | PSA_ERROR_BAD_STATE ); |
| 2546 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2547 | |
| 2548 | /* Call verify finish twice in a row. */ |
| 2549 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2550 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2551 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2552 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2553 | verify_mac, sizeof( verify_mac ) ) ); |
| 2554 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2555 | verify_mac, sizeof( verify_mac ) ), |
| 2556 | PSA_ERROR_BAD_STATE ); |
| 2557 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2558 | |
| 2559 | /* Setup sign but try verify. */ |
| 2560 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2561 | handle, alg ) ); |
| 2562 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2563 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2564 | verify_mac, sizeof( verify_mac ) ), |
| 2565 | PSA_ERROR_BAD_STATE ); |
| 2566 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2567 | |
| 2568 | /* Setup verify but try sign. */ |
| 2569 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2570 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2571 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2572 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2573 | sign_mac, sizeof( sign_mac ), |
| 2574 | &sign_mac_length ), |
| 2575 | PSA_ERROR_BAD_STATE ); |
| 2576 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2577 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2578 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2579 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2580 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2581 | PSA_DONE( ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2582 | } |
| 2583 | /* END_CASE */ |
| 2584 | |
| 2585 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2586 | void mac_sign( int key_type_arg, |
| 2587 | data_t *key, |
| 2588 | int alg_arg, |
| 2589 | data_t *input, |
| 2590 | data_t *expected_mac ) |
| 2591 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2592 | psa_key_handle_t handle = 0; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2593 | psa_key_type_t key_type = key_type_arg; |
| 2594 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2595 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2596 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2597 | /* Leave a little extra room in the output buffer. At the end of the |
| 2598 | * test, we'll check that the implementation didn't overwrite onto |
| 2599 | * this extra room. */ |
| 2600 | uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10]; |
| 2601 | size_t mac_buffer_size = |
| 2602 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 2603 | size_t mac_length = 0; |
| 2604 | |
| 2605 | memset( actual_mac, '+', sizeof( actual_mac ) ); |
| 2606 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
| 2607 | TEST_ASSERT( expected_mac->len <= mac_buffer_size ); |
| 2608 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2609 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2610 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2611 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 2612 | psa_set_key_algorithm( &attributes, alg ); |
| 2613 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2614 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2615 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2616 | |
| 2617 | /* Calculate the MAC. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2618 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2619 | handle, alg ) ); |
| 2620 | PSA_ASSERT( psa_mac_update( &operation, |
| 2621 | input->x, input->len ) ); |
| 2622 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2623 | actual_mac, mac_buffer_size, |
| 2624 | &mac_length ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2625 | |
| 2626 | /* Compare with the expected value. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 2627 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2628 | actual_mac, mac_length ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2629 | |
| 2630 | /* Verify that the end of the buffer is untouched. */ |
| 2631 | TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+', |
| 2632 | sizeof( actual_mac ) - mac_length ) ); |
| 2633 | |
| 2634 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2635 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2636 | PSA_DONE( ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2637 | } |
| 2638 | /* END_CASE */ |
| 2639 | |
| 2640 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2641 | void mac_verify( int key_type_arg, |
| 2642 | data_t *key, |
| 2643 | int alg_arg, |
| 2644 | data_t *input, |
| 2645 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2646 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2647 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2648 | psa_key_type_t key_type = key_type_arg; |
| 2649 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2650 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2651 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2652 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2653 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2654 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2655 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2656 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2657 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 2658 | psa_set_key_algorithm( &attributes, alg ); |
| 2659 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2660 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2661 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2662 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2663 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 2664 | handle, alg ) ); |
| 2665 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2666 | PSA_ASSERT( psa_mac_update( &operation, |
| 2667 | input->x, input->len ) ); |
| 2668 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2669 | expected_mac->x, |
| 2670 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2671 | |
| 2672 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2673 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2674 | PSA_DONE( ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2675 | } |
| 2676 | /* END_CASE */ |
| 2677 | |
| 2678 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2679 | void cipher_operation_init( ) |
| 2680 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2681 | const uint8_t input[1] = { 0 }; |
| 2682 | unsigned char output[1] = { 0 }; |
| 2683 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2684 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2685 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2686 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2687 | * to supress the Clang warning for the test. */ |
| 2688 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2689 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2690 | psa_cipher_operation_t zero; |
| 2691 | |
| 2692 | memset( &zero, 0, sizeof( zero ) ); |
| 2693 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2694 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2695 | TEST_EQUAL( psa_cipher_update( &func, |
| 2696 | input, sizeof( input ), |
| 2697 | output, sizeof( output ), |
| 2698 | &output_length ), |
| 2699 | PSA_ERROR_BAD_STATE ); |
| 2700 | TEST_EQUAL( psa_cipher_update( &init, |
| 2701 | input, sizeof( input ), |
| 2702 | output, sizeof( output ), |
| 2703 | &output_length ), |
| 2704 | PSA_ERROR_BAD_STATE ); |
| 2705 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2706 | input, sizeof( input ), |
| 2707 | output, sizeof( output ), |
| 2708 | &output_length ), |
| 2709 | PSA_ERROR_BAD_STATE ); |
| 2710 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2711 | /* A default cipher operation should be abortable without error. */ |
| 2712 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2713 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2714 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2715 | } |
| 2716 | /* END_CASE */ |
| 2717 | |
| 2718 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2719 | void cipher_setup( int key_type_arg, |
| 2720 | data_t *key, |
| 2721 | int alg_arg, |
| 2722 | int expected_status_arg ) |
| 2723 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2724 | psa_key_type_t key_type = key_type_arg; |
| 2725 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2726 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2727 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2728 | psa_status_t status; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2729 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2730 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2731 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2732 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2733 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2734 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2735 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2736 | &operation, &status ) ) |
| 2737 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2738 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2739 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2740 | /* The operation object should be reusable. */ |
| 2741 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2742 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2743 | smoke_test_key_data, |
| 2744 | sizeof( smoke_test_key_data ), |
| 2745 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2746 | &operation, &status ) ) |
| 2747 | goto exit; |
| 2748 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2749 | #endif |
| 2750 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2751 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2752 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2753 | } |
| 2754 | /* END_CASE */ |
| 2755 | |
| 2756 | /* BEGIN_CASE */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2757 | void cipher_bad_order( ) |
| 2758 | { |
| 2759 | psa_key_handle_t handle = 0; |
| 2760 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2761 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2762 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2763 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2764 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2765 | const uint8_t key[] = { |
| 2766 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2767 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2768 | const uint8_t text[] = { |
| 2769 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2770 | 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2771 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2772 | size_t length = 0; |
| 2773 | |
| 2774 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2775 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2776 | psa_set_key_algorithm( &attributes, alg ); |
| 2777 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2778 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2779 | |
| 2780 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2781 | /* Call encrypt setup twice in a row. */ |
| 2782 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2783 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ), |
| 2784 | PSA_ERROR_BAD_STATE ); |
| 2785 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2786 | |
| 2787 | /* Call decrypt setup twice in a row. */ |
| 2788 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) ); |
| 2789 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ), |
| 2790 | PSA_ERROR_BAD_STATE ); |
| 2791 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2792 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2793 | /* Generate an IV without calling setup beforehand. */ |
| 2794 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2795 | buffer, sizeof( buffer ), |
| 2796 | &length ), |
| 2797 | PSA_ERROR_BAD_STATE ); |
| 2798 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2799 | |
| 2800 | /* Generate an IV twice in a row. */ |
| 2801 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2802 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2803 | buffer, sizeof( buffer ), |
| 2804 | &length ) ); |
| 2805 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2806 | buffer, sizeof( buffer ), |
| 2807 | &length ), |
| 2808 | PSA_ERROR_BAD_STATE ); |
| 2809 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2810 | |
| 2811 | /* Generate an IV after it's already set. */ |
| 2812 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2813 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2814 | iv, sizeof( iv ) ) ); |
| 2815 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2816 | buffer, sizeof( buffer ), |
| 2817 | &length ), |
| 2818 | PSA_ERROR_BAD_STATE ); |
| 2819 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2820 | |
| 2821 | /* Set an IV without calling setup beforehand. */ |
| 2822 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2823 | iv, sizeof( iv ) ), |
| 2824 | PSA_ERROR_BAD_STATE ); |
| 2825 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2826 | |
| 2827 | /* Set an IV after it's already set. */ |
| 2828 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2829 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2830 | iv, sizeof( iv ) ) ); |
| 2831 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2832 | iv, sizeof( iv ) ), |
| 2833 | PSA_ERROR_BAD_STATE ); |
| 2834 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2835 | |
| 2836 | /* Set an IV after it's already generated. */ |
| 2837 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2838 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2839 | buffer, sizeof( buffer ), |
| 2840 | &length ) ); |
| 2841 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2842 | iv, sizeof( iv ) ), |
| 2843 | PSA_ERROR_BAD_STATE ); |
| 2844 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2845 | |
| 2846 | /* Call update without calling setup beforehand. */ |
| 2847 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2848 | text, sizeof( text ), |
| 2849 | buffer, sizeof( buffer ), |
| 2850 | &length ), |
| 2851 | PSA_ERROR_BAD_STATE ); |
| 2852 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2853 | |
| 2854 | /* Call update without an IV where an IV is required. */ |
| 2855 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2856 | text, sizeof( text ), |
| 2857 | buffer, sizeof( buffer ), |
| 2858 | &length ), |
| 2859 | PSA_ERROR_BAD_STATE ); |
| 2860 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2861 | |
| 2862 | /* Call update after finish. */ |
| 2863 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2864 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2865 | iv, sizeof( iv ) ) ); |
| 2866 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2867 | buffer, sizeof( buffer ), &length ) ); |
| 2868 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2869 | text, sizeof( text ), |
| 2870 | buffer, sizeof( buffer ), |
| 2871 | &length ), |
| 2872 | PSA_ERROR_BAD_STATE ); |
| 2873 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2874 | |
| 2875 | /* Call finish without calling setup beforehand. */ |
| 2876 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2877 | buffer, sizeof( buffer ), &length ), |
| 2878 | PSA_ERROR_BAD_STATE ); |
| 2879 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2880 | |
| 2881 | /* Call finish without an IV where an IV is required. */ |
| 2882 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2883 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2884 | * for cipher modes with padding. */ |
| 2885 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2886 | buffer, sizeof( buffer ), &length ), |
| 2887 | PSA_ERROR_BAD_STATE ); |
| 2888 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2889 | |
| 2890 | /* Call finish twice in a row. */ |
| 2891 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2892 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2893 | iv, sizeof( iv ) ) ); |
| 2894 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2895 | buffer, sizeof( buffer ), &length ) ); |
| 2896 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2897 | buffer, sizeof( buffer ), &length ), |
| 2898 | PSA_ERROR_BAD_STATE ); |
| 2899 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2900 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2901 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2902 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2903 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2904 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2905 | } |
| 2906 | /* END_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2907 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2908 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2909 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 2910 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2911 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2912 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2913 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2914 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2915 | psa_status_t status; |
| 2916 | psa_key_type_t key_type = key_type_arg; |
| 2917 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2918 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2919 | unsigned char *output = NULL; |
| 2920 | size_t output_buffer_size = 0; |
| 2921 | size_t function_output_length = 0; |
| 2922 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2923 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2924 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2925 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2926 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2927 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2928 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2929 | psa_set_key_algorithm( &attributes, alg ); |
| 2930 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2931 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2932 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2933 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2934 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 2935 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2936 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 2937 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 2938 | output_buffer_size = ( (size_t) input->len + |
| 2939 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2940 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2941 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2942 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2943 | input->x, input->len, |
| 2944 | output, output_buffer_size, |
| 2945 | &function_output_length ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2946 | total_output_length += function_output_length; |
| 2947 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2948 | output + total_output_length, |
| 2949 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2950 | &function_output_length ); |
| 2951 | total_output_length += function_output_length; |
| 2952 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2953 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2954 | if( expected_status == PSA_SUCCESS ) |
| 2955 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2956 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2957 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2958 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | exit: |
| 2962 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2963 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2964 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2965 | } |
| 2966 | /* END_CASE */ |
| 2967 | |
| 2968 | /* BEGIN_CASE */ |
| 2969 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 2970 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2971 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2972 | int first_part_size_arg, |
| 2973 | int output1_length_arg, int output2_length_arg, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2974 | data_t *expected_output ) |
| 2975 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2976 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2977 | psa_key_type_t key_type = key_type_arg; |
| 2978 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2979 | size_t first_part_size = first_part_size_arg; |
| 2980 | size_t output1_length = output1_length_arg; |
| 2981 | size_t output2_length = output2_length_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2982 | unsigned char *output = NULL; |
| 2983 | size_t output_buffer_size = 0; |
| 2984 | size_t function_output_length = 0; |
| 2985 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2986 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2987 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2988 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2989 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2990 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2991 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2992 | psa_set_key_algorithm( &attributes, alg ); |
| 2993 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2994 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2995 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2996 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2997 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 2998 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2999 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3000 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3001 | output_buffer_size = ( (size_t) input->len + |
| 3002 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3003 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3004 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3005 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3006 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3007 | output, output_buffer_size, |
| 3008 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3009 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3010 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3011 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3012 | input->x + first_part_size, |
| 3013 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3014 | output + total_output_length, |
| 3015 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3016 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3017 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3018 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3019 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3020 | output + total_output_length, |
| 3021 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3022 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3023 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3024 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3025 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3026 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3027 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3028 | |
| 3029 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3030 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3031 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3032 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3033 | } |
| 3034 | /* END_CASE */ |
| 3035 | |
| 3036 | /* BEGIN_CASE */ |
| 3037 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3038 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3039 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3040 | int first_part_size_arg, |
| 3041 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3042 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3043 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3044 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3045 | |
| 3046 | psa_key_type_t key_type = key_type_arg; |
| 3047 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3048 | size_t first_part_size = first_part_size_arg; |
| 3049 | size_t output1_length = output1_length_arg; |
| 3050 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3051 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3052 | size_t output_buffer_size = 0; |
| 3053 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3054 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3055 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3056 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3057 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3058 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3059 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3060 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3061 | psa_set_key_algorithm( &attributes, alg ); |
| 3062 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3063 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3064 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3065 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3066 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3067 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3068 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3069 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3070 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3071 | output_buffer_size = ( (size_t) input->len + |
| 3072 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3073 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3074 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3075 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3076 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3077 | input->x, first_part_size, |
| 3078 | output, output_buffer_size, |
| 3079 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3080 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3081 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3082 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3083 | input->x + first_part_size, |
| 3084 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3085 | output + total_output_length, |
| 3086 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3087 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3088 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3089 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3090 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3091 | output + total_output_length, |
| 3092 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3093 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3094 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3095 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3096 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3097 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3098 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3099 | |
| 3100 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3101 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3102 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3103 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3104 | } |
| 3105 | /* END_CASE */ |
| 3106 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3107 | /* BEGIN_CASE */ |
| 3108 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3109 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3110 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3111 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3112 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3113 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3114 | psa_status_t status; |
| 3115 | psa_key_type_t key_type = key_type_arg; |
| 3116 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3117 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3118 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3119 | size_t output_buffer_size = 0; |
| 3120 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3121 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3122 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3123 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3124 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3125 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3126 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3127 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3128 | psa_set_key_algorithm( &attributes, alg ); |
| 3129 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3130 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3131 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3132 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3133 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3134 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3135 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3136 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3137 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3138 | output_buffer_size = ( (size_t) input->len + |
| 3139 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3140 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3141 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3142 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3143 | input->x, input->len, |
| 3144 | output, output_buffer_size, |
| 3145 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3146 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3147 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3148 | output + total_output_length, |
| 3149 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3150 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3151 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3152 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3153 | |
| 3154 | if( expected_status == PSA_SUCCESS ) |
| 3155 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3156 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3157 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3158 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3159 | } |
| 3160 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3161 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3162 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3163 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3164 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3165 | } |
| 3166 | /* END_CASE */ |
| 3167 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3168 | /* BEGIN_CASE */ |
| 3169 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3170 | data_t *key, |
| 3171 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3172 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3173 | psa_key_handle_t handle = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3174 | psa_key_type_t key_type = key_type_arg; |
| 3175 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 3176 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3177 | size_t iv_size = 16; |
| 3178 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3179 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3180 | size_t output1_size = 0; |
| 3181 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3182 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3183 | size_t output2_size = 0; |
| 3184 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3185 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3186 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3187 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3188 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3189 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3190 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3191 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3192 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3193 | psa_set_key_algorithm( &attributes, alg ); |
| 3194 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3195 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3196 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3197 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3198 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3199 | handle, alg ) ); |
| 3200 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3201 | handle, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3202 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3203 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3204 | iv, iv_size, |
| 3205 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3206 | output1_size = ( (size_t) input->len + |
| 3207 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3208 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3209 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3210 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3211 | output1, output1_size, |
| 3212 | &output1_length ) ); |
| 3213 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3214 | output1 + output1_length, |
| 3215 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3216 | &function_output_length ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3217 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3218 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3219 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3220 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3221 | |
| 3222 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3223 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3224 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3225 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3226 | iv, iv_length ) ); |
| 3227 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3228 | output2, output2_size, |
| 3229 | &output2_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3230 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3231 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3232 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3233 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3234 | &function_output_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3235 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3236 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3237 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3238 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3239 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3240 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3241 | |
| 3242 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3243 | mbedtls_free( output1 ); |
| 3244 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3245 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3246 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3247 | } |
| 3248 | /* END_CASE */ |
| 3249 | |
| 3250 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3251 | void cipher_verify_output_multipart( int alg_arg, |
| 3252 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3253 | data_t *key, |
| 3254 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3255 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3256 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3257 | psa_key_handle_t handle = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3258 | psa_key_type_t key_type = key_type_arg; |
| 3259 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3260 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3261 | unsigned char iv[16] = {0}; |
| 3262 | size_t iv_size = 16; |
| 3263 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3264 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3265 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3266 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3267 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3268 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3269 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3270 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3271 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3272 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3273 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3274 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3275 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3276 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3277 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3278 | psa_set_key_algorithm( &attributes, alg ); |
| 3279 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3280 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3281 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3282 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3283 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3284 | handle, alg ) ); |
| 3285 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3286 | handle, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3287 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3288 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3289 | iv, iv_size, |
| 3290 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3291 | output1_buffer_size = ( (size_t) input->len + |
| 3292 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3293 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3294 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3295 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3296 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3297 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3298 | output1, output1_buffer_size, |
| 3299 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3300 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3301 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3302 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3303 | input->x + first_part_size, |
| 3304 | input->len - first_part_size, |
| 3305 | output1, output1_buffer_size, |
| 3306 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3307 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3308 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3309 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3310 | output1 + output1_length, |
| 3311 | output1_buffer_size - output1_length, |
| 3312 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3313 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3314 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3315 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3316 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3317 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3318 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3319 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3320 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3321 | iv, iv_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3322 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3323 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3324 | output2, output2_buffer_size, |
| 3325 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3326 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3327 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3328 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3329 | output1 + first_part_size, |
| 3330 | output1_length - first_part_size, |
| 3331 | output2, output2_buffer_size, |
| 3332 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3333 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3334 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3335 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3336 | output2 + output2_length, |
| 3337 | output2_buffer_size - output2_length, |
| 3338 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3339 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3340 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3341 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3342 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3343 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3344 | |
| 3345 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3346 | mbedtls_free( output1 ); |
| 3347 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3348 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3349 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3350 | } |
| 3351 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3352 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3353 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3354 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3355 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3356 | data_t *nonce, |
| 3357 | data_t *additional_data, |
| 3358 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3359 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3360 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3361 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3362 | psa_key_type_t key_type = key_type_arg; |
| 3363 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3364 | unsigned char *output_data = NULL; |
| 3365 | size_t output_size = 0; |
| 3366 | size_t output_length = 0; |
| 3367 | unsigned char *output_data2 = NULL; |
| 3368 | size_t output_length2 = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3369 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3370 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3371 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3372 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3373 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3374 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3375 | * should be exact. */ |
| 3376 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3377 | TEST_EQUAL( output_size, |
| 3378 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3379 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3380 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3381 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3382 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3383 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3384 | psa_set_key_algorithm( &attributes, alg ); |
| 3385 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3386 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3387 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3388 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3389 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3390 | TEST_EQUAL( psa_aead_encrypt( handle, alg, |
| 3391 | nonce->x, nonce->len, |
| 3392 | additional_data->x, |
| 3393 | additional_data->len, |
| 3394 | input_data->x, input_data->len, |
| 3395 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3396 | &output_length ), |
| 3397 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3398 | |
| 3399 | if( PSA_SUCCESS == expected_result ) |
| 3400 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3401 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3402 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3403 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3404 | * should be exact. */ |
| 3405 | TEST_EQUAL( input_data->len, |
| 3406 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) ); |
| 3407 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3408 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3409 | nonce->x, nonce->len, |
| 3410 | additional_data->x, |
| 3411 | additional_data->len, |
| 3412 | output_data, output_length, |
| 3413 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3414 | &output_length2 ), |
| 3415 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3416 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3417 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3418 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3419 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3420 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3421 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3422 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3423 | mbedtls_free( output_data ); |
| 3424 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3425 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3426 | } |
| 3427 | /* END_CASE */ |
| 3428 | |
| 3429 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3430 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3431 | int alg_arg, |
| 3432 | data_t *nonce, |
| 3433 | data_t *additional_data, |
| 3434 | data_t *input_data, |
| 3435 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3436 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3437 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3438 | psa_key_type_t key_type = key_type_arg; |
| 3439 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3440 | unsigned char *output_data = NULL; |
| 3441 | size_t output_size = 0; |
| 3442 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3443 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3444 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3445 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3446 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3447 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3448 | * should be exact. */ |
| 3449 | TEST_EQUAL( output_size, |
| 3450 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3451 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3452 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3453 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3454 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3455 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3456 | psa_set_key_algorithm( &attributes, alg ); |
| 3457 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3458 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3459 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3460 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3461 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3462 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 3463 | nonce->x, nonce->len, |
| 3464 | additional_data->x, additional_data->len, |
| 3465 | input_data->x, input_data->len, |
| 3466 | output_data, output_size, |
| 3467 | &output_length ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3468 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3469 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3470 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3471 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3472 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3473 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3474 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3475 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3476 | } |
| 3477 | /* END_CASE */ |
| 3478 | |
| 3479 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3480 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3481 | int alg_arg, |
| 3482 | data_t *nonce, |
| 3483 | data_t *additional_data, |
| 3484 | data_t *input_data, |
| 3485 | data_t *expected_data, |
| 3486 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3487 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3488 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3489 | psa_key_type_t key_type = key_type_arg; |
| 3490 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3491 | unsigned char *output_data = NULL; |
| 3492 | size_t output_size = 0; |
| 3493 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3494 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3495 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3496 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3497 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3498 | output_size = input_data->len - tag_length; |
| 3499 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3500 | * should be exact. */ |
| 3501 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3502 | TEST_EQUAL( output_size, |
| 3503 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3504 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3505 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3506 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3507 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3508 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3509 | psa_set_key_algorithm( &attributes, alg ); |
| 3510 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3511 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3512 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3513 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3514 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3515 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3516 | nonce->x, nonce->len, |
| 3517 | additional_data->x, |
| 3518 | additional_data->len, |
| 3519 | input_data->x, input_data->len, |
| 3520 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3521 | &output_length ), |
| 3522 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3523 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3524 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3525 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3526 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3527 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3528 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3529 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3530 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3531 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3532 | } |
| 3533 | /* END_CASE */ |
| 3534 | |
| 3535 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3536 | void signature_size( int type_arg, |
| 3537 | int bits, |
| 3538 | int alg_arg, |
| 3539 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3540 | { |
| 3541 | psa_key_type_t type = type_arg; |
| 3542 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3543 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3544 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3545 | exit: |
| 3546 | ; |
| 3547 | } |
| 3548 | /* END_CASE */ |
| 3549 | |
| 3550 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3551 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 3552 | int alg_arg, data_t *input_data, |
| 3553 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3554 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3555 | psa_key_handle_t handle = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3556 | psa_key_type_t key_type = key_type_arg; |
| 3557 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3558 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3559 | unsigned char *signature = NULL; |
| 3560 | size_t signature_size; |
| 3561 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3562 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3563 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3564 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3565 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3566 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 3567 | psa_set_key_algorithm( &attributes, alg ); |
| 3568 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3569 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3570 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3571 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3572 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3573 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3574 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3575 | /* Allocate a buffer which has the size advertized by the |
| 3576 | * library. */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3577 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3578 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3579 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3580 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3581 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3582 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3583 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3584 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3585 | input_data->x, input_data->len, |
| 3586 | signature, signature_size, |
| 3587 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3588 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3589 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3590 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3591 | |
| 3592 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3593 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3594 | psa_destroy_key( handle ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3595 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3596 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3597 | } |
| 3598 | /* END_CASE */ |
| 3599 | |
| 3600 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3601 | void sign_fail( int key_type_arg, data_t *key_data, |
| 3602 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3603 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3604 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3605 | psa_key_handle_t handle = 0; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3606 | psa_key_type_t key_type = key_type_arg; |
| 3607 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3608 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3609 | psa_status_t actual_status; |
| 3610 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3611 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3612 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3613 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3614 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3615 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3616 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3617 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3618 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3619 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 3620 | psa_set_key_algorithm( &attributes, alg ); |
| 3621 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3622 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3623 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3624 | &handle ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3625 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3626 | actual_status = psa_asymmetric_sign( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3627 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3628 | signature, signature_size, |
| 3629 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3630 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3631 | /* The value of *signature_length is unspecified on error, but |
| 3632 | * whatever it is, it should be less than signature_size, so that |
| 3633 | * if the caller tries to read *signature_length bytes without |
| 3634 | * checking the error code then they don't overflow a buffer. */ |
| 3635 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3636 | |
| 3637 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3638 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3639 | psa_destroy_key( handle ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3640 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3641 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3642 | } |
| 3643 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3644 | |
| 3645 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3646 | void sign_verify( int key_type_arg, data_t *key_data, |
| 3647 | int alg_arg, data_t *input_data ) |
| 3648 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3649 | psa_key_handle_t handle = 0; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3650 | psa_key_type_t key_type = key_type_arg; |
| 3651 | psa_algorithm_t alg = alg_arg; |
| 3652 | size_t key_bits; |
| 3653 | unsigned char *signature = NULL; |
| 3654 | size_t signature_size; |
| 3655 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3656 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3657 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3658 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3659 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3660 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 3661 | psa_set_key_algorithm( &attributes, alg ); |
| 3662 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3663 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3664 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3665 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3666 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3667 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3668 | |
| 3669 | /* Allocate a buffer which has the size advertized by the |
| 3670 | * library. */ |
| 3671 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3672 | key_bits, alg ); |
| 3673 | TEST_ASSERT( signature_size != 0 ); |
| 3674 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3675 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3676 | |
| 3677 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3678 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3679 | input_data->x, input_data->len, |
| 3680 | signature, signature_size, |
| 3681 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3682 | /* Check that the signature length looks sensible. */ |
| 3683 | TEST_ASSERT( signature_length <= signature_size ); |
| 3684 | TEST_ASSERT( signature_length > 0 ); |
| 3685 | |
| 3686 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3687 | PSA_ASSERT( psa_asymmetric_verify( |
| 3688 | handle, alg, |
| 3689 | input_data->x, input_data->len, |
| 3690 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3691 | |
| 3692 | if( input_data->len != 0 ) |
| 3693 | { |
| 3694 | /* Flip a bit in the input and verify that the signature is now |
| 3695 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3696 | * because ECDSA may ignore the last few bits of the input. */ |
| 3697 | input_data->x[0] ^= 1; |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3698 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 3699 | input_data->x, input_data->len, |
| 3700 | signature, signature_length ), |
| 3701 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3705 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3706 | psa_destroy_key( handle ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3707 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3708 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3709 | } |
| 3710 | /* END_CASE */ |
| 3711 | |
| 3712 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3713 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 3714 | int alg_arg, data_t *hash_data, |
| 3715 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3716 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3717 | psa_key_handle_t handle = 0; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3718 | psa_key_type_t key_type = key_type_arg; |
| 3719 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3720 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3721 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3722 | TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
| 3723 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3724 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3725 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3726 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 3727 | psa_set_key_algorithm( &attributes, alg ); |
| 3728 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3729 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3730 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3731 | &handle ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3732 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3733 | PSA_ASSERT( psa_asymmetric_verify( handle, alg, |
| 3734 | hash_data->x, hash_data->len, |
| 3735 | signature_data->x, |
| 3736 | signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3737 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3738 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3739 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3740 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3741 | } |
| 3742 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3743 | |
| 3744 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3745 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 3746 | int alg_arg, data_t *hash_data, |
| 3747 | data_t *signature_data, |
| 3748 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3749 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3750 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3751 | psa_key_type_t key_type = key_type_arg; |
| 3752 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3753 | psa_status_t actual_status; |
| 3754 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3755 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3756 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3757 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3758 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3759 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 3760 | psa_set_key_algorithm( &attributes, alg ); |
| 3761 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3762 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3763 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3764 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3765 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3766 | actual_status = psa_asymmetric_verify( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3767 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3768 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3769 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3770 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3771 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3772 | |
| 3773 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3774 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3775 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3776 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3777 | } |
| 3778 | /* END_CASE */ |
| 3779 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3780 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3781 | void asymmetric_encrypt( int key_type_arg, |
| 3782 | data_t *key_data, |
| 3783 | int alg_arg, |
| 3784 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3785 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3786 | int expected_output_length_arg, |
| 3787 | int expected_status_arg ) |
| 3788 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3789 | psa_key_handle_t handle = 0; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3790 | psa_key_type_t key_type = key_type_arg; |
| 3791 | psa_algorithm_t alg = alg_arg; |
| 3792 | size_t expected_output_length = expected_output_length_arg; |
| 3793 | size_t key_bits; |
| 3794 | unsigned char *output = NULL; |
| 3795 | size_t output_size; |
| 3796 | size_t output_length = ~0; |
| 3797 | psa_status_t actual_status; |
| 3798 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3799 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3800 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3801 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3802 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3803 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3804 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3805 | psa_set_key_algorithm( &attributes, alg ); |
| 3806 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3807 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3808 | &handle ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3809 | |
| 3810 | /* Determine the maximum output length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3811 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3812 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3813 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3814 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3815 | |
| 3816 | /* Encrypt the input */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3817 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3818 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3819 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3820 | output, output_size, |
| 3821 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3822 | TEST_EQUAL( actual_status, expected_status ); |
| 3823 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3824 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3825 | /* If the label is empty, the test framework puts a non-null pointer |
| 3826 | * in label->x. Test that a null pointer works as well. */ |
| 3827 | if( label->len == 0 ) |
| 3828 | { |
| 3829 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3830 | if( output_size != 0 ) |
| 3831 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3832 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3833 | input_data->x, input_data->len, |
| 3834 | NULL, label->len, |
| 3835 | output, output_size, |
| 3836 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3837 | TEST_EQUAL( actual_status, expected_status ); |
| 3838 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3839 | } |
| 3840 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3841 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3842 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3843 | psa_destroy_key( handle ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3844 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3845 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3846 | } |
| 3847 | /* END_CASE */ |
| 3848 | |
| 3849 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3850 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 3851 | data_t *key_data, |
| 3852 | int alg_arg, |
| 3853 | data_t *input_data, |
| 3854 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3855 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3856 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3857 | psa_key_type_t key_type = key_type_arg; |
| 3858 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3859 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3860 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3861 | size_t output_size; |
| 3862 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3863 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3864 | size_t output2_size; |
| 3865 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3866 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3867 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3868 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3869 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3870 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3871 | psa_set_key_algorithm( &attributes, alg ); |
| 3872 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3873 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3874 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3875 | &handle ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3876 | |
| 3877 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3878 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3879 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3880 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3881 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3882 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3883 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3884 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 3885 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 3886 | * the original plaintext because of the non-optional random |
| 3887 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3888 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 3889 | input_data->x, input_data->len, |
| 3890 | label->x, label->len, |
| 3891 | output, output_size, |
| 3892 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3893 | /* We don't know what ciphertext length to expect, but check that |
| 3894 | * it looks sensible. */ |
| 3895 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3896 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3897 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 3898 | output, output_length, |
| 3899 | label->x, label->len, |
| 3900 | output2, output2_size, |
| 3901 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3902 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3903 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3904 | |
| 3905 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3906 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3907 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3908 | mbedtls_free( output ); |
| 3909 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3910 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3911 | } |
| 3912 | /* END_CASE */ |
| 3913 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3914 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3915 | void asymmetric_decrypt( int key_type_arg, |
| 3916 | data_t *key_data, |
| 3917 | int alg_arg, |
| 3918 | data_t *input_data, |
| 3919 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 3920 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3921 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3922 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3923 | psa_key_type_t key_type = key_type_arg; |
| 3924 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3925 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 3926 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3927 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3928 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3929 | |
Jaeden Amero | 412654a | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 3930 | output_size = expected_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3931 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3932 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3933 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3934 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3935 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3936 | psa_set_key_algorithm( &attributes, alg ); |
| 3937 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3938 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3939 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3940 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3941 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3942 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 3943 | input_data->x, input_data->len, |
| 3944 | label->x, label->len, |
| 3945 | output, |
| 3946 | output_size, |
| 3947 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3948 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3949 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3950 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3951 | /* If the label is empty, the test framework puts a non-null pointer |
| 3952 | * in label->x. Test that a null pointer works as well. */ |
| 3953 | if( label->len == 0 ) |
| 3954 | { |
| 3955 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3956 | if( output_size != 0 ) |
| 3957 | memset( output, 0, output_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3958 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 3959 | input_data->x, input_data->len, |
| 3960 | NULL, label->len, |
| 3961 | output, |
| 3962 | output_size, |
| 3963 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3964 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3965 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3966 | } |
| 3967 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3968 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3969 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3970 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3971 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3972 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3973 | } |
| 3974 | /* END_CASE */ |
| 3975 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3976 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3977 | void asymmetric_decrypt_fail( int key_type_arg, |
| 3978 | data_t *key_data, |
| 3979 | int alg_arg, |
| 3980 | data_t *input_data, |
| 3981 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 3982 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3983 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3984 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3985 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3986 | psa_key_type_t key_type = key_type_arg; |
| 3987 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3988 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 3989 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3990 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3991 | psa_status_t actual_status; |
| 3992 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3993 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3994 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3995 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3996 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3997 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3998 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3999 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4000 | psa_set_key_algorithm( &attributes, alg ); |
| 4001 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4002 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4003 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4004 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4005 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4006 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4007 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4008 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4009 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4010 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4011 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4012 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4013 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4014 | /* If the label is empty, the test framework puts a non-null pointer |
| 4015 | * in label->x. Test that a null pointer works as well. */ |
| 4016 | if( label->len == 0 ) |
| 4017 | { |
| 4018 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4019 | if( output_size != 0 ) |
| 4020 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4021 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4022 | input_data->x, input_data->len, |
| 4023 | NULL, label->len, |
| 4024 | output, output_size, |
| 4025 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4026 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4027 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4028 | } |
| 4029 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4030 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4031 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4032 | psa_destroy_key( handle ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4033 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4034 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4035 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4036 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4037 | |
| 4038 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4039 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4040 | { |
| 4041 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4042 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4043 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4044 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4045 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4046 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4047 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4048 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4049 | |
| 4050 | memset( &zero, 0, sizeof( zero ) ); |
| 4051 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4052 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4053 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4054 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4055 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4056 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4057 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4058 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4059 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4060 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4061 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4062 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4063 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4064 | } |
| 4065 | /* END_CASE */ |
| 4066 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4067 | /* BEGIN_CASE */ |
| 4068 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4069 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4070 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4071 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4072 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4073 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4074 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4075 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4076 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4077 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4078 | |
| 4079 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4080 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4081 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4082 | } |
| 4083 | /* END_CASE */ |
| 4084 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4085 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4086 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4087 | int expected_status_arg ) |
| 4088 | { |
| 4089 | psa_algorithm_t alg = alg_arg; |
| 4090 | size_t capacity = capacity_arg; |
| 4091 | psa_status_t expected_status = expected_status_arg; |
| 4092 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4093 | |
| 4094 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4095 | |
| 4096 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4097 | |
| 4098 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4099 | expected_status ); |
| 4100 | |
| 4101 | exit: |
| 4102 | psa_key_derivation_abort( &operation ); |
| 4103 | PSA_DONE( ); |
| 4104 | } |
| 4105 | /* END_CASE */ |
| 4106 | |
| 4107 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4108 | void derive_input( int alg_arg, |
| 4109 | int key_type_arg, |
| 4110 | int step1_arg, data_t *input1, |
| 4111 | int step2_arg, data_t *input2, |
| 4112 | int step3_arg, data_t *input3, |
| 4113 | int expected_status_arg1, |
| 4114 | int expected_status_arg2, |
| 4115 | int expected_status_arg3 ) |
| 4116 | { |
| 4117 | psa_algorithm_t alg = alg_arg; |
| 4118 | size_t key_type = key_type_arg; |
| 4119 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4120 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4121 | expected_status_arg2, |
| 4122 | expected_status_arg3}; |
| 4123 | data_t *inputs[] = {input1, input2, input3}; |
| 4124 | psa_key_handle_t handles[] = {0, 0, 0}; |
| 4125 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4126 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4127 | size_t i; |
| 4128 | |
| 4129 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4130 | |
| 4131 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4132 | psa_set_key_algorithm( &attributes, alg ); |
| 4133 | psa_set_key_type( &attributes, key_type ); |
| 4134 | |
| 4135 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4136 | |
| 4137 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4138 | { |
| 4139 | switch( steps[i] ) |
| 4140 | { |
| 4141 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4142 | PSA_ASSERT( psa_import_key( &attributes, |
| 4143 | inputs[i]->x, inputs[i]->len, |
| 4144 | &handles[i] ) ); |
| 4145 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
| 4146 | handles[i] ), |
| 4147 | expected_statuses[i] ); |
| 4148 | break; |
| 4149 | default: |
| 4150 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4151 | &operation, steps[i], |
| 4152 | inputs[i]->x, inputs[i]->len ), |
| 4153 | expected_statuses[i] ); |
| 4154 | break; |
| 4155 | } |
| 4156 | } |
| 4157 | |
| 4158 | exit: |
| 4159 | psa_key_derivation_abort( &operation ); |
| 4160 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4161 | psa_destroy_key( handles[i] ); |
| 4162 | PSA_DONE( ); |
| 4163 | } |
| 4164 | /* END_CASE */ |
| 4165 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4166 | /* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4167 | void test_derive_invalid_key_derivation_state( ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4168 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4169 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4170 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4171 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4172 | psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4173 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4174 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4175 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4176 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4177 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4178 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4179 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4180 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4181 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4182 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4183 | psa_set_key_algorithm( &attributes, alg ); |
| 4184 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4185 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4186 | PSA_ASSERT( psa_import_key( &attributes, |
| 4187 | key_data, sizeof( key_data ), |
| 4188 | &handle ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4189 | |
| 4190 | /* valid key derivation */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4191 | PSA_ASSERT( psa_key_derivation( &operation, handle, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4192 | NULL, 0, |
| 4193 | NULL, 0, |
| 4194 | capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4195 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4196 | /* state of operation shouldn't allow additional generation */ |
| 4197 | TEST_EQUAL( psa_key_derivation( &operation, handle, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4198 | NULL, 0, |
| 4199 | NULL, 0, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4200 | capacity ), |
| 4201 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4202 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4203 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4204 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4205 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4206 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4207 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4208 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4209 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4210 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4211 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4212 | } |
| 4213 | /* END_CASE */ |
| 4214 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4215 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4216 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4217 | { |
| 4218 | uint8_t output_buffer[16]; |
| 4219 | size_t buffer_size = 16; |
| 4220 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4221 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4222 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4223 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4224 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4225 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4226 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4227 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4228 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4229 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4230 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4231 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4232 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4233 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4234 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4235 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4236 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4237 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4238 | |
| 4239 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4240 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4241 | } |
| 4242 | /* END_CASE */ |
| 4243 | |
| 4244 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4245 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4246 | int step1_arg, data_t *input1, |
| 4247 | int step2_arg, data_t *input2, |
| 4248 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4249 | int requested_capacity_arg, |
| 4250 | data_t *expected_output1, |
| 4251 | data_t *expected_output2 ) |
| 4252 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4253 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4254 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4255 | data_t *inputs[] = {input1, input2, input3}; |
| 4256 | psa_key_handle_t handles[] = {0, 0, 0}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4257 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4258 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4259 | uint8_t *expected_outputs[2] = |
| 4260 | {expected_output1->x, expected_output2->x}; |
| 4261 | size_t output_sizes[2] = |
| 4262 | {expected_output1->len, expected_output2->len}; |
| 4263 | size_t output_buffer_size = 0; |
| 4264 | uint8_t *output_buffer = NULL; |
| 4265 | size_t expected_capacity; |
| 4266 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4267 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4268 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4269 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4270 | |
| 4271 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4272 | { |
| 4273 | if( output_sizes[i] > output_buffer_size ) |
| 4274 | output_buffer_size = output_sizes[i]; |
| 4275 | if( output_sizes[i] == 0 ) |
| 4276 | expected_outputs[i] = NULL; |
| 4277 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4278 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4279 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4280 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4281 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4282 | psa_set_key_algorithm( &attributes, alg ); |
| 4283 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4284 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4285 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4286 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4287 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4288 | requested_capacity ) ); |
| 4289 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4290 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4291 | switch( steps[i] ) |
| 4292 | { |
| 4293 | case 0: |
| 4294 | break; |
| 4295 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4296 | PSA_ASSERT( psa_import_key( &attributes, |
| 4297 | inputs[i]->x, inputs[i]->len, |
| 4298 | &handles[i] ) ); |
| 4299 | PSA_ASSERT( psa_key_derivation_input_key( |
| 4300 | &operation, steps[i], |
| 4301 | handles[i] ) ); |
| 4302 | break; |
| 4303 | default: |
| 4304 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4305 | &operation, steps[i], |
| 4306 | inputs[i]->x, inputs[i]->len ) ); |
| 4307 | break; |
| 4308 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4309 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4310 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4311 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4312 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4313 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4314 | expected_capacity = requested_capacity; |
| 4315 | |
| 4316 | /* Expansion phase. */ |
| 4317 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4318 | { |
| 4319 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4320 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4321 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4322 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4323 | { |
| 4324 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4325 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4326 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4327 | continue; |
| 4328 | } |
| 4329 | else if( expected_capacity == 0 || |
| 4330 | output_sizes[i] > expected_capacity ) |
| 4331 | { |
| 4332 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4333 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4334 | expected_capacity = 0; |
| 4335 | continue; |
| 4336 | } |
| 4337 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4338 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4339 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4340 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4341 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4342 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4343 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4344 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4345 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4346 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4347 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4348 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4349 | |
| 4350 | exit: |
| 4351 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4352 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4353 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4354 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4355 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4356 | } |
| 4357 | /* END_CASE */ |
| 4358 | |
| 4359 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4360 | void derive_full( int alg_arg, |
| 4361 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4362 | data_t *input1, |
| 4363 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4364 | int requested_capacity_arg ) |
| 4365 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4366 | psa_key_handle_t handle = 0; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4367 | psa_algorithm_t alg = alg_arg; |
| 4368 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4369 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4370 | unsigned char output_buffer[16]; |
| 4371 | size_t expected_capacity = requested_capacity; |
| 4372 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4373 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4374 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4375 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4376 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4377 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4378 | psa_set_key_algorithm( &attributes, alg ); |
| 4379 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4380 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4381 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4382 | &handle ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4383 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame^] | 4384 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4385 | input1->x, input1->len, |
| 4386 | input2->x, input2->len, |
| 4387 | requested_capacity ) ) |
| 4388 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4389 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4390 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4391 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4392 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4393 | |
| 4394 | /* Expansion phase. */ |
| 4395 | while( current_capacity > 0 ) |
| 4396 | { |
| 4397 | size_t read_size = sizeof( output_buffer ); |
| 4398 | if( read_size > current_capacity ) |
| 4399 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4400 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4401 | output_buffer, |
| 4402 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4403 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4404 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4405 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4406 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4407 | } |
| 4408 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4409 | /* Check that the operation refuses to go over capacity. */ |
| 4410 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4411 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4412 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4413 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4414 | |
| 4415 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4416 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4417 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4418 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4419 | } |
| 4420 | /* END_CASE */ |
| 4421 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4422 | /* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4423 | void derive_key_exercise( int alg_arg, |
| 4424 | data_t *key_data, |
| 4425 | data_t *salt, |
| 4426 | data_t *label, |
| 4427 | int derived_type_arg, |
| 4428 | int derived_bits_arg, |
| 4429 | int derived_usage_arg, |
| 4430 | int derived_alg_arg ) |
| 4431 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4432 | psa_key_handle_t base_handle = 0; |
| 4433 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4434 | psa_algorithm_t alg = alg_arg; |
| 4435 | psa_key_type_t derived_type = derived_type_arg; |
| 4436 | size_t derived_bits = derived_bits_arg; |
| 4437 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4438 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4439 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4440 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4441 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4442 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4443 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4444 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4445 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4446 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4447 | psa_set_key_algorithm( &attributes, alg ); |
| 4448 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4449 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4450 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4451 | |
| 4452 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4453 | PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4454 | salt->x, salt->len, |
| 4455 | label->x, label->len, |
| 4456 | capacity ) ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4457 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4458 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4459 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4460 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4461 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4462 | &derived_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4463 | |
| 4464 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4465 | PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) ); |
| 4466 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4467 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4468 | |
| 4469 | /* Exercise the derived key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4470 | if( ! exercise_key( derived_handle, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4471 | goto exit; |
| 4472 | |
| 4473 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4474 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4475 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4476 | psa_destroy_key( base_handle ); |
| 4477 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4478 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4479 | } |
| 4480 | /* END_CASE */ |
| 4481 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4482 | /* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4483 | void derive_key_export( int alg_arg, |
| 4484 | data_t *key_data, |
| 4485 | data_t *salt, |
| 4486 | data_t *label, |
| 4487 | int bytes1_arg, |
| 4488 | int bytes2_arg ) |
| 4489 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4490 | psa_key_handle_t base_handle = 0; |
| 4491 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4492 | psa_algorithm_t alg = alg_arg; |
| 4493 | size_t bytes1 = bytes1_arg; |
| 4494 | size_t bytes2 = bytes2_arg; |
| 4495 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4496 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4497 | uint8_t *output_buffer = NULL; |
| 4498 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4499 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4500 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4501 | size_t length; |
| 4502 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4503 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4504 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4505 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4506 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4507 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4508 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4509 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4510 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 4511 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4512 | |
| 4513 | /* Derive some material and output it. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4514 | PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4515 | salt->x, salt->len, |
| 4516 | label->x, label->len, |
| 4517 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4518 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4519 | output_buffer, |
| 4520 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4521 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4522 | |
| 4523 | /* Derive the same output again, but this time store it in key objects. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4524 | PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4525 | salt->x, salt->len, |
| 4526 | label->x, label->len, |
| 4527 | capacity ) ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4528 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4529 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 4530 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4531 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4532 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4533 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4534 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4535 | export_buffer, bytes1, |
| 4536 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4537 | TEST_EQUAL( length, bytes1 ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4538 | PSA_ASSERT( psa_destroy_key( derived_handle ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4539 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4540 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4541 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4542 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4543 | export_buffer + bytes1, bytes2, |
| 4544 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4545 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4546 | |
| 4547 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4548 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 4549 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4550 | |
| 4551 | exit: |
| 4552 | mbedtls_free( output_buffer ); |
| 4553 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4554 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4555 | psa_destroy_key( base_handle ); |
| 4556 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4557 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4558 | } |
| 4559 | /* END_CASE */ |
| 4560 | |
| 4561 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4562 | void key_agreement_setup( int alg_arg, |
| 4563 | int our_key_type_arg, data_t *our_key_data, |
| 4564 | data_t *peer_key_data, |
| 4565 | int expected_status_arg ) |
| 4566 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4567 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4568 | psa_algorithm_t alg = alg_arg; |
| 4569 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4570 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4571 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4572 | psa_status_t expected_status = expected_status_arg; |
| 4573 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4574 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4575 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4576 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4577 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4578 | psa_set_key_algorithm( &attributes, alg ); |
| 4579 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4580 | PSA_ASSERT( psa_import_key( &attributes, |
| 4581 | our_key_data->x, our_key_data->len, |
| 4582 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4583 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4584 | /* The tests currently include inputs that should fail at either step. |
| 4585 | * Test cases that fail at the setup step should be changed to call |
| 4586 | * key_derivation_setup instead, and this function should be renamed |
| 4587 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4588 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4589 | if( status == PSA_SUCCESS ) |
| 4590 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4591 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 4592 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 4593 | our_key, |
| 4594 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4595 | expected_status ); |
| 4596 | } |
| 4597 | else |
| 4598 | { |
| 4599 | TEST_ASSERT( status == expected_status ); |
| 4600 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4601 | |
| 4602 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4603 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4604 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4605 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4606 | } |
| 4607 | /* END_CASE */ |
| 4608 | |
| 4609 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4610 | void raw_key_agreement( int alg_arg, |
| 4611 | int our_key_type_arg, data_t *our_key_data, |
| 4612 | data_t *peer_key_data, |
| 4613 | data_t *expected_output ) |
| 4614 | { |
| 4615 | psa_key_handle_t our_key = 0; |
| 4616 | psa_algorithm_t alg = alg_arg; |
| 4617 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4618 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4619 | unsigned char *output = NULL; |
| 4620 | size_t output_length = ~0; |
| 4621 | |
| 4622 | ASSERT_ALLOC( output, expected_output->len ); |
| 4623 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4624 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4625 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4626 | psa_set_key_algorithm( &attributes, alg ); |
| 4627 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4628 | PSA_ASSERT( psa_import_key( &attributes, |
| 4629 | our_key_data->x, our_key_data->len, |
| 4630 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4631 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4632 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 4633 | peer_key_data->x, peer_key_data->len, |
| 4634 | output, expected_output->len, |
| 4635 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4636 | ASSERT_COMPARE( output, output_length, |
| 4637 | expected_output->x, expected_output->len ); |
| 4638 | |
| 4639 | exit: |
| 4640 | mbedtls_free( output ); |
| 4641 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4642 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4643 | } |
| 4644 | /* END_CASE */ |
| 4645 | |
| 4646 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4647 | void key_agreement_capacity( int alg_arg, |
| 4648 | int our_key_type_arg, data_t *our_key_data, |
| 4649 | data_t *peer_key_data, |
| 4650 | int expected_capacity_arg ) |
| 4651 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4652 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4653 | psa_algorithm_t alg = alg_arg; |
| 4654 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4655 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4656 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4657 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4658 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4659 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4660 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4661 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4662 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4663 | psa_set_key_algorithm( &attributes, alg ); |
| 4664 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4665 | PSA_ASSERT( psa_import_key( &attributes, |
| 4666 | our_key_data->x, our_key_data->len, |
| 4667 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4668 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4669 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4670 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4671 | &operation, |
| 4672 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4673 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4674 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4675 | { |
| 4676 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4677 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4678 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4679 | NULL, 0 ) ); |
| 4680 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4681 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4682 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4683 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4684 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4685 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4686 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4687 | /* Test the actual capacity by reading the output. */ |
| 4688 | while( actual_capacity > sizeof( output ) ) |
| 4689 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4690 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4691 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4692 | actual_capacity -= sizeof( output ); |
| 4693 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4694 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4695 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4696 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4697 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4698 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4699 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4700 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4701 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4702 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4703 | } |
| 4704 | /* END_CASE */ |
| 4705 | |
| 4706 | /* BEGIN_CASE */ |
| 4707 | void key_agreement_output( int alg_arg, |
| 4708 | int our_key_type_arg, data_t *our_key_data, |
| 4709 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4710 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4711 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4712 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4713 | psa_algorithm_t alg = alg_arg; |
| 4714 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4715 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4716 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4717 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4718 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4719 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 4720 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4721 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4722 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4723 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4724 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4725 | psa_set_key_algorithm( &attributes, alg ); |
| 4726 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4727 | PSA_ASSERT( psa_import_key( &attributes, |
| 4728 | our_key_data->x, our_key_data->len, |
| 4729 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4730 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4731 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4732 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4733 | &operation, |
| 4734 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4735 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4736 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4737 | { |
| 4738 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4739 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4740 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4741 | NULL, 0 ) ); |
| 4742 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4743 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4744 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4745 | actual_output, |
| 4746 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4747 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 4748 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4749 | if( expected_output2->len != 0 ) |
| 4750 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4751 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4752 | actual_output, |
| 4753 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4754 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 4755 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4756 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4757 | |
| 4758 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4759 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4760 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4761 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4762 | mbedtls_free( actual_output ); |
| 4763 | } |
| 4764 | /* END_CASE */ |
| 4765 | |
| 4766 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4767 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4768 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4769 | size_t bytes = bytes_arg; |
| 4770 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4771 | unsigned char *output = NULL; |
| 4772 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4773 | size_t i; |
| 4774 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4775 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4776 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 4777 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4778 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4779 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4780 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4781 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4782 | /* Run several times, to ensure that every output byte will be |
| 4783 | * nonzero at least once with overwhelming probability |
| 4784 | * (2^(-8*number_of_runs)). */ |
| 4785 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4786 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4787 | if( bytes != 0 ) |
| 4788 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4789 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4790 | |
| 4791 | /* Check that no more than bytes have been overwritten */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4792 | ASSERT_COMPARE( output + bytes, sizeof( trail ), |
| 4793 | trail, sizeof( trail ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4794 | |
| 4795 | for( i = 0; i < bytes; i++ ) |
| 4796 | { |
| 4797 | if( output[i] != 0 ) |
| 4798 | ++changed[i]; |
| 4799 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4800 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4801 | |
| 4802 | /* Check that every byte was changed to nonzero at least once. This |
| 4803 | * validates that psa_generate_random is overwriting every byte of |
| 4804 | * the output buffer. */ |
| 4805 | for( i = 0; i < bytes; i++ ) |
| 4806 | { |
| 4807 | TEST_ASSERT( changed[i] != 0 ); |
| 4808 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4809 | |
| 4810 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4811 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4812 | mbedtls_free( output ); |
| 4813 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4814 | } |
| 4815 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4816 | |
| 4817 | /* BEGIN_CASE */ |
| 4818 | void generate_key( int type_arg, |
| 4819 | int bits_arg, |
| 4820 | int usage_arg, |
| 4821 | int alg_arg, |
| 4822 | int expected_status_arg ) |
| 4823 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4824 | psa_key_handle_t handle = 0; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4825 | psa_key_type_t type = type_arg; |
| 4826 | psa_key_usage_t usage = usage_arg; |
| 4827 | size_t bits = bits_arg; |
| 4828 | psa_algorithm_t alg = alg_arg; |
| 4829 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4830 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4831 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4832 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4833 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4834 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4835 | psa_set_key_usage_flags( &attributes, usage ); |
| 4836 | psa_set_key_algorithm( &attributes, alg ); |
| 4837 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4838 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4839 | |
| 4840 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 4841 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4842 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4843 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4844 | |
| 4845 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4846 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 4847 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 4848 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4849 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 4850 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4851 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 4852 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4853 | |
| 4854 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4855 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4856 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4857 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4858 | } |
| 4859 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 4860 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4861 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */ |
| 4862 | void generate_key_rsa( int bits_arg, |
| 4863 | data_t *e_arg, |
| 4864 | int expected_status_arg ) |
| 4865 | { |
| 4866 | psa_key_handle_t handle = 0; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 4867 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4868 | size_t bits = bits_arg; |
| 4869 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 4870 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 4871 | psa_status_t expected_status = expected_status_arg; |
| 4872 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4873 | uint8_t *exported = NULL; |
| 4874 | size_t exported_size = |
| 4875 | PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
| 4876 | size_t exported_length = SIZE_MAX; |
| 4877 | uint8_t *e_read_buffer = NULL; |
| 4878 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 4879 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4880 | size_t e_read_length = SIZE_MAX; |
| 4881 | |
| 4882 | if( e_arg->len == 0 || |
| 4883 | ( e_arg->len == 3 && |
| 4884 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 4885 | { |
| 4886 | is_default_public_exponent = 1; |
| 4887 | e_read_size = 0; |
| 4888 | } |
| 4889 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 4890 | ASSERT_ALLOC( exported, exported_size ); |
| 4891 | |
| 4892 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4893 | |
| 4894 | psa_set_key_usage_flags( &attributes, usage ); |
| 4895 | psa_set_key_algorithm( &attributes, alg ); |
| 4896 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 4897 | e_arg->x, e_arg->len ) ); |
| 4898 | psa_set_key_bits( &attributes, bits ); |
| 4899 | |
| 4900 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 4901 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4902 | if( expected_status != PSA_SUCCESS ) |
| 4903 | goto exit; |
| 4904 | |
| 4905 | /* Test the key information */ |
| 4906 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4907 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 4908 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 4909 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 4910 | e_read_buffer, e_read_size, |
| 4911 | &e_read_length ) ); |
| 4912 | if( is_default_public_exponent ) |
| 4913 | TEST_EQUAL( e_read_length, 0 ); |
| 4914 | else |
| 4915 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 4916 | |
| 4917 | /* Do something with the key according to its type and permitted usage. */ |
| 4918 | if( ! exercise_key( handle, usage, alg ) ) |
| 4919 | goto exit; |
| 4920 | |
| 4921 | /* Export the key and check the public exponent. */ |
| 4922 | PSA_ASSERT( psa_export_public_key( handle, |
| 4923 | exported, exported_size, |
| 4924 | &exported_length ) ); |
| 4925 | { |
| 4926 | uint8_t *p = exported; |
| 4927 | uint8_t *end = exported + exported_length; |
| 4928 | size_t len; |
| 4929 | /* RSAPublicKey ::= SEQUENCE { |
| 4930 | * modulus INTEGER, -- n |
| 4931 | * publicExponent INTEGER } -- e |
| 4932 | */ |
| 4933 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4934 | MBEDTLS_ASN1_SEQUENCE | |
| 4935 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4936 | TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
| 4937 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 4938 | MBEDTLS_ASN1_INTEGER ) ); |
| 4939 | if( len >= 1 && p[0] == 0 ) |
| 4940 | { |
| 4941 | ++p; |
| 4942 | --len; |
| 4943 | } |
| 4944 | if( e_arg->len == 0 ) |
| 4945 | { |
| 4946 | TEST_EQUAL( len, 3 ); |
| 4947 | TEST_EQUAL( p[0], 1 ); |
| 4948 | TEST_EQUAL( p[1], 0 ); |
| 4949 | TEST_EQUAL( p[2], 1 ); |
| 4950 | } |
| 4951 | else |
| 4952 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 4953 | } |
| 4954 | |
| 4955 | exit: |
| 4956 | psa_reset_key_attributes( &attributes ); |
| 4957 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4958 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4959 | mbedtls_free( e_read_buffer ); |
| 4960 | mbedtls_free( exported ); |
| 4961 | } |
| 4962 | /* END_CASE */ |
| 4963 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4964 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 4965 | void persistent_key_load_key_from_storage( data_t *data, |
| 4966 | int type_arg, int bits_arg, |
| 4967 | int usage_flags_arg, int alg_arg, |
| 4968 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4969 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 4970 | psa_key_id_t key_id = 1; |
| 4971 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4972 | psa_key_handle_t handle = 0; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 4973 | psa_key_handle_t base_key = 0; |
| 4974 | psa_key_type_t type = type_arg; |
| 4975 | size_t bits = bits_arg; |
| 4976 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 4977 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4978 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4979 | unsigned char *first_export = NULL; |
| 4980 | unsigned char *second_export = NULL; |
| 4981 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 4982 | size_t first_exported_length; |
| 4983 | size_t second_exported_length; |
| 4984 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 4985 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 4986 | { |
| 4987 | ASSERT_ALLOC( first_export, export_size ); |
| 4988 | ASSERT_ALLOC( second_export, export_size ); |
| 4989 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4990 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4991 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4992 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 4993 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 4994 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 4995 | psa_set_key_algorithm( &attributes, alg ); |
| 4996 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4997 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4998 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4999 | switch( generation_method ) |
| 5000 | { |
| 5001 | case IMPORT_KEY: |
| 5002 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5003 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
| 5004 | &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5005 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5006 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5007 | case GENERATE_KEY: |
| 5008 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5009 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5010 | break; |
| 5011 | |
| 5012 | case DERIVE_KEY: |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5013 | { |
| 5014 | /* Create base key */ |
| 5015 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5016 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5017 | psa_set_key_usage_flags( &base_attributes, |
| 5018 | PSA_KEY_USAGE_DERIVE ); |
| 5019 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5020 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5021 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5022 | data->x, data->len, |
| 5023 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5024 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5025 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5026 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5027 | &operation, |
| 5028 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5029 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5030 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5031 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5032 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5033 | &operation, |
| 5034 | &handle ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5035 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5036 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
| 5037 | base_key = 0; |
| 5038 | } |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5039 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5040 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5041 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5042 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5043 | /* Export the key if permitted by the key policy. */ |
| 5044 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5045 | { |
| 5046 | PSA_ASSERT( psa_export_key( handle, |
| 5047 | first_export, export_size, |
| 5048 | &first_exported_length ) ); |
| 5049 | if( generation_method == IMPORT_KEY ) |
| 5050 | ASSERT_COMPARE( data->x, data->len, |
| 5051 | first_export, first_exported_length ); |
| 5052 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5053 | |
| 5054 | /* Shutdown and restart */ |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 5055 | PSA_ASSERT( psa_close_key( handle ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5056 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5057 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5058 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5059 | /* Check key slot still contains key data */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5060 | PSA_ASSERT( psa_open_key( key_id, &handle ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5061 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5062 | TEST_EQUAL( psa_get_key_id( &attributes ), key_id ); |
| 5063 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5064 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5065 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5066 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5067 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 5068 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5069 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5070 | /* Export the key again if permitted by the key policy. */ |
| 5071 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5072 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5073 | PSA_ASSERT( psa_export_key( handle, |
| 5074 | second_export, export_size, |
| 5075 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5076 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5077 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5081 | if( ! exercise_key( handle, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5082 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5083 | |
| 5084 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5085 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5086 | mbedtls_free( first_export ); |
| 5087 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5088 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5089 | psa_destroy_key( base_key ); |
| 5090 | if( handle == 0 ) |
| 5091 | { |
| 5092 | /* In case there was a test failure after creating the persistent key |
| 5093 | * but while it was not open, try to re-open the persistent key |
| 5094 | * to delete it. */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5095 | psa_open_key( key_id, &handle ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5096 | } |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5097 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5098 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5099 | } |
| 5100 | /* END_CASE */ |