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