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