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