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