Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
| 7 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 8 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 9 | * uses mbedtls_ctr_drbg internally. */ |
| 10 | #include "mbedtls/ctr_drbg.h" |
| 11 | |
Ronald Cron | 02c78b7 | 2020-05-27 09:22:32 +0200 | [diff] [blame] | 12 | #include "test/psa_crypto_helpers.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 13 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 14 | /* Tests that require more than 128kB of RAM plus change have this symbol |
| 15 | * as a dependency. Currently we always define this symbol, so the tests |
| 16 | * are always executed. In the future we should make this conditional |
| 17 | * so that tests that require a lot of memory are skipped on constrained |
| 18 | * platforms. */ |
Gilles Peskine | 49232e8 | 2019-08-07 11:01:30 +0200 | [diff] [blame] | 19 | #define HAVE_RAM_AVAILABLE_128K |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 20 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 21 | #include "psa/crypto.h" |
| 22 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 23 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 24 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 25 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 26 | /* A hash algorithm that is known to be supported. |
| 27 | * |
| 28 | * This is used in some smoke tests. |
| 29 | */ |
| 30 | #if defined(MBEDTLS_MD2_C) |
| 31 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2 |
| 32 | #elif defined(MBEDTLS_MD4_C) |
| 33 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4 |
| 34 | #elif defined(MBEDTLS_MD5_C) |
| 35 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5 |
| 36 | /* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of |
| 37 | * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160 |
| 38 | * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be |
| 39 | * implausible anyway. */ |
| 40 | #elif defined(MBEDTLS_SHA1_C) |
| 41 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1 |
| 42 | #elif defined(MBEDTLS_SHA256_C) |
| 43 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256 |
| 44 | #elif defined(MBEDTLS_SHA512_C) |
| 45 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384 |
| 46 | #elif defined(MBEDTLS_SHA3_C) |
| 47 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256 |
| 48 | #else |
| 49 | #undef KNOWN_SUPPORTED_HASH_ALG |
| 50 | #endif |
| 51 | |
| 52 | /* A block cipher that is known to be supported. |
| 53 | * |
| 54 | * For simplicity's sake, stick to block ciphers with 16-byte blocks. |
| 55 | */ |
| 56 | #if defined(MBEDTLS_AES_C) |
| 57 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES |
| 58 | #elif defined(MBEDTLS_ARIA_C) |
| 59 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA |
| 60 | #elif defined(MBEDTLS_CAMELLIA_C) |
| 61 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA |
| 62 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER |
| 63 | #endif |
| 64 | |
| 65 | /* A MAC mode that is known to be supported. |
| 66 | * |
| 67 | * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or |
| 68 | * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER. |
| 69 | * |
| 70 | * This is used in some smoke tests. |
| 71 | */ |
| 72 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 73 | #define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) ) |
| 74 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC |
| 75 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C) |
| 76 | #define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC |
| 77 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 78 | #else |
| 79 | #undef KNOWN_SUPPORTED_MAC_ALG |
| 80 | #undef KNOWN_SUPPORTED_MAC_KEY_TYPE |
| 81 | #endif |
| 82 | |
| 83 | /* A cipher algorithm and key type that are known to be supported. |
| 84 | * |
| 85 | * This is used in some smoke tests. |
| 86 | */ |
| 87 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR) |
| 88 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR |
| 89 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 90 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING |
| 91 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB) |
| 92 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB |
| 93 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB) |
| 94 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB |
| 95 | #else |
| 96 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 97 | #endif |
| 98 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG) |
| 99 | #define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 100 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 101 | #elif defined(MBEDTLS_RC4_C) |
| 102 | #define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4 |
| 103 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4 |
| 104 | #else |
| 105 | #undef KNOWN_SUPPORTED_CIPHER_ALG |
| 106 | #undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE |
| 107 | #endif |
| 108 | |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 109 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 110 | int lifetime_is_secure_element( psa_key_lifetime_t lifetime ) |
| 111 | { |
| 112 | /* At the moment, anything that isn't a built-in lifetime is either |
| 113 | * a secure element or unassigned. */ |
| 114 | return( lifetime != PSA_KEY_LIFETIME_VOLATILE && |
| 115 | lifetime != PSA_KEY_LIFETIME_PERSISTENT ); |
| 116 | } |
| 117 | #else |
| 118 | int lifetime_is_secure_element( psa_key_lifetime_t lifetime ) |
| 119 | { |
| 120 | (void) lifetime; |
| 121 | return( 0 ); |
| 122 | } |
| 123 | #endif |
| 124 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 125 | /** Test if a buffer contains a constant byte value. |
| 126 | * |
| 127 | * `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] | 128 | * |
| 129 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 130 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 131 | * \param size Size of the buffer in bytes. |
| 132 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 133 | * \return 1 if the buffer is all-bits-zero. |
| 134 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 135 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 136 | 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] | 137 | { |
| 138 | size_t i; |
| 139 | for( i = 0; i < size; i++ ) |
| 140 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 141 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 142 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 143 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 144 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 145 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 146 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 147 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 148 | static int asn1_write_10x( unsigned char **p, |
| 149 | unsigned char *start, |
| 150 | size_t bits, |
| 151 | unsigned char x ) |
| 152 | { |
| 153 | int ret; |
| 154 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 155 | if( bits == 0 ) |
| 156 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 157 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 158 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 159 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 160 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 161 | *p -= len; |
| 162 | ( *p )[len-1] = x; |
| 163 | if( bits % 8 == 0 ) |
| 164 | ( *p )[1] |= 1; |
| 165 | else |
| 166 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 167 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 168 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 169 | MBEDTLS_ASN1_INTEGER ) ); |
| 170 | return( len ); |
| 171 | } |
| 172 | |
| 173 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 174 | size_t buffer_size, |
| 175 | unsigned char **p, |
| 176 | size_t bits, |
| 177 | int keypair ) |
| 178 | { |
| 179 | size_t half_bits = ( bits + 1 ) / 2; |
| 180 | int ret; |
| 181 | int len = 0; |
| 182 | /* Construct something that looks like a DER encoding of |
| 183 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 184 | * RSAPrivateKey ::= SEQUENCE { |
| 185 | * version Version, |
| 186 | * modulus INTEGER, -- n |
| 187 | * publicExponent INTEGER, -- e |
| 188 | * privateExponent INTEGER, -- d |
| 189 | * prime1 INTEGER, -- p |
| 190 | * prime2 INTEGER, -- q |
| 191 | * exponent1 INTEGER, -- d mod (p-1) |
| 192 | * exponent2 INTEGER, -- d mod (q-1) |
| 193 | * coefficient INTEGER, -- (inverse of q) mod p |
| 194 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 195 | * } |
| 196 | * Or, for a public key, the same structure with only |
| 197 | * version, modulus and publicExponent. |
| 198 | */ |
| 199 | *p = buffer + buffer_size; |
| 200 | if( keypair ) |
| 201 | { |
| 202 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 203 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 204 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 205 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 206 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 207 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 208 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 209 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 210 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 211 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 212 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 213 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 214 | } |
| 215 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 216 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 217 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 218 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 219 | if( keypair ) |
| 220 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 221 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 222 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 223 | { |
| 224 | const unsigned char tag = |
| 225 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 226 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 227 | } |
| 228 | return( len ); |
| 229 | } |
| 230 | |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 231 | int check_key_attributes_sanity( psa_key_handle_t key ) |
| 232 | { |
| 233 | int ok = 0; |
| 234 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 235 | psa_key_lifetime_t lifetime; |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 236 | mbedtls_svc_key_id_t id; |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 237 | psa_key_type_t type; |
| 238 | psa_key_type_t bits; |
| 239 | |
| 240 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 241 | lifetime = psa_get_key_lifetime( &attributes ); |
| 242 | id = psa_get_key_id( &attributes ); |
| 243 | type = psa_get_key_type( &attributes ); |
| 244 | bits = psa_get_key_bits( &attributes ); |
| 245 | |
| 246 | /* Persistence */ |
| 247 | if( lifetime == PSA_KEY_LIFETIME_VOLATILE ) |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 248 | TEST_ASSERT( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) == 0 ); |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 249 | else |
| 250 | { |
| 251 | TEST_ASSERT( |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 252 | ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) && |
| 253 | ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) ); |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 254 | } |
| 255 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 256 | /* randomly-generated 64-bit constant, should never appear in test data */ |
| 257 | psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21; |
| 258 | psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number ); |
| 259 | if( lifetime_is_secure_element( lifetime ) ) |
| 260 | { |
| 261 | /* Mbed Crypto currently always exposes the slot number to |
| 262 | * applications. This is not mandated by the PSA specification |
| 263 | * and may change in future versions. */ |
| 264 | TEST_EQUAL( status, 0 ); |
| 265 | TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 ); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 270 | } |
| 271 | #endif |
| 272 | |
| 273 | /* Type and size */ |
| 274 | TEST_ASSERT( type != 0 ); |
| 275 | TEST_ASSERT( bits != 0 ); |
| 276 | TEST_ASSERT( bits <= PSA_MAX_KEY_BITS ); |
| 277 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
| 278 | TEST_ASSERT( bits % 8 == 0 ); |
| 279 | |
| 280 | /* MAX macros concerning specific key types */ |
| 281 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 282 | TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS ); |
| 283 | else if( PSA_KEY_TYPE_IS_RSA( type ) ) |
| 284 | TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS ); |
| 285 | TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) <= PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE ); |
| 286 | |
| 287 | ok = 1; |
| 288 | |
| 289 | exit: |
| 290 | psa_reset_key_attributes( &attributes ); |
| 291 | return( ok ); |
| 292 | } |
| 293 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 294 | int exercise_mac_setup( psa_key_type_t key_type, |
| 295 | const unsigned char *key_bytes, |
| 296 | size_t key_length, |
| 297 | psa_algorithm_t alg, |
| 298 | psa_mac_operation_t *operation, |
| 299 | psa_status_t *status ) |
| 300 | { |
| 301 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 302 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 303 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 304 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 305 | psa_set_key_algorithm( &attributes, alg ); |
| 306 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 307 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 308 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 309 | |
| 310 | *status = psa_mac_sign_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 311 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 312 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 313 | /* If setup failed, reproduce the failure, so that the caller can |
| 314 | * test the resulting state of the operation object. */ |
| 315 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 316 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 317 | TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ), |
| 318 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | psa_destroy_key( handle ); |
| 322 | return( 1 ); |
| 323 | |
| 324 | exit: |
| 325 | psa_destroy_key( handle ); |
| 326 | return( 0 ); |
| 327 | } |
| 328 | |
| 329 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 330 | const unsigned char *key_bytes, |
| 331 | size_t key_length, |
| 332 | psa_algorithm_t alg, |
| 333 | psa_cipher_operation_t *operation, |
| 334 | psa_status_t *status ) |
| 335 | { |
| 336 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 337 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 338 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 339 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 340 | psa_set_key_algorithm( &attributes, alg ); |
| 341 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 342 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 343 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 344 | |
| 345 | *status = psa_cipher_encrypt_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 346 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 347 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 348 | /* If setup failed, reproduce the failure, so that the caller can |
| 349 | * test the resulting state of the operation object. */ |
| 350 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 351 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 352 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ), |
| 353 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | psa_destroy_key( handle ); |
| 357 | return( 1 ); |
| 358 | |
| 359 | exit: |
| 360 | psa_destroy_key( handle ); |
| 361 | return( 0 ); |
| 362 | } |
| 363 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 364 | static int exercise_mac_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 365 | psa_key_usage_t usage, |
| 366 | psa_algorithm_t alg ) |
| 367 | { |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 368 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 369 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 370 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 371 | size_t mac_length = sizeof( mac ); |
| 372 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 373 | if( usage & PSA_KEY_USAGE_SIGN_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 374 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 375 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 376 | handle, alg ) ); |
| 377 | PSA_ASSERT( psa_mac_update( &operation, |
| 378 | input, sizeof( input ) ) ); |
| 379 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 380 | mac, sizeof( mac ), |
| 381 | &mac_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 384 | if( usage & PSA_KEY_USAGE_VERIFY_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 385 | { |
| 386 | psa_status_t verify_status = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 387 | ( usage & PSA_KEY_USAGE_SIGN_HASH ? |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 388 | PSA_SUCCESS : |
| 389 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 390 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 391 | handle, alg ) ); |
| 392 | PSA_ASSERT( psa_mac_update( &operation, |
| 393 | input, sizeof( input ) ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 394 | TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ), |
| 395 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | return( 1 ); |
| 399 | |
| 400 | exit: |
| 401 | psa_mac_abort( &operation ); |
| 402 | return( 0 ); |
| 403 | } |
| 404 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 405 | static int exercise_cipher_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 406 | psa_key_usage_t usage, |
| 407 | psa_algorithm_t alg ) |
| 408 | { |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 409 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 410 | unsigned char iv[16] = {0}; |
| 411 | size_t iv_length = sizeof( iv ); |
| 412 | const unsigned char plaintext[16] = "Hello, world..."; |
| 413 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 414 | size_t ciphertext_length = sizeof( ciphertext ); |
| 415 | unsigned char decrypted[sizeof( ciphertext )]; |
| 416 | size_t part_length; |
| 417 | |
| 418 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 419 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 420 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 421 | handle, alg ) ); |
| 422 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 423 | iv, sizeof( iv ), |
| 424 | &iv_length ) ); |
| 425 | PSA_ASSERT( psa_cipher_update( &operation, |
| 426 | plaintext, sizeof( plaintext ), |
| 427 | ciphertext, sizeof( ciphertext ), |
| 428 | &ciphertext_length ) ); |
| 429 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 430 | ciphertext + ciphertext_length, |
| 431 | sizeof( ciphertext ) - ciphertext_length, |
| 432 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 433 | ciphertext_length += part_length; |
| 434 | } |
| 435 | |
| 436 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 437 | { |
| 438 | psa_status_t status; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 439 | int maybe_invalid_padding = 0; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 440 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 441 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 442 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 443 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 444 | /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't |
| 445 | * have this macro yet. */ |
| 446 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( |
| 447 | psa_get_key_type( &attributes ) ); |
| 448 | maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 449 | } |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 450 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 451 | handle, alg ) ); |
| 452 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 453 | iv, iv_length ) ); |
| 454 | PSA_ASSERT( psa_cipher_update( &operation, |
| 455 | ciphertext, ciphertext_length, |
| 456 | decrypted, sizeof( decrypted ), |
| 457 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 458 | status = psa_cipher_finish( &operation, |
| 459 | decrypted + part_length, |
| 460 | sizeof( decrypted ) - part_length, |
| 461 | &part_length ); |
| 462 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 463 | * if the input is some aribtrary data rather than an actual |
| 464 | ciphertext, a padding error is likely. */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 465 | if( maybe_invalid_padding ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 466 | TEST_ASSERT( status == PSA_SUCCESS || |
| 467 | status == PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 468 | else |
| 469 | PSA_ASSERT( status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | return( 1 ); |
| 473 | |
| 474 | exit: |
| 475 | psa_cipher_abort( &operation ); |
| 476 | return( 0 ); |
| 477 | } |
| 478 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 479 | static int exercise_aead_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 480 | psa_key_usage_t usage, |
| 481 | psa_algorithm_t alg ) |
| 482 | { |
| 483 | unsigned char nonce[16] = {0}; |
| 484 | size_t nonce_length = sizeof( nonce ); |
| 485 | unsigned char plaintext[16] = "Hello, world..."; |
| 486 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 487 | size_t ciphertext_length = sizeof( ciphertext ); |
| 488 | size_t plaintext_length = sizeof( ciphertext ); |
| 489 | |
| 490 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 491 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 492 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 493 | nonce, nonce_length, |
| 494 | NULL, 0, |
| 495 | plaintext, sizeof( plaintext ), |
| 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 verify_status = |
| 503 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 504 | PSA_SUCCESS : |
| 505 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 506 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 507 | nonce, nonce_length, |
| 508 | NULL, 0, |
| 509 | ciphertext, ciphertext_length, |
| 510 | plaintext, sizeof( plaintext ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 511 | &plaintext_length ), |
| 512 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | return( 1 ); |
| 516 | |
| 517 | exit: |
| 518 | return( 0 ); |
| 519 | } |
| 520 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 521 | static int exercise_signature_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 522 | psa_key_usage_t usage, |
| 523 | psa_algorithm_t alg ) |
| 524 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 525 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 526 | size_t payload_length = 16; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 527 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 528 | size_t signature_length = sizeof( signature ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 529 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 530 | |
| 531 | /* If the policy allows signing with any hash, just pick one. */ |
| 532 | if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH ) |
| 533 | { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 534 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 535 | hash_alg = KNOWN_SUPPORTED_HASH_ALG; |
| 536 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 537 | #else |
| 538 | test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 539 | return( 1 ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 540 | #endif |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 541 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 542 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 543 | if( usage & PSA_KEY_USAGE_SIGN_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 544 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 545 | /* Some algorithms require the payload to have the size of |
| 546 | * the hash encoded in the algorithm. Use this input size |
| 547 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 548 | if( hash_alg != 0 ) |
| 549 | payload_length = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 550 | PSA_ASSERT( psa_sign_hash( handle, alg, |
| 551 | payload, payload_length, |
| 552 | signature, sizeof( signature ), |
| 553 | &signature_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 556 | if( usage & PSA_KEY_USAGE_VERIFY_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 557 | { |
| 558 | psa_status_t verify_status = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 559 | ( usage & PSA_KEY_USAGE_SIGN_HASH ? |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 560 | PSA_SUCCESS : |
| 561 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 562 | TEST_EQUAL( psa_verify_hash( handle, alg, |
| 563 | payload, payload_length, |
| 564 | signature, signature_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 565 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | return( 1 ); |
| 569 | |
| 570 | exit: |
| 571 | return( 0 ); |
| 572 | } |
| 573 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 574 | static int exercise_asymmetric_encryption_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 575 | psa_key_usage_t usage, |
| 576 | psa_algorithm_t alg ) |
| 577 | { |
| 578 | unsigned char plaintext[256] = "Hello, world..."; |
| 579 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 580 | size_t ciphertext_length = sizeof( ciphertext ); |
| 581 | size_t plaintext_length = 16; |
| 582 | |
| 583 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 584 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 585 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 586 | plaintext, plaintext_length, |
| 587 | NULL, 0, |
| 588 | ciphertext, sizeof( ciphertext ), |
| 589 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 593 | { |
| 594 | psa_status_t status = |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 595 | psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 596 | ciphertext, ciphertext_length, |
| 597 | NULL, 0, |
| 598 | plaintext, sizeof( plaintext ), |
| 599 | &plaintext_length ); |
| 600 | TEST_ASSERT( status == PSA_SUCCESS || |
| 601 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 602 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 603 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 604 | } |
| 605 | |
| 606 | return( 1 ); |
| 607 | |
| 608 | exit: |
| 609 | return( 0 ); |
| 610 | } |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 611 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 612 | static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation, |
| 613 | psa_key_handle_t handle, |
| 614 | psa_algorithm_t alg, |
| 615 | unsigned char* input1, size_t input1_length, |
| 616 | unsigned char* input2, size_t input2_length, |
| 617 | size_t capacity ) |
| 618 | { |
| 619 | PSA_ASSERT( psa_key_derivation_setup( operation, alg ) ); |
| 620 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 621 | { |
| 622 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 623 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 624 | input1, input1_length ) ); |
| 625 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 626 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 627 | handle ) ); |
| 628 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 629 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 630 | input2, |
| 631 | input2_length ) ); |
| 632 | } |
| 633 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 634 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 635 | { |
| 636 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 637 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 638 | input1, input1_length ) ); |
| 639 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 640 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 641 | handle ) ); |
| 642 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 643 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 644 | input2, input2_length ) ); |
| 645 | } |
| 646 | else |
| 647 | { |
| 648 | TEST_ASSERT( ! "Key derivation algorithm not supported" ); |
| 649 | } |
| 650 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 651 | if( capacity != SIZE_MAX ) |
| 652 | PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) ); |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 653 | |
| 654 | return( 1 ); |
| 655 | |
| 656 | exit: |
| 657 | return( 0 ); |
| 658 | } |
| 659 | |
| 660 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 661 | static int exercise_key_derivation_key( psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 662 | psa_key_usage_t usage, |
| 663 | psa_algorithm_t alg ) |
| 664 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 665 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 666 | unsigned char input1[] = "Input 1"; |
| 667 | size_t input1_length = sizeof( input1 ); |
| 668 | unsigned char input2[] = "Input 2"; |
| 669 | size_t input2_length = sizeof( input2 ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 670 | unsigned char output[1]; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 671 | size_t capacity = sizeof( output ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 672 | |
| 673 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 674 | { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 675 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 676 | input1, input1_length, |
| 677 | input2, input2_length, capacity ) ) |
| 678 | goto exit; |
Gilles Peskine | 7607cd6 | 2019-05-29 17:35:00 +0200 | [diff] [blame] | 679 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 680 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 681 | output, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 682 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 683 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | return( 1 ); |
| 687 | |
| 688 | exit: |
| 689 | return( 0 ); |
| 690 | } |
| 691 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 692 | /* We need two keys to exercise key agreement. Exercise the |
| 693 | * private key against its own public key. */ |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 694 | static psa_status_t key_agreement_with_self( |
| 695 | psa_key_derivation_operation_t *operation, |
| 696 | psa_key_handle_t handle ) |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 697 | { |
| 698 | psa_key_type_t private_key_type; |
| 699 | psa_key_type_t public_key_type; |
| 700 | size_t key_bits; |
| 701 | uint8_t *public_key = NULL; |
| 702 | size_t public_key_length; |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 703 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 704 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 705 | * 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] | 706 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 707 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 708 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 709 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 710 | private_key_type = psa_get_key_type( &attributes ); |
| 711 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 712 | 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] | 713 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 714 | ASSERT_ALLOC( public_key, public_key_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 715 | PSA_ASSERT( psa_export_public_key( handle, |
| 716 | public_key, public_key_length, |
| 717 | &public_key_length ) ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 718 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 719 | status = psa_key_derivation_key_agreement( |
| 720 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle, |
| 721 | public_key, public_key_length ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 722 | exit: |
| 723 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 724 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 725 | return( status ); |
| 726 | } |
| 727 | |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 728 | /* We need two keys to exercise key agreement. Exercise the |
| 729 | * private key against its own public key. */ |
| 730 | static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg, |
| 731 | psa_key_handle_t handle ) |
| 732 | { |
| 733 | psa_key_type_t private_key_type; |
| 734 | psa_key_type_t public_key_type; |
| 735 | size_t key_bits; |
| 736 | uint8_t *public_key = NULL; |
| 737 | size_t public_key_length; |
| 738 | uint8_t output[1024]; |
| 739 | size_t output_length; |
| 740 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 741 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 742 | * 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] | 743 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 744 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 745 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 746 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 747 | private_key_type = psa_get_key_type( &attributes ); |
| 748 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 749 | 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] | 750 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 751 | ASSERT_ALLOC( public_key, public_key_length ); |
| 752 | PSA_ASSERT( psa_export_public_key( handle, |
| 753 | public_key, public_key_length, |
| 754 | &public_key_length ) ); |
| 755 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 756 | status = psa_raw_key_agreement( alg, handle, |
| 757 | public_key, public_key_length, |
| 758 | output, sizeof( output ), &output_length ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 759 | exit: |
| 760 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 761 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 762 | return( status ); |
| 763 | } |
| 764 | |
| 765 | static int exercise_raw_key_agreement_key( psa_key_handle_t handle, |
| 766 | psa_key_usage_t usage, |
| 767 | psa_algorithm_t alg ) |
| 768 | { |
| 769 | int ok = 0; |
| 770 | |
| 771 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 772 | { |
| 773 | /* We need two keys to exercise key agreement. Exercise the |
| 774 | * private key against its own public key. */ |
| 775 | PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) ); |
| 776 | } |
| 777 | ok = 1; |
| 778 | |
| 779 | exit: |
| 780 | return( ok ); |
| 781 | } |
| 782 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 783 | static int exercise_key_agreement_key( psa_key_handle_t handle, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 784 | psa_key_usage_t usage, |
| 785 | psa_algorithm_t alg ) |
| 786 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 787 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 788 | unsigned char output[1]; |
| 789 | int ok = 0; |
| 790 | |
| 791 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 792 | { |
| 793 | /* We need two keys to exercise key agreement. Exercise the |
| 794 | * private key against its own public key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 795 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 796 | PSA_ASSERT( key_agreement_with_self( &operation, handle ) ); |
| 797 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 798 | output, |
| 799 | sizeof( output ) ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 800 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 801 | } |
| 802 | ok = 1; |
| 803 | |
| 804 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 805 | return( ok ); |
| 806 | } |
| 807 | |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 808 | int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 809 | size_t min_bits, size_t max_bits, |
| 810 | int must_be_odd ) |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 811 | { |
| 812 | size_t len; |
| 813 | size_t actual_bits; |
| 814 | unsigned char msb; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 815 | TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 816 | MBEDTLS_ASN1_INTEGER ), |
| 817 | 0 ); |
k-stachowiak | 9b88efc | 2019-09-13 15:26:53 +0200 | [diff] [blame] | 818 | |
| 819 | /* Check if the retrieved length doesn't extend the actual buffer's size. |
| 820 | * It is assumed here, that end >= p, which validates casting to size_t. */ |
| 821 | TEST_ASSERT( len <= (size_t)( end - *p) ); |
| 822 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 823 | /* Tolerate a slight departure from DER encoding: |
| 824 | * - 0 may be represented by an empty string or a 1-byte string. |
| 825 | * - The sign bit may be used as a value bit. */ |
| 826 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 827 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 828 | { |
| 829 | ++( *p ); |
| 830 | --len; |
| 831 | } |
| 832 | if( min_bits == 0 && len == 0 ) |
| 833 | return( 1 ); |
| 834 | msb = ( *p )[0]; |
| 835 | TEST_ASSERT( msb != 0 ); |
| 836 | actual_bits = 8 * ( len - 1 ); |
| 837 | while( msb != 0 ) |
| 838 | { |
| 839 | msb >>= 1; |
| 840 | ++actual_bits; |
| 841 | } |
| 842 | TEST_ASSERT( actual_bits >= min_bits ); |
| 843 | TEST_ASSERT( actual_bits <= max_bits ); |
| 844 | if( must_be_odd ) |
| 845 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 846 | *p += len; |
| 847 | return( 1 ); |
| 848 | exit: |
| 849 | return( 0 ); |
| 850 | } |
| 851 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 852 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 853 | uint8_t *exported, size_t exported_length ) |
| 854 | { |
| 855 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 856 | TEST_EQUAL( exported_length, ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 857 | else |
| 858 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 859 | |
| 860 | #if defined(MBEDTLS_DES_C) |
| 861 | if( type == PSA_KEY_TYPE_DES ) |
| 862 | { |
| 863 | /* Check the parity bits. */ |
| 864 | unsigned i; |
| 865 | for( i = 0; i < bits / 8; i++ ) |
| 866 | { |
| 867 | unsigned bit_count = 0; |
| 868 | unsigned m; |
| 869 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 870 | { |
| 871 | if( exported[i] & m ) |
| 872 | ++bit_count; |
| 873 | } |
| 874 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 875 | } |
| 876 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 877 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 878 | #endif |
| 879 | |
| 880 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 881 | if( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 882 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 883 | uint8_t *p = exported; |
| 884 | uint8_t *end = exported + exported_length; |
| 885 | size_t len; |
| 886 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 887 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 888 | * modulus INTEGER, -- n |
| 889 | * publicExponent INTEGER, -- e |
| 890 | * privateExponent INTEGER, -- d |
| 891 | * prime1 INTEGER, -- p |
| 892 | * prime2 INTEGER, -- q |
| 893 | * exponent1 INTEGER, -- d mod (p-1) |
| 894 | * exponent2 INTEGER, -- d mod (q-1) |
| 895 | * coefficient INTEGER, -- (inverse of q) mod p |
| 896 | * } |
| 897 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 898 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 899 | MBEDTLS_ASN1_SEQUENCE | |
| 900 | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); |
| 901 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 902 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 903 | goto exit; |
| 904 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 905 | goto exit; |
| 906 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 907 | goto exit; |
| 908 | /* Require d to be at least half the size of n. */ |
| 909 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 910 | goto exit; |
| 911 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 912 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 913 | goto exit; |
| 914 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 915 | goto exit; |
| 916 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 917 | goto exit; |
| 918 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 919 | goto exit; |
| 920 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 921 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 922 | TEST_EQUAL( p, end ); |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 923 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 924 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 925 | #endif /* MBEDTLS_RSA_C */ |
| 926 | |
| 927 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 928 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 929 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 930 | /* Just the secret value */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 931 | TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 932 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 933 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 934 | #endif /* MBEDTLS_ECP_C */ |
| 935 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 936 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 937 | { |
| 938 | uint8_t *p = exported; |
| 939 | uint8_t *end = exported + exported_length; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 940 | #if defined(MBEDTLS_RSA_C) |
| 941 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 942 | { |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 943 | size_t len; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 944 | /* RSAPublicKey ::= SEQUENCE { |
| 945 | * modulus INTEGER, -- n |
| 946 | * publicExponent INTEGER } -- e |
| 947 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 948 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 949 | MBEDTLS_ASN1_SEQUENCE | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 950 | MBEDTLS_ASN1_CONSTRUCTED ), |
| 951 | 0 ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 952 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 953 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 954 | goto exit; |
| 955 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 956 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 957 | TEST_EQUAL( p, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 958 | } |
| 959 | else |
| 960 | #endif /* MBEDTLS_RSA_C */ |
| 961 | #if defined(MBEDTLS_ECP_C) |
| 962 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 963 | { |
Steven Cooreman | 3fa684e | 2020-07-30 15:04:07 +0200 | [diff] [blame] | 964 | if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY ) |
| 965 | { |
| 966 | /* The representation of an ECC Montgomery public key is |
| 967 | * the raw compressed point */ |
| 968 | TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end ); |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | /* The representation of an ECC Weierstrass public key is: |
| 973 | * - The byte 0x04; |
| 974 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 975 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 976 | * - where m is the bit size associated with the curve. |
| 977 | */ |
| 978 | TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end ); |
| 979 | TEST_EQUAL( p[0], 4 ); |
| 980 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 981 | } |
| 982 | else |
| 983 | #endif /* MBEDTLS_ECP_C */ |
| 984 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 985 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 986 | mbedtls_snprintf( message, sizeof( message ), |
| 987 | "No sanity check for public key type=0x%08lx", |
| 988 | (unsigned long) type ); |
| 989 | test_fail( message, __LINE__, __FILE__ ); |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 990 | (void) p; |
| 991 | (void) end; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 992 | return( 0 ); |
| 993 | } |
| 994 | } |
| 995 | else |
| 996 | |
| 997 | { |
| 998 | /* No sanity checks for other types */ |
| 999 | } |
| 1000 | |
| 1001 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1002 | |
| 1003 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 1004 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1007 | static int exercise_export_key( psa_key_handle_t handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1008 | psa_key_usage_t usage ) |
| 1009 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1010 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1011 | uint8_t *exported = NULL; |
| 1012 | size_t exported_size = 0; |
| 1013 | size_t exported_length = 0; |
| 1014 | int ok = 0; |
| 1015 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1016 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 1017 | |
| 1018 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1019 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1020 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1021 | TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ), |
| 1022 | PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1023 | ok = 1; |
| 1024 | goto exit; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1025 | } |
| 1026 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1027 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ), |
| 1028 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1029 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1030 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1031 | PSA_ASSERT( psa_export_key( handle, |
| 1032 | exported, exported_size, |
| 1033 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1034 | ok = exported_key_sanity_check( psa_get_key_type( &attributes ), |
| 1035 | psa_get_key_bits( &attributes ), |
| 1036 | exported, exported_length ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1037 | |
| 1038 | exit: |
| 1039 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1040 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1041 | return( ok ); |
| 1042 | } |
| 1043 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1044 | static int exercise_export_public_key( psa_key_handle_t handle ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1045 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1046 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1047 | psa_key_type_t public_type; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1048 | uint8_t *exported = NULL; |
| 1049 | size_t exported_size = 0; |
| 1050 | size_t exported_length = 0; |
| 1051 | int ok = 0; |
| 1052 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1053 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1054 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1055 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1056 | TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1057 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1058 | return( 1 ); |
| 1059 | } |
| 1060 | |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1061 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1062 | psa_get_key_type( &attributes ) ); |
| 1063 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, |
| 1064 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1065 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1066 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1067 | PSA_ASSERT( psa_export_public_key( handle, |
| 1068 | exported, exported_size, |
| 1069 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1070 | ok = exported_key_sanity_check( public_type, |
| 1071 | psa_get_key_bits( &attributes ), |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1072 | exported, exported_length ); |
| 1073 | |
| 1074 | exit: |
| 1075 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1076 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1077 | return( ok ); |
| 1078 | } |
| 1079 | |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 1080 | /** Do smoke tests on a key. |
| 1081 | * |
| 1082 | * Perform one of each operation indicated by \p alg (decrypt/encrypt, |
| 1083 | * sign/verify, or derivation) that is permitted according to \p usage. |
| 1084 | * \p usage and \p alg should correspond to the expected policy on the |
| 1085 | * key. |
| 1086 | * |
| 1087 | * Export the key if permitted by \p usage, and check that the output |
| 1088 | * looks sensible. If \p usage forbids export, check that |
| 1089 | * \p psa_export_key correctly rejects the attempt. If the key is |
| 1090 | * asymmetric, also check \p psa_export_public_key. |
| 1091 | * |
| 1092 | * If the key fails the tests, this function calls the test framework's |
| 1093 | * `test_fail` function and returns false. Otherwise this function returns |
| 1094 | * true. Therefore it should be used as follows: |
| 1095 | * ``` |
| 1096 | * if( ! exercise_key( ... ) ) goto exit; |
| 1097 | * ``` |
| 1098 | * |
| 1099 | * \param handle The key to exercise. It should be capable of performing |
| 1100 | * \p alg. |
| 1101 | * \param usage The usage flags to assume. |
| 1102 | * \param alg The algorithm to exercise. |
| 1103 | * |
| 1104 | * \retval 0 The key failed the smoke tests. |
| 1105 | * \retval 1 The key passed the smoke tests. |
| 1106 | */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1107 | static int exercise_key( psa_key_handle_t handle, |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1108 | psa_key_usage_t usage, |
| 1109 | psa_algorithm_t alg ) |
| 1110 | { |
| 1111 | int ok; |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 1112 | |
| 1113 | if( ! check_key_attributes_sanity( handle ) ) |
| 1114 | return( 0 ); |
| 1115 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1116 | if( alg == 0 ) |
| 1117 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 1118 | else if( PSA_ALG_IS_MAC( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1119 | ok = exercise_mac_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1120 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1121 | ok = exercise_cipher_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1122 | else if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1123 | ok = exercise_aead_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1124 | else if( PSA_ALG_IS_SIGN( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1125 | ok = exercise_signature_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1126 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1127 | ok = exercise_asymmetric_encryption_key( handle, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1128 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1129 | ok = exercise_key_derivation_key( handle, usage, alg ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 1130 | else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 1131 | ok = exercise_raw_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1132 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1133 | ok = exercise_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1134 | else |
| 1135 | { |
| 1136 | char message[40]; |
| 1137 | mbedtls_snprintf( message, sizeof( message ), |
| 1138 | "No code to exercise alg=0x%08lx", |
| 1139 | (unsigned long) alg ); |
| 1140 | test_fail( message, __LINE__, __FILE__ ); |
| 1141 | ok = 0; |
| 1142 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1143 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1144 | ok = ok && exercise_export_key( handle, usage ); |
| 1145 | ok = ok && exercise_export_public_key( handle ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1146 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1147 | return( ok ); |
| 1148 | } |
| 1149 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1150 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 1151 | psa_algorithm_t alg ) |
| 1152 | { |
| 1153 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 1154 | { |
| 1155 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1156 | PSA_KEY_USAGE_VERIFY_HASH : |
| 1157 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1158 | } |
| 1159 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 1160 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 1161 | { |
| 1162 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1163 | PSA_KEY_USAGE_ENCRYPT : |
| 1164 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 1165 | } |
| 1166 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 1167 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 1168 | { |
| 1169 | return( PSA_KEY_USAGE_DERIVE ); |
| 1170 | } |
| 1171 | else |
| 1172 | { |
| 1173 | return( 0 ); |
| 1174 | } |
| 1175 | |
| 1176 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1177 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1178 | static int test_operations_on_invalid_handle( psa_key_handle_t handle ) |
| 1179 | { |
| 1180 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1181 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1182 | uint8_t buffer[1]; |
| 1183 | size_t length; |
| 1184 | int ok = 0; |
| 1185 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1186 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1187 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 1188 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 1189 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
| 1190 | TEST_EQUAL( psa_get_key_attributes( handle, &attributes ), |
| 1191 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1192 | TEST_EQUAL( |
| 1193 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 1194 | TEST_EQUAL( |
| 1195 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 1196 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1197 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1198 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1199 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 1200 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 1201 | |
| 1202 | TEST_EQUAL( psa_export_key( handle, |
| 1203 | buffer, sizeof( buffer ), &length ), |
| 1204 | PSA_ERROR_INVALID_HANDLE ); |
| 1205 | TEST_EQUAL( psa_export_public_key( handle, |
| 1206 | buffer, sizeof( buffer ), &length ), |
| 1207 | PSA_ERROR_INVALID_HANDLE ); |
| 1208 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1209 | ok = 1; |
| 1210 | |
| 1211 | exit: |
| 1212 | psa_reset_key_attributes( &attributes ); |
| 1213 | return( ok ); |
| 1214 | } |
| 1215 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1216 | /* Assert that a key isn't reported as having a slot number. */ |
| 1217 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1218 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1219 | do \ |
| 1220 | { \ |
| 1221 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 1222 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 1223 | attributes, \ |
| 1224 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 1225 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 1226 | } \ |
| 1227 | while( 0 ) |
| 1228 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1229 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1230 | ( (void) 0 ) |
| 1231 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1232 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1233 | /* An overapproximation of the amount of storage needed for a key of the |
| 1234 | * given type and with the given content. The API doesn't make it easy |
| 1235 | * to find a good value for the size. The current implementation doesn't |
| 1236 | * care about the value anyway. */ |
| 1237 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 1238 | ( data )->len |
| 1239 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1240 | typedef enum { |
| 1241 | IMPORT_KEY = 0, |
| 1242 | GENERATE_KEY = 1, |
| 1243 | DERIVE_KEY = 2 |
| 1244 | } generate_method; |
| 1245 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1246 | /* END_HEADER */ |
| 1247 | |
| 1248 | /* BEGIN_DEPENDENCIES |
| 1249 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1250 | * END_DEPENDENCIES |
| 1251 | */ |
| 1252 | |
| 1253 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1254 | void static_checks( ) |
| 1255 | { |
| 1256 | size_t max_truncated_mac_size = |
| 1257 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1258 | |
| 1259 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1260 | * encoding. The shifted mask is the maximum truncated value. The |
| 1261 | * untruncated algorithm may be one byte larger. */ |
| 1262 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 1263 | |
| 1264 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 1265 | /* Check deprecated constants. */ |
| 1266 | TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR ); |
| 1267 | TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS ); |
| 1268 | TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST ); |
| 1269 | TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA ); |
| 1270 | TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED ); |
| 1271 | TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH ); |
| 1272 | TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH ); |
| 1273 | TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 1274 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 1275 | TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1276 | TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1277 | TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1278 | TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1279 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1280 | TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1281 | TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1282 | TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1283 | TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1284 | TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1285 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 1286 | TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1287 | TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1288 | TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1289 | TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1290 | TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1291 | TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1292 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1293 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1294 | TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1295 | TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1296 | TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1297 | TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1298 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 1299 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 1300 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1301 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1302 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1303 | TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY ); |
| 1304 | TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY ); |
| 1305 | |
| 1306 | TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1307 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1308 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 1309 | TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1310 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1311 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 1312 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1313 | TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 1314 | |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 1315 | TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 ); |
| 1316 | TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 ); |
| 1317 | TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 ); |
| 1318 | TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 ); |
| 1319 | TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 ); |
| 1320 | |
| 1321 | TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 ); |
| 1322 | TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 1323 | #endif |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1324 | } |
| 1325 | /* END_CASE */ |
| 1326 | |
| 1327 | /* BEGIN_CASE */ |
Ronald Cron | 81e0050 | 2020-07-28 15:06:14 +0200 | [diff] [blame] | 1328 | void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg, |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1329 | int usage_flags_arg, int alg_arg, |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1330 | int type_arg, int bits_arg ) |
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_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 81e0050 | 2020-07-28 15:06:14 +0200 | [diff] [blame] | 1333 | mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1334 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1335 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 1336 | psa_algorithm_t alg = alg_arg; |
| 1337 | psa_key_type_t type = type_arg; |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1338 | size_t bits = bits_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1339 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1340 | TEST_EQUAL( |
| 1341 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 1342 | TEST_EQUAL( |
| 1343 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1344 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1345 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1346 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1347 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1348 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1349 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1350 | psa_set_key_id( &attributes, id ); |
| 1351 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1352 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 1353 | psa_set_key_algorithm( &attributes, alg ); |
| 1354 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1355 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1356 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1357 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 1358 | psa_get_key_id( &attributes ), id ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1359 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime ); |
| 1360 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 1361 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
| 1362 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1363 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1364 | |
| 1365 | psa_reset_key_attributes( &attributes ); |
| 1366 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1367 | TEST_EQUAL( |
| 1368 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 1369 | TEST_EQUAL( |
| 1370 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1371 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1372 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1373 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1374 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1375 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1376 | } |
| 1377 | /* END_CASE */ |
| 1378 | |
| 1379 | /* BEGIN_CASE */ |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1380 | void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg, |
| 1381 | int id2_arg, int owner_id2_arg, |
| 1382 | int expected_id_arg, int expected_owner_id_arg, |
| 1383 | int expected_lifetime_arg ) |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1384 | { |
| 1385 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1386 | mbedtls_svc_key_id_t id1 = |
| 1387 | mbedtls_svc_key_id_make( owner_id1_arg, id1_arg ); |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1388 | psa_key_lifetime_t lifetime = lifetime_arg; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1389 | mbedtls_svc_key_id_t id2 = |
| 1390 | mbedtls_svc_key_id_make( owner_id2_arg, id2_arg ); |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 1391 | mbedtls_svc_key_id_t expected_id = |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1392 | mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg ); |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1393 | psa_key_lifetime_t expected_lifetime = expected_lifetime_arg; |
| 1394 | |
| 1395 | if( id1_arg != -1 ) |
| 1396 | psa_set_key_id( &attributes, id1 ); |
| 1397 | if( lifetime_arg != -1 ) |
| 1398 | psa_set_key_lifetime( &attributes, lifetime ); |
| 1399 | if( id2_arg != -1 ) |
| 1400 | psa_set_key_id( &attributes, id2 ); |
| 1401 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1402 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 1403 | psa_get_key_id( &attributes ), expected_id ) ); |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1404 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime ); |
| 1405 | } |
| 1406 | /* END_CASE */ |
| 1407 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1408 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1409 | void slot_number_attribute( ) |
| 1410 | { |
| 1411 | psa_key_slot_number_t slot_number = 0xdeadbeef; |
| 1412 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1413 | |
| 1414 | /* Initially, there is no slot number. */ |
| 1415 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1416 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1417 | |
| 1418 | /* Test setting a slot number. */ |
| 1419 | psa_set_key_slot_number( &attributes, 0 ); |
| 1420 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1421 | TEST_EQUAL( slot_number, 0 ); |
| 1422 | |
| 1423 | /* Test changing the slot number. */ |
| 1424 | psa_set_key_slot_number( &attributes, 42 ); |
| 1425 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1426 | TEST_EQUAL( slot_number, 42 ); |
| 1427 | |
| 1428 | /* Test clearing the slot number. */ |
| 1429 | psa_clear_key_slot_number( &attributes ); |
| 1430 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1431 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1432 | |
| 1433 | /* Clearing again should have no effect. */ |
| 1434 | psa_clear_key_slot_number( &attributes ); |
| 1435 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1436 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1437 | |
| 1438 | /* Test that reset clears the slot number. */ |
| 1439 | psa_set_key_slot_number( &attributes, 42 ); |
| 1440 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1441 | TEST_EQUAL( slot_number, 42 ); |
| 1442 | psa_reset_key_attributes( &attributes ); |
| 1443 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1444 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1445 | } |
| 1446 | /* END_CASE */ |
| 1447 | |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1448 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1449 | void import_with_policy( int type_arg, |
| 1450 | int usage_arg, int alg_arg, |
| 1451 | int expected_status_arg ) |
| 1452 | { |
| 1453 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1454 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1455 | psa_key_handle_t handle = 0; |
| 1456 | psa_key_type_t type = type_arg; |
| 1457 | psa_key_usage_t usage = usage_arg; |
| 1458 | psa_algorithm_t alg = alg_arg; |
| 1459 | psa_status_t expected_status = expected_status_arg; |
| 1460 | const uint8_t key_material[16] = {0}; |
| 1461 | psa_status_t status; |
| 1462 | |
| 1463 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1464 | |
| 1465 | psa_set_key_type( &attributes, type ); |
| 1466 | psa_set_key_usage_flags( &attributes, usage ); |
| 1467 | psa_set_key_algorithm( &attributes, alg ); |
| 1468 | |
| 1469 | status = psa_import_key( &attributes, |
| 1470 | key_material, sizeof( key_material ), |
| 1471 | &handle ); |
| 1472 | TEST_EQUAL( status, expected_status ); |
| 1473 | if( status != PSA_SUCCESS ) |
| 1474 | goto exit; |
| 1475 | |
| 1476 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1477 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1478 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1479 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1480 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1481 | |
| 1482 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1483 | test_operations_on_invalid_handle( handle ); |
| 1484 | |
| 1485 | exit: |
| 1486 | psa_destroy_key( handle ); |
| 1487 | psa_reset_key_attributes( &got_attributes ); |
| 1488 | PSA_DONE( ); |
| 1489 | } |
| 1490 | /* END_CASE */ |
| 1491 | |
| 1492 | /* BEGIN_CASE */ |
| 1493 | void import_with_data( data_t *data, int type_arg, |
| 1494 | int attr_bits_arg, |
| 1495 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1496 | { |
| 1497 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1498 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1499 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1500 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1501 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1502 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1503 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1504 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1505 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1506 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1507 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1508 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1509 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1510 | status = psa_import_key( &attributes, data->x, data->len, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1511 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1512 | if( status != PSA_SUCCESS ) |
| 1513 | goto exit; |
| 1514 | |
| 1515 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1516 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1517 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1518 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1519 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1520 | |
| 1521 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1522 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1523 | |
| 1524 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1525 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1526 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1527 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1528 | } |
| 1529 | /* END_CASE */ |
| 1530 | |
| 1531 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1532 | void import_large_key( int type_arg, int byte_size_arg, |
| 1533 | int expected_status_arg ) |
| 1534 | { |
| 1535 | psa_key_type_t type = type_arg; |
| 1536 | size_t byte_size = byte_size_arg; |
| 1537 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1538 | psa_status_t expected_status = expected_status_arg; |
| 1539 | psa_key_handle_t handle = 0; |
| 1540 | psa_status_t status; |
| 1541 | uint8_t *buffer = NULL; |
| 1542 | size_t buffer_size = byte_size + 1; |
| 1543 | size_t n; |
| 1544 | |
| 1545 | /* It would be better to skip the test than fail it if the allocation |
| 1546 | * fails, but the test framework doesn't support this yet. */ |
| 1547 | ASSERT_ALLOC( buffer, buffer_size ); |
| 1548 | memset( buffer, 'K', byte_size ); |
| 1549 | |
| 1550 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1551 | |
| 1552 | /* Try importing the key */ |
| 1553 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1554 | psa_set_key_type( &attributes, type ); |
| 1555 | status = psa_import_key( &attributes, buffer, byte_size, &handle ); |
| 1556 | TEST_EQUAL( status, expected_status ); |
| 1557 | |
| 1558 | if( status == PSA_SUCCESS ) |
| 1559 | { |
| 1560 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1561 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1562 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1563 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1564 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1565 | memset( buffer, 0, byte_size + 1 ); |
| 1566 | PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) ); |
| 1567 | for( n = 0; n < byte_size; n++ ) |
| 1568 | TEST_EQUAL( buffer[n], 'K' ); |
| 1569 | for( n = byte_size; n < buffer_size; n++ ) |
| 1570 | TEST_EQUAL( buffer[n], 0 ); |
| 1571 | } |
| 1572 | |
| 1573 | exit: |
| 1574 | psa_destroy_key( handle ); |
| 1575 | PSA_DONE( ); |
| 1576 | mbedtls_free( buffer ); |
| 1577 | } |
| 1578 | /* END_CASE */ |
| 1579 | |
| 1580 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1581 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1582 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1583 | psa_key_handle_t handle = 0; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1584 | size_t bits = bits_arg; |
| 1585 | psa_status_t expected_status = expected_status_arg; |
| 1586 | psa_status_t status; |
| 1587 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1588 | 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] | 1589 | size_t buffer_size = /* Slight overapproximations */ |
| 1590 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1591 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1592 | unsigned char *p; |
| 1593 | int ret; |
| 1594 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1595 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1596 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1597 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1598 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1599 | |
| 1600 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1601 | bits, keypair ) ) >= 0 ); |
| 1602 | length = ret; |
| 1603 | |
| 1604 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1605 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1606 | status = psa_import_key( &attributes, p, length, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1607 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1608 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1609 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1610 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1611 | |
| 1612 | exit: |
| 1613 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1614 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1615 | } |
| 1616 | /* END_CASE */ |
| 1617 | |
| 1618 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1619 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1620 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1621 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1622 | int expected_bits, |
| 1623 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1624 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1625 | int canonical_input ) |
| 1626 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1627 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1628 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1629 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1630 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1631 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1632 | unsigned char *exported = NULL; |
| 1633 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1634 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1635 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1636 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1637 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1638 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1639 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1640 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1641 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1642 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1643 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1644 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1645 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1646 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1647 | psa_set_key_algorithm( &attributes, alg ); |
| 1648 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1649 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1650 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1651 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1652 | |
| 1653 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1654 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1655 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1656 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1657 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1658 | |
| 1659 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1660 | status = psa_export_key( handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1661 | exported, export_size, |
| 1662 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1663 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1664 | |
| 1665 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1666 | * and export_size. On errors, the exported length must be 0. */ |
| 1667 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1668 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1669 | TEST_ASSERT( exported_length <= export_size ); |
| 1670 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1671 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1672 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1673 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1674 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1675 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1676 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1677 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1678 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1679 | if( ! exercise_export_key( handle, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1680 | goto exit; |
| 1681 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1682 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1683 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1684 | else |
| 1685 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1686 | psa_key_handle_t handle2; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1687 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
| 1688 | &handle2 ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1689 | PSA_ASSERT( psa_export_key( handle2, |
| 1690 | reexported, |
| 1691 | export_size, |
| 1692 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1693 | ASSERT_COMPARE( exported, exported_length, |
| 1694 | reexported, reexported_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1695 | PSA_ASSERT( psa_close_key( handle2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1696 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1697 | 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] | 1698 | |
| 1699 | destroy: |
| 1700 | /* Destroy the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1701 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1702 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1703 | |
| 1704 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1705 | mbedtls_free( exported ); |
| 1706 | mbedtls_free( reexported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1707 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1708 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1709 | } |
| 1710 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1711 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1712 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1713 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1714 | int type_arg, |
| 1715 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1716 | int export_size_delta, |
| 1717 | int expected_export_status_arg, |
| 1718 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1719 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1720 | psa_key_handle_t handle = 0; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1721 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1722 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1723 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1724 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1725 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1726 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1727 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1728 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1729 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1730 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1731 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1732 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1733 | psa_set_key_algorithm( &attributes, alg ); |
| 1734 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1735 | |
| 1736 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1737 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1738 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1739 | /* Export the public key */ |
| 1740 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1741 | status = psa_export_public_key( handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1742 | exported, export_size, |
| 1743 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1744 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1745 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1746 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1747 | 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] | 1748 | size_t bits; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1749 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1750 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1751 | TEST_ASSERT( expected_public_key->len <= |
| 1752 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1753 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1754 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1755 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1756 | |
| 1757 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1758 | mbedtls_free( exported ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1759 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1760 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1761 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1762 | } |
| 1763 | /* END_CASE */ |
| 1764 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1765 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1766 | void import_and_exercise_key( data_t *data, |
| 1767 | int type_arg, |
| 1768 | int bits_arg, |
| 1769 | int alg_arg ) |
| 1770 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1771 | psa_key_handle_t handle = 0; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1772 | psa_key_type_t type = type_arg; |
| 1773 | size_t bits = bits_arg; |
| 1774 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1775 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1776 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1777 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1778 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1779 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1780 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1781 | psa_set_key_usage_flags( &attributes, usage ); |
| 1782 | psa_set_key_algorithm( &attributes, alg ); |
| 1783 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1784 | |
| 1785 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1786 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1787 | |
| 1788 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1789 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1790 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1791 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1792 | |
| 1793 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1794 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1795 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1796 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1797 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1798 | test_operations_on_invalid_handle( handle ); |
| 1799 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1800 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1801 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1802 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1803 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1804 | } |
| 1805 | /* END_CASE */ |
| 1806 | |
| 1807 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1808 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1809 | int bits_arg, int expected_bits_arg, |
| 1810 | int usage_arg, int expected_usage_arg, |
| 1811 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1812 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1813 | psa_key_handle_t handle = 0; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1814 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1815 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1816 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1817 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1818 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1819 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1820 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1821 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1822 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1823 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1824 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1825 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1826 | psa_set_key_usage_flags( &attributes, usage ); |
| 1827 | psa_set_key_algorithm( &attributes, alg ); |
| 1828 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1829 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1830 | |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1831 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
| 1832 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1833 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1834 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1835 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1836 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1837 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1838 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1839 | |
| 1840 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1841 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1842 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1843 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1844 | } |
| 1845 | /* END_CASE */ |
| 1846 | |
| 1847 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1848 | void check_key_policy( int type_arg, int bits_arg, |
| 1849 | int usage_arg, int alg_arg ) |
| 1850 | { |
| 1851 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
| 1852 | usage_arg, usage_arg, alg_arg, alg_arg ); |
| 1853 | goto exit; |
| 1854 | } |
| 1855 | /* END_CASE */ |
| 1856 | |
| 1857 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1858 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1859 | { |
| 1860 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1861 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1862 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1863 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1864 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1865 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1866 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1867 | |
| 1868 | memset( &zero, 0, sizeof( zero ) ); |
| 1869 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1870 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1871 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1872 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1873 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1874 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1875 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1876 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1877 | |
| 1878 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1879 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1880 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1881 | |
| 1882 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1883 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1884 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1885 | |
| 1886 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1887 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1888 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1889 | } |
| 1890 | /* END_CASE */ |
| 1891 | |
| 1892 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1893 | void mac_key_policy( int policy_usage, |
| 1894 | int policy_alg, |
| 1895 | int key_type, |
| 1896 | data_t *key_data, |
| 1897 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1898 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1899 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1900 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1901 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1902 | psa_status_t status; |
| 1903 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1904 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1905 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1906 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1907 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1908 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1909 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1910 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1911 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1912 | &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1913 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1914 | status = psa_mac_sign_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1915 | if( policy_alg == exercise_alg && |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1916 | ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1917 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1918 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1919 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1920 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1921 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1922 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1923 | status = psa_mac_verify_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1924 | if( policy_alg == exercise_alg && |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1925 | ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1926 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1927 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1928 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1929 | |
| 1930 | exit: |
| 1931 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1932 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1933 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1934 | } |
| 1935 | /* END_CASE */ |
| 1936 | |
| 1937 | /* BEGIN_CASE */ |
| 1938 | void cipher_key_policy( int policy_usage, |
| 1939 | int policy_alg, |
| 1940 | int key_type, |
| 1941 | data_t *key_data, |
| 1942 | int exercise_alg ) |
| 1943 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1944 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1945 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1946 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1947 | psa_status_t status; |
| 1948 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1949 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1950 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1951 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1952 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1953 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1954 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1955 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1956 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1957 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1958 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1959 | if( policy_alg == exercise_alg && |
| 1960 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1961 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1962 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1963 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1964 | psa_cipher_abort( &operation ); |
| 1965 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1966 | status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1967 | if( policy_alg == exercise_alg && |
| 1968 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1969 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1970 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1971 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1972 | |
| 1973 | exit: |
| 1974 | psa_cipher_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1975 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1976 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1977 | } |
| 1978 | /* END_CASE */ |
| 1979 | |
| 1980 | /* BEGIN_CASE */ |
| 1981 | void aead_key_policy( int policy_usage, |
| 1982 | int policy_alg, |
| 1983 | int key_type, |
| 1984 | data_t *key_data, |
| 1985 | int nonce_length_arg, |
| 1986 | int tag_length_arg, |
| 1987 | int exercise_alg ) |
| 1988 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1989 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1990 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1991 | psa_status_t status; |
| 1992 | unsigned char nonce[16] = {0}; |
| 1993 | size_t nonce_length = nonce_length_arg; |
| 1994 | unsigned char tag[16]; |
| 1995 | size_t tag_length = tag_length_arg; |
| 1996 | size_t output_length; |
| 1997 | |
| 1998 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1999 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 2000 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2001 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2002 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2003 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2004 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2005 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2006 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2007 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2008 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2009 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2010 | status = psa_aead_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2011 | nonce, nonce_length, |
| 2012 | NULL, 0, |
| 2013 | NULL, 0, |
| 2014 | tag, tag_length, |
| 2015 | &output_length ); |
| 2016 | if( policy_alg == exercise_alg && |
| 2017 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2018 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2019 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2020 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2021 | |
| 2022 | memset( tag, 0, sizeof( tag ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2023 | status = psa_aead_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2024 | nonce, nonce_length, |
| 2025 | NULL, 0, |
| 2026 | tag, tag_length, |
| 2027 | NULL, 0, |
| 2028 | &output_length ); |
| 2029 | if( policy_alg == exercise_alg && |
| 2030 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2031 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2032 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2033 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2034 | |
| 2035 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2036 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2037 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2038 | } |
| 2039 | /* END_CASE */ |
| 2040 | |
| 2041 | /* BEGIN_CASE */ |
| 2042 | void asymmetric_encryption_key_policy( int policy_usage, |
| 2043 | int policy_alg, |
| 2044 | int key_type, |
| 2045 | data_t *key_data, |
| 2046 | int exercise_alg ) |
| 2047 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2048 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2049 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2050 | psa_status_t status; |
| 2051 | size_t key_bits; |
| 2052 | size_t buffer_length; |
| 2053 | unsigned char *buffer = NULL; |
| 2054 | size_t output_length; |
| 2055 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2056 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2057 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2058 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2059 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2060 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2061 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2062 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2063 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2064 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2065 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 2066 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2067 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2068 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2069 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2070 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2071 | status = psa_asymmetric_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2072 | NULL, 0, |
| 2073 | NULL, 0, |
| 2074 | buffer, buffer_length, |
| 2075 | &output_length ); |
| 2076 | if( policy_alg == exercise_alg && |
| 2077 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2078 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2079 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2080 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2081 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2082 | if( buffer_length != 0 ) |
| 2083 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2084 | status = psa_asymmetric_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2085 | buffer, buffer_length, |
| 2086 | NULL, 0, |
| 2087 | buffer, buffer_length, |
| 2088 | &output_length ); |
| 2089 | if( policy_alg == exercise_alg && |
| 2090 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2091 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2092 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2093 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2094 | |
| 2095 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2096 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2097 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2098 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2099 | mbedtls_free( buffer ); |
| 2100 | } |
| 2101 | /* END_CASE */ |
| 2102 | |
| 2103 | /* BEGIN_CASE */ |
| 2104 | void asymmetric_signature_key_policy( int policy_usage, |
| 2105 | int policy_alg, |
| 2106 | int key_type, |
| 2107 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2108 | int exercise_alg, |
| 2109 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2110 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2111 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2112 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2113 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2114 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2115 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2116 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2117 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2118 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2119 | int compatible_alg = payload_length_arg > 0; |
| 2120 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2121 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2122 | size_t signature_length; |
| 2123 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2124 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2125 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2126 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2127 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2128 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2129 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2130 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2131 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2132 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2133 | status = psa_sign_hash( handle, exercise_alg, |
| 2134 | payload, payload_length, |
| 2135 | signature, sizeof( signature ), |
| 2136 | &signature_length ); |
| 2137 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2138 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2139 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2140 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2141 | |
| 2142 | memset( signature, 0, sizeof( signature ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2143 | status = psa_verify_hash( handle, exercise_alg, |
| 2144 | payload, payload_length, |
| 2145 | signature, sizeof( signature ) ); |
| 2146 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2147 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2148 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2149 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2150 | |
| 2151 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2152 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2153 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2154 | } |
| 2155 | /* END_CASE */ |
| 2156 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2157 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2158 | void derive_key_policy( int policy_usage, |
| 2159 | int policy_alg, |
| 2160 | int key_type, |
| 2161 | data_t *key_data, |
| 2162 | int exercise_alg ) |
| 2163 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2164 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2165 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2166 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2167 | psa_status_t status; |
| 2168 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2169 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2170 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2171 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2172 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2173 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2174 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2175 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2176 | &handle ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2177 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2178 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2179 | |
| 2180 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2181 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2182 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2183 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2184 | &operation, |
| 2185 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2186 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2187 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2188 | |
| 2189 | status = psa_key_derivation_input_key( &operation, |
| 2190 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2191 | handle ); |
| 2192 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2193 | if( policy_alg == exercise_alg && |
| 2194 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2195 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2196 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2197 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2198 | |
| 2199 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2200 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2201 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2202 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2203 | } |
| 2204 | /* END_CASE */ |
| 2205 | |
| 2206 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2207 | void agreement_key_policy( int policy_usage, |
| 2208 | int policy_alg, |
| 2209 | int key_type_arg, |
| 2210 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2211 | int exercise_alg, |
| 2212 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2213 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2214 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2215 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2216 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2217 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2218 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2219 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2220 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2221 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2222 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2223 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2224 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2225 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2226 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2227 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2228 | &handle ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2229 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2230 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2231 | status = key_agreement_with_self( &operation, handle ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2232 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2233 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2234 | |
| 2235 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2236 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2237 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2238 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2239 | } |
| 2240 | /* END_CASE */ |
| 2241 | |
| 2242 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2243 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2244 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2245 | { |
| 2246 | psa_key_handle_t handle = 0; |
| 2247 | psa_key_type_t key_type = key_type_arg; |
| 2248 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2249 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2250 | psa_key_usage_t usage = usage_arg; |
| 2251 | psa_algorithm_t alg = alg_arg; |
| 2252 | psa_algorithm_t alg2 = alg2_arg; |
| 2253 | |
| 2254 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2255 | |
| 2256 | psa_set_key_usage_flags( &attributes, usage ); |
| 2257 | psa_set_key_algorithm( &attributes, alg ); |
| 2258 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2259 | psa_set_key_type( &attributes, key_type ); |
| 2260 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2261 | &handle ) ); |
| 2262 | |
| 2263 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 2264 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2265 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2266 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2267 | |
| 2268 | if( ! exercise_key( handle, usage, alg ) ) |
| 2269 | goto exit; |
| 2270 | if( ! exercise_key( handle, usage, alg2 ) ) |
| 2271 | goto exit; |
| 2272 | |
| 2273 | exit: |
| 2274 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2275 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2276 | } |
| 2277 | /* END_CASE */ |
| 2278 | |
| 2279 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2280 | void raw_agreement_key_policy( int policy_usage, |
| 2281 | int policy_alg, |
| 2282 | int key_type_arg, |
| 2283 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2284 | int exercise_alg, |
| 2285 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2286 | { |
| 2287 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2288 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2289 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2290 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2291 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2292 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2293 | |
| 2294 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2295 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2296 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2297 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2298 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2299 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2300 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2301 | &handle ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2302 | |
| 2303 | status = raw_key_agreement_with_self( exercise_alg, handle ); |
| 2304 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2305 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2306 | |
| 2307 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2308 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2309 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2310 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2311 | } |
| 2312 | /* END_CASE */ |
| 2313 | |
| 2314 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2315 | void copy_success( int source_usage_arg, |
| 2316 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2317 | int type_arg, data_t *material, |
| 2318 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2319 | int target_usage_arg, |
| 2320 | int target_alg_arg, int target_alg2_arg, |
| 2321 | int expected_usage_arg, |
| 2322 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2323 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2324 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2325 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2326 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2327 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2328 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2329 | psa_key_handle_t source_handle = 0; |
| 2330 | psa_key_handle_t target_handle = 0; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2331 | uint8_t *export_buffer = NULL; |
| 2332 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2333 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2334 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2335 | /* Prepare the source key. */ |
| 2336 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2337 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2338 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2339 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2340 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2341 | material->x, material->len, |
| 2342 | &source_handle ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2343 | PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2344 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2345 | /* Prepare the target attributes. */ |
| 2346 | if( copy_attributes ) |
| 2347 | target_attributes = source_attributes; |
| 2348 | if( target_usage_arg != -1 ) |
| 2349 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2350 | if( target_alg_arg != -1 ) |
| 2351 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2352 | if( target_alg2_arg != -1 ) |
| 2353 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2354 | |
| 2355 | /* Copy the key. */ |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2356 | PSA_ASSERT( psa_copy_key( source_handle, |
| 2357 | &target_attributes, &target_handle ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2358 | |
| 2359 | /* Destroy the source to ensure that this doesn't affect the target. */ |
| 2360 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2361 | |
| 2362 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2363 | PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) ); |
| 2364 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2365 | psa_get_key_type( &target_attributes ) ); |
| 2366 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2367 | psa_get_key_bits( &target_attributes ) ); |
| 2368 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2369 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2370 | TEST_EQUAL( expected_alg2, |
| 2371 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2372 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2373 | { |
| 2374 | size_t length; |
| 2375 | ASSERT_ALLOC( export_buffer, material->len ); |
| 2376 | PSA_ASSERT( psa_export_key( target_handle, export_buffer, |
| 2377 | material->len, &length ) ); |
| 2378 | ASSERT_COMPARE( material->x, material->len, |
| 2379 | export_buffer, length ); |
| 2380 | } |
| 2381 | if( ! exercise_key( target_handle, expected_usage, expected_alg ) ) |
| 2382 | goto exit; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2383 | if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) ) |
| 2384 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2385 | |
| 2386 | PSA_ASSERT( psa_close_key( target_handle ) ); |
| 2387 | |
| 2388 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2389 | psa_reset_key_attributes( &source_attributes ); |
| 2390 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2391 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2392 | mbedtls_free( export_buffer ); |
| 2393 | } |
| 2394 | /* END_CASE */ |
| 2395 | |
| 2396 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2397 | void copy_fail( int source_usage_arg, |
| 2398 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2399 | int type_arg, data_t *material, |
| 2400 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2401 | int target_usage_arg, |
| 2402 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2403 | int expected_status_arg ) |
| 2404 | { |
| 2405 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2406 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2407 | psa_key_handle_t source_handle = 0; |
| 2408 | psa_key_handle_t target_handle = 0; |
| 2409 | |
| 2410 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2411 | |
| 2412 | /* Prepare the source key. */ |
| 2413 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2414 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2415 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2416 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2417 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2418 | material->x, material->len, |
| 2419 | &source_handle ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2420 | |
| 2421 | /* Prepare the target attributes. */ |
| 2422 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2423 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2424 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2425 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2426 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2427 | |
| 2428 | /* Try to copy the key. */ |
| 2429 | TEST_EQUAL( psa_copy_key( source_handle, |
| 2430 | &target_attributes, &target_handle ), |
| 2431 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2432 | |
| 2433 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2434 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2435 | exit: |
| 2436 | psa_reset_key_attributes( &source_attributes ); |
| 2437 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2438 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2439 | } |
| 2440 | /* END_CASE */ |
| 2441 | |
| 2442 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2443 | void hash_operation_init( ) |
| 2444 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2445 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2446 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2447 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2448 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2449 | * to supress the Clang warning for the test. */ |
| 2450 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2451 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2452 | psa_hash_operation_t zero; |
| 2453 | |
| 2454 | memset( &zero, 0, sizeof( zero ) ); |
| 2455 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2456 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2457 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2458 | PSA_ERROR_BAD_STATE ); |
| 2459 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2460 | PSA_ERROR_BAD_STATE ); |
| 2461 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2462 | PSA_ERROR_BAD_STATE ); |
| 2463 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2464 | /* A default hash operation should be abortable without error. */ |
| 2465 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2466 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2467 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2468 | } |
| 2469 | /* END_CASE */ |
| 2470 | |
| 2471 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2472 | void hash_setup( int alg_arg, |
| 2473 | int expected_status_arg ) |
| 2474 | { |
| 2475 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2476 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2477 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2478 | psa_status_t status; |
| 2479 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2480 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2481 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2482 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2483 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2484 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2485 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2486 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2487 | |
| 2488 | /* If setup failed, reproduce the failure, so as to |
| 2489 | * test the resulting state of the operation object. */ |
| 2490 | if( status != PSA_SUCCESS ) |
| 2491 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2492 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2493 | /* Now the operation object should be reusable. */ |
| 2494 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2495 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2496 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2497 | #endif |
| 2498 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2499 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2500 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2501 | } |
| 2502 | /* END_CASE */ |
| 2503 | |
| 2504 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2505 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2506 | int output_size_arg, int expected_status_arg ) |
| 2507 | { |
| 2508 | psa_algorithm_t alg = alg_arg; |
| 2509 | uint8_t *output = NULL; |
| 2510 | size_t output_size = output_size_arg; |
| 2511 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 2512 | psa_status_t expected_status = expected_status_arg; |
| 2513 | psa_status_t status; |
| 2514 | |
| 2515 | ASSERT_ALLOC( output, output_size ); |
| 2516 | |
| 2517 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2518 | |
| 2519 | status = psa_hash_compute( alg, input->x, input->len, |
| 2520 | output, output_size, &output_length ); |
| 2521 | TEST_EQUAL( status, expected_status ); |
| 2522 | TEST_ASSERT( output_length <= output_size ); |
| 2523 | |
| 2524 | exit: |
| 2525 | mbedtls_free( output ); |
| 2526 | PSA_DONE( ); |
| 2527 | } |
| 2528 | /* END_CASE */ |
| 2529 | |
| 2530 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2531 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2532 | data_t *reference_hash, |
| 2533 | int expected_status_arg ) |
| 2534 | { |
| 2535 | psa_algorithm_t alg = alg_arg; |
| 2536 | psa_status_t expected_status = expected_status_arg; |
| 2537 | psa_status_t status; |
| 2538 | |
| 2539 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2540 | |
| 2541 | status = psa_hash_compare( alg, input->x, input->len, |
| 2542 | reference_hash->x, reference_hash->len ); |
| 2543 | TEST_EQUAL( status, expected_status ); |
| 2544 | |
| 2545 | exit: |
| 2546 | PSA_DONE( ); |
| 2547 | } |
| 2548 | /* END_CASE */ |
| 2549 | |
| 2550 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2551 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2552 | data_t *expected_output ) |
| 2553 | { |
| 2554 | psa_algorithm_t alg = alg_arg; |
| 2555 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2556 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 2557 | size_t i; |
| 2558 | |
| 2559 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2560 | |
| 2561 | /* Compute with tight buffer */ |
| 2562 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2563 | output, PSA_HASH_SIZE( alg ), |
| 2564 | &output_length ) ); |
| 2565 | TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) ); |
| 2566 | ASSERT_COMPARE( output, output_length, |
| 2567 | expected_output->x, expected_output->len ); |
| 2568 | |
| 2569 | /* Compute with larger buffer */ |
| 2570 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2571 | output, sizeof( output ), |
| 2572 | &output_length ) ); |
| 2573 | TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) ); |
| 2574 | ASSERT_COMPARE( output, output_length, |
| 2575 | expected_output->x, expected_output->len ); |
| 2576 | |
| 2577 | /* Compare with correct hash */ |
| 2578 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2579 | output, output_length ) ); |
| 2580 | |
| 2581 | /* Compare with trailing garbage */ |
| 2582 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2583 | output, output_length + 1 ), |
| 2584 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2585 | |
| 2586 | /* Compare with truncated hash */ |
| 2587 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2588 | output, output_length - 1 ), |
| 2589 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2590 | |
| 2591 | /* Compare with corrupted value */ |
| 2592 | for( i = 0; i < output_length; i++ ) |
| 2593 | { |
| 2594 | test_set_step( i ); |
| 2595 | output[i] ^= 1; |
| 2596 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2597 | output, output_length ), |
| 2598 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2599 | output[i] ^= 1; |
| 2600 | } |
| 2601 | |
| 2602 | exit: |
| 2603 | PSA_DONE( ); |
| 2604 | } |
| 2605 | /* END_CASE */ |
| 2606 | |
| 2607 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2608 | void hash_bad_order( ) |
| 2609 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2610 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2611 | unsigned char input[] = ""; |
| 2612 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2613 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2614 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2615 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2616 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2617 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2618 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2619 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2620 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2621 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2622 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2623 | /* Call setup twice in a row. */ |
| 2624 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2625 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2626 | PSA_ERROR_BAD_STATE ); |
| 2627 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2628 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2629 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2630 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2631 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2632 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2633 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2634 | /* Call update after finish. */ |
| 2635 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2636 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2637 | hash, sizeof( hash ), &hash_len ) ); |
| 2638 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2639 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2640 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2641 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2642 | /* Call verify without calling setup beforehand. */ |
| 2643 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2644 | valid_hash, sizeof( valid_hash ) ), |
| 2645 | PSA_ERROR_BAD_STATE ); |
| 2646 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2647 | |
| 2648 | /* Call verify after finish. */ |
| 2649 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2650 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2651 | hash, sizeof( hash ), &hash_len ) ); |
| 2652 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2653 | valid_hash, sizeof( valid_hash ) ), |
| 2654 | PSA_ERROR_BAD_STATE ); |
| 2655 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2656 | |
| 2657 | /* Call verify twice in a row. */ |
| 2658 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2659 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2660 | valid_hash, sizeof( valid_hash ) ) ); |
| 2661 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2662 | valid_hash, sizeof( valid_hash ) ), |
| 2663 | PSA_ERROR_BAD_STATE ); |
| 2664 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2665 | |
| 2666 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2667 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2668 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2669 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2670 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2671 | |
| 2672 | /* Call finish twice in a row. */ |
| 2673 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2674 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2675 | hash, sizeof( hash ), &hash_len ) ); |
| 2676 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2677 | hash, sizeof( hash ), &hash_len ), |
| 2678 | PSA_ERROR_BAD_STATE ); |
| 2679 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2680 | |
| 2681 | /* Call finish after calling verify. */ |
| 2682 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2683 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2684 | valid_hash, sizeof( valid_hash ) ) ); |
| 2685 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2686 | hash, sizeof( hash ), &hash_len ), |
| 2687 | PSA_ERROR_BAD_STATE ); |
| 2688 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2689 | |
| 2690 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2691 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2692 | } |
| 2693 | /* END_CASE */ |
| 2694 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2695 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2696 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2697 | { |
| 2698 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2699 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2700 | * appended to it */ |
| 2701 | unsigned char hash[] = { |
| 2702 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2703 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2704 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2705 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2706 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2707 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2708 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2709 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2710 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2711 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2712 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2713 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2714 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2715 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2716 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2717 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2718 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2719 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2720 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2721 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2722 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2723 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2724 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2725 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2726 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2727 | } |
| 2728 | /* END_CASE */ |
| 2729 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2730 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2731 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2732 | { |
| 2733 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2734 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2735 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2736 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2737 | size_t hash_len; |
| 2738 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2739 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2740 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2741 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2742 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2743 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2744 | hash, expected_size - 1, &hash_len ), |
| 2745 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2746 | |
| 2747 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2748 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2749 | } |
| 2750 | /* END_CASE */ |
| 2751 | |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2752 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2753 | void hash_clone_source_state( ) |
| 2754 | { |
| 2755 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2756 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2757 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2758 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2759 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2760 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2761 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2762 | size_t hash_len; |
| 2763 | |
| 2764 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2765 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2766 | |
| 2767 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2768 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2769 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2770 | hash, sizeof( hash ), &hash_len ) ); |
| 2771 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2772 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2773 | |
| 2774 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2775 | PSA_ERROR_BAD_STATE ); |
| 2776 | |
| 2777 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2778 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2779 | hash, sizeof( hash ), &hash_len ) ); |
| 2780 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2781 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2782 | hash, sizeof( hash ), &hash_len ) ); |
| 2783 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2784 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2785 | hash, sizeof( hash ), &hash_len ) ); |
| 2786 | |
| 2787 | exit: |
| 2788 | psa_hash_abort( &op_source ); |
| 2789 | psa_hash_abort( &op_init ); |
| 2790 | psa_hash_abort( &op_setup ); |
| 2791 | psa_hash_abort( &op_finished ); |
| 2792 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2793 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2794 | } |
| 2795 | /* END_CASE */ |
| 2796 | |
| 2797 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2798 | void hash_clone_target_state( ) |
| 2799 | { |
| 2800 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2801 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2802 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2803 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2804 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2805 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2806 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2807 | size_t hash_len; |
| 2808 | |
| 2809 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2810 | |
| 2811 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2812 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2813 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2814 | hash, sizeof( hash ), &hash_len ) ); |
| 2815 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2816 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2817 | |
| 2818 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2819 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2820 | hash, sizeof( hash ), &hash_len ) ); |
| 2821 | |
| 2822 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2823 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2824 | PSA_ERROR_BAD_STATE ); |
| 2825 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2826 | PSA_ERROR_BAD_STATE ); |
| 2827 | |
| 2828 | exit: |
| 2829 | psa_hash_abort( &op_target ); |
| 2830 | psa_hash_abort( &op_init ); |
| 2831 | psa_hash_abort( &op_setup ); |
| 2832 | psa_hash_abort( &op_finished ); |
| 2833 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2834 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2835 | } |
| 2836 | /* END_CASE */ |
| 2837 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2838 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2839 | void mac_operation_init( ) |
| 2840 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2841 | const uint8_t input[1] = { 0 }; |
| 2842 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2843 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2844 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2845 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2846 | * to supress the Clang warning for the test. */ |
| 2847 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2848 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2849 | psa_mac_operation_t zero; |
| 2850 | |
| 2851 | memset( &zero, 0, sizeof( zero ) ); |
| 2852 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2853 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2854 | TEST_EQUAL( psa_mac_update( &func, |
| 2855 | input, sizeof( input ) ), |
| 2856 | PSA_ERROR_BAD_STATE ); |
| 2857 | TEST_EQUAL( psa_mac_update( &init, |
| 2858 | input, sizeof( input ) ), |
| 2859 | PSA_ERROR_BAD_STATE ); |
| 2860 | TEST_EQUAL( psa_mac_update( &zero, |
| 2861 | input, sizeof( input ) ), |
| 2862 | PSA_ERROR_BAD_STATE ); |
| 2863 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2864 | /* A default MAC operation should be abortable without error. */ |
| 2865 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2866 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2867 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2868 | } |
| 2869 | /* END_CASE */ |
| 2870 | |
| 2871 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2872 | void mac_setup( int key_type_arg, |
| 2873 | data_t *key, |
| 2874 | int alg_arg, |
| 2875 | int expected_status_arg ) |
| 2876 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2877 | psa_key_type_t key_type = key_type_arg; |
| 2878 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2879 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2880 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2881 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2882 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2883 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2884 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2885 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2886 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2887 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2888 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2889 | &operation, &status ) ) |
| 2890 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2891 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2892 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2893 | /* The operation object should be reusable. */ |
| 2894 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2895 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2896 | smoke_test_key_data, |
| 2897 | sizeof( smoke_test_key_data ), |
| 2898 | KNOWN_SUPPORTED_MAC_ALG, |
| 2899 | &operation, &status ) ) |
| 2900 | goto exit; |
| 2901 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2902 | #endif |
| 2903 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2904 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2905 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2906 | } |
| 2907 | /* END_CASE */ |
| 2908 | |
| 2909 | /* BEGIN_CASE */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2910 | void mac_bad_order( ) |
| 2911 | { |
| 2912 | psa_key_handle_t handle = 0; |
| 2913 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2914 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
| 2915 | const uint8_t key[] = { |
| 2916 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2917 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2918 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2919 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2920 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2921 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2922 | size_t sign_mac_length = 0; |
| 2923 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2924 | const uint8_t verify_mac[] = { |
| 2925 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2926 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2927 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2928 | |
| 2929 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2930 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2931 | psa_set_key_algorithm( &attributes, alg ); |
| 2932 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2933 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2934 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2935 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2936 | /* Call update without calling setup beforehand. */ |
| 2937 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2938 | PSA_ERROR_BAD_STATE ); |
| 2939 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2940 | |
| 2941 | /* Call sign finish without calling setup beforehand. */ |
| 2942 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2943 | &sign_mac_length), |
| 2944 | PSA_ERROR_BAD_STATE ); |
| 2945 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2946 | |
| 2947 | /* Call verify finish without calling setup beforehand. */ |
| 2948 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2949 | verify_mac, sizeof( verify_mac ) ), |
| 2950 | PSA_ERROR_BAD_STATE ); |
| 2951 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2952 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2953 | /* Call setup twice in a row. */ |
| 2954 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2955 | handle, alg ) ); |
| 2956 | TEST_EQUAL( psa_mac_sign_setup( &operation, |
| 2957 | handle, alg ), |
| 2958 | PSA_ERROR_BAD_STATE ); |
| 2959 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2960 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2961 | /* Call update after sign finish. */ |
| 2962 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2963 | handle, alg ) ); |
| 2964 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2965 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2966 | sign_mac, sizeof( sign_mac ), |
| 2967 | &sign_mac_length ) ); |
| 2968 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2969 | PSA_ERROR_BAD_STATE ); |
| 2970 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2971 | |
| 2972 | /* Call update after verify finish. */ |
| 2973 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2974 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2975 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2976 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2977 | verify_mac, sizeof( verify_mac ) ) ); |
| 2978 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2979 | PSA_ERROR_BAD_STATE ); |
| 2980 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2981 | |
| 2982 | /* Call sign finish twice in a row. */ |
| 2983 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2984 | handle, alg ) ); |
| 2985 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2986 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2987 | sign_mac, sizeof( sign_mac ), |
| 2988 | &sign_mac_length ) ); |
| 2989 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2990 | sign_mac, sizeof( sign_mac ), |
| 2991 | &sign_mac_length ), |
| 2992 | PSA_ERROR_BAD_STATE ); |
| 2993 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2994 | |
| 2995 | /* Call verify finish twice in a row. */ |
| 2996 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2997 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2998 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2999 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3000 | verify_mac, sizeof( verify_mac ) ) ); |
| 3001 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3002 | verify_mac, sizeof( verify_mac ) ), |
| 3003 | PSA_ERROR_BAD_STATE ); |
| 3004 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3005 | |
| 3006 | /* Setup sign but try verify. */ |
| 3007 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 3008 | handle, alg ) ); |
| 3009 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3010 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3011 | verify_mac, sizeof( verify_mac ) ), |
| 3012 | PSA_ERROR_BAD_STATE ); |
| 3013 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3014 | |
| 3015 | /* Setup verify but try sign. */ |
| 3016 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 3017 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3018 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3019 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3020 | sign_mac, sizeof( sign_mac ), |
| 3021 | &sign_mac_length ), |
| 3022 | PSA_ERROR_BAD_STATE ); |
| 3023 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3024 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3025 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 3026 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 3027 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3028 | PSA_DONE( ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 3029 | } |
| 3030 | /* END_CASE */ |
| 3031 | |
| 3032 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3033 | void mac_sign( int key_type_arg, |
| 3034 | data_t *key, |
| 3035 | int alg_arg, |
| 3036 | data_t *input, |
| 3037 | data_t *expected_mac ) |
| 3038 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3039 | psa_key_handle_t handle = 0; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3040 | psa_key_type_t key_type = key_type_arg; |
| 3041 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3042 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3043 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3044 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3045 | size_t mac_buffer_size = |
| 3046 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 3047 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3048 | const size_t output_sizes_to_test[] = { |
| 3049 | 0, |
| 3050 | 1, |
| 3051 | expected_mac->len - 1, |
| 3052 | expected_mac->len, |
| 3053 | expected_mac->len + 1, |
| 3054 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3055 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3056 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3057 | /* We expect PSA_MAC_FINAL_SIZE to be exact. */ |
| 3058 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3059 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3060 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3061 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3062 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3063 | psa_set_key_algorithm( &attributes, alg ); |
| 3064 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3065 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3066 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3067 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3068 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3069 | { |
| 3070 | const size_t output_size = output_sizes_to_test[i]; |
| 3071 | psa_status_t expected_status = |
| 3072 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3073 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3074 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3075 | test_set_step( output_size ); |
| 3076 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3077 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3078 | /* Calculate the MAC. */ |
| 3079 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 3080 | handle, alg ) ); |
| 3081 | PSA_ASSERT( psa_mac_update( &operation, |
| 3082 | input->x, input->len ) ); |
| 3083 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3084 | actual_mac, output_size, |
| 3085 | &mac_length ), |
| 3086 | expected_status ); |
| 3087 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3088 | |
| 3089 | if( expected_status == PSA_SUCCESS ) |
| 3090 | { |
| 3091 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3092 | actual_mac, mac_length ); |
| 3093 | } |
| 3094 | mbedtls_free( actual_mac ); |
| 3095 | actual_mac = NULL; |
| 3096 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3097 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3098 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3099 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3100 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3101 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3102 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3103 | } |
| 3104 | /* END_CASE */ |
| 3105 | |
| 3106 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3107 | void mac_verify( int key_type_arg, |
| 3108 | data_t *key, |
| 3109 | int alg_arg, |
| 3110 | data_t *input, |
| 3111 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3112 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3113 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3114 | psa_key_type_t key_type = key_type_arg; |
| 3115 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3116 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3117 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3118 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3119 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3120 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 3121 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3122 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3123 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3124 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3125 | psa_set_key_algorithm( &attributes, alg ); |
| 3126 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3127 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3128 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3129 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3130 | /* Test the correct MAC. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3131 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 3132 | handle, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3133 | PSA_ASSERT( psa_mac_update( &operation, |
| 3134 | input->x, input->len ) ); |
| 3135 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3136 | expected_mac->x, |
| 3137 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3138 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3139 | /* Test a MAC that's too short. */ |
| 3140 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 3141 | handle, alg ) ); |
| 3142 | PSA_ASSERT( psa_mac_update( &operation, |
| 3143 | input->x, input->len ) ); |
| 3144 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3145 | expected_mac->x, |
| 3146 | expected_mac->len - 1 ), |
| 3147 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3148 | |
| 3149 | /* Test a MAC that's too long. */ |
| 3150 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3151 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
| 3152 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 3153 | handle, alg ) ); |
| 3154 | PSA_ASSERT( psa_mac_update( &operation, |
| 3155 | input->x, input->len ) ); |
| 3156 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3157 | perturbed_mac, |
| 3158 | expected_mac->len + 1 ), |
| 3159 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3160 | |
| 3161 | /* Test changing one byte. */ |
| 3162 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3163 | { |
| 3164 | test_set_step( i ); |
| 3165 | perturbed_mac[i] ^= 1; |
| 3166 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 3167 | handle, alg ) ); |
| 3168 | PSA_ASSERT( psa_mac_update( &operation, |
| 3169 | input->x, input->len ) ); |
| 3170 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3171 | perturbed_mac, |
| 3172 | expected_mac->len ), |
| 3173 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3174 | perturbed_mac[i] ^= 1; |
| 3175 | } |
| 3176 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3177 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3178 | psa_mac_abort( &operation ); |
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 | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3181 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3182 | } |
| 3183 | /* END_CASE */ |
| 3184 | |
| 3185 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3186 | void cipher_operation_init( ) |
| 3187 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3188 | const uint8_t input[1] = { 0 }; |
| 3189 | unsigned char output[1] = { 0 }; |
| 3190 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3191 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3192 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3193 | * though it's OK by the C standard. We could test for this, but we'd need |
| 3194 | * to supress the Clang warning for the test. */ |
| 3195 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3196 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3197 | psa_cipher_operation_t zero; |
| 3198 | |
| 3199 | memset( &zero, 0, sizeof( zero ) ); |
| 3200 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3201 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3202 | TEST_EQUAL( psa_cipher_update( &func, |
| 3203 | input, sizeof( input ), |
| 3204 | output, sizeof( output ), |
| 3205 | &output_length ), |
| 3206 | PSA_ERROR_BAD_STATE ); |
| 3207 | TEST_EQUAL( psa_cipher_update( &init, |
| 3208 | input, sizeof( input ), |
| 3209 | output, sizeof( output ), |
| 3210 | &output_length ), |
| 3211 | PSA_ERROR_BAD_STATE ); |
| 3212 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3213 | input, sizeof( input ), |
| 3214 | output, sizeof( output ), |
| 3215 | &output_length ), |
| 3216 | PSA_ERROR_BAD_STATE ); |
| 3217 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3218 | /* A default cipher operation should be abortable without error. */ |
| 3219 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3220 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3221 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3222 | } |
| 3223 | /* END_CASE */ |
| 3224 | |
| 3225 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3226 | void cipher_setup( int key_type_arg, |
| 3227 | data_t *key, |
| 3228 | int alg_arg, |
| 3229 | int expected_status_arg ) |
| 3230 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3231 | psa_key_type_t key_type = key_type_arg; |
| 3232 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3233 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3234 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3235 | psa_status_t status; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3236 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3237 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3238 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3239 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3240 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3241 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3242 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3243 | &operation, &status ) ) |
| 3244 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3245 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3246 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3247 | /* The operation object should be reusable. */ |
| 3248 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3249 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3250 | smoke_test_key_data, |
| 3251 | sizeof( smoke_test_key_data ), |
| 3252 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3253 | &operation, &status ) ) |
| 3254 | goto exit; |
| 3255 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3256 | #endif |
| 3257 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3258 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3259 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3260 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3261 | } |
| 3262 | /* END_CASE */ |
| 3263 | |
| 3264 | /* BEGIN_CASE */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3265 | void cipher_bad_order( ) |
| 3266 | { |
| 3267 | psa_key_handle_t handle = 0; |
| 3268 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3269 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3270 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3271 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3272 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 3273 | const uint8_t key[] = { |
| 3274 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3275 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3276 | const uint8_t text[] = { |
| 3277 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3278 | 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3279 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 3280 | size_t length = 0; |
| 3281 | |
| 3282 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3283 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3284 | psa_set_key_algorithm( &attributes, alg ); |
| 3285 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3286 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3287 | |
| 3288 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3289 | /* Call encrypt setup twice in a row. */ |
| 3290 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3291 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ), |
| 3292 | PSA_ERROR_BAD_STATE ); |
| 3293 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3294 | |
| 3295 | /* Call decrypt setup twice in a row. */ |
| 3296 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) ); |
| 3297 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ), |
| 3298 | PSA_ERROR_BAD_STATE ); |
| 3299 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3300 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3301 | /* Generate an IV without calling setup beforehand. */ |
| 3302 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3303 | buffer, sizeof( buffer ), |
| 3304 | &length ), |
| 3305 | PSA_ERROR_BAD_STATE ); |
| 3306 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3307 | |
| 3308 | /* Generate an IV twice in a row. */ |
| 3309 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3310 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3311 | buffer, sizeof( buffer ), |
| 3312 | &length ) ); |
| 3313 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3314 | buffer, sizeof( buffer ), |
| 3315 | &length ), |
| 3316 | PSA_ERROR_BAD_STATE ); |
| 3317 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3318 | |
| 3319 | /* Generate an IV after it's already set. */ |
| 3320 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3321 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3322 | iv, sizeof( iv ) ) ); |
| 3323 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3324 | buffer, sizeof( buffer ), |
| 3325 | &length ), |
| 3326 | PSA_ERROR_BAD_STATE ); |
| 3327 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3328 | |
| 3329 | /* Set an IV without calling setup beforehand. */ |
| 3330 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3331 | iv, sizeof( iv ) ), |
| 3332 | PSA_ERROR_BAD_STATE ); |
| 3333 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3334 | |
| 3335 | /* Set an IV after it's already set. */ |
| 3336 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3337 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3338 | iv, sizeof( iv ) ) ); |
| 3339 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3340 | iv, sizeof( iv ) ), |
| 3341 | PSA_ERROR_BAD_STATE ); |
| 3342 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3343 | |
| 3344 | /* Set an IV after it's already generated. */ |
| 3345 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3346 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3347 | buffer, sizeof( buffer ), |
| 3348 | &length ) ); |
| 3349 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3350 | iv, sizeof( iv ) ), |
| 3351 | PSA_ERROR_BAD_STATE ); |
| 3352 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3353 | |
| 3354 | /* Call update without calling setup beforehand. */ |
| 3355 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3356 | text, sizeof( text ), |
| 3357 | buffer, sizeof( buffer ), |
| 3358 | &length ), |
| 3359 | PSA_ERROR_BAD_STATE ); |
| 3360 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3361 | |
| 3362 | /* Call update without an IV where an IV is required. */ |
| 3363 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3364 | text, sizeof( text ), |
| 3365 | buffer, sizeof( buffer ), |
| 3366 | &length ), |
| 3367 | PSA_ERROR_BAD_STATE ); |
| 3368 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3369 | |
| 3370 | /* Call update after finish. */ |
| 3371 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3372 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3373 | iv, sizeof( iv ) ) ); |
| 3374 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3375 | buffer, sizeof( buffer ), &length ) ); |
| 3376 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3377 | text, sizeof( text ), |
| 3378 | buffer, sizeof( buffer ), |
| 3379 | &length ), |
| 3380 | PSA_ERROR_BAD_STATE ); |
| 3381 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3382 | |
| 3383 | /* Call finish without calling setup beforehand. */ |
| 3384 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3385 | buffer, sizeof( buffer ), &length ), |
| 3386 | PSA_ERROR_BAD_STATE ); |
| 3387 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3388 | |
| 3389 | /* Call finish without an IV where an IV is required. */ |
| 3390 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3391 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3392 | * for cipher modes with padding. */ |
| 3393 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3394 | buffer, sizeof( buffer ), &length ), |
| 3395 | PSA_ERROR_BAD_STATE ); |
| 3396 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3397 | |
| 3398 | /* Call finish twice in a row. */ |
| 3399 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3400 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3401 | iv, sizeof( iv ) ) ); |
| 3402 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3403 | buffer, sizeof( buffer ), &length ) ); |
| 3404 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3405 | buffer, sizeof( buffer ), &length ), |
| 3406 | PSA_ERROR_BAD_STATE ); |
| 3407 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3408 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3409 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 3410 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3411 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3412 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3413 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3414 | } |
| 3415 | /* END_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3416 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3417 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3418 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3419 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3420 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3421 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3422 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3423 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3424 | psa_status_t status; |
| 3425 | psa_key_type_t key_type = key_type_arg; |
| 3426 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3427 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3428 | unsigned char *output = NULL; |
| 3429 | size_t output_buffer_size = 0; |
| 3430 | size_t function_output_length = 0; |
| 3431 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3432 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3433 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3434 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3435 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3436 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3437 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3438 | psa_set_key_algorithm( &attributes, alg ); |
| 3439 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3440 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3441 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3442 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3443 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3444 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3445 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3446 | if( iv->len > 0 ) |
| 3447 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3448 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3449 | } |
| 3450 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3451 | output_buffer_size = ( (size_t) input->len + |
| 3452 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3453 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3454 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3455 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3456 | input->x, input->len, |
| 3457 | output, output_buffer_size, |
| 3458 | &function_output_length ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3459 | total_output_length += function_output_length; |
| 3460 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3461 | output + total_output_length, |
| 3462 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3463 | &function_output_length ); |
| 3464 | total_output_length += function_output_length; |
| 3465 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3466 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3467 | if( expected_status == PSA_SUCCESS ) |
| 3468 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3469 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3470 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3471 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3475 | psa_cipher_abort( &operation ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3476 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3477 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3478 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3479 | } |
| 3480 | /* END_CASE */ |
| 3481 | |
| 3482 | /* BEGIN_CASE */ |
| 3483 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3484 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3485 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3486 | int first_part_size_arg, |
| 3487 | int output1_length_arg, int output2_length_arg, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3488 | data_t *expected_output ) |
| 3489 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3490 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3491 | psa_key_type_t key_type = key_type_arg; |
| 3492 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3493 | size_t first_part_size = first_part_size_arg; |
| 3494 | size_t output1_length = output1_length_arg; |
| 3495 | size_t output2_length = output2_length_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3496 | unsigned char *output = NULL; |
| 3497 | size_t output_buffer_size = 0; |
| 3498 | size_t function_output_length = 0; |
| 3499 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3500 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3501 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3502 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3503 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3504 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3505 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3506 | psa_set_key_algorithm( &attributes, alg ); |
| 3507 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3508 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3509 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3510 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3511 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3512 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3513 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3514 | if( iv->len > 0 ) |
| 3515 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3516 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3517 | } |
| 3518 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3519 | output_buffer_size = ( (size_t) input->len + |
| 3520 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3521 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3522 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3523 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3524 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3525 | output, output_buffer_size, |
| 3526 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3527 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3528 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3529 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3530 | input->x + first_part_size, |
| 3531 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3532 | output + total_output_length, |
| 3533 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3534 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3535 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3536 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3537 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3538 | output + total_output_length, |
| 3539 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3540 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3541 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3542 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3543 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3544 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3545 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3546 | |
| 3547 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3548 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3549 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3550 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3551 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3552 | } |
| 3553 | /* END_CASE */ |
| 3554 | |
| 3555 | /* BEGIN_CASE */ |
| 3556 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3557 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3558 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3559 | int first_part_size_arg, |
| 3560 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3561 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3562 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3563 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3564 | |
| 3565 | psa_key_type_t key_type = key_type_arg; |
| 3566 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3567 | size_t first_part_size = first_part_size_arg; |
| 3568 | size_t output1_length = output1_length_arg; |
| 3569 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3570 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3571 | size_t output_buffer_size = 0; |
| 3572 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3573 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3574 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3575 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3576 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3577 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3578 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3579 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3580 | psa_set_key_algorithm( &attributes, alg ); |
| 3581 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3582 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3583 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3584 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3585 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3586 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3587 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3588 | if( iv->len > 0 ) |
| 3589 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3590 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3591 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3592 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3593 | output_buffer_size = ( (size_t) input->len + |
| 3594 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3595 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3596 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3597 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3598 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3599 | input->x, first_part_size, |
| 3600 | output, output_buffer_size, |
| 3601 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3602 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3603 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3604 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3605 | input->x + first_part_size, |
| 3606 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3607 | output + total_output_length, |
| 3608 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3609 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3610 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3611 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3612 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3613 | output + total_output_length, |
| 3614 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3615 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3616 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3617 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3618 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3619 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3620 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3621 | |
| 3622 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3623 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3624 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3625 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3626 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3627 | } |
| 3628 | /* END_CASE */ |
| 3629 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3630 | /* BEGIN_CASE */ |
| 3631 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3632 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3633 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3634 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3635 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3636 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3637 | psa_status_t status; |
| 3638 | psa_key_type_t key_type = key_type_arg; |
| 3639 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3640 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3641 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3642 | size_t output_buffer_size = 0; |
| 3643 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3644 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3645 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3646 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3647 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3648 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3649 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3650 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3651 | psa_set_key_algorithm( &attributes, alg ); |
| 3652 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3653 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3654 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3655 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3656 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3657 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3658 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3659 | if( iv->len > 0 ) |
| 3660 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3661 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3662 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3663 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3664 | output_buffer_size = ( (size_t) input->len + |
| 3665 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3666 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3667 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3668 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3669 | input->x, input->len, |
| 3670 | output, output_buffer_size, |
| 3671 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3672 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3673 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3674 | output + total_output_length, |
| 3675 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3676 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3677 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3678 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3679 | |
| 3680 | if( expected_status == PSA_SUCCESS ) |
| 3681 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3682 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3683 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3684 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3685 | } |
| 3686 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3687 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3688 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3689 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3690 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3691 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3692 | } |
| 3693 | /* END_CASE */ |
| 3694 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3695 | /* BEGIN_CASE */ |
| 3696 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3697 | data_t *key, |
| 3698 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3699 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3700 | psa_key_handle_t handle = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3701 | psa_key_type_t key_type = key_type_arg; |
| 3702 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 3703 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3704 | size_t iv_size = 16; |
| 3705 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3706 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3707 | size_t output1_size = 0; |
| 3708 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3709 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3710 | size_t output2_size = 0; |
| 3711 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3712 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3713 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3714 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3715 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3716 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3717 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3718 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3719 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3720 | psa_set_key_algorithm( &attributes, alg ); |
| 3721 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3722 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3723 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3724 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3725 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3726 | handle, alg ) ); |
| 3727 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3728 | handle, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3729 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3730 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3731 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3732 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3733 | iv, iv_size, |
| 3734 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3735 | } |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3736 | output1_size = ( (size_t) input->len + |
| 3737 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3738 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3739 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3740 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3741 | output1, output1_size, |
| 3742 | &output1_length ) ); |
| 3743 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3744 | output1 + output1_length, |
| 3745 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3746 | &function_output_length ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3747 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3748 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3749 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3750 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3751 | |
| 3752 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3753 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3754 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3755 | if( iv_length > 0 ) |
| 3756 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3757 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3758 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3759 | } |
| 3760 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3761 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3762 | output2, output2_size, |
| 3763 | &output2_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3764 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3765 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3766 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3767 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3768 | &function_output_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3769 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3770 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3771 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3772 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3773 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3774 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3775 | |
| 3776 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3777 | psa_cipher_abort( &operation1 ); |
| 3778 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3779 | mbedtls_free( output1 ); |
| 3780 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3781 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3782 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3783 | } |
| 3784 | /* END_CASE */ |
| 3785 | |
| 3786 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3787 | void cipher_verify_output_multipart( int alg_arg, |
| 3788 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3789 | data_t *key, |
| 3790 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3791 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3792 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3793 | psa_key_handle_t handle = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3794 | psa_key_type_t key_type = key_type_arg; |
| 3795 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3796 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3797 | unsigned char iv[16] = {0}; |
| 3798 | size_t iv_size = 16; |
| 3799 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3800 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3801 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3802 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3803 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3804 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3805 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3806 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3807 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3808 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3809 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3810 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3811 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3812 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3813 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3814 | psa_set_key_algorithm( &attributes, alg ); |
| 3815 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3816 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3817 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3818 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3819 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3820 | handle, alg ) ); |
| 3821 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3822 | handle, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3823 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3824 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3825 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3826 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3827 | iv, iv_size, |
| 3828 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3829 | } |
| 3830 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3831 | output1_buffer_size = ( (size_t) input->len + |
| 3832 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3833 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3834 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3835 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3836 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3837 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3838 | output1, output1_buffer_size, |
| 3839 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3840 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3841 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3842 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3843 | input->x + first_part_size, |
| 3844 | input->len - first_part_size, |
| 3845 | output1, output1_buffer_size, |
| 3846 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3847 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3848 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3849 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3850 | output1 + output1_length, |
| 3851 | output1_buffer_size - output1_length, |
| 3852 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3853 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3854 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3855 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3856 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3857 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3858 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3859 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3860 | if( iv_length > 0 ) |
| 3861 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3862 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3863 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3864 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3865 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3866 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3867 | output2, output2_buffer_size, |
| 3868 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3869 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3870 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3871 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3872 | output1 + first_part_size, |
| 3873 | output1_length - first_part_size, |
| 3874 | output2, output2_buffer_size, |
| 3875 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3876 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3877 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3878 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3879 | output2 + output2_length, |
| 3880 | output2_buffer_size - output2_length, |
| 3881 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3882 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3883 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3884 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3885 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3886 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3887 | |
| 3888 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3889 | psa_cipher_abort( &operation1 ); |
| 3890 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3891 | mbedtls_free( output1 ); |
| 3892 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3893 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3894 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3895 | } |
| 3896 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3897 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3898 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3899 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3900 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3901 | data_t *nonce, |
| 3902 | data_t *additional_data, |
| 3903 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3904 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3905 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3906 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3907 | psa_key_type_t key_type = key_type_arg; |
| 3908 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3909 | unsigned char *output_data = NULL; |
| 3910 | size_t output_size = 0; |
| 3911 | size_t output_length = 0; |
| 3912 | unsigned char *output_data2 = NULL; |
| 3913 | size_t output_length2 = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3914 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3915 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3916 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3917 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3918 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3919 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3920 | * should be exact. */ |
| 3921 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3922 | TEST_EQUAL( output_size, |
| 3923 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3924 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3925 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3926 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3927 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3928 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3929 | psa_set_key_algorithm( &attributes, alg ); |
| 3930 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3931 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3932 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3933 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3934 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3935 | TEST_EQUAL( psa_aead_encrypt( handle, alg, |
| 3936 | nonce->x, nonce->len, |
| 3937 | additional_data->x, |
| 3938 | additional_data->len, |
| 3939 | input_data->x, input_data->len, |
| 3940 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3941 | &output_length ), |
| 3942 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3943 | |
| 3944 | if( PSA_SUCCESS == expected_result ) |
| 3945 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3946 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3947 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3948 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3949 | * should be exact. */ |
| 3950 | TEST_EQUAL( input_data->len, |
| 3951 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) ); |
| 3952 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3953 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3954 | nonce->x, nonce->len, |
| 3955 | additional_data->x, |
| 3956 | additional_data->len, |
| 3957 | output_data, output_length, |
| 3958 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3959 | &output_length2 ), |
| 3960 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3961 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3962 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3963 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3964 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3965 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3966 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3967 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3968 | mbedtls_free( output_data ); |
| 3969 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3970 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3971 | } |
| 3972 | /* END_CASE */ |
| 3973 | |
| 3974 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3975 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3976 | int alg_arg, |
| 3977 | data_t *nonce, |
| 3978 | data_t *additional_data, |
| 3979 | data_t *input_data, |
| 3980 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3981 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3982 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3983 | psa_key_type_t key_type = key_type_arg; |
| 3984 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3985 | unsigned char *output_data = NULL; |
| 3986 | size_t output_size = 0; |
| 3987 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3988 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3989 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3990 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3991 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3992 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3993 | * should be exact. */ |
| 3994 | TEST_EQUAL( output_size, |
| 3995 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3996 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3997 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3998 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3999 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4000 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4001 | psa_set_key_algorithm( &attributes, alg ); |
| 4002 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4003 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4004 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4005 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4006 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4007 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 4008 | nonce->x, nonce->len, |
| 4009 | additional_data->x, additional_data->len, |
| 4010 | input_data->x, input_data->len, |
| 4011 | output_data, output_size, |
| 4012 | &output_length ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4013 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4014 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4015 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4016 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4017 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4018 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4019 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4020 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4021 | } |
| 4022 | /* END_CASE */ |
| 4023 | |
| 4024 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4025 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4026 | int alg_arg, |
| 4027 | data_t *nonce, |
| 4028 | data_t *additional_data, |
| 4029 | data_t *input_data, |
| 4030 | data_t *expected_data, |
| 4031 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4032 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4033 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4034 | psa_key_type_t key_type = key_type_arg; |
| 4035 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4036 | unsigned char *output_data = NULL; |
| 4037 | size_t output_size = 0; |
| 4038 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4039 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4040 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4041 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4042 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4043 | output_size = input_data->len - tag_length; |
| 4044 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4045 | * should be exact. */ |
| 4046 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 4047 | TEST_EQUAL( output_size, |
| 4048 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4049 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4050 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4051 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4052 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4053 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4054 | psa_set_key_algorithm( &attributes, alg ); |
| 4055 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4056 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4057 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4058 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4059 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4060 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 4061 | nonce->x, nonce->len, |
| 4062 | additional_data->x, |
| 4063 | additional_data->len, |
| 4064 | input_data->x, input_data->len, |
| 4065 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4066 | &output_length ), |
| 4067 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4068 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4069 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4070 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4071 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4072 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4073 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4074 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4075 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4076 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4077 | } |
| 4078 | /* END_CASE */ |
| 4079 | |
| 4080 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4081 | void signature_size( int type_arg, |
| 4082 | int bits, |
| 4083 | int alg_arg, |
| 4084 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4085 | { |
| 4086 | psa_key_type_t type = type_arg; |
| 4087 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4088 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4089 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4090 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4091 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4092 | TEST_EQUAL( actual_size, |
| 4093 | PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) ); |
| 4094 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4095 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4096 | exit: |
| 4097 | ; |
| 4098 | } |
| 4099 | /* END_CASE */ |
| 4100 | |
| 4101 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4102 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 4103 | int alg_arg, data_t *input_data, |
| 4104 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4105 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4106 | psa_key_handle_t handle = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4107 | psa_key_type_t key_type = key_type_arg; |
| 4108 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4109 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4110 | unsigned char *signature = NULL; |
| 4111 | size_t signature_size; |
| 4112 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4113 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4114 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4115 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4116 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4117 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4118 | psa_set_key_algorithm( &attributes, alg ); |
| 4119 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4120 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4121 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4122 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4123 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4124 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4125 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4126 | /* Allocate a buffer which has the size advertized by the |
| 4127 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4128 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4129 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4130 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4131 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4132 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4133 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4134 | /* Perform the signature. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4135 | PSA_ASSERT( psa_sign_hash( handle, alg, |
| 4136 | input_data->x, input_data->len, |
| 4137 | signature, signature_size, |
| 4138 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4139 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4140 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4141 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4142 | |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4143 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4144 | memset( signature, 0, signature_size ); |
| 4145 | signature_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4146 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 4147 | input_data->x, input_data->len, |
| 4148 | signature, signature_size, |
| 4149 | &signature_length ) ); |
| 4150 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4151 | signature, signature_length ); |
| 4152 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4153 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4154 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4155 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4156 | psa_destroy_key( handle ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 4157 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4158 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4159 | } |
| 4160 | /* END_CASE */ |
| 4161 | |
| 4162 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4163 | void sign_fail( int key_type_arg, data_t *key_data, |
| 4164 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4165 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4166 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4167 | psa_key_handle_t handle = 0; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4168 | psa_key_type_t key_type = key_type_arg; |
| 4169 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4170 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4171 | psa_status_t actual_status; |
| 4172 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 4173 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 4174 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4175 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4176 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4177 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4178 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4179 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4180 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4181 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4182 | psa_set_key_algorithm( &attributes, alg ); |
| 4183 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4184 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4185 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4186 | &handle ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4187 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4188 | actual_status = psa_sign_hash( handle, alg, |
| 4189 | input_data->x, input_data->len, |
| 4190 | signature, signature_size, |
| 4191 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4192 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4193 | /* The value of *signature_length is unspecified on error, but |
| 4194 | * whatever it is, it should be less than signature_size, so that |
| 4195 | * if the caller tries to read *signature_length bytes without |
| 4196 | * checking the error code then they don't overflow a buffer. */ |
| 4197 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4198 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4199 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4200 | signature_length = INVALID_EXPORT_LENGTH; |
| 4201 | TEST_EQUAL( psa_asymmetric_sign( handle, alg, |
| 4202 | input_data->x, input_data->len, |
| 4203 | signature, signature_size, |
| 4204 | &signature_length ), |
| 4205 | expected_status ); |
| 4206 | TEST_ASSERT( signature_length <= signature_size ); |
| 4207 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4208 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4209 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4210 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4211 | psa_destroy_key( handle ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4212 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4213 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4214 | } |
| 4215 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 4216 | |
| 4217 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4218 | void sign_verify( int key_type_arg, data_t *key_data, |
| 4219 | int alg_arg, data_t *input_data ) |
| 4220 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4221 | psa_key_handle_t handle = 0; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4222 | psa_key_type_t key_type = key_type_arg; |
| 4223 | psa_algorithm_t alg = alg_arg; |
| 4224 | size_t key_bits; |
| 4225 | unsigned char *signature = NULL; |
| 4226 | size_t signature_size; |
| 4227 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4228 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4229 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4230 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4231 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4232 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4233 | psa_set_key_algorithm( &attributes, alg ); |
| 4234 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4235 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4236 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4237 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4238 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4239 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4240 | |
| 4241 | /* Allocate a buffer which has the size advertized by the |
| 4242 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4243 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4244 | key_bits, alg ); |
| 4245 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4246 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4247 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4248 | |
| 4249 | /* Perform the signature. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4250 | PSA_ASSERT( psa_sign_hash( handle, alg, |
| 4251 | input_data->x, input_data->len, |
| 4252 | signature, signature_size, |
| 4253 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4254 | /* Check that the signature length looks sensible. */ |
| 4255 | TEST_ASSERT( signature_length <= signature_size ); |
| 4256 | TEST_ASSERT( signature_length > 0 ); |
| 4257 | |
| 4258 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4259 | PSA_ASSERT( psa_verify_hash( handle, alg, |
| 4260 | input_data->x, input_data->len, |
| 4261 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4262 | |
| 4263 | if( input_data->len != 0 ) |
| 4264 | { |
| 4265 | /* Flip a bit in the input and verify that the signature is now |
| 4266 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4267 | * because ECDSA may ignore the last few bits of the input. */ |
| 4268 | input_data->x[0] ^= 1; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4269 | TEST_EQUAL( psa_verify_hash( handle, alg, |
| 4270 | input_data->x, input_data->len, |
| 4271 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4272 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4276 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4277 | psa_destroy_key( handle ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4278 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4279 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4280 | } |
| 4281 | /* END_CASE */ |
| 4282 | |
| 4283 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4284 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 4285 | int alg_arg, data_t *hash_data, |
| 4286 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4287 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4288 | psa_key_handle_t handle = 0; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4289 | psa_key_type_t key_type = key_type_arg; |
| 4290 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4291 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4292 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4293 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 4294 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4295 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4296 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4297 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4298 | psa_set_key_algorithm( &attributes, alg ); |
| 4299 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4300 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4301 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4302 | &handle ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4303 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4304 | PSA_ASSERT( psa_verify_hash( handle, alg, |
| 4305 | hash_data->x, hash_data->len, |
| 4306 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4307 | |
| 4308 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4309 | PSA_ASSERT( psa_asymmetric_verify( handle, alg, |
| 4310 | hash_data->x, hash_data->len, |
| 4311 | signature_data->x, |
| 4312 | signature_data->len ) ); |
| 4313 | |
| 4314 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4315 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4316 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4317 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4318 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4319 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4320 | } |
| 4321 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4322 | |
| 4323 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4324 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 4325 | int alg_arg, data_t *hash_data, |
| 4326 | data_t *signature_data, |
| 4327 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4328 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4329 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4330 | psa_key_type_t key_type = key_type_arg; |
| 4331 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4332 | psa_status_t actual_status; |
| 4333 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4334 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4335 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4336 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4337 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4338 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4339 | psa_set_key_algorithm( &attributes, alg ); |
| 4340 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4341 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4342 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4343 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4344 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4345 | actual_status = psa_verify_hash( handle, alg, |
| 4346 | hash_data->x, hash_data->len, |
| 4347 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4348 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4349 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4350 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4351 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 4352 | hash_data->x, hash_data->len, |
| 4353 | signature_data->x, signature_data->len ), |
| 4354 | expected_status ); |
| 4355 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4356 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4357 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4358 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4359 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4360 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4361 | } |
| 4362 | /* END_CASE */ |
| 4363 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4364 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4365 | void asymmetric_encrypt( int key_type_arg, |
| 4366 | data_t *key_data, |
| 4367 | int alg_arg, |
| 4368 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4369 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4370 | int expected_output_length_arg, |
| 4371 | int expected_status_arg ) |
| 4372 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4373 | psa_key_handle_t handle = 0; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4374 | psa_key_type_t key_type = key_type_arg; |
| 4375 | psa_algorithm_t alg = alg_arg; |
| 4376 | size_t expected_output_length = expected_output_length_arg; |
| 4377 | size_t key_bits; |
| 4378 | unsigned char *output = NULL; |
| 4379 | size_t output_size; |
| 4380 | size_t output_length = ~0; |
| 4381 | psa_status_t actual_status; |
| 4382 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4383 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4384 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4385 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4386 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4387 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4388 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4389 | psa_set_key_algorithm( &attributes, alg ); |
| 4390 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4391 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4392 | &handle ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4393 | |
| 4394 | /* Determine the maximum output length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4395 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4396 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4397 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4398 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4399 | |
| 4400 | /* Encrypt the input */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4401 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4402 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4403 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4404 | output, output_size, |
| 4405 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4406 | TEST_EQUAL( actual_status, expected_status ); |
| 4407 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4408 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4409 | /* If the label is empty, the test framework puts a non-null pointer |
| 4410 | * in label->x. Test that a null pointer works as well. */ |
| 4411 | if( label->len == 0 ) |
| 4412 | { |
| 4413 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4414 | if( output_size != 0 ) |
| 4415 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4416 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4417 | input_data->x, input_data->len, |
| 4418 | NULL, label->len, |
| 4419 | output, output_size, |
| 4420 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4421 | TEST_EQUAL( actual_status, expected_status ); |
| 4422 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4423 | } |
| 4424 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4425 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4426 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4427 | psa_destroy_key( handle ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4428 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4429 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4430 | } |
| 4431 | /* END_CASE */ |
| 4432 | |
| 4433 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4434 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4435 | data_t *key_data, |
| 4436 | int alg_arg, |
| 4437 | data_t *input_data, |
| 4438 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4439 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4440 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4441 | psa_key_type_t key_type = key_type_arg; |
| 4442 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4443 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4444 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4445 | size_t output_size; |
| 4446 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4447 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4448 | size_t output2_size; |
| 4449 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4450 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4451 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4452 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4453 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4454 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4455 | psa_set_key_algorithm( &attributes, alg ); |
| 4456 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4457 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4458 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4459 | &handle ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4460 | |
| 4461 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4462 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4463 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4464 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4465 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4466 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4467 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4468 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4469 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4470 | * the original plaintext because of the non-optional random |
| 4471 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4472 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 4473 | input_data->x, input_data->len, |
| 4474 | label->x, label->len, |
| 4475 | output, output_size, |
| 4476 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4477 | /* We don't know what ciphertext length to expect, but check that |
| 4478 | * it looks sensible. */ |
| 4479 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4480 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4481 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4482 | output, output_length, |
| 4483 | label->x, label->len, |
| 4484 | output2, output2_size, |
| 4485 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4486 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4487 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4488 | |
| 4489 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4490 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4491 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4492 | mbedtls_free( output ); |
| 4493 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4494 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4495 | } |
| 4496 | /* END_CASE */ |
| 4497 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4498 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4499 | void asymmetric_decrypt( int key_type_arg, |
| 4500 | data_t *key_data, |
| 4501 | int alg_arg, |
| 4502 | data_t *input_data, |
| 4503 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4504 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4505 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4506 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4507 | psa_key_type_t key_type = key_type_arg; |
| 4508 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4509 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4510 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4511 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4512 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4513 | |
Jaeden Amero | 412654a | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4514 | output_size = expected_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4515 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4516 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4517 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4518 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4519 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4520 | psa_set_key_algorithm( &attributes, alg ); |
| 4521 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4522 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4523 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4524 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4525 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4526 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4527 | input_data->x, input_data->len, |
| 4528 | label->x, label->len, |
| 4529 | output, |
| 4530 | output_size, |
| 4531 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4532 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4533 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4534 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4535 | /* If the label is empty, the test framework puts a non-null pointer |
| 4536 | * in label->x. Test that a null pointer works as well. */ |
| 4537 | if( label->len == 0 ) |
| 4538 | { |
| 4539 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4540 | if( output_size != 0 ) |
| 4541 | memset( output, 0, output_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4542 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4543 | input_data->x, input_data->len, |
| 4544 | NULL, label->len, |
| 4545 | output, |
| 4546 | output_size, |
| 4547 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4548 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4549 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4550 | } |
| 4551 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4552 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4553 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4554 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4555 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4556 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4557 | } |
| 4558 | /* END_CASE */ |
| 4559 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4560 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4561 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4562 | data_t *key_data, |
| 4563 | int alg_arg, |
| 4564 | data_t *input_data, |
| 4565 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4566 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4567 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4568 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4569 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4570 | psa_key_type_t key_type = key_type_arg; |
| 4571 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4572 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4573 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4574 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4575 | psa_status_t actual_status; |
| 4576 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4577 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4578 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4579 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4580 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4581 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4582 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4583 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4584 | psa_set_key_algorithm( &attributes, alg ); |
| 4585 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4586 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4587 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4588 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4589 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4590 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4591 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4592 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4593 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4594 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4595 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4596 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4597 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4598 | /* If the label is empty, the test framework puts a non-null pointer |
| 4599 | * in label->x. Test that a null pointer works as well. */ |
| 4600 | if( label->len == 0 ) |
| 4601 | { |
| 4602 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4603 | if( output_size != 0 ) |
| 4604 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4605 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4606 | input_data->x, input_data->len, |
| 4607 | NULL, label->len, |
| 4608 | output, output_size, |
| 4609 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4610 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4611 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4612 | } |
| 4613 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4614 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4615 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4616 | psa_destroy_key( handle ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4617 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4618 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4619 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4620 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4621 | |
| 4622 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4623 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4624 | { |
| 4625 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4626 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4627 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4628 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4629 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4630 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4631 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4632 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4633 | |
| 4634 | memset( &zero, 0, sizeof( zero ) ); |
| 4635 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4636 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4637 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4638 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4639 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4640 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4641 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4642 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4643 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4644 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4645 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4646 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4647 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4648 | } |
| 4649 | /* END_CASE */ |
| 4650 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4651 | /* BEGIN_CASE */ |
| 4652 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4653 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4654 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4655 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4656 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4657 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4658 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4659 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4660 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4661 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4662 | |
| 4663 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4664 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4665 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4666 | } |
| 4667 | /* END_CASE */ |
| 4668 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4669 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4670 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4671 | int expected_status_arg ) |
| 4672 | { |
| 4673 | psa_algorithm_t alg = alg_arg; |
| 4674 | size_t capacity = capacity_arg; |
| 4675 | psa_status_t expected_status = expected_status_arg; |
| 4676 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4677 | |
| 4678 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4679 | |
| 4680 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4681 | |
| 4682 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4683 | expected_status ); |
| 4684 | |
| 4685 | exit: |
| 4686 | psa_key_derivation_abort( &operation ); |
| 4687 | PSA_DONE( ); |
| 4688 | } |
| 4689 | /* END_CASE */ |
| 4690 | |
| 4691 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4692 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4693 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4694 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4695 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4696 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4697 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4698 | int expected_status_arg3, |
| 4699 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4700 | { |
| 4701 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4702 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 4703 | psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3}; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4704 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4705 | expected_status_arg2, |
| 4706 | expected_status_arg3}; |
| 4707 | data_t *inputs[] = {input1, input2, input3}; |
| 4708 | psa_key_handle_t handles[] = {0, 0, 0}; |
| 4709 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4710 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4711 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4712 | psa_key_type_t output_key_type = output_key_type_arg; |
| 4713 | psa_key_handle_t output_handle = 0; |
| 4714 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4715 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4716 | |
| 4717 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4718 | |
| 4719 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4720 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4721 | |
| 4722 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4723 | |
| 4724 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4725 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4726 | if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4727 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4728 | psa_set_key_type( &attributes, key_types[i] ); |
| 4729 | PSA_ASSERT( psa_import_key( &attributes, |
| 4730 | inputs[i]->x, inputs[i]->len, |
| 4731 | &handles[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame^] | 4732 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 4733 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4734 | { |
| 4735 | // When taking a private key as secret input, use key agreement |
| 4736 | // to add the shared secret to the derivation |
| 4737 | TEST_EQUAL( key_agreement_with_self( &operation, handles[i] ), |
| 4738 | expected_statuses[i] ); |
| 4739 | } |
| 4740 | else |
| 4741 | { |
| 4742 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
| 4743 | handles[i] ), |
| 4744 | expected_statuses[i] ); |
| 4745 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4746 | } |
| 4747 | else |
| 4748 | { |
| 4749 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4750 | &operation, steps[i], |
| 4751 | inputs[i]->x, inputs[i]->len ), |
| 4752 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4753 | } |
| 4754 | } |
| 4755 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4756 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 4757 | { |
| 4758 | psa_reset_key_attributes( &attributes ); |
| 4759 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 4760 | psa_set_key_bits( &attributes, 8 ); |
| 4761 | actual_output_status = |
| 4762 | psa_key_derivation_output_key( &attributes, &operation, |
| 4763 | &output_handle ); |
| 4764 | } |
| 4765 | else |
| 4766 | { |
| 4767 | uint8_t buffer[1]; |
| 4768 | actual_output_status = |
| 4769 | psa_key_derivation_output_bytes( &operation, |
| 4770 | buffer, sizeof( buffer ) ); |
| 4771 | } |
| 4772 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 4773 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4774 | exit: |
| 4775 | psa_key_derivation_abort( &operation ); |
| 4776 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4777 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4778 | psa_destroy_key( output_handle ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4779 | PSA_DONE( ); |
| 4780 | } |
| 4781 | /* END_CASE */ |
| 4782 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4783 | /* BEGIN_CASE */ |
| 4784 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4785 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4786 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4787 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4788 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4789 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4790 | unsigned char input1[] = "Input 1"; |
| 4791 | size_t input1_length = sizeof( input1 ); |
| 4792 | unsigned char input2[] = "Input 2"; |
| 4793 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4794 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4795 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4796 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4797 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4798 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4799 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4800 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4801 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4802 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4803 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4804 | psa_set_key_algorithm( &attributes, alg ); |
| 4805 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4806 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4807 | PSA_ASSERT( psa_import_key( &attributes, |
| 4808 | key_data, sizeof( key_data ), |
| 4809 | &handle ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4810 | |
| 4811 | /* valid key derivation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4812 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4813 | input1, input1_length, |
| 4814 | input2, input2_length, |
| 4815 | capacity ) ) |
| 4816 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4817 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4818 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4819 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4820 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4821 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4822 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4823 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4824 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4825 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4826 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4827 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4828 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4829 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4830 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4831 | } |
| 4832 | /* END_CASE */ |
| 4833 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4834 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4835 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4836 | { |
| 4837 | uint8_t output_buffer[16]; |
| 4838 | size_t buffer_size = 16; |
| 4839 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4840 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4841 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4842 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4843 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4844 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4845 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4846 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4847 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4848 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4849 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4850 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4851 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4852 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4853 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4854 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4855 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4856 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4857 | |
| 4858 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4859 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4860 | } |
| 4861 | /* END_CASE */ |
| 4862 | |
| 4863 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4864 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4865 | int step1_arg, data_t *input1, |
| 4866 | int step2_arg, data_t *input2, |
| 4867 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4868 | int requested_capacity_arg, |
| 4869 | data_t *expected_output1, |
| 4870 | data_t *expected_output2 ) |
| 4871 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4872 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4873 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4874 | data_t *inputs[] = {input1, input2, input3}; |
| 4875 | psa_key_handle_t handles[] = {0, 0, 0}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4876 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4877 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4878 | uint8_t *expected_outputs[2] = |
| 4879 | {expected_output1->x, expected_output2->x}; |
| 4880 | size_t output_sizes[2] = |
| 4881 | {expected_output1->len, expected_output2->len}; |
| 4882 | size_t output_buffer_size = 0; |
| 4883 | uint8_t *output_buffer = NULL; |
| 4884 | size_t expected_capacity; |
| 4885 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4886 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4887 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4888 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4889 | |
| 4890 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4891 | { |
| 4892 | if( output_sizes[i] > output_buffer_size ) |
| 4893 | output_buffer_size = output_sizes[i]; |
| 4894 | if( output_sizes[i] == 0 ) |
| 4895 | expected_outputs[i] = NULL; |
| 4896 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4897 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4898 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4899 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4900 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4901 | psa_set_key_algorithm( &attributes, alg ); |
| 4902 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4903 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4904 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4905 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4906 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4907 | requested_capacity ) ); |
| 4908 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4909 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4910 | switch( steps[i] ) |
| 4911 | { |
| 4912 | case 0: |
| 4913 | break; |
| 4914 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4915 | PSA_ASSERT( psa_import_key( &attributes, |
| 4916 | inputs[i]->x, inputs[i]->len, |
| 4917 | &handles[i] ) ); |
| 4918 | PSA_ASSERT( psa_key_derivation_input_key( |
| 4919 | &operation, steps[i], |
| 4920 | handles[i] ) ); |
| 4921 | break; |
| 4922 | default: |
| 4923 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4924 | &operation, steps[i], |
| 4925 | inputs[i]->x, inputs[i]->len ) ); |
| 4926 | break; |
| 4927 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4928 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4929 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4930 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4931 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4932 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4933 | expected_capacity = requested_capacity; |
| 4934 | |
| 4935 | /* Expansion phase. */ |
| 4936 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4937 | { |
| 4938 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4939 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4940 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4941 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4942 | { |
| 4943 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4944 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4945 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4946 | continue; |
| 4947 | } |
| 4948 | else if( expected_capacity == 0 || |
| 4949 | output_sizes[i] > expected_capacity ) |
| 4950 | { |
| 4951 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4952 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4953 | expected_capacity = 0; |
| 4954 | continue; |
| 4955 | } |
| 4956 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4957 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4958 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4959 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4960 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4961 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4962 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4963 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4964 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4965 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4966 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4967 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4968 | |
| 4969 | exit: |
| 4970 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4971 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4972 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4973 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4974 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4975 | } |
| 4976 | /* END_CASE */ |
| 4977 | |
| 4978 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4979 | void derive_full( int alg_arg, |
| 4980 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4981 | data_t *input1, |
| 4982 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4983 | int requested_capacity_arg ) |
| 4984 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4985 | psa_key_handle_t handle = 0; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4986 | psa_algorithm_t alg = alg_arg; |
| 4987 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4988 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4989 | unsigned char output_buffer[16]; |
| 4990 | size_t expected_capacity = requested_capacity; |
| 4991 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4992 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4993 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4994 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4995 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4996 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4997 | psa_set_key_algorithm( &attributes, alg ); |
| 4998 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4999 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5000 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5001 | &handle ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5002 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 5003 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 5004 | input1->x, input1->len, |
| 5005 | input2->x, input2->len, |
| 5006 | requested_capacity ) ) |
| 5007 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5008 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5009 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5010 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5011 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5012 | |
| 5013 | /* Expansion phase. */ |
| 5014 | while( current_capacity > 0 ) |
| 5015 | { |
| 5016 | size_t read_size = sizeof( output_buffer ); |
| 5017 | if( read_size > current_capacity ) |
| 5018 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5019 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5020 | output_buffer, |
| 5021 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5022 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5023 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5024 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5025 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5026 | } |
| 5027 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5028 | /* Check that the operation refuses to go over capacity. */ |
| 5029 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5030 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5031 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5032 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5033 | |
| 5034 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5035 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5036 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5037 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5038 | } |
| 5039 | /* END_CASE */ |
| 5040 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5041 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5042 | void derive_key_exercise( int alg_arg, |
| 5043 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5044 | data_t *input1, |
| 5045 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5046 | int derived_type_arg, |
| 5047 | int derived_bits_arg, |
| 5048 | int derived_usage_arg, |
| 5049 | int derived_alg_arg ) |
| 5050 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5051 | psa_key_handle_t base_handle = 0; |
| 5052 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5053 | psa_algorithm_t alg = alg_arg; |
| 5054 | psa_key_type_t derived_type = derived_type_arg; |
| 5055 | size_t derived_bits = derived_bits_arg; |
| 5056 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 5057 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 5058 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5059 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5060 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5061 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5062 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5063 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5064 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5065 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5066 | psa_set_key_algorithm( &attributes, alg ); |
| 5067 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5068 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5069 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5070 | |
| 5071 | /* Derive a key. */ |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5072 | if ( setup_key_derivation_wrap( &operation, base_handle, alg, |
| 5073 | input1->x, input1->len, |
| 5074 | input2->x, input2->len, capacity ) ) |
| 5075 | goto exit; |
| 5076 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5077 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 5078 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 5079 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5080 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5081 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5082 | &derived_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5083 | |
| 5084 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5085 | PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) ); |
| 5086 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 5087 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5088 | |
| 5089 | /* Exercise the derived key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5090 | if( ! exercise_key( derived_handle, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5091 | goto exit; |
| 5092 | |
| 5093 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5094 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5095 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5096 | psa_destroy_key( base_handle ); |
| 5097 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5098 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5099 | } |
| 5100 | /* END_CASE */ |
| 5101 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5102 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5103 | void derive_key_export( int alg_arg, |
| 5104 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5105 | data_t *input1, |
| 5106 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5107 | int bytes1_arg, |
| 5108 | int bytes2_arg ) |
| 5109 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5110 | psa_key_handle_t base_handle = 0; |
| 5111 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5112 | psa_algorithm_t alg = alg_arg; |
| 5113 | size_t bytes1 = bytes1_arg; |
| 5114 | size_t bytes2 = bytes2_arg; |
| 5115 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5116 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5117 | uint8_t *output_buffer = NULL; |
| 5118 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5119 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5120 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5121 | size_t length; |
| 5122 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5123 | ASSERT_ALLOC( output_buffer, capacity ); |
| 5124 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5125 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5126 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5127 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5128 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5129 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5130 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 5131 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5132 | |
| 5133 | /* Derive some material and output it. */ |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5134 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 5135 | input1->x, input1->len, |
| 5136 | input2->x, input2->len, capacity ) ) |
| 5137 | goto exit; |
| 5138 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5139 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5140 | output_buffer, |
| 5141 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5142 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5143 | |
| 5144 | /* Derive the same output again, but this time store it in key objects. */ |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5145 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 5146 | input1->x, input1->len, |
| 5147 | input2->x, input2->len, capacity ) ) |
| 5148 | goto exit; |
| 5149 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5150 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5151 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 5152 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5153 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5154 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5155 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5156 | PSA_ASSERT( psa_export_key( derived_handle, |
| 5157 | export_buffer, bytes1, |
| 5158 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5159 | TEST_EQUAL( length, bytes1 ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5160 | PSA_ASSERT( psa_destroy_key( derived_handle ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5161 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5162 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5163 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5164 | PSA_ASSERT( psa_export_key( derived_handle, |
| 5165 | export_buffer + bytes1, bytes2, |
| 5166 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5167 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5168 | |
| 5169 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5170 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 5171 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5172 | |
| 5173 | exit: |
| 5174 | mbedtls_free( output_buffer ); |
| 5175 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5176 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5177 | psa_destroy_key( base_handle ); |
| 5178 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5179 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5180 | } |
| 5181 | /* END_CASE */ |
| 5182 | |
| 5183 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5184 | void derive_key( int alg_arg, |
| 5185 | data_t *key_data, data_t *input1, data_t *input2, |
| 5186 | int type_arg, int bits_arg, |
| 5187 | int expected_status_arg ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5188 | { |
| 5189 | psa_key_handle_t base_handle = 0; |
| 5190 | psa_key_handle_t derived_handle = 0; |
| 5191 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5192 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5193 | size_t bits = bits_arg; |
| 5194 | psa_status_t expected_status = expected_status_arg; |
| 5195 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5196 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5197 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5198 | |
| 5199 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5200 | |
| 5201 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5202 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5203 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5204 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 5205 | &base_handle ) ); |
| 5206 | |
| 5207 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 5208 | input1->x, input1->len, |
| 5209 | input2->x, input2->len, SIZE_MAX ) ) |
| 5210 | goto exit; |
| 5211 | |
| 5212 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5213 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5214 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5215 | psa_set_key_bits( &derived_attributes, bits ); |
| 5216 | TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 5217 | &derived_handle ), |
| 5218 | expected_status ); |
| 5219 | |
| 5220 | exit: |
| 5221 | psa_key_derivation_abort( &operation ); |
| 5222 | psa_destroy_key( base_handle ); |
| 5223 | psa_destroy_key( derived_handle ); |
| 5224 | PSA_DONE( ); |
| 5225 | } |
| 5226 | /* END_CASE */ |
| 5227 | |
| 5228 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5229 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 5230 | int our_key_type_arg, int our_key_alg_arg, |
| 5231 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5232 | int expected_status_arg ) |
| 5233 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5234 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5235 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 5236 | psa_algorithm_t key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5237 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5238 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5239 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5240 | psa_status_t expected_status = expected_status_arg; |
| 5241 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5242 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5243 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5244 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5245 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 5246 | psa_set_key_algorithm( &attributes, key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5247 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5248 | PSA_ASSERT( psa_import_key( &attributes, |
| 5249 | our_key_data->x, our_key_data->len, |
| 5250 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5251 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5252 | /* The tests currently include inputs that should fail at either step. |
| 5253 | * Test cases that fail at the setup step should be changed to call |
| 5254 | * key_derivation_setup instead, and this function should be renamed |
| 5255 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5256 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5257 | if( status == PSA_SUCCESS ) |
| 5258 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5259 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 5260 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5261 | our_key, |
| 5262 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5263 | expected_status ); |
| 5264 | } |
| 5265 | else |
| 5266 | { |
| 5267 | TEST_ASSERT( status == expected_status ); |
| 5268 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5269 | |
| 5270 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5271 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5272 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5273 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5274 | } |
| 5275 | /* END_CASE */ |
| 5276 | |
| 5277 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5278 | void raw_key_agreement( int alg_arg, |
| 5279 | int our_key_type_arg, data_t *our_key_data, |
| 5280 | data_t *peer_key_data, |
| 5281 | data_t *expected_output ) |
| 5282 | { |
| 5283 | psa_key_handle_t our_key = 0; |
| 5284 | psa_algorithm_t alg = alg_arg; |
| 5285 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5286 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5287 | unsigned char *output = NULL; |
| 5288 | size_t output_length = ~0; |
| 5289 | |
| 5290 | ASSERT_ALLOC( output, expected_output->len ); |
| 5291 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5292 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5293 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5294 | psa_set_key_algorithm( &attributes, alg ); |
| 5295 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5296 | PSA_ASSERT( psa_import_key( &attributes, |
| 5297 | our_key_data->x, our_key_data->len, |
| 5298 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5299 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5300 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 5301 | peer_key_data->x, peer_key_data->len, |
| 5302 | output, expected_output->len, |
| 5303 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5304 | ASSERT_COMPARE( output, output_length, |
| 5305 | expected_output->x, expected_output->len ); |
| 5306 | |
| 5307 | exit: |
| 5308 | mbedtls_free( output ); |
| 5309 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5310 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5311 | } |
| 5312 | /* END_CASE */ |
| 5313 | |
| 5314 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5315 | void key_agreement_capacity( int alg_arg, |
| 5316 | int our_key_type_arg, data_t *our_key_data, |
| 5317 | data_t *peer_key_data, |
| 5318 | int expected_capacity_arg ) |
| 5319 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5320 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5321 | psa_algorithm_t alg = alg_arg; |
| 5322 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5323 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5324 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5325 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5326 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5327 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5328 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5329 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5330 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5331 | psa_set_key_algorithm( &attributes, alg ); |
| 5332 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5333 | PSA_ASSERT( psa_import_key( &attributes, |
| 5334 | our_key_data->x, our_key_data->len, |
| 5335 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5336 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5337 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5338 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5339 | &operation, |
| 5340 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5341 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5342 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5343 | { |
| 5344 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5345 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5346 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5347 | NULL, 0 ) ); |
| 5348 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5349 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5350 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5351 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5352 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5353 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5354 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5355 | /* Test the actual capacity by reading the output. */ |
| 5356 | while( actual_capacity > sizeof( output ) ) |
| 5357 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5358 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5359 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5360 | actual_capacity -= sizeof( output ); |
| 5361 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5362 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5363 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5364 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5365 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5366 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5367 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5368 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5369 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5370 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5371 | } |
| 5372 | /* END_CASE */ |
| 5373 | |
| 5374 | /* BEGIN_CASE */ |
| 5375 | void key_agreement_output( int alg_arg, |
| 5376 | int our_key_type_arg, data_t *our_key_data, |
| 5377 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5378 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5379 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5380 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5381 | psa_algorithm_t alg = alg_arg; |
| 5382 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5383 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5384 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5385 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5386 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5387 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 5388 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5389 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5390 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5391 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5392 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5393 | psa_set_key_algorithm( &attributes, alg ); |
| 5394 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5395 | PSA_ASSERT( psa_import_key( &attributes, |
| 5396 | our_key_data->x, our_key_data->len, |
| 5397 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5398 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5399 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5400 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5401 | &operation, |
| 5402 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5403 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5404 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5405 | { |
| 5406 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5407 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5408 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5409 | NULL, 0 ) ); |
| 5410 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5411 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5412 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5413 | actual_output, |
| 5414 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5415 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 5416 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5417 | if( expected_output2->len != 0 ) |
| 5418 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5419 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5420 | actual_output, |
| 5421 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5422 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 5423 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5424 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5425 | |
| 5426 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5427 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5428 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5429 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5430 | mbedtls_free( actual_output ); |
| 5431 | } |
| 5432 | /* END_CASE */ |
| 5433 | |
| 5434 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5435 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5436 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5437 | size_t bytes = bytes_arg; |
| 5438 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5439 | unsigned char *output = NULL; |
| 5440 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5441 | size_t i; |
| 5442 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5443 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5444 | TEST_ASSERT( bytes_arg >= 0 ); |
| 5445 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5446 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 5447 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5448 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5449 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5450 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5451 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5452 | /* Run several times, to ensure that every output byte will be |
| 5453 | * nonzero at least once with overwhelming probability |
| 5454 | * (2^(-8*number_of_runs)). */ |
| 5455 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5456 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5457 | if( bytes != 0 ) |
| 5458 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5459 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5460 | |
| 5461 | /* Check that no more than bytes have been overwritten */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5462 | ASSERT_COMPARE( output + bytes, sizeof( trail ), |
| 5463 | trail, sizeof( trail ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5464 | |
| 5465 | for( i = 0; i < bytes; i++ ) |
| 5466 | { |
| 5467 | if( output[i] != 0 ) |
| 5468 | ++changed[i]; |
| 5469 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5470 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5471 | |
| 5472 | /* Check that every byte was changed to nonzero at least once. This |
| 5473 | * validates that psa_generate_random is overwriting every byte of |
| 5474 | * the output buffer. */ |
| 5475 | for( i = 0; i < bytes; i++ ) |
| 5476 | { |
| 5477 | TEST_ASSERT( changed[i] != 0 ); |
| 5478 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5479 | |
| 5480 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5481 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5482 | mbedtls_free( output ); |
| 5483 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5484 | } |
| 5485 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5486 | |
| 5487 | /* BEGIN_CASE */ |
| 5488 | void generate_key( int type_arg, |
| 5489 | int bits_arg, |
| 5490 | int usage_arg, |
| 5491 | int alg_arg, |
| 5492 | int expected_status_arg ) |
| 5493 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5494 | psa_key_handle_t handle = 0; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5495 | psa_key_type_t type = type_arg; |
| 5496 | psa_key_usage_t usage = usage_arg; |
| 5497 | size_t bits = bits_arg; |
| 5498 | psa_algorithm_t alg = alg_arg; |
| 5499 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5500 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5501 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5502 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5503 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5504 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5505 | psa_set_key_usage_flags( &attributes, usage ); |
| 5506 | psa_set_key_algorithm( &attributes, alg ); |
| 5507 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5508 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5509 | |
| 5510 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5511 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5512 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5513 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5514 | |
| 5515 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5516 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 5517 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5518 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5519 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5520 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5521 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5522 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5523 | |
| 5524 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5525 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5526 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5527 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5528 | } |
| 5529 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5530 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5531 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */ |
| 5532 | void generate_key_rsa( int bits_arg, |
| 5533 | data_t *e_arg, |
| 5534 | int expected_status_arg ) |
| 5535 | { |
| 5536 | psa_key_handle_t handle = 0; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5537 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5538 | size_t bits = bits_arg; |
| 5539 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5540 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5541 | psa_status_t expected_status = expected_status_arg; |
| 5542 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5543 | uint8_t *exported = NULL; |
| 5544 | size_t exported_size = |
| 5545 | PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
| 5546 | size_t exported_length = SIZE_MAX; |
| 5547 | uint8_t *e_read_buffer = NULL; |
| 5548 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5549 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5550 | size_t e_read_length = SIZE_MAX; |
| 5551 | |
| 5552 | if( e_arg->len == 0 || |
| 5553 | ( e_arg->len == 3 && |
| 5554 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5555 | { |
| 5556 | is_default_public_exponent = 1; |
| 5557 | e_read_size = 0; |
| 5558 | } |
| 5559 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5560 | ASSERT_ALLOC( exported, exported_size ); |
| 5561 | |
| 5562 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5563 | |
| 5564 | psa_set_key_usage_flags( &attributes, usage ); |
| 5565 | psa_set_key_algorithm( &attributes, alg ); |
| 5566 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5567 | e_arg->x, e_arg->len ) ); |
| 5568 | psa_set_key_bits( &attributes, bits ); |
| 5569 | |
| 5570 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5571 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5572 | if( expected_status != PSA_SUCCESS ) |
| 5573 | goto exit; |
| 5574 | |
| 5575 | /* Test the key information */ |
| 5576 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5577 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5578 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5579 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5580 | e_read_buffer, e_read_size, |
| 5581 | &e_read_length ) ); |
| 5582 | if( is_default_public_exponent ) |
| 5583 | TEST_EQUAL( e_read_length, 0 ); |
| 5584 | else |
| 5585 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5586 | |
| 5587 | /* Do something with the key according to its type and permitted usage. */ |
| 5588 | if( ! exercise_key( handle, usage, alg ) ) |
| 5589 | goto exit; |
| 5590 | |
| 5591 | /* Export the key and check the public exponent. */ |
| 5592 | PSA_ASSERT( psa_export_public_key( handle, |
| 5593 | exported, exported_size, |
| 5594 | &exported_length ) ); |
| 5595 | { |
| 5596 | uint8_t *p = exported; |
| 5597 | uint8_t *end = exported + exported_length; |
| 5598 | size_t len; |
| 5599 | /* RSAPublicKey ::= SEQUENCE { |
| 5600 | * modulus INTEGER, -- n |
| 5601 | * publicExponent INTEGER } -- e |
| 5602 | */ |
| 5603 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5604 | MBEDTLS_ASN1_SEQUENCE | |
| 5605 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5606 | TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
| 5607 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5608 | MBEDTLS_ASN1_INTEGER ) ); |
| 5609 | if( len >= 1 && p[0] == 0 ) |
| 5610 | { |
| 5611 | ++p; |
| 5612 | --len; |
| 5613 | } |
| 5614 | if( e_arg->len == 0 ) |
| 5615 | { |
| 5616 | TEST_EQUAL( len, 3 ); |
| 5617 | TEST_EQUAL( p[0], 1 ); |
| 5618 | TEST_EQUAL( p[1], 0 ); |
| 5619 | TEST_EQUAL( p[2], 1 ); |
| 5620 | } |
| 5621 | else |
| 5622 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5623 | } |
| 5624 | |
| 5625 | exit: |
| 5626 | psa_reset_key_attributes( &attributes ); |
| 5627 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5628 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5629 | mbedtls_free( e_read_buffer ); |
| 5630 | mbedtls_free( exported ); |
| 5631 | } |
| 5632 | /* END_CASE */ |
| 5633 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5634 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5635 | void persistent_key_load_key_from_storage( data_t *data, |
| 5636 | int type_arg, int bits_arg, |
| 5637 | int usage_flags_arg, int alg_arg, |
| 5638 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5639 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 5640 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5641 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5642 | psa_key_handle_t handle = 0; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5643 | psa_key_handle_t base_key = 0; |
| 5644 | psa_key_type_t type = type_arg; |
| 5645 | size_t bits = bits_arg; |
| 5646 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5647 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5648 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5649 | unsigned char *first_export = NULL; |
| 5650 | unsigned char *second_export = NULL; |
| 5651 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 5652 | size_t first_exported_length; |
| 5653 | size_t second_exported_length; |
| 5654 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5655 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5656 | { |
| 5657 | ASSERT_ALLOC( first_export, export_size ); |
| 5658 | ASSERT_ALLOC( second_export, export_size ); |
| 5659 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5660 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5661 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5662 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5663 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5664 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5665 | psa_set_key_algorithm( &attributes, alg ); |
| 5666 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5667 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5668 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5669 | switch( generation_method ) |
| 5670 | { |
| 5671 | case IMPORT_KEY: |
| 5672 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5673 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
| 5674 | &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5675 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5676 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5677 | case GENERATE_KEY: |
| 5678 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5679 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5680 | break; |
| 5681 | |
| 5682 | case DERIVE_KEY: |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5683 | { |
| 5684 | /* Create base key */ |
| 5685 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5686 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5687 | psa_set_key_usage_flags( &base_attributes, |
| 5688 | PSA_KEY_USAGE_DERIVE ); |
| 5689 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5690 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5691 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5692 | data->x, data->len, |
| 5693 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5694 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5695 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5696 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5697 | &operation, |
| 5698 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5699 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5700 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5701 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5702 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5703 | &operation, |
| 5704 | &handle ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5705 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5706 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
| 5707 | base_key = 0; |
| 5708 | } |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5709 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5710 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5711 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5712 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5713 | /* Export the key if permitted by the key policy. */ |
| 5714 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5715 | { |
| 5716 | PSA_ASSERT( psa_export_key( handle, |
| 5717 | first_export, export_size, |
| 5718 | &first_exported_length ) ); |
| 5719 | if( generation_method == IMPORT_KEY ) |
| 5720 | ASSERT_COMPARE( data->x, data->len, |
| 5721 | first_export, first_exported_length ); |
| 5722 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5723 | |
| 5724 | /* Shutdown and restart */ |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 5725 | PSA_ASSERT( psa_close_key( handle ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5726 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5727 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5728 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5729 | /* Check key slot still contains key data */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5730 | PSA_ASSERT( psa_open_key( key_id, &handle ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5731 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 5732 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 5733 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5734 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5735 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5736 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5737 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5738 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 5739 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5740 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5741 | /* Export the key again if permitted by the key policy. */ |
| 5742 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5743 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5744 | PSA_ASSERT( psa_export_key( handle, |
| 5745 | second_export, export_size, |
| 5746 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5747 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5748 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
| 5751 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5752 | if( ! exercise_key( handle, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5753 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5754 | |
| 5755 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5756 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5757 | mbedtls_free( first_export ); |
| 5758 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5759 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5760 | psa_destroy_key( base_key ); |
| 5761 | if( handle == 0 ) |
| 5762 | { |
| 5763 | /* In case there was a test failure after creating the persistent key |
| 5764 | * but while it was not open, try to re-open the persistent key |
| 5765 | * to delete it. */ |
Gilles Peskine | e92c68a | 2020-08-26 00:06:25 +0200 | [diff] [blame] | 5766 | (void) psa_open_key( key_id, &handle ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5767 | } |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5768 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5769 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5770 | } |
| 5771 | /* END_CASE */ |