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