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