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