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