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