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