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 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
| 7 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 8 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 9 | * uses mbedtls_ctr_drbg internally. */ |
| 10 | #include "mbedtls/ctr_drbg.h" |
| 11 | |
Gilles Peskine | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 12 | #include "psa_crypto_helpers.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 13 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 14 | /* Tests that require more than 128kB of RAM plus change have this symbol |
| 15 | * as a dependency. Currently we always define this symbol, so the tests |
| 16 | * are always executed. In the future we should make this conditional |
| 17 | * so that tests that require a lot of memory are skipped on constrained |
| 18 | * platforms. */ |
Gilles Peskine | 49232e8 | 2019-08-07 11:01:30 +0200 | [diff] [blame] | 19 | #define HAVE_RAM_AVAILABLE_128K |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 20 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 21 | #include "psa/crypto.h" |
| 22 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 23 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 24 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 25 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 26 | /* A hash algorithm that is known to be supported. |
| 27 | * |
| 28 | * This is used in some smoke tests. |
| 29 | */ |
| 30 | #if defined(MBEDTLS_MD2_C) |
| 31 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2 |
| 32 | #elif defined(MBEDTLS_MD4_C) |
| 33 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4 |
| 34 | #elif defined(MBEDTLS_MD5_C) |
| 35 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5 |
| 36 | /* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of |
| 37 | * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160 |
| 38 | * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be |
| 39 | * implausible anyway. */ |
| 40 | #elif defined(MBEDTLS_SHA1_C) |
| 41 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1 |
| 42 | #elif defined(MBEDTLS_SHA256_C) |
| 43 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256 |
| 44 | #elif defined(MBEDTLS_SHA512_C) |
| 45 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384 |
| 46 | #elif defined(MBEDTLS_SHA3_C) |
| 47 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256 |
| 48 | #else |
| 49 | #undef KNOWN_SUPPORTED_HASH_ALG |
| 50 | #endif |
| 51 | |
| 52 | /* A block cipher that is known to be supported. |
| 53 | * |
| 54 | * For simplicity's sake, stick to block ciphers with 16-byte blocks. |
| 55 | */ |
| 56 | #if defined(MBEDTLS_AES_C) |
| 57 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES |
| 58 | #elif defined(MBEDTLS_ARIA_C) |
| 59 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA |
| 60 | #elif defined(MBEDTLS_CAMELLIA_C) |
| 61 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA |
| 62 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER |
| 63 | #endif |
| 64 | |
| 65 | /* A MAC mode that is known to be supported. |
| 66 | * |
| 67 | * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or |
| 68 | * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER. |
| 69 | * |
| 70 | * This is used in some smoke tests. |
| 71 | */ |
| 72 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 73 | #define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) ) |
| 74 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC |
| 75 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C) |
| 76 | #define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC |
| 77 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 78 | #else |
| 79 | #undef KNOWN_SUPPORTED_MAC_ALG |
| 80 | #undef KNOWN_SUPPORTED_MAC_KEY_TYPE |
| 81 | #endif |
| 82 | |
| 83 | /* A cipher algorithm and key type that are known to be supported. |
| 84 | * |
| 85 | * This is used in some smoke tests. |
| 86 | */ |
| 87 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR) |
| 88 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR |
| 89 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 90 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING |
| 91 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB) |
| 92 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB |
| 93 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB) |
| 94 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB |
| 95 | #else |
| 96 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 97 | #endif |
| 98 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG) |
| 99 | #define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 100 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 101 | #elif defined(MBEDTLS_RC4_C) |
| 102 | #define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4 |
| 103 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4 |
| 104 | #else |
| 105 | #undef KNOWN_SUPPORTED_CIPHER_ALG |
| 106 | #undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE |
| 107 | #endif |
| 108 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 109 | /** Test if a buffer contains a constant byte value. |
| 110 | * |
| 111 | * `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] | 112 | * |
| 113 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 114 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 115 | * \param size Size of the buffer in bytes. |
| 116 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 117 | * \return 1 if the buffer is all-bits-zero. |
| 118 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 119 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 120 | 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] | 121 | { |
| 122 | size_t i; |
| 123 | for( i = 0; i < size; i++ ) |
| 124 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 125 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 126 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 127 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 128 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 129 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 130 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 131 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 132 | static int asn1_write_10x( unsigned char **p, |
| 133 | unsigned char *start, |
| 134 | size_t bits, |
| 135 | unsigned char x ) |
| 136 | { |
| 137 | int ret; |
| 138 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 139 | if( bits == 0 ) |
| 140 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 141 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 142 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 143 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 144 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 145 | *p -= len; |
| 146 | ( *p )[len-1] = x; |
| 147 | if( bits % 8 == 0 ) |
| 148 | ( *p )[1] |= 1; |
| 149 | else |
| 150 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 151 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 152 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 153 | MBEDTLS_ASN1_INTEGER ) ); |
| 154 | return( len ); |
| 155 | } |
| 156 | |
| 157 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 158 | size_t buffer_size, |
| 159 | unsigned char **p, |
| 160 | size_t bits, |
| 161 | int keypair ) |
| 162 | { |
| 163 | size_t half_bits = ( bits + 1 ) / 2; |
| 164 | int ret; |
| 165 | int len = 0; |
| 166 | /* Construct something that looks like a DER encoding of |
| 167 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 168 | * RSAPrivateKey ::= SEQUENCE { |
| 169 | * version Version, |
| 170 | * modulus INTEGER, -- n |
| 171 | * publicExponent INTEGER, -- e |
| 172 | * privateExponent INTEGER, -- d |
| 173 | * prime1 INTEGER, -- p |
| 174 | * prime2 INTEGER, -- q |
| 175 | * exponent1 INTEGER, -- d mod (p-1) |
| 176 | * exponent2 INTEGER, -- d mod (q-1) |
| 177 | * coefficient INTEGER, -- (inverse of q) mod p |
| 178 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 179 | * } |
| 180 | * Or, for a public key, the same structure with only |
| 181 | * version, modulus and publicExponent. |
| 182 | */ |
| 183 | *p = buffer + buffer_size; |
| 184 | if( keypair ) |
| 185 | { |
| 186 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 187 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 188 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 189 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 190 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 191 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 192 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 193 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 194 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 195 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 196 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 197 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 198 | } |
| 199 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 200 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 201 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 202 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 203 | if( keypair ) |
| 204 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 205 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 206 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 207 | { |
| 208 | const unsigned char tag = |
| 209 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 210 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 211 | } |
| 212 | return( len ); |
| 213 | } |
| 214 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 215 | int exercise_mac_setup( psa_key_type_t key_type, |
| 216 | const unsigned char *key_bytes, |
| 217 | size_t key_length, |
| 218 | psa_algorithm_t alg, |
| 219 | psa_mac_operation_t *operation, |
| 220 | psa_status_t *status ) |
| 221 | { |
| 222 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 223 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 224 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 225 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 226 | psa_set_key_algorithm( &attributes, alg ); |
| 227 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 228 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 229 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 230 | |
| 231 | *status = psa_mac_sign_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 232 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 233 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 234 | /* If setup failed, reproduce the failure, so that the caller can |
| 235 | * test the resulting state of the operation object. */ |
| 236 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 237 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 238 | TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ), |
| 239 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | psa_destroy_key( handle ); |
| 243 | return( 1 ); |
| 244 | |
| 245 | exit: |
| 246 | psa_destroy_key( handle ); |
| 247 | return( 0 ); |
| 248 | } |
| 249 | |
| 250 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 251 | const unsigned char *key_bytes, |
| 252 | size_t key_length, |
| 253 | psa_algorithm_t alg, |
| 254 | psa_cipher_operation_t *operation, |
| 255 | psa_status_t *status ) |
| 256 | { |
| 257 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 258 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 259 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 260 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 261 | psa_set_key_algorithm( &attributes, alg ); |
| 262 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 263 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 264 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 265 | |
| 266 | *status = psa_cipher_encrypt_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 267 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 268 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 269 | /* If setup failed, reproduce the failure, so that the caller can |
| 270 | * test the resulting state of the operation object. */ |
| 271 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 272 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 273 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ), |
| 274 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | psa_destroy_key( handle ); |
| 278 | return( 1 ); |
| 279 | |
| 280 | exit: |
| 281 | psa_destroy_key( handle ); |
| 282 | return( 0 ); |
| 283 | } |
| 284 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 285 | static int exercise_mac_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 286 | psa_key_usage_t usage, |
| 287 | psa_algorithm_t alg ) |
| 288 | { |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 289 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 290 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 291 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 292 | size_t mac_length = sizeof( mac ); |
| 293 | |
| 294 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 295 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 296 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 297 | handle, alg ) ); |
| 298 | PSA_ASSERT( psa_mac_update( &operation, |
| 299 | input, sizeof( input ) ) ); |
| 300 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 301 | mac, sizeof( mac ), |
| 302 | &mac_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 306 | { |
| 307 | psa_status_t verify_status = |
| 308 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 309 | PSA_SUCCESS : |
| 310 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 311 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 312 | handle, alg ) ); |
| 313 | PSA_ASSERT( psa_mac_update( &operation, |
| 314 | input, sizeof( input ) ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 315 | TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ), |
| 316 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | return( 1 ); |
| 320 | |
| 321 | exit: |
| 322 | psa_mac_abort( &operation ); |
| 323 | return( 0 ); |
| 324 | } |
| 325 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 326 | static int exercise_cipher_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 327 | psa_key_usage_t usage, |
| 328 | psa_algorithm_t alg ) |
| 329 | { |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 330 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 331 | unsigned char iv[16] = {0}; |
| 332 | size_t iv_length = sizeof( iv ); |
| 333 | const unsigned char plaintext[16] = "Hello, world..."; |
| 334 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 335 | size_t ciphertext_length = sizeof( ciphertext ); |
| 336 | unsigned char decrypted[sizeof( ciphertext )]; |
| 337 | size_t part_length; |
| 338 | |
| 339 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 340 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 341 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 342 | handle, alg ) ); |
| 343 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 344 | iv, sizeof( iv ), |
| 345 | &iv_length ) ); |
| 346 | PSA_ASSERT( psa_cipher_update( &operation, |
| 347 | plaintext, sizeof( plaintext ), |
| 348 | ciphertext, sizeof( ciphertext ), |
| 349 | &ciphertext_length ) ); |
| 350 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 351 | ciphertext + ciphertext_length, |
| 352 | sizeof( ciphertext ) - ciphertext_length, |
| 353 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 354 | ciphertext_length += part_length; |
| 355 | } |
| 356 | |
| 357 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 358 | { |
| 359 | psa_status_t status; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 360 | int maybe_invalid_padding = 0; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 361 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 362 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 363 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 364 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 365 | /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't |
| 366 | * have this macro yet. */ |
| 367 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( |
| 368 | psa_get_key_type( &attributes ) ); |
| 369 | maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 370 | } |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 371 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 372 | handle, alg ) ); |
| 373 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 374 | iv, iv_length ) ); |
| 375 | PSA_ASSERT( psa_cipher_update( &operation, |
| 376 | ciphertext, ciphertext_length, |
| 377 | decrypted, sizeof( decrypted ), |
| 378 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 379 | status = psa_cipher_finish( &operation, |
| 380 | decrypted + part_length, |
| 381 | sizeof( decrypted ) - part_length, |
| 382 | &part_length ); |
| 383 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 384 | * if the input is some aribtrary data rather than an actual |
| 385 | ciphertext, a padding error is likely. */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 386 | if( maybe_invalid_padding ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 387 | TEST_ASSERT( status == PSA_SUCCESS || |
| 388 | status == PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 389 | else |
| 390 | PSA_ASSERT( status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return( 1 ); |
| 394 | |
| 395 | exit: |
| 396 | psa_cipher_abort( &operation ); |
| 397 | return( 0 ); |
| 398 | } |
| 399 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 400 | static int exercise_aead_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 401 | psa_key_usage_t usage, |
| 402 | psa_algorithm_t alg ) |
| 403 | { |
| 404 | unsigned char nonce[16] = {0}; |
| 405 | size_t nonce_length = sizeof( nonce ); |
| 406 | unsigned char plaintext[16] = "Hello, world..."; |
| 407 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 408 | size_t ciphertext_length = sizeof( ciphertext ); |
| 409 | size_t plaintext_length = sizeof( ciphertext ); |
| 410 | |
| 411 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 412 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 413 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 414 | nonce, nonce_length, |
| 415 | NULL, 0, |
| 416 | plaintext, sizeof( plaintext ), |
| 417 | ciphertext, sizeof( ciphertext ), |
| 418 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 422 | { |
| 423 | psa_status_t verify_status = |
| 424 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 425 | PSA_SUCCESS : |
| 426 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 427 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 428 | nonce, nonce_length, |
| 429 | NULL, 0, |
| 430 | ciphertext, ciphertext_length, |
| 431 | plaintext, sizeof( plaintext ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 432 | &plaintext_length ), |
| 433 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | return( 1 ); |
| 437 | |
| 438 | exit: |
| 439 | return( 0 ); |
| 440 | } |
| 441 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 442 | static int exercise_signature_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 443 | psa_key_usage_t usage, |
| 444 | psa_algorithm_t alg ) |
| 445 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 446 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 447 | size_t payload_length = 16; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 448 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 449 | size_t signature_length = sizeof( signature ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 450 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 451 | |
| 452 | /* If the policy allows signing with any hash, just pick one. */ |
| 453 | if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH ) |
| 454 | { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 455 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 456 | hash_alg = KNOWN_SUPPORTED_HASH_ALG; |
| 457 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 458 | #else |
| 459 | test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 460 | return( 1 ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 461 | #endif |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 462 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 463 | |
| 464 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 465 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 466 | /* Some algorithms require the payload to have the size of |
| 467 | * the hash encoded in the algorithm. Use this input size |
| 468 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 469 | if( hash_alg != 0 ) |
| 470 | payload_length = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 471 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 472 | payload, payload_length, |
| 473 | signature, sizeof( signature ), |
| 474 | &signature_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 478 | { |
| 479 | psa_status_t verify_status = |
| 480 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 481 | PSA_SUCCESS : |
| 482 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 483 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 484 | payload, payload_length, |
| 485 | signature, signature_length ), |
| 486 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | return( 1 ); |
| 490 | |
| 491 | exit: |
| 492 | return( 0 ); |
| 493 | } |
| 494 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 495 | static int exercise_asymmetric_encryption_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 496 | psa_key_usage_t usage, |
| 497 | psa_algorithm_t alg ) |
| 498 | { |
| 499 | unsigned char plaintext[256] = "Hello, world..."; |
| 500 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 501 | size_t ciphertext_length = sizeof( ciphertext ); |
| 502 | size_t plaintext_length = 16; |
| 503 | |
| 504 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 505 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 506 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 507 | plaintext, plaintext_length, |
| 508 | NULL, 0, |
| 509 | ciphertext, sizeof( ciphertext ), |
| 510 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 514 | { |
| 515 | psa_status_t status = |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 516 | psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 517 | ciphertext, ciphertext_length, |
| 518 | NULL, 0, |
| 519 | plaintext, sizeof( plaintext ), |
| 520 | &plaintext_length ); |
| 521 | TEST_ASSERT( status == PSA_SUCCESS || |
| 522 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 523 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 524 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 525 | } |
| 526 | |
| 527 | return( 1 ); |
| 528 | |
| 529 | exit: |
| 530 | return( 0 ); |
| 531 | } |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 532 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 533 | static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation, |
| 534 | psa_key_handle_t handle, |
| 535 | psa_algorithm_t alg, |
| 536 | unsigned char* input1, size_t input1_length, |
| 537 | unsigned char* input2, size_t input2_length, |
| 538 | size_t capacity ) |
| 539 | { |
| 540 | PSA_ASSERT( psa_key_derivation_setup( operation, alg ) ); |
| 541 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 542 | { |
| 543 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 544 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 545 | input1, input1_length ) ); |
| 546 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 547 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 548 | handle ) ); |
| 549 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 550 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 551 | input2, |
| 552 | input2_length ) ); |
| 553 | } |
| 554 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 555 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 556 | { |
| 557 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 558 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 559 | input1, input1_length ) ); |
| 560 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 561 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 562 | handle ) ); |
| 563 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 564 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 565 | input2, input2_length ) ); |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | TEST_ASSERT( ! "Key derivation algorithm not supported" ); |
| 570 | } |
| 571 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 572 | if( capacity != SIZE_MAX ) |
| 573 | PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) ); |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 574 | |
| 575 | return( 1 ); |
| 576 | |
| 577 | exit: |
| 578 | return( 0 ); |
| 579 | } |
| 580 | |
| 581 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 582 | static int exercise_key_derivation_key( psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 583 | psa_key_usage_t usage, |
| 584 | psa_algorithm_t alg ) |
| 585 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 586 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 587 | unsigned char input1[] = "Input 1"; |
| 588 | size_t input1_length = sizeof( input1 ); |
| 589 | unsigned char input2[] = "Input 2"; |
| 590 | size_t input2_length = sizeof( input2 ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 591 | unsigned char output[1]; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 592 | size_t capacity = sizeof( output ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 593 | |
| 594 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 595 | { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 596 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 597 | input1, input1_length, |
| 598 | input2, input2_length, capacity ) ) |
| 599 | goto exit; |
Gilles Peskine | 7607cd6 | 2019-05-29 17:35:00 +0200 | [diff] [blame] | 600 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 601 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 602 | output, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 603 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 604 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | return( 1 ); |
| 608 | |
| 609 | exit: |
| 610 | return( 0 ); |
| 611 | } |
| 612 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 613 | /* We need two keys to exercise key agreement. Exercise the |
| 614 | * private key against its own public key. */ |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 615 | static psa_status_t key_agreement_with_self( |
| 616 | psa_key_derivation_operation_t *operation, |
| 617 | psa_key_handle_t handle ) |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 618 | { |
| 619 | psa_key_type_t private_key_type; |
| 620 | psa_key_type_t public_key_type; |
| 621 | size_t key_bits; |
| 622 | uint8_t *public_key = NULL; |
| 623 | size_t public_key_length; |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 624 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 625 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 626 | * 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] | 627 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 628 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 629 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 630 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 631 | private_key_type = psa_get_key_type( &attributes ); |
| 632 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 633 | 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] | 634 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 635 | ASSERT_ALLOC( public_key, public_key_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 636 | PSA_ASSERT( psa_export_public_key( handle, |
| 637 | public_key, public_key_length, |
| 638 | &public_key_length ) ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 639 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 640 | status = psa_key_derivation_key_agreement( |
| 641 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle, |
| 642 | public_key, public_key_length ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 643 | exit: |
| 644 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 645 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 646 | return( status ); |
| 647 | } |
| 648 | |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 649 | /* We need two keys to exercise key agreement. Exercise the |
| 650 | * private key against its own public key. */ |
| 651 | static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg, |
| 652 | psa_key_handle_t handle ) |
| 653 | { |
| 654 | psa_key_type_t private_key_type; |
| 655 | psa_key_type_t public_key_type; |
| 656 | size_t key_bits; |
| 657 | uint8_t *public_key = NULL; |
| 658 | size_t public_key_length; |
| 659 | uint8_t output[1024]; |
| 660 | size_t output_length; |
| 661 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 662 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 663 | * 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] | 664 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 665 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 666 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 667 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 668 | private_key_type = psa_get_key_type( &attributes ); |
| 669 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 670 | 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] | 671 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 672 | ASSERT_ALLOC( public_key, public_key_length ); |
| 673 | PSA_ASSERT( psa_export_public_key( handle, |
| 674 | public_key, public_key_length, |
| 675 | &public_key_length ) ); |
| 676 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 677 | status = psa_raw_key_agreement( alg, handle, |
| 678 | public_key, public_key_length, |
| 679 | output, sizeof( output ), &output_length ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 680 | exit: |
| 681 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 682 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 683 | return( status ); |
| 684 | } |
| 685 | |
| 686 | static int exercise_raw_key_agreement_key( psa_key_handle_t handle, |
| 687 | psa_key_usage_t usage, |
| 688 | psa_algorithm_t alg ) |
| 689 | { |
| 690 | int ok = 0; |
| 691 | |
| 692 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 693 | { |
| 694 | /* We need two keys to exercise key agreement. Exercise the |
| 695 | * private key against its own public key. */ |
| 696 | PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) ); |
| 697 | } |
| 698 | ok = 1; |
| 699 | |
| 700 | exit: |
| 701 | return( ok ); |
| 702 | } |
| 703 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 704 | static int exercise_key_agreement_key( psa_key_handle_t handle, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 705 | psa_key_usage_t usage, |
| 706 | psa_algorithm_t alg ) |
| 707 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 708 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 709 | unsigned char output[1]; |
| 710 | int ok = 0; |
| 711 | |
| 712 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 713 | { |
| 714 | /* We need two keys to exercise key agreement. Exercise the |
| 715 | * private key against its own public key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 716 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 717 | PSA_ASSERT( key_agreement_with_self( &operation, handle ) ); |
| 718 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 719 | output, |
| 720 | sizeof( output ) ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 721 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 722 | } |
| 723 | ok = 1; |
| 724 | |
| 725 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 726 | return( ok ); |
| 727 | } |
| 728 | |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 729 | int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 730 | size_t min_bits, size_t max_bits, |
| 731 | int must_be_odd ) |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 732 | { |
| 733 | size_t len; |
| 734 | size_t actual_bits; |
| 735 | unsigned char msb; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 736 | TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 737 | MBEDTLS_ASN1_INTEGER ), |
| 738 | 0 ); |
k-stachowiak | 9b88efc | 2019-09-13 15:26:53 +0200 | [diff] [blame^] | 739 | |
| 740 | /* Check if the retrieved length doesn't extend the actual buffer's size. |
| 741 | * It is assumed here, that end >= p, which validates casting to size_t. */ |
| 742 | TEST_ASSERT( len <= (size_t)( end - *p) ); |
| 743 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 744 | /* Tolerate a slight departure from DER encoding: |
| 745 | * - 0 may be represented by an empty string or a 1-byte string. |
| 746 | * - The sign bit may be used as a value bit. */ |
| 747 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 748 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 749 | { |
| 750 | ++( *p ); |
| 751 | --len; |
| 752 | } |
| 753 | if( min_bits == 0 && len == 0 ) |
| 754 | return( 1 ); |
| 755 | msb = ( *p )[0]; |
| 756 | TEST_ASSERT( msb != 0 ); |
| 757 | actual_bits = 8 * ( len - 1 ); |
| 758 | while( msb != 0 ) |
| 759 | { |
| 760 | msb >>= 1; |
| 761 | ++actual_bits; |
| 762 | } |
| 763 | TEST_ASSERT( actual_bits >= min_bits ); |
| 764 | TEST_ASSERT( actual_bits <= max_bits ); |
| 765 | if( must_be_odd ) |
| 766 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 767 | *p += len; |
| 768 | return( 1 ); |
| 769 | exit: |
| 770 | return( 0 ); |
| 771 | } |
| 772 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 773 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 774 | uint8_t *exported, size_t exported_length ) |
| 775 | { |
| 776 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 777 | TEST_EQUAL( exported_length, ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 778 | else |
| 779 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 780 | |
| 781 | #if defined(MBEDTLS_DES_C) |
| 782 | if( type == PSA_KEY_TYPE_DES ) |
| 783 | { |
| 784 | /* Check the parity bits. */ |
| 785 | unsigned i; |
| 786 | for( i = 0; i < bits / 8; i++ ) |
| 787 | { |
| 788 | unsigned bit_count = 0; |
| 789 | unsigned m; |
| 790 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 791 | { |
| 792 | if( exported[i] & m ) |
| 793 | ++bit_count; |
| 794 | } |
| 795 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 796 | } |
| 797 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 798 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 799 | #endif |
| 800 | |
| 801 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 802 | if( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 803 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 804 | uint8_t *p = exported; |
| 805 | uint8_t *end = exported + exported_length; |
| 806 | size_t len; |
| 807 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 808 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 809 | * modulus INTEGER, -- n |
| 810 | * publicExponent INTEGER, -- e |
| 811 | * privateExponent INTEGER, -- d |
| 812 | * prime1 INTEGER, -- p |
| 813 | * prime2 INTEGER, -- q |
| 814 | * exponent1 INTEGER, -- d mod (p-1) |
| 815 | * exponent2 INTEGER, -- d mod (q-1) |
| 816 | * coefficient INTEGER, -- (inverse of q) mod p |
| 817 | * } |
| 818 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 819 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 820 | MBEDTLS_ASN1_SEQUENCE | |
| 821 | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); |
| 822 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 823 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 824 | goto exit; |
| 825 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 826 | goto exit; |
| 827 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 828 | goto exit; |
| 829 | /* Require d to be at least half the size of n. */ |
| 830 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 831 | goto exit; |
| 832 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 833 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 834 | goto exit; |
| 835 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 836 | goto exit; |
| 837 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 838 | goto exit; |
| 839 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 840 | goto exit; |
| 841 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 842 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 843 | TEST_EQUAL( p, end ); |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 844 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 845 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 846 | #endif /* MBEDTLS_RSA_C */ |
| 847 | |
| 848 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 849 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 850 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 851 | /* Just the secret value */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 852 | TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 853 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 854 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 855 | #endif /* MBEDTLS_ECP_C */ |
| 856 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 857 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 858 | { |
| 859 | uint8_t *p = exported; |
| 860 | uint8_t *end = exported + exported_length; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 861 | #if defined(MBEDTLS_RSA_C) |
| 862 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 863 | { |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 864 | size_t len; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 865 | /* RSAPublicKey ::= SEQUENCE { |
| 866 | * modulus INTEGER, -- n |
| 867 | * publicExponent INTEGER } -- e |
| 868 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 869 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 870 | MBEDTLS_ASN1_SEQUENCE | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 871 | MBEDTLS_ASN1_CONSTRUCTED ), |
| 872 | 0 ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 873 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 874 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 875 | goto exit; |
| 876 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 877 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 878 | TEST_EQUAL( p, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 879 | } |
| 880 | else |
| 881 | #endif /* MBEDTLS_RSA_C */ |
| 882 | #if defined(MBEDTLS_ECP_C) |
| 883 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 884 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 885 | /* The representation of an ECC public key is: |
| 886 | * - The byte 0x04; |
| 887 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 888 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 889 | * - where m is the bit size associated with the curve. |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 890 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 891 | TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end ); |
| 892 | TEST_EQUAL( p[0], 4 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 893 | } |
| 894 | else |
| 895 | #endif /* MBEDTLS_ECP_C */ |
| 896 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 897 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 898 | mbedtls_snprintf( message, sizeof( message ), |
| 899 | "No sanity check for public key type=0x%08lx", |
| 900 | (unsigned long) type ); |
| 901 | test_fail( message, __LINE__, __FILE__ ); |
| 902 | return( 0 ); |
| 903 | } |
| 904 | } |
| 905 | else |
| 906 | |
| 907 | { |
| 908 | /* No sanity checks for other types */ |
| 909 | } |
| 910 | |
| 911 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 912 | |
| 913 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 914 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 915 | } |
| 916 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 917 | static int exercise_export_key( psa_key_handle_t handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 918 | psa_key_usage_t usage ) |
| 919 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 920 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 921 | uint8_t *exported = NULL; |
| 922 | size_t exported_size = 0; |
| 923 | size_t exported_length = 0; |
| 924 | int ok = 0; |
| 925 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 926 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 927 | |
| 928 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 929 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 930 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 931 | TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ), |
| 932 | PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 933 | ok = 1; |
| 934 | goto exit; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 937 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ), |
| 938 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 939 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 940 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 941 | PSA_ASSERT( psa_export_key( handle, |
| 942 | exported, exported_size, |
| 943 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 944 | ok = exported_key_sanity_check( psa_get_key_type( &attributes ), |
| 945 | psa_get_key_bits( &attributes ), |
| 946 | exported, exported_length ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 947 | |
| 948 | exit: |
| 949 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 950 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 951 | return( ok ); |
| 952 | } |
| 953 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 954 | static int exercise_export_public_key( psa_key_handle_t handle ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 955 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 956 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 957 | psa_key_type_t public_type; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 958 | uint8_t *exported = NULL; |
| 959 | size_t exported_size = 0; |
| 960 | size_t exported_length = 0; |
| 961 | int ok = 0; |
| 962 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 963 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 964 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 965 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 966 | TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 967 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 968 | return( 1 ); |
| 969 | } |
| 970 | |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 971 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 972 | psa_get_key_type( &attributes ) ); |
| 973 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, |
| 974 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 975 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 976 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 977 | PSA_ASSERT( psa_export_public_key( handle, |
| 978 | exported, exported_size, |
| 979 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 980 | ok = exported_key_sanity_check( public_type, |
| 981 | psa_get_key_bits( &attributes ), |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 982 | exported, exported_length ); |
| 983 | |
| 984 | exit: |
| 985 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 986 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 987 | return( ok ); |
| 988 | } |
| 989 | |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 990 | /** Do smoke tests on a key. |
| 991 | * |
| 992 | * Perform one of each operation indicated by \p alg (decrypt/encrypt, |
| 993 | * sign/verify, or derivation) that is permitted according to \p usage. |
| 994 | * \p usage and \p alg should correspond to the expected policy on the |
| 995 | * key. |
| 996 | * |
| 997 | * Export the key if permitted by \p usage, and check that the output |
| 998 | * looks sensible. If \p usage forbids export, check that |
| 999 | * \p psa_export_key correctly rejects the attempt. If the key is |
| 1000 | * asymmetric, also check \p psa_export_public_key. |
| 1001 | * |
| 1002 | * If the key fails the tests, this function calls the test framework's |
| 1003 | * `test_fail` function and returns false. Otherwise this function returns |
| 1004 | * true. Therefore it should be used as follows: |
| 1005 | * ``` |
| 1006 | * if( ! exercise_key( ... ) ) goto exit; |
| 1007 | * ``` |
| 1008 | * |
| 1009 | * \param handle The key to exercise. It should be capable of performing |
| 1010 | * \p alg. |
| 1011 | * \param usage The usage flags to assume. |
| 1012 | * \param alg The algorithm to exercise. |
| 1013 | * |
| 1014 | * \retval 0 The key failed the smoke tests. |
| 1015 | * \retval 1 The key passed the smoke tests. |
| 1016 | */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1017 | static int exercise_key( psa_key_handle_t handle, |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1018 | psa_key_usage_t usage, |
| 1019 | psa_algorithm_t alg ) |
| 1020 | { |
| 1021 | int ok; |
| 1022 | if( alg == 0 ) |
| 1023 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 1024 | else if( PSA_ALG_IS_MAC( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1025 | ok = exercise_mac_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1026 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1027 | ok = exercise_cipher_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1028 | else if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1029 | ok = exercise_aead_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1030 | else if( PSA_ALG_IS_SIGN( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1031 | ok = exercise_signature_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1032 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1033 | ok = exercise_asymmetric_encryption_key( handle, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1034 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1035 | ok = exercise_key_derivation_key( handle, usage, alg ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 1036 | else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 1037 | ok = exercise_raw_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1038 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1039 | ok = exercise_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1040 | else |
| 1041 | { |
| 1042 | char message[40]; |
| 1043 | mbedtls_snprintf( message, sizeof( message ), |
| 1044 | "No code to exercise alg=0x%08lx", |
| 1045 | (unsigned long) alg ); |
| 1046 | test_fail( message, __LINE__, __FILE__ ); |
| 1047 | ok = 0; |
| 1048 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1049 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1050 | ok = ok && exercise_export_key( handle, usage ); |
| 1051 | ok = ok && exercise_export_public_key( handle ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1052 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1053 | return( ok ); |
| 1054 | } |
| 1055 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1056 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 1057 | psa_algorithm_t alg ) |
| 1058 | { |
| 1059 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 1060 | { |
| 1061 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1062 | PSA_KEY_USAGE_VERIFY : |
| 1063 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 1064 | } |
| 1065 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 1066 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 1067 | { |
| 1068 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1069 | PSA_KEY_USAGE_ENCRYPT : |
| 1070 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 1071 | } |
| 1072 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 1073 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 1074 | { |
| 1075 | return( PSA_KEY_USAGE_DERIVE ); |
| 1076 | } |
| 1077 | else |
| 1078 | { |
| 1079 | return( 0 ); |
| 1080 | } |
| 1081 | |
| 1082 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1083 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1084 | static int test_operations_on_invalid_handle( psa_key_handle_t handle ) |
| 1085 | { |
| 1086 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1087 | uint8_t buffer[1]; |
| 1088 | size_t length; |
| 1089 | int ok = 0; |
| 1090 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1091 | psa_set_key_id( &attributes, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1092 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 1093 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 1094 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
| 1095 | TEST_EQUAL( psa_get_key_attributes( handle, &attributes ), |
| 1096 | PSA_ERROR_INVALID_HANDLE ); |
| 1097 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 1098 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1099 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1100 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1101 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 1102 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 1103 | |
| 1104 | TEST_EQUAL( psa_export_key( handle, |
| 1105 | buffer, sizeof( buffer ), &length ), |
| 1106 | PSA_ERROR_INVALID_HANDLE ); |
| 1107 | TEST_EQUAL( psa_export_public_key( handle, |
| 1108 | buffer, sizeof( buffer ), &length ), |
| 1109 | PSA_ERROR_INVALID_HANDLE ); |
| 1110 | |
| 1111 | TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE ); |
| 1112 | TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE ); |
| 1113 | |
| 1114 | ok = 1; |
| 1115 | |
| 1116 | exit: |
| 1117 | psa_reset_key_attributes( &attributes ); |
| 1118 | return( ok ); |
| 1119 | } |
| 1120 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1121 | /* Assert that a key isn't reported as having a slot number. */ |
| 1122 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1123 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1124 | do \ |
| 1125 | { \ |
| 1126 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 1127 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 1128 | attributes, \ |
| 1129 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 1130 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 1131 | } \ |
| 1132 | while( 0 ) |
| 1133 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1134 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1135 | ( (void) 0 ) |
| 1136 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1137 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1138 | /* An overapproximation of the amount of storage needed for a key of the |
| 1139 | * given type and with the given content. The API doesn't make it easy |
| 1140 | * to find a good value for the size. The current implementation doesn't |
| 1141 | * care about the value anyway. */ |
| 1142 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 1143 | ( data )->len |
| 1144 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1145 | typedef enum { |
| 1146 | IMPORT_KEY = 0, |
| 1147 | GENERATE_KEY = 1, |
| 1148 | DERIVE_KEY = 2 |
| 1149 | } generate_method; |
| 1150 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1151 | /* END_HEADER */ |
| 1152 | |
| 1153 | /* BEGIN_DEPENDENCIES |
| 1154 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1155 | * END_DEPENDENCIES |
| 1156 | */ |
| 1157 | |
| 1158 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1159 | void static_checks( ) |
| 1160 | { |
| 1161 | size_t max_truncated_mac_size = |
| 1162 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1163 | |
| 1164 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1165 | * encoding. The shifted mask is the maximum truncated value. The |
| 1166 | * untruncated algorithm may be one byte larger. */ |
| 1167 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 1168 | } |
| 1169 | /* END_CASE */ |
| 1170 | |
| 1171 | /* BEGIN_CASE */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1172 | void attributes_set_get( int id_arg, int lifetime_arg, |
| 1173 | int usage_flags_arg, int alg_arg, |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1174 | int type_arg, int bits_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1175 | { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1176 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1177 | psa_key_id_t id = id_arg; |
| 1178 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1179 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 1180 | psa_algorithm_t alg = alg_arg; |
| 1181 | psa_key_type_t type = type_arg; |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1182 | size_t bits = bits_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1183 | |
| 1184 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1185 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1186 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1187 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1188 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1189 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1190 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1191 | psa_set_key_id( &attributes, id ); |
| 1192 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1193 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 1194 | psa_set_key_algorithm( &attributes, alg ); |
| 1195 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1196 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1197 | |
| 1198 | TEST_EQUAL( psa_get_key_id( &attributes ), id ); |
| 1199 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime ); |
| 1200 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 1201 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
| 1202 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1203 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1204 | |
| 1205 | psa_reset_key_attributes( &attributes ); |
| 1206 | |
| 1207 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1208 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1209 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1210 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1211 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1212 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1213 | } |
| 1214 | /* END_CASE */ |
| 1215 | |
| 1216 | /* BEGIN_CASE */ |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1217 | void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg, |
| 1218 | int expected_id_arg, int expected_lifetime_arg ) |
| 1219 | { |
| 1220 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1221 | psa_key_id_t id1 = id1_arg; |
| 1222 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1223 | psa_key_id_t id2 = id2_arg; |
| 1224 | psa_key_id_t expected_id = expected_id_arg; |
| 1225 | psa_key_lifetime_t expected_lifetime = expected_lifetime_arg; |
| 1226 | |
| 1227 | if( id1_arg != -1 ) |
| 1228 | psa_set_key_id( &attributes, id1 ); |
| 1229 | if( lifetime_arg != -1 ) |
| 1230 | psa_set_key_lifetime( &attributes, lifetime ); |
| 1231 | if( id2_arg != -1 ) |
| 1232 | psa_set_key_id( &attributes, id2 ); |
| 1233 | |
| 1234 | TEST_EQUAL( psa_get_key_id( &attributes ), expected_id ); |
| 1235 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime ); |
| 1236 | } |
| 1237 | /* END_CASE */ |
| 1238 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1239 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1240 | void slot_number_attribute( ) |
| 1241 | { |
| 1242 | psa_key_slot_number_t slot_number = 0xdeadbeef; |
| 1243 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1244 | |
| 1245 | /* Initially, there is no slot number. */ |
| 1246 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1247 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1248 | |
| 1249 | /* Test setting a slot number. */ |
| 1250 | psa_set_key_slot_number( &attributes, 0 ); |
| 1251 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1252 | TEST_EQUAL( slot_number, 0 ); |
| 1253 | |
| 1254 | /* Test changing the slot number. */ |
| 1255 | psa_set_key_slot_number( &attributes, 42 ); |
| 1256 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1257 | TEST_EQUAL( slot_number, 42 ); |
| 1258 | |
| 1259 | /* Test clearing the slot number. */ |
| 1260 | psa_clear_key_slot_number( &attributes ); |
| 1261 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1262 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1263 | |
| 1264 | /* Clearing again should have no effect. */ |
| 1265 | psa_clear_key_slot_number( &attributes ); |
| 1266 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1267 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1268 | |
| 1269 | /* Test that reset clears the slot number. */ |
| 1270 | psa_set_key_slot_number( &attributes, 42 ); |
| 1271 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1272 | TEST_EQUAL( slot_number, 42 ); |
| 1273 | psa_reset_key_attributes( &attributes ); |
| 1274 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1275 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1276 | } |
| 1277 | /* END_CASE */ |
| 1278 | |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1279 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1280 | void import_with_policy( int type_arg, |
| 1281 | int usage_arg, int alg_arg, |
| 1282 | int expected_status_arg ) |
| 1283 | { |
| 1284 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1285 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1286 | psa_key_handle_t handle = 0; |
| 1287 | psa_key_type_t type = type_arg; |
| 1288 | psa_key_usage_t usage = usage_arg; |
| 1289 | psa_algorithm_t alg = alg_arg; |
| 1290 | psa_status_t expected_status = expected_status_arg; |
| 1291 | const uint8_t key_material[16] = {0}; |
| 1292 | psa_status_t status; |
| 1293 | |
| 1294 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1295 | |
| 1296 | psa_set_key_type( &attributes, type ); |
| 1297 | psa_set_key_usage_flags( &attributes, usage ); |
| 1298 | psa_set_key_algorithm( &attributes, alg ); |
| 1299 | |
| 1300 | status = psa_import_key( &attributes, |
| 1301 | key_material, sizeof( key_material ), |
| 1302 | &handle ); |
| 1303 | TEST_EQUAL( status, expected_status ); |
| 1304 | if( status != PSA_SUCCESS ) |
| 1305 | goto exit; |
| 1306 | |
| 1307 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1308 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1309 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1310 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1311 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1312 | |
| 1313 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1314 | test_operations_on_invalid_handle( handle ); |
| 1315 | |
| 1316 | exit: |
| 1317 | psa_destroy_key( handle ); |
| 1318 | psa_reset_key_attributes( &got_attributes ); |
| 1319 | PSA_DONE( ); |
| 1320 | } |
| 1321 | /* END_CASE */ |
| 1322 | |
| 1323 | /* BEGIN_CASE */ |
| 1324 | void import_with_data( data_t *data, int type_arg, |
| 1325 | int attr_bits_arg, |
| 1326 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1327 | { |
| 1328 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1329 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1330 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1331 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1332 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1333 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1334 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1335 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1336 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1337 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1338 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1339 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1340 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1341 | status = psa_import_key( &attributes, data->x, data->len, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1342 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1343 | if( status != PSA_SUCCESS ) |
| 1344 | goto exit; |
| 1345 | |
| 1346 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1347 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1348 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1349 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1350 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1351 | |
| 1352 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1353 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1354 | |
| 1355 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1356 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1357 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1358 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1359 | } |
| 1360 | /* END_CASE */ |
| 1361 | |
| 1362 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1363 | void import_large_key( int type_arg, int byte_size_arg, |
| 1364 | int expected_status_arg ) |
| 1365 | { |
| 1366 | psa_key_type_t type = type_arg; |
| 1367 | size_t byte_size = byte_size_arg; |
| 1368 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1369 | psa_status_t expected_status = expected_status_arg; |
| 1370 | psa_key_handle_t handle = 0; |
| 1371 | psa_status_t status; |
| 1372 | uint8_t *buffer = NULL; |
| 1373 | size_t buffer_size = byte_size + 1; |
| 1374 | size_t n; |
| 1375 | |
| 1376 | /* It would be better to skip the test than fail it if the allocation |
| 1377 | * fails, but the test framework doesn't support this yet. */ |
| 1378 | ASSERT_ALLOC( buffer, buffer_size ); |
| 1379 | memset( buffer, 'K', byte_size ); |
| 1380 | |
| 1381 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1382 | |
| 1383 | /* Try importing the key */ |
| 1384 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1385 | psa_set_key_type( &attributes, type ); |
| 1386 | status = psa_import_key( &attributes, buffer, byte_size, &handle ); |
| 1387 | TEST_EQUAL( status, expected_status ); |
| 1388 | |
| 1389 | if( status == PSA_SUCCESS ) |
| 1390 | { |
| 1391 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1392 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1393 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1394 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1395 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1396 | memset( buffer, 0, byte_size + 1 ); |
| 1397 | PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) ); |
| 1398 | for( n = 0; n < byte_size; n++ ) |
| 1399 | TEST_EQUAL( buffer[n], 'K' ); |
| 1400 | for( n = byte_size; n < buffer_size; n++ ) |
| 1401 | TEST_EQUAL( buffer[n], 0 ); |
| 1402 | } |
| 1403 | |
| 1404 | exit: |
| 1405 | psa_destroy_key( handle ); |
| 1406 | PSA_DONE( ); |
| 1407 | mbedtls_free( buffer ); |
| 1408 | } |
| 1409 | /* END_CASE */ |
| 1410 | |
| 1411 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1412 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1413 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1414 | psa_key_handle_t handle = 0; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1415 | size_t bits = bits_arg; |
| 1416 | psa_status_t expected_status = expected_status_arg; |
| 1417 | psa_status_t status; |
| 1418 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1419 | 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] | 1420 | size_t buffer_size = /* Slight overapproximations */ |
| 1421 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1422 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1423 | unsigned char *p; |
| 1424 | int ret; |
| 1425 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1426 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1427 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1428 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1429 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1430 | |
| 1431 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1432 | bits, keypair ) ) >= 0 ); |
| 1433 | length = ret; |
| 1434 | |
| 1435 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1436 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1437 | status = psa_import_key( &attributes, p, length, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1438 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1439 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1440 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1441 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1442 | |
| 1443 | exit: |
| 1444 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1445 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1446 | } |
| 1447 | /* END_CASE */ |
| 1448 | |
| 1449 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1450 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1451 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1452 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1453 | int expected_bits, |
| 1454 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1455 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1456 | int canonical_input ) |
| 1457 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1458 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1459 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1460 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1461 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1462 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1463 | unsigned char *exported = NULL; |
| 1464 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1465 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1466 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1467 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1468 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1469 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1470 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1471 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1472 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1473 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1474 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1475 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1476 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1477 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1478 | psa_set_key_algorithm( &attributes, alg ); |
| 1479 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1480 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1481 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1482 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1483 | |
| 1484 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1485 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1486 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1487 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1488 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1489 | |
| 1490 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1491 | status = psa_export_key( handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1492 | exported, export_size, |
| 1493 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1494 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1495 | |
| 1496 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1497 | * and export_size. On errors, the exported length must be 0. */ |
| 1498 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1499 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1500 | TEST_ASSERT( exported_length <= export_size ); |
| 1501 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1502 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1503 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1504 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1505 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1506 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1507 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1508 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1509 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1510 | if( ! exercise_export_key( handle, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1511 | goto exit; |
| 1512 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1513 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1514 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1515 | else |
| 1516 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1517 | psa_key_handle_t handle2; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1518 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
| 1519 | &handle2 ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1520 | PSA_ASSERT( psa_export_key( handle2, |
| 1521 | reexported, |
| 1522 | export_size, |
| 1523 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1524 | ASSERT_COMPARE( exported, exported_length, |
| 1525 | reexported, reexported_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1526 | PSA_ASSERT( psa_close_key( handle2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1527 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1528 | 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] | 1529 | |
| 1530 | destroy: |
| 1531 | /* Destroy the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1532 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1533 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1534 | |
| 1535 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1536 | mbedtls_free( exported ); |
| 1537 | mbedtls_free( reexported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1538 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1539 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1540 | } |
| 1541 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1542 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1543 | /* BEGIN_CASE */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1544 | void invalid_handle( int handle ) |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1545 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1546 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1547 | test_operations_on_invalid_handle( handle ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1548 | |
| 1549 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1550 | PSA_DONE( ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1551 | } |
| 1552 | /* END_CASE */ |
| 1553 | |
| 1554 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1555 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1556 | int type_arg, |
| 1557 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1558 | int export_size_delta, |
| 1559 | int expected_export_status_arg, |
| 1560 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1561 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1562 | psa_key_handle_t handle = 0; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1563 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1564 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1565 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1566 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1567 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1568 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1569 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1570 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1571 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1572 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1573 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1574 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1575 | psa_set_key_algorithm( &attributes, alg ); |
| 1576 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1577 | |
| 1578 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1579 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1580 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1581 | /* Export the public key */ |
| 1582 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1583 | status = psa_export_public_key( handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1584 | exported, export_size, |
| 1585 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1586 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1587 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1588 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1589 | 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] | 1590 | size_t bits; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1591 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1592 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1593 | TEST_ASSERT( expected_public_key->len <= |
| 1594 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1595 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1596 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1597 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1598 | |
| 1599 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1600 | mbedtls_free( exported ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1601 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1602 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1603 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1604 | } |
| 1605 | /* END_CASE */ |
| 1606 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1607 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1608 | void import_and_exercise_key( data_t *data, |
| 1609 | int type_arg, |
| 1610 | int bits_arg, |
| 1611 | int alg_arg ) |
| 1612 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1613 | psa_key_handle_t handle = 0; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1614 | psa_key_type_t type = type_arg; |
| 1615 | size_t bits = bits_arg; |
| 1616 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1617 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1618 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1619 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1620 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1621 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1622 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1623 | psa_set_key_usage_flags( &attributes, usage ); |
| 1624 | psa_set_key_algorithm( &attributes, alg ); |
| 1625 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1626 | |
| 1627 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1628 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1629 | |
| 1630 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1631 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1632 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1633 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1634 | |
| 1635 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1636 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1637 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1638 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1639 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1640 | test_operations_on_invalid_handle( handle ); |
| 1641 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1642 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1643 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1644 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1645 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1646 | } |
| 1647 | /* END_CASE */ |
| 1648 | |
| 1649 | /* BEGIN_CASE */ |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1650 | void key_policy( int usage_arg, int alg_arg ) |
| 1651 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1652 | psa_key_handle_t handle = 0; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1653 | psa_algorithm_t alg = alg_arg; |
| 1654 | psa_key_usage_t usage = usage_arg; |
| 1655 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 1656 | unsigned char key[32] = {0}; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1657 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1658 | |
| 1659 | memset( key, 0x2a, sizeof( key ) ); |
| 1660 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1661 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1662 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1663 | psa_set_key_usage_flags( &attributes, usage ); |
| 1664 | psa_set_key_algorithm( &attributes, alg ); |
| 1665 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1666 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1667 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1668 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1669 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1670 | TEST_EQUAL( psa_get_key_type( &attributes ), key_type ); |
| 1671 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage ); |
| 1672 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1673 | |
| 1674 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1675 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1676 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1677 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1678 | } |
| 1679 | /* END_CASE */ |
| 1680 | |
| 1681 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1682 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1683 | { |
| 1684 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1685 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1686 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1687 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1688 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1689 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1690 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1691 | |
| 1692 | memset( &zero, 0, sizeof( zero ) ); |
| 1693 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1694 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1695 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1696 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1697 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1698 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1699 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1700 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1701 | |
| 1702 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1703 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1704 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1705 | |
| 1706 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1707 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1708 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1709 | |
| 1710 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1711 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1712 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1713 | } |
| 1714 | /* END_CASE */ |
| 1715 | |
| 1716 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1717 | void mac_key_policy( int policy_usage, |
| 1718 | int policy_alg, |
| 1719 | int key_type, |
| 1720 | data_t *key_data, |
| 1721 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1722 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1723 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1724 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1725 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1726 | psa_status_t status; |
| 1727 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1728 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1729 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1730 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1731 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1732 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1733 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1734 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1735 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1736 | &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1737 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1738 | status = psa_mac_sign_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1739 | if( policy_alg == exercise_alg && |
| 1740 | ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1741 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1742 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1743 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1744 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1745 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1746 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1747 | status = psa_mac_verify_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1748 | if( policy_alg == exercise_alg && |
| 1749 | ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1750 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1751 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1752 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1753 | |
| 1754 | exit: |
| 1755 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1756 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1757 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1758 | } |
| 1759 | /* END_CASE */ |
| 1760 | |
| 1761 | /* BEGIN_CASE */ |
| 1762 | void cipher_key_policy( int policy_usage, |
| 1763 | int policy_alg, |
| 1764 | int key_type, |
| 1765 | data_t *key_data, |
| 1766 | int exercise_alg ) |
| 1767 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1768 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1769 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1770 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1771 | psa_status_t status; |
| 1772 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1773 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1774 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1775 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1776 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1777 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1778 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1779 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1780 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1781 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1782 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1783 | if( policy_alg == exercise_alg && |
| 1784 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1785 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1786 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1787 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1788 | psa_cipher_abort( &operation ); |
| 1789 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1790 | status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1791 | if( policy_alg == exercise_alg && |
| 1792 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1793 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1794 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1795 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1796 | |
| 1797 | exit: |
| 1798 | psa_cipher_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1799 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1800 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1801 | } |
| 1802 | /* END_CASE */ |
| 1803 | |
| 1804 | /* BEGIN_CASE */ |
| 1805 | void aead_key_policy( int policy_usage, |
| 1806 | int policy_alg, |
| 1807 | int key_type, |
| 1808 | data_t *key_data, |
| 1809 | int nonce_length_arg, |
| 1810 | int tag_length_arg, |
| 1811 | int exercise_alg ) |
| 1812 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1813 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1814 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1815 | psa_status_t status; |
| 1816 | unsigned char nonce[16] = {0}; |
| 1817 | size_t nonce_length = nonce_length_arg; |
| 1818 | unsigned char tag[16]; |
| 1819 | size_t tag_length = tag_length_arg; |
| 1820 | size_t output_length; |
| 1821 | |
| 1822 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1823 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1824 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1825 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1826 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1827 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1828 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1829 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1830 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1831 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1832 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1833 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1834 | status = psa_aead_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1835 | nonce, nonce_length, |
| 1836 | NULL, 0, |
| 1837 | NULL, 0, |
| 1838 | tag, tag_length, |
| 1839 | &output_length ); |
| 1840 | if( policy_alg == exercise_alg && |
| 1841 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1842 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1843 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1844 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1845 | |
| 1846 | memset( tag, 0, sizeof( tag ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1847 | status = psa_aead_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1848 | nonce, nonce_length, |
| 1849 | NULL, 0, |
| 1850 | tag, tag_length, |
| 1851 | NULL, 0, |
| 1852 | &output_length ); |
| 1853 | if( policy_alg == exercise_alg && |
| 1854 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1855 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1856 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1857 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1858 | |
| 1859 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1860 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1861 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1862 | } |
| 1863 | /* END_CASE */ |
| 1864 | |
| 1865 | /* BEGIN_CASE */ |
| 1866 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1867 | int policy_alg, |
| 1868 | int key_type, |
| 1869 | data_t *key_data, |
| 1870 | int exercise_alg ) |
| 1871 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1872 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1873 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1874 | psa_status_t status; |
| 1875 | size_t key_bits; |
| 1876 | size_t buffer_length; |
| 1877 | unsigned char *buffer = NULL; |
| 1878 | size_t output_length; |
| 1879 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1880 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1881 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1882 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1883 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1884 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1885 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1886 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1887 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1888 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1889 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1890 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1891 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1892 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1893 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1894 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1895 | status = psa_asymmetric_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1896 | NULL, 0, |
| 1897 | NULL, 0, |
| 1898 | buffer, buffer_length, |
| 1899 | &output_length ); |
| 1900 | if( policy_alg == exercise_alg && |
| 1901 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1902 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1903 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1904 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1905 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1906 | if( buffer_length != 0 ) |
| 1907 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1908 | status = psa_asymmetric_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1909 | buffer, buffer_length, |
| 1910 | NULL, 0, |
| 1911 | buffer, buffer_length, |
| 1912 | &output_length ); |
| 1913 | if( policy_alg == exercise_alg && |
| 1914 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1915 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1916 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1917 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1918 | |
| 1919 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1920 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1921 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1922 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1923 | mbedtls_free( buffer ); |
| 1924 | } |
| 1925 | /* END_CASE */ |
| 1926 | |
| 1927 | /* BEGIN_CASE */ |
| 1928 | void asymmetric_signature_key_policy( int policy_usage, |
| 1929 | int policy_alg, |
| 1930 | int key_type, |
| 1931 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1932 | int exercise_alg, |
| 1933 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1934 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1935 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1936 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1937 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1938 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1939 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1940 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1941 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1942 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1943 | int compatible_alg = payload_length_arg > 0; |
| 1944 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1945 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
| 1946 | size_t signature_length; |
| 1947 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1948 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1949 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1950 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1951 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1952 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1953 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1954 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1955 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1956 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1957 | status = psa_asymmetric_sign( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1958 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1959 | signature, sizeof( signature ), |
| 1960 | &signature_length ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1961 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1962 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1963 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1964 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1965 | |
| 1966 | memset( signature, 0, sizeof( signature ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1967 | status = psa_asymmetric_verify( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1968 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1969 | signature, sizeof( signature ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1970 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1971 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1972 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1973 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1974 | |
| 1975 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1976 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1977 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1978 | } |
| 1979 | /* END_CASE */ |
| 1980 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1981 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1982 | void derive_key_policy( int policy_usage, |
| 1983 | int policy_alg, |
| 1984 | int key_type, |
| 1985 | data_t *key_data, |
| 1986 | int exercise_alg ) |
| 1987 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1988 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1989 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1990 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1991 | psa_status_t status; |
| 1992 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1993 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1994 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1995 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1996 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1997 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1998 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1999 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2000 | &handle ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2001 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2002 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2003 | |
| 2004 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2005 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2006 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2007 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2008 | &operation, |
| 2009 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2010 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2011 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2012 | |
| 2013 | status = psa_key_derivation_input_key( &operation, |
| 2014 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2015 | handle ); |
| 2016 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2017 | if( policy_alg == exercise_alg && |
| 2018 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2019 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2020 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2021 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2022 | |
| 2023 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2024 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2025 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2026 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2027 | } |
| 2028 | /* END_CASE */ |
| 2029 | |
| 2030 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2031 | void agreement_key_policy( int policy_usage, |
| 2032 | int policy_alg, |
| 2033 | int key_type_arg, |
| 2034 | data_t *key_data, |
| 2035 | int exercise_alg ) |
| 2036 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2037 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2038 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2039 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2040 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2041 | psa_status_t status; |
| 2042 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2043 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2044 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2045 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2046 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2047 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2048 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2049 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2050 | &handle ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2051 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2052 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2053 | status = key_agreement_with_self( &operation, handle ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2054 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2055 | if( policy_alg == exercise_alg && |
| 2056 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2057 | PSA_ASSERT( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2058 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2059 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2060 | |
| 2061 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2062 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2063 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2064 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2065 | } |
| 2066 | /* END_CASE */ |
| 2067 | |
| 2068 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2069 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2070 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2071 | { |
| 2072 | psa_key_handle_t handle = 0; |
| 2073 | psa_key_type_t key_type = key_type_arg; |
| 2074 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2075 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2076 | psa_key_usage_t usage = usage_arg; |
| 2077 | psa_algorithm_t alg = alg_arg; |
| 2078 | psa_algorithm_t alg2 = alg2_arg; |
| 2079 | |
| 2080 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2081 | |
| 2082 | psa_set_key_usage_flags( &attributes, usage ); |
| 2083 | psa_set_key_algorithm( &attributes, alg ); |
| 2084 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2085 | psa_set_key_type( &attributes, key_type ); |
| 2086 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2087 | &handle ) ); |
| 2088 | |
| 2089 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 2090 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2091 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2092 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2093 | |
| 2094 | if( ! exercise_key( handle, usage, alg ) ) |
| 2095 | goto exit; |
| 2096 | if( ! exercise_key( handle, usage, alg2 ) ) |
| 2097 | goto exit; |
| 2098 | |
| 2099 | exit: |
| 2100 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2101 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2102 | } |
| 2103 | /* END_CASE */ |
| 2104 | |
| 2105 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2106 | void raw_agreement_key_policy( int policy_usage, |
| 2107 | int policy_alg, |
| 2108 | int key_type_arg, |
| 2109 | data_t *key_data, |
| 2110 | int exercise_alg ) |
| 2111 | { |
| 2112 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2113 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2114 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2115 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2116 | psa_status_t status; |
| 2117 | |
| 2118 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2119 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2120 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2121 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2122 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2123 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2124 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2125 | &handle ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2126 | |
| 2127 | status = raw_key_agreement_with_self( exercise_alg, handle ); |
| 2128 | |
| 2129 | if( policy_alg == exercise_alg && |
| 2130 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 2131 | PSA_ASSERT( status ); |
| 2132 | else |
| 2133 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2134 | |
| 2135 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2136 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2137 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2138 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2139 | } |
| 2140 | /* END_CASE */ |
| 2141 | |
| 2142 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2143 | void copy_success( int source_usage_arg, |
| 2144 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2145 | int type_arg, data_t *material, |
| 2146 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2147 | int target_usage_arg, |
| 2148 | int target_alg_arg, int target_alg2_arg, |
| 2149 | int expected_usage_arg, |
| 2150 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2151 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2152 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2153 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2154 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2155 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2156 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2157 | psa_key_handle_t source_handle = 0; |
| 2158 | psa_key_handle_t target_handle = 0; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2159 | uint8_t *export_buffer = NULL; |
| 2160 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2161 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2162 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2163 | /* Prepare the source key. */ |
| 2164 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2165 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2166 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2167 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2168 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2169 | material->x, material->len, |
| 2170 | &source_handle ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2171 | PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2172 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2173 | /* Prepare the target attributes. */ |
| 2174 | if( copy_attributes ) |
| 2175 | target_attributes = source_attributes; |
| 2176 | if( target_usage_arg != -1 ) |
| 2177 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2178 | if( target_alg_arg != -1 ) |
| 2179 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2180 | if( target_alg2_arg != -1 ) |
| 2181 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2182 | |
| 2183 | /* Copy the key. */ |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2184 | PSA_ASSERT( psa_copy_key( source_handle, |
| 2185 | &target_attributes, &target_handle ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2186 | |
| 2187 | /* Destroy the source to ensure that this doesn't affect the target. */ |
| 2188 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2189 | |
| 2190 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2191 | PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) ); |
| 2192 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2193 | psa_get_key_type( &target_attributes ) ); |
| 2194 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2195 | psa_get_key_bits( &target_attributes ) ); |
| 2196 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2197 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2198 | TEST_EQUAL( expected_alg2, |
| 2199 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2200 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2201 | { |
| 2202 | size_t length; |
| 2203 | ASSERT_ALLOC( export_buffer, material->len ); |
| 2204 | PSA_ASSERT( psa_export_key( target_handle, export_buffer, |
| 2205 | material->len, &length ) ); |
| 2206 | ASSERT_COMPARE( material->x, material->len, |
| 2207 | export_buffer, length ); |
| 2208 | } |
| 2209 | if( ! exercise_key( target_handle, expected_usage, expected_alg ) ) |
| 2210 | goto exit; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2211 | if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) ) |
| 2212 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2213 | |
| 2214 | PSA_ASSERT( psa_close_key( target_handle ) ); |
| 2215 | |
| 2216 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2217 | psa_reset_key_attributes( &source_attributes ); |
| 2218 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2219 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2220 | mbedtls_free( export_buffer ); |
| 2221 | } |
| 2222 | /* END_CASE */ |
| 2223 | |
| 2224 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2225 | void copy_fail( int source_usage_arg, |
| 2226 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2227 | int type_arg, data_t *material, |
| 2228 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2229 | int target_usage_arg, |
| 2230 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2231 | int expected_status_arg ) |
| 2232 | { |
| 2233 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2234 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2235 | psa_key_handle_t source_handle = 0; |
| 2236 | psa_key_handle_t target_handle = 0; |
| 2237 | |
| 2238 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2239 | |
| 2240 | /* Prepare the source key. */ |
| 2241 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2242 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2243 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2244 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2245 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2246 | material->x, material->len, |
| 2247 | &source_handle ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2248 | |
| 2249 | /* Prepare the target attributes. */ |
| 2250 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2251 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2252 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2253 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2254 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2255 | |
| 2256 | /* Try to copy the key. */ |
| 2257 | TEST_EQUAL( psa_copy_key( source_handle, |
| 2258 | &target_attributes, &target_handle ), |
| 2259 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2260 | |
| 2261 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2262 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2263 | exit: |
| 2264 | psa_reset_key_attributes( &source_attributes ); |
| 2265 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2266 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2267 | } |
| 2268 | /* END_CASE */ |
| 2269 | |
| 2270 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2271 | void hash_operation_init( ) |
| 2272 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2273 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2274 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2275 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2276 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2277 | * to supress the Clang warning for the test. */ |
| 2278 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2279 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2280 | psa_hash_operation_t zero; |
| 2281 | |
| 2282 | memset( &zero, 0, sizeof( zero ) ); |
| 2283 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2284 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2285 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2286 | PSA_ERROR_BAD_STATE ); |
| 2287 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2288 | PSA_ERROR_BAD_STATE ); |
| 2289 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2290 | PSA_ERROR_BAD_STATE ); |
| 2291 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2292 | /* A default hash operation should be abortable without error. */ |
| 2293 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2294 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2295 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2296 | } |
| 2297 | /* END_CASE */ |
| 2298 | |
| 2299 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2300 | void hash_setup( int alg_arg, |
| 2301 | int expected_status_arg ) |
| 2302 | { |
| 2303 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2304 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2305 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2306 | psa_status_t status; |
| 2307 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2308 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2309 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2310 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2311 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2312 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2313 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2314 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2315 | |
| 2316 | /* If setup failed, reproduce the failure, so as to |
| 2317 | * test the resulting state of the operation object. */ |
| 2318 | if( status != PSA_SUCCESS ) |
| 2319 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2320 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2321 | /* Now the operation object should be reusable. */ |
| 2322 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2323 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2324 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2325 | #endif |
| 2326 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2327 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2328 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2329 | } |
| 2330 | /* END_CASE */ |
| 2331 | |
| 2332 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2333 | void hash_bad_order( ) |
| 2334 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2335 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2336 | unsigned char input[] = ""; |
| 2337 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2338 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2339 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2340 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2341 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2342 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2343 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2344 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2345 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2346 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2347 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2348 | /* Call setup twice in a row. */ |
| 2349 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2350 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2351 | PSA_ERROR_BAD_STATE ); |
| 2352 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2353 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2354 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2355 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2356 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2357 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2358 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2359 | /* Call update after finish. */ |
| 2360 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2361 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2362 | hash, sizeof( hash ), &hash_len ) ); |
| 2363 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2364 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2365 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2366 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2367 | /* Call verify without calling setup beforehand. */ |
| 2368 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2369 | valid_hash, sizeof( valid_hash ) ), |
| 2370 | PSA_ERROR_BAD_STATE ); |
| 2371 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2372 | |
| 2373 | /* Call verify after finish. */ |
| 2374 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2375 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2376 | hash, sizeof( hash ), &hash_len ) ); |
| 2377 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2378 | valid_hash, sizeof( valid_hash ) ), |
| 2379 | PSA_ERROR_BAD_STATE ); |
| 2380 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2381 | |
| 2382 | /* Call verify twice in a row. */ |
| 2383 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2384 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2385 | valid_hash, sizeof( valid_hash ) ) ); |
| 2386 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2387 | valid_hash, sizeof( valid_hash ) ), |
| 2388 | PSA_ERROR_BAD_STATE ); |
| 2389 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2390 | |
| 2391 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2392 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2393 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2394 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2395 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2396 | |
| 2397 | /* Call finish twice in a row. */ |
| 2398 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2399 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2400 | hash, sizeof( hash ), &hash_len ) ); |
| 2401 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2402 | hash, sizeof( hash ), &hash_len ), |
| 2403 | PSA_ERROR_BAD_STATE ); |
| 2404 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2405 | |
| 2406 | /* Call finish after calling verify. */ |
| 2407 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2408 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2409 | valid_hash, sizeof( valid_hash ) ) ); |
| 2410 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2411 | hash, sizeof( hash ), &hash_len ), |
| 2412 | PSA_ERROR_BAD_STATE ); |
| 2413 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2414 | |
| 2415 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2416 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2417 | } |
| 2418 | /* END_CASE */ |
| 2419 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2420 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2421 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2422 | { |
| 2423 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2424 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2425 | * appended to it */ |
| 2426 | unsigned char hash[] = { |
| 2427 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2428 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2429 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2430 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2431 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2432 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2433 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2434 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2435 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2436 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2437 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2438 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2439 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2440 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2441 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2442 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2443 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2444 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2445 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2446 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2447 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2448 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2449 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2450 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2451 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2452 | } |
| 2453 | /* END_CASE */ |
| 2454 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2455 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2456 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2457 | { |
| 2458 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2459 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2460 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2461 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2462 | size_t hash_len; |
| 2463 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2464 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2465 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2466 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2467 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2468 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2469 | hash, expected_size - 1, &hash_len ), |
| 2470 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2471 | |
| 2472 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2473 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2474 | } |
| 2475 | /* END_CASE */ |
| 2476 | |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2477 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2478 | void hash_clone_source_state( ) |
| 2479 | { |
| 2480 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2481 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2482 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2483 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2484 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2485 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2486 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2487 | size_t hash_len; |
| 2488 | |
| 2489 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2490 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2491 | |
| 2492 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2493 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2494 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2495 | hash, sizeof( hash ), &hash_len ) ); |
| 2496 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2497 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2498 | |
| 2499 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2500 | PSA_ERROR_BAD_STATE ); |
| 2501 | |
| 2502 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2503 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2504 | hash, sizeof( hash ), &hash_len ) ); |
| 2505 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2506 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2507 | hash, sizeof( hash ), &hash_len ) ); |
| 2508 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2509 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2510 | hash, sizeof( hash ), &hash_len ) ); |
| 2511 | |
| 2512 | exit: |
| 2513 | psa_hash_abort( &op_source ); |
| 2514 | psa_hash_abort( &op_init ); |
| 2515 | psa_hash_abort( &op_setup ); |
| 2516 | psa_hash_abort( &op_finished ); |
| 2517 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2518 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2519 | } |
| 2520 | /* END_CASE */ |
| 2521 | |
| 2522 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2523 | void hash_clone_target_state( ) |
| 2524 | { |
| 2525 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2526 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2527 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2528 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2529 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2530 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2531 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2532 | size_t hash_len; |
| 2533 | |
| 2534 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2535 | |
| 2536 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2537 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2538 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2539 | hash, sizeof( hash ), &hash_len ) ); |
| 2540 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2541 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2542 | |
| 2543 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2544 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2545 | hash, sizeof( hash ), &hash_len ) ); |
| 2546 | |
| 2547 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2548 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2549 | PSA_ERROR_BAD_STATE ); |
| 2550 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2551 | PSA_ERROR_BAD_STATE ); |
| 2552 | |
| 2553 | exit: |
| 2554 | psa_hash_abort( &op_target ); |
| 2555 | psa_hash_abort( &op_init ); |
| 2556 | psa_hash_abort( &op_setup ); |
| 2557 | psa_hash_abort( &op_finished ); |
| 2558 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2559 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2560 | } |
| 2561 | /* END_CASE */ |
| 2562 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2563 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2564 | void mac_operation_init( ) |
| 2565 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2566 | const uint8_t input[1] = { 0 }; |
| 2567 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2568 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2569 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2570 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2571 | * to supress the Clang warning for the test. */ |
| 2572 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2573 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2574 | psa_mac_operation_t zero; |
| 2575 | |
| 2576 | memset( &zero, 0, sizeof( zero ) ); |
| 2577 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2578 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2579 | TEST_EQUAL( psa_mac_update( &func, |
| 2580 | input, sizeof( input ) ), |
| 2581 | PSA_ERROR_BAD_STATE ); |
| 2582 | TEST_EQUAL( psa_mac_update( &init, |
| 2583 | input, sizeof( input ) ), |
| 2584 | PSA_ERROR_BAD_STATE ); |
| 2585 | TEST_EQUAL( psa_mac_update( &zero, |
| 2586 | input, sizeof( input ) ), |
| 2587 | PSA_ERROR_BAD_STATE ); |
| 2588 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2589 | /* A default MAC operation should be abortable without error. */ |
| 2590 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2591 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2592 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2593 | } |
| 2594 | /* END_CASE */ |
| 2595 | |
| 2596 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2597 | void mac_setup( int key_type_arg, |
| 2598 | data_t *key, |
| 2599 | int alg_arg, |
| 2600 | int expected_status_arg ) |
| 2601 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2602 | psa_key_type_t key_type = key_type_arg; |
| 2603 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2604 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2605 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2606 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2607 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2608 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2609 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2610 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2611 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2612 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2613 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2614 | &operation, &status ) ) |
| 2615 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2616 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2617 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2618 | /* The operation object should be reusable. */ |
| 2619 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2620 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2621 | smoke_test_key_data, |
| 2622 | sizeof( smoke_test_key_data ), |
| 2623 | KNOWN_SUPPORTED_MAC_ALG, |
| 2624 | &operation, &status ) ) |
| 2625 | goto exit; |
| 2626 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2627 | #endif |
| 2628 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2629 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2630 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2631 | } |
| 2632 | /* END_CASE */ |
| 2633 | |
| 2634 | /* BEGIN_CASE */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2635 | void mac_bad_order( ) |
| 2636 | { |
| 2637 | psa_key_handle_t handle = 0; |
| 2638 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2639 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
| 2640 | const uint8_t key[] = { |
| 2641 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2642 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2643 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2644 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2645 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2646 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2647 | size_t sign_mac_length = 0; |
| 2648 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2649 | const uint8_t verify_mac[] = { |
| 2650 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2651 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2652 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2653 | |
| 2654 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2655 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 2656 | psa_set_key_algorithm( &attributes, alg ); |
| 2657 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2658 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2659 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2660 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2661 | /* Call update without calling setup beforehand. */ |
| 2662 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2663 | PSA_ERROR_BAD_STATE ); |
| 2664 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2665 | |
| 2666 | /* Call sign finish without calling setup beforehand. */ |
| 2667 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2668 | &sign_mac_length), |
| 2669 | PSA_ERROR_BAD_STATE ); |
| 2670 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2671 | |
| 2672 | /* Call verify finish without calling setup beforehand. */ |
| 2673 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2674 | verify_mac, sizeof( verify_mac ) ), |
| 2675 | PSA_ERROR_BAD_STATE ); |
| 2676 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2677 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2678 | /* Call setup twice in a row. */ |
| 2679 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2680 | handle, alg ) ); |
| 2681 | TEST_EQUAL( psa_mac_sign_setup( &operation, |
| 2682 | handle, alg ), |
| 2683 | PSA_ERROR_BAD_STATE ); |
| 2684 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2685 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2686 | /* Call update after sign finish. */ |
| 2687 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2688 | handle, alg ) ); |
| 2689 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2690 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2691 | sign_mac, sizeof( sign_mac ), |
| 2692 | &sign_mac_length ) ); |
| 2693 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2694 | PSA_ERROR_BAD_STATE ); |
| 2695 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2696 | |
| 2697 | /* Call update after verify finish. */ |
| 2698 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2699 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2700 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2701 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2702 | verify_mac, sizeof( verify_mac ) ) ); |
| 2703 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2704 | PSA_ERROR_BAD_STATE ); |
| 2705 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2706 | |
| 2707 | /* Call sign finish twice in a row. */ |
| 2708 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2709 | handle, alg ) ); |
| 2710 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2711 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2712 | sign_mac, sizeof( sign_mac ), |
| 2713 | &sign_mac_length ) ); |
| 2714 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2715 | sign_mac, sizeof( sign_mac ), |
| 2716 | &sign_mac_length ), |
| 2717 | PSA_ERROR_BAD_STATE ); |
| 2718 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2719 | |
| 2720 | /* Call verify finish twice in a row. */ |
| 2721 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2722 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2723 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2724 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2725 | verify_mac, sizeof( verify_mac ) ) ); |
| 2726 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2727 | verify_mac, sizeof( verify_mac ) ), |
| 2728 | PSA_ERROR_BAD_STATE ); |
| 2729 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2730 | |
| 2731 | /* Setup sign but try verify. */ |
| 2732 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2733 | handle, alg ) ); |
| 2734 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2735 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2736 | verify_mac, sizeof( verify_mac ) ), |
| 2737 | PSA_ERROR_BAD_STATE ); |
| 2738 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2739 | |
| 2740 | /* Setup verify but try sign. */ |
| 2741 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2742 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2743 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2744 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2745 | sign_mac, sizeof( sign_mac ), |
| 2746 | &sign_mac_length ), |
| 2747 | PSA_ERROR_BAD_STATE ); |
| 2748 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2749 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2750 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2751 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2752 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2753 | PSA_DONE( ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2754 | } |
| 2755 | /* END_CASE */ |
| 2756 | |
| 2757 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2758 | void mac_sign( int key_type_arg, |
| 2759 | data_t *key, |
| 2760 | int alg_arg, |
| 2761 | data_t *input, |
| 2762 | data_t *expected_mac ) |
| 2763 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2764 | psa_key_handle_t handle = 0; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2765 | psa_key_type_t key_type = key_type_arg; |
| 2766 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2767 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2768 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2769 | /* Leave a little extra room in the output buffer. At the end of the |
| 2770 | * test, we'll check that the implementation didn't overwrite onto |
| 2771 | * this extra room. */ |
| 2772 | uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10]; |
| 2773 | size_t mac_buffer_size = |
| 2774 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 2775 | size_t mac_length = 0; |
| 2776 | |
| 2777 | memset( actual_mac, '+', sizeof( actual_mac ) ); |
| 2778 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
| 2779 | TEST_ASSERT( expected_mac->len <= mac_buffer_size ); |
| 2780 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2781 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2782 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2783 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 2784 | psa_set_key_algorithm( &attributes, alg ); |
| 2785 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2786 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2787 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2788 | |
| 2789 | /* Calculate the MAC. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2790 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2791 | handle, alg ) ); |
| 2792 | PSA_ASSERT( psa_mac_update( &operation, |
| 2793 | input->x, input->len ) ); |
| 2794 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2795 | actual_mac, mac_buffer_size, |
| 2796 | &mac_length ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2797 | |
| 2798 | /* Compare with the expected value. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 2799 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2800 | actual_mac, mac_length ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2801 | |
| 2802 | /* Verify that the end of the buffer is untouched. */ |
| 2803 | TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+', |
| 2804 | sizeof( actual_mac ) - mac_length ) ); |
| 2805 | |
| 2806 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2807 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2808 | PSA_DONE( ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2809 | } |
| 2810 | /* END_CASE */ |
| 2811 | |
| 2812 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2813 | void mac_verify( int key_type_arg, |
| 2814 | data_t *key, |
| 2815 | int alg_arg, |
| 2816 | data_t *input, |
| 2817 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2818 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2819 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2820 | psa_key_type_t key_type = key_type_arg; |
| 2821 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2822 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2823 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2824 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2825 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2826 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2827 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2828 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2829 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 2830 | psa_set_key_algorithm( &attributes, alg ); |
| 2831 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2832 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2833 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2834 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2835 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 2836 | handle, alg ) ); |
| 2837 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2838 | PSA_ASSERT( psa_mac_update( &operation, |
| 2839 | input->x, input->len ) ); |
| 2840 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2841 | expected_mac->x, |
| 2842 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2843 | |
| 2844 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2845 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2846 | PSA_DONE( ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2847 | } |
| 2848 | /* END_CASE */ |
| 2849 | |
| 2850 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2851 | void cipher_operation_init( ) |
| 2852 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2853 | const uint8_t input[1] = { 0 }; |
| 2854 | unsigned char output[1] = { 0 }; |
| 2855 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2856 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2857 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2858 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2859 | * to supress the Clang warning for the test. */ |
| 2860 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2861 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2862 | psa_cipher_operation_t zero; |
| 2863 | |
| 2864 | memset( &zero, 0, sizeof( zero ) ); |
| 2865 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2866 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2867 | TEST_EQUAL( psa_cipher_update( &func, |
| 2868 | input, sizeof( input ), |
| 2869 | output, sizeof( output ), |
| 2870 | &output_length ), |
| 2871 | PSA_ERROR_BAD_STATE ); |
| 2872 | TEST_EQUAL( psa_cipher_update( &init, |
| 2873 | input, sizeof( input ), |
| 2874 | output, sizeof( output ), |
| 2875 | &output_length ), |
| 2876 | PSA_ERROR_BAD_STATE ); |
| 2877 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2878 | input, sizeof( input ), |
| 2879 | output, sizeof( output ), |
| 2880 | &output_length ), |
| 2881 | PSA_ERROR_BAD_STATE ); |
| 2882 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2883 | /* A default cipher operation should be abortable without error. */ |
| 2884 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2885 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2886 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2887 | } |
| 2888 | /* END_CASE */ |
| 2889 | |
| 2890 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2891 | void cipher_setup( int key_type_arg, |
| 2892 | data_t *key, |
| 2893 | int alg_arg, |
| 2894 | int expected_status_arg ) |
| 2895 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2896 | psa_key_type_t key_type = key_type_arg; |
| 2897 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2898 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2899 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2900 | psa_status_t status; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2901 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2902 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2903 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2904 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2905 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2906 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2907 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2908 | &operation, &status ) ) |
| 2909 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2910 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2911 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2912 | /* The operation object should be reusable. */ |
| 2913 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2914 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2915 | smoke_test_key_data, |
| 2916 | sizeof( smoke_test_key_data ), |
| 2917 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2918 | &operation, &status ) ) |
| 2919 | goto exit; |
| 2920 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2921 | #endif |
| 2922 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2923 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2924 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2925 | } |
| 2926 | /* END_CASE */ |
| 2927 | |
| 2928 | /* BEGIN_CASE */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2929 | void cipher_bad_order( ) |
| 2930 | { |
| 2931 | psa_key_handle_t handle = 0; |
| 2932 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2933 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2934 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2935 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2936 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2937 | const uint8_t key[] = { |
| 2938 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2939 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2940 | const uint8_t text[] = { |
| 2941 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2942 | 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2943 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2944 | size_t length = 0; |
| 2945 | |
| 2946 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2947 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2948 | psa_set_key_algorithm( &attributes, alg ); |
| 2949 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2950 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2951 | |
| 2952 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2953 | /* Call encrypt setup twice in a row. */ |
| 2954 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2955 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ), |
| 2956 | PSA_ERROR_BAD_STATE ); |
| 2957 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2958 | |
| 2959 | /* Call decrypt setup twice in a row. */ |
| 2960 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) ); |
| 2961 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ), |
| 2962 | PSA_ERROR_BAD_STATE ); |
| 2963 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2964 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2965 | /* Generate an IV without calling setup beforehand. */ |
| 2966 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2967 | buffer, sizeof( buffer ), |
| 2968 | &length ), |
| 2969 | PSA_ERROR_BAD_STATE ); |
| 2970 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2971 | |
| 2972 | /* Generate an IV twice in a row. */ |
| 2973 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2974 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2975 | buffer, sizeof( buffer ), |
| 2976 | &length ) ); |
| 2977 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2978 | buffer, sizeof( buffer ), |
| 2979 | &length ), |
| 2980 | PSA_ERROR_BAD_STATE ); |
| 2981 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2982 | |
| 2983 | /* Generate an IV after it's already set. */ |
| 2984 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2985 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2986 | iv, sizeof( iv ) ) ); |
| 2987 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2988 | buffer, sizeof( buffer ), |
| 2989 | &length ), |
| 2990 | PSA_ERROR_BAD_STATE ); |
| 2991 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2992 | |
| 2993 | /* Set an IV without calling setup beforehand. */ |
| 2994 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2995 | iv, sizeof( iv ) ), |
| 2996 | PSA_ERROR_BAD_STATE ); |
| 2997 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2998 | |
| 2999 | /* Set an IV after it's already set. */ |
| 3000 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3001 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3002 | iv, sizeof( iv ) ) ); |
| 3003 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3004 | iv, sizeof( iv ) ), |
| 3005 | PSA_ERROR_BAD_STATE ); |
| 3006 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3007 | |
| 3008 | /* Set an IV after it's already generated. */ |
| 3009 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3010 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3011 | buffer, sizeof( buffer ), |
| 3012 | &length ) ); |
| 3013 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3014 | iv, sizeof( iv ) ), |
| 3015 | PSA_ERROR_BAD_STATE ); |
| 3016 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3017 | |
| 3018 | /* Call update without calling setup beforehand. */ |
| 3019 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3020 | text, sizeof( text ), |
| 3021 | buffer, sizeof( buffer ), |
| 3022 | &length ), |
| 3023 | PSA_ERROR_BAD_STATE ); |
| 3024 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3025 | |
| 3026 | /* Call update without an IV where an IV is required. */ |
| 3027 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3028 | text, sizeof( text ), |
| 3029 | buffer, sizeof( buffer ), |
| 3030 | &length ), |
| 3031 | PSA_ERROR_BAD_STATE ); |
| 3032 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3033 | |
| 3034 | /* Call update after finish. */ |
| 3035 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3036 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3037 | iv, sizeof( iv ) ) ); |
| 3038 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3039 | buffer, sizeof( buffer ), &length ) ); |
| 3040 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3041 | text, sizeof( text ), |
| 3042 | buffer, sizeof( buffer ), |
| 3043 | &length ), |
| 3044 | PSA_ERROR_BAD_STATE ); |
| 3045 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3046 | |
| 3047 | /* Call finish without calling setup beforehand. */ |
| 3048 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3049 | buffer, sizeof( buffer ), &length ), |
| 3050 | PSA_ERROR_BAD_STATE ); |
| 3051 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3052 | |
| 3053 | /* Call finish without an IV where an IV is required. */ |
| 3054 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3055 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3056 | * for cipher modes with padding. */ |
| 3057 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3058 | buffer, sizeof( buffer ), &length ), |
| 3059 | PSA_ERROR_BAD_STATE ); |
| 3060 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3061 | |
| 3062 | /* Call finish twice in a row. */ |
| 3063 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 3064 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3065 | iv, sizeof( iv ) ) ); |
| 3066 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3067 | buffer, sizeof( buffer ), &length ) ); |
| 3068 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3069 | buffer, sizeof( buffer ), &length ), |
| 3070 | PSA_ERROR_BAD_STATE ); |
| 3071 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3072 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3073 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 3074 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3075 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3076 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3077 | } |
| 3078 | /* END_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3079 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3080 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3081 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3082 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3083 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3084 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3085 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3086 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3087 | psa_status_t status; |
| 3088 | psa_key_type_t key_type = key_type_arg; |
| 3089 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3090 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3091 | unsigned char *output = NULL; |
| 3092 | size_t output_buffer_size = 0; |
| 3093 | size_t function_output_length = 0; |
| 3094 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3095 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3096 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3097 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3098 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3099 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3100 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3101 | psa_set_key_algorithm( &attributes, alg ); |
| 3102 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3103 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3104 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3105 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3106 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3107 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3108 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3109 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3110 | output_buffer_size = ( (size_t) input->len + |
| 3111 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3112 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3113 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3114 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3115 | input->x, input->len, |
| 3116 | output, output_buffer_size, |
| 3117 | &function_output_length ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3118 | total_output_length += function_output_length; |
| 3119 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3120 | output + total_output_length, |
| 3121 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3122 | &function_output_length ); |
| 3123 | total_output_length += function_output_length; |
| 3124 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3125 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3126 | if( expected_status == PSA_SUCCESS ) |
| 3127 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3128 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3129 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3130 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | exit: |
| 3134 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3135 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3136 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3137 | } |
| 3138 | /* END_CASE */ |
| 3139 | |
| 3140 | /* BEGIN_CASE */ |
| 3141 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3142 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3143 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3144 | int first_part_size_arg, |
| 3145 | int output1_length_arg, int output2_length_arg, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3146 | data_t *expected_output ) |
| 3147 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3148 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3149 | psa_key_type_t key_type = key_type_arg; |
| 3150 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3151 | size_t first_part_size = first_part_size_arg; |
| 3152 | size_t output1_length = output1_length_arg; |
| 3153 | size_t output2_length = output2_length_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3154 | unsigned char *output = NULL; |
| 3155 | size_t output_buffer_size = 0; |
| 3156 | size_t function_output_length = 0; |
| 3157 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3158 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3159 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3160 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3161 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3162 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3163 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3164 | psa_set_key_algorithm( &attributes, alg ); |
| 3165 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3166 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3167 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3168 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3169 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3170 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3171 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3172 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3173 | output_buffer_size = ( (size_t) input->len + |
| 3174 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3175 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3176 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3177 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3178 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3179 | output, output_buffer_size, |
| 3180 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3181 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3182 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3183 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3184 | input->x + first_part_size, |
| 3185 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3186 | output + total_output_length, |
| 3187 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3188 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3189 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3190 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3191 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3192 | output + total_output_length, |
| 3193 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3194 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3195 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3196 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3197 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3198 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3199 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3200 | |
| 3201 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3202 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3203 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3204 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3205 | } |
| 3206 | /* END_CASE */ |
| 3207 | |
| 3208 | /* BEGIN_CASE */ |
| 3209 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3210 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3211 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3212 | int first_part_size_arg, |
| 3213 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3214 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3215 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3216 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3217 | |
| 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; |
| 3221 | size_t output1_length = output1_length_arg; |
| 3222 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3223 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3224 | size_t output_buffer_size = 0; |
| 3225 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3226 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3227 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3228 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3229 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3230 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3231 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3232 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3233 | psa_set_key_algorithm( &attributes, alg ); |
| 3234 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3235 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3236 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3237 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3238 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3239 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3240 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3241 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3242 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3243 | output_buffer_size = ( (size_t) input->len + |
| 3244 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3245 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3246 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3247 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3248 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3249 | input->x, first_part_size, |
| 3250 | output, output_buffer_size, |
| 3251 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3252 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3253 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3254 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3255 | input->x + first_part_size, |
| 3256 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3257 | output + total_output_length, |
| 3258 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3259 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3260 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3261 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3262 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3263 | output + total_output_length, |
| 3264 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3265 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3266 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3267 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3268 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3269 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3270 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3271 | |
| 3272 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3273 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3274 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3275 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3276 | } |
| 3277 | /* END_CASE */ |
| 3278 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3279 | /* BEGIN_CASE */ |
| 3280 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3281 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3282 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3283 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3284 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3285 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3286 | psa_status_t status; |
| 3287 | psa_key_type_t key_type = key_type_arg; |
| 3288 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3289 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3290 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3291 | size_t output_buffer_size = 0; |
| 3292 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3293 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3294 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3295 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3296 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3297 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3298 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3299 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3300 | psa_set_key_algorithm( &attributes, alg ); |
| 3301 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3302 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3303 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3304 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3305 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3306 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3307 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3308 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3309 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3310 | output_buffer_size = ( (size_t) input->len + |
| 3311 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3312 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3313 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3314 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3315 | input->x, input->len, |
| 3316 | output, output_buffer_size, |
| 3317 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3318 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3319 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3320 | output + total_output_length, |
| 3321 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3322 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3323 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3324 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3325 | |
| 3326 | if( expected_status == PSA_SUCCESS ) |
| 3327 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3328 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3329 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3330 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3331 | } |
| 3332 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3333 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3334 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3335 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3336 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3337 | } |
| 3338 | /* END_CASE */ |
| 3339 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3340 | /* BEGIN_CASE */ |
| 3341 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3342 | data_t *key, |
| 3343 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3344 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3345 | psa_key_handle_t handle = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3346 | psa_key_type_t key_type = key_type_arg; |
| 3347 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 3348 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3349 | size_t iv_size = 16; |
| 3350 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3351 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3352 | size_t output1_size = 0; |
| 3353 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3354 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3355 | size_t output2_size = 0; |
| 3356 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3357 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3358 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3359 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3360 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3361 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3362 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3363 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3364 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3365 | psa_set_key_algorithm( &attributes, alg ); |
| 3366 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3367 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3368 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3369 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3370 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3371 | handle, alg ) ); |
| 3372 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3373 | handle, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3374 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3375 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3376 | iv, iv_size, |
| 3377 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3378 | output1_size = ( (size_t) input->len + |
| 3379 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3380 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3381 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3382 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3383 | output1, output1_size, |
| 3384 | &output1_length ) ); |
| 3385 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3386 | output1 + output1_length, |
| 3387 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3388 | &function_output_length ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3389 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3390 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3391 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3392 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3393 | |
| 3394 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3395 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3396 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3397 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3398 | iv, iv_length ) ); |
| 3399 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3400 | output2, output2_size, |
| 3401 | &output2_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3402 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3403 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3404 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3405 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3406 | &function_output_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3407 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3408 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3409 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3410 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3411 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3412 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3413 | |
| 3414 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3415 | mbedtls_free( output1 ); |
| 3416 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3417 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3418 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3419 | } |
| 3420 | /* END_CASE */ |
| 3421 | |
| 3422 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3423 | void cipher_verify_output_multipart( int alg_arg, |
| 3424 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3425 | data_t *key, |
| 3426 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3427 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3428 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3429 | psa_key_handle_t handle = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3430 | psa_key_type_t key_type = key_type_arg; |
| 3431 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3432 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3433 | unsigned char iv[16] = {0}; |
| 3434 | size_t iv_size = 16; |
| 3435 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3436 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3437 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3438 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3439 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3440 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3441 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3442 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3443 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3444 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3445 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3446 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3447 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3448 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3449 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3450 | psa_set_key_algorithm( &attributes, alg ); |
| 3451 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3452 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3453 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3454 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3455 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3456 | handle, alg ) ); |
| 3457 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3458 | handle, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3459 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3460 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3461 | iv, iv_size, |
| 3462 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3463 | output1_buffer_size = ( (size_t) input->len + |
| 3464 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3465 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3466 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3467 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3468 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3469 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3470 | output1, output1_buffer_size, |
| 3471 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3472 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3473 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3474 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3475 | input->x + first_part_size, |
| 3476 | input->len - first_part_size, |
| 3477 | output1, output1_buffer_size, |
| 3478 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3479 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3480 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3481 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3482 | output1 + output1_length, |
| 3483 | output1_buffer_size - output1_length, |
| 3484 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3485 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3486 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3487 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3488 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3489 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3490 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3491 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3492 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3493 | iv, iv_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3494 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3495 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3496 | output2, output2_buffer_size, |
| 3497 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3498 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3499 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3500 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3501 | output1 + first_part_size, |
| 3502 | output1_length - first_part_size, |
| 3503 | output2, output2_buffer_size, |
| 3504 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3505 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3506 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3507 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3508 | output2 + output2_length, |
| 3509 | output2_buffer_size - output2_length, |
| 3510 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3511 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3512 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3513 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3514 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3515 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3516 | |
| 3517 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3518 | mbedtls_free( output1 ); |
| 3519 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3520 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3521 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3522 | } |
| 3523 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3524 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3525 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3526 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3527 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3528 | data_t *nonce, |
| 3529 | data_t *additional_data, |
| 3530 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3531 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3532 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3533 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3534 | psa_key_type_t key_type = key_type_arg; |
| 3535 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3536 | unsigned char *output_data = NULL; |
| 3537 | size_t output_size = 0; |
| 3538 | size_t output_length = 0; |
| 3539 | unsigned char *output_data2 = NULL; |
| 3540 | size_t output_length2 = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3541 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3542 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3543 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3544 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3545 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3546 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3547 | * should be exact. */ |
| 3548 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3549 | TEST_EQUAL( output_size, |
| 3550 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3551 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3552 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3553 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3554 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3555 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3556 | psa_set_key_algorithm( &attributes, alg ); |
| 3557 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3558 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3559 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3560 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3561 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3562 | TEST_EQUAL( psa_aead_encrypt( handle, alg, |
| 3563 | nonce->x, nonce->len, |
| 3564 | additional_data->x, |
| 3565 | additional_data->len, |
| 3566 | input_data->x, input_data->len, |
| 3567 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3568 | &output_length ), |
| 3569 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3570 | |
| 3571 | if( PSA_SUCCESS == expected_result ) |
| 3572 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3573 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3574 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3575 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3576 | * should be exact. */ |
| 3577 | TEST_EQUAL( input_data->len, |
| 3578 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) ); |
| 3579 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3580 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3581 | nonce->x, nonce->len, |
| 3582 | additional_data->x, |
| 3583 | additional_data->len, |
| 3584 | output_data, output_length, |
| 3585 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3586 | &output_length2 ), |
| 3587 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3588 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3589 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3590 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3591 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3592 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3593 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3594 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3595 | mbedtls_free( output_data ); |
| 3596 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3597 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3598 | } |
| 3599 | /* END_CASE */ |
| 3600 | |
| 3601 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3602 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3603 | int alg_arg, |
| 3604 | data_t *nonce, |
| 3605 | data_t *additional_data, |
| 3606 | data_t *input_data, |
| 3607 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3608 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3609 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3610 | psa_key_type_t key_type = key_type_arg; |
| 3611 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3612 | unsigned char *output_data = NULL; |
| 3613 | size_t output_size = 0; |
| 3614 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3615 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3616 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3617 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3618 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3619 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3620 | * should be exact. */ |
| 3621 | TEST_EQUAL( output_size, |
| 3622 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3623 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3624 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3625 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3626 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3627 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3628 | psa_set_key_algorithm( &attributes, alg ); |
| 3629 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3630 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3631 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3632 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3633 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3634 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 3635 | nonce->x, nonce->len, |
| 3636 | additional_data->x, additional_data->len, |
| 3637 | input_data->x, input_data->len, |
| 3638 | output_data, output_size, |
| 3639 | &output_length ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3640 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3641 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3642 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3643 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3644 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3645 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3646 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3647 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3648 | } |
| 3649 | /* END_CASE */ |
| 3650 | |
| 3651 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3652 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3653 | int alg_arg, |
| 3654 | data_t *nonce, |
| 3655 | data_t *additional_data, |
| 3656 | data_t *input_data, |
| 3657 | data_t *expected_data, |
| 3658 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3659 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3660 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3661 | psa_key_type_t key_type = key_type_arg; |
| 3662 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3663 | unsigned char *output_data = NULL; |
| 3664 | size_t output_size = 0; |
| 3665 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3666 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3667 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3668 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3669 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3670 | output_size = input_data->len - tag_length; |
| 3671 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3672 | * should be exact. */ |
| 3673 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3674 | TEST_EQUAL( output_size, |
| 3675 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3676 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3677 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3678 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3679 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3680 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3681 | psa_set_key_algorithm( &attributes, alg ); |
| 3682 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3683 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3684 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3685 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3686 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3687 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3688 | nonce->x, nonce->len, |
| 3689 | additional_data->x, |
| 3690 | additional_data->len, |
| 3691 | input_data->x, input_data->len, |
| 3692 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3693 | &output_length ), |
| 3694 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3695 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3696 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3697 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3698 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3699 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3700 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3701 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3702 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3703 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3704 | } |
| 3705 | /* END_CASE */ |
| 3706 | |
| 3707 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3708 | void signature_size( int type_arg, |
| 3709 | int bits, |
| 3710 | int alg_arg, |
| 3711 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3712 | { |
| 3713 | psa_key_type_t type = type_arg; |
| 3714 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3715 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3716 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3717 | exit: |
| 3718 | ; |
| 3719 | } |
| 3720 | /* END_CASE */ |
| 3721 | |
| 3722 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3723 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 3724 | int alg_arg, data_t *input_data, |
| 3725 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3726 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3727 | psa_key_handle_t handle = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3728 | psa_key_type_t key_type = key_type_arg; |
| 3729 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3730 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3731 | unsigned char *signature = NULL; |
| 3732 | size_t signature_size; |
| 3733 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3734 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3735 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3736 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3737 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3738 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 3739 | psa_set_key_algorithm( &attributes, alg ); |
| 3740 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3741 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3742 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3743 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3744 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3745 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3746 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3747 | /* Allocate a buffer which has the size advertized by the |
| 3748 | * library. */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3749 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3750 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3751 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3752 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3753 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3754 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3755 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3756 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3757 | input_data->x, input_data->len, |
| 3758 | signature, signature_size, |
| 3759 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3760 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3761 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3762 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3763 | |
| 3764 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3765 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3766 | psa_destroy_key( handle ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3767 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3768 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3769 | } |
| 3770 | /* END_CASE */ |
| 3771 | |
| 3772 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3773 | void sign_fail( int key_type_arg, data_t *key_data, |
| 3774 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3775 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3776 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3777 | psa_key_handle_t handle = 0; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3778 | psa_key_type_t key_type = key_type_arg; |
| 3779 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3780 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3781 | psa_status_t actual_status; |
| 3782 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3783 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3784 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3785 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3786 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3787 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3788 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3789 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3790 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3791 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 3792 | psa_set_key_algorithm( &attributes, alg ); |
| 3793 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3794 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3795 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3796 | &handle ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3797 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3798 | actual_status = psa_asymmetric_sign( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3799 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3800 | signature, signature_size, |
| 3801 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3802 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3803 | /* The value of *signature_length is unspecified on error, but |
| 3804 | * whatever it is, it should be less than signature_size, so that |
| 3805 | * if the caller tries to read *signature_length bytes without |
| 3806 | * checking the error code then they don't overflow a buffer. */ |
| 3807 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3808 | |
| 3809 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3810 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3811 | psa_destroy_key( handle ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3812 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3813 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3814 | } |
| 3815 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3816 | |
| 3817 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3818 | void sign_verify( int key_type_arg, data_t *key_data, |
| 3819 | int alg_arg, data_t *input_data ) |
| 3820 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3821 | psa_key_handle_t handle = 0; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3822 | psa_key_type_t key_type = key_type_arg; |
| 3823 | psa_algorithm_t alg = alg_arg; |
| 3824 | size_t key_bits; |
| 3825 | unsigned char *signature = NULL; |
| 3826 | size_t signature_size; |
| 3827 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3828 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3829 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3830 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3831 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3832 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 3833 | psa_set_key_algorithm( &attributes, alg ); |
| 3834 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3835 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3836 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3837 | &handle ) ); |
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 | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3840 | |
| 3841 | /* Allocate a buffer which has the size advertized by the |
| 3842 | * library. */ |
| 3843 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3844 | key_bits, alg ); |
| 3845 | TEST_ASSERT( signature_size != 0 ); |
| 3846 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3847 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3848 | |
| 3849 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3850 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3851 | input_data->x, input_data->len, |
| 3852 | signature, signature_size, |
| 3853 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3854 | /* Check that the signature length looks sensible. */ |
| 3855 | TEST_ASSERT( signature_length <= signature_size ); |
| 3856 | TEST_ASSERT( signature_length > 0 ); |
| 3857 | |
| 3858 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3859 | PSA_ASSERT( psa_asymmetric_verify( |
| 3860 | handle, alg, |
| 3861 | input_data->x, input_data->len, |
| 3862 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3863 | |
| 3864 | if( input_data->len != 0 ) |
| 3865 | { |
| 3866 | /* Flip a bit in the input and verify that the signature is now |
| 3867 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3868 | * because ECDSA may ignore the last few bits of the input. */ |
| 3869 | input_data->x[0] ^= 1; |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3870 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 3871 | input_data->x, input_data->len, |
| 3872 | signature, signature_length ), |
| 3873 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3877 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3878 | psa_destroy_key( handle ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3879 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3880 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3881 | } |
| 3882 | /* END_CASE */ |
| 3883 | |
| 3884 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3885 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 3886 | int alg_arg, data_t *hash_data, |
| 3887 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3888 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3889 | psa_key_handle_t handle = 0; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3890 | psa_key_type_t key_type = key_type_arg; |
| 3891 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3892 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3893 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3894 | TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
| 3895 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3896 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3897 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3898 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 3899 | psa_set_key_algorithm( &attributes, alg ); |
| 3900 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3901 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3902 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3903 | &handle ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3904 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3905 | PSA_ASSERT( psa_asymmetric_verify( handle, alg, |
| 3906 | hash_data->x, hash_data->len, |
| 3907 | signature_data->x, |
| 3908 | signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3909 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3910 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3911 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3912 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3913 | } |
| 3914 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3915 | |
| 3916 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3917 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 3918 | int alg_arg, data_t *hash_data, |
| 3919 | data_t *signature_data, |
| 3920 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3921 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3922 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3923 | psa_key_type_t key_type = key_type_arg; |
| 3924 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3925 | psa_status_t actual_status; |
| 3926 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3927 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3928 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3929 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3930 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3931 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 3932 | psa_set_key_algorithm( &attributes, alg ); |
| 3933 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3934 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3935 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3936 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3937 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3938 | actual_status = psa_asymmetric_verify( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3939 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3940 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3941 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3942 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3943 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3944 | |
| 3945 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3946 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3947 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3948 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3949 | } |
| 3950 | /* END_CASE */ |
| 3951 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3952 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3953 | void asymmetric_encrypt( int key_type_arg, |
| 3954 | data_t *key_data, |
| 3955 | int alg_arg, |
| 3956 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3957 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3958 | int expected_output_length_arg, |
| 3959 | int expected_status_arg ) |
| 3960 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3961 | psa_key_handle_t handle = 0; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3962 | psa_key_type_t key_type = key_type_arg; |
| 3963 | psa_algorithm_t alg = alg_arg; |
| 3964 | size_t expected_output_length = expected_output_length_arg; |
| 3965 | size_t key_bits; |
| 3966 | unsigned char *output = NULL; |
| 3967 | size_t output_size; |
| 3968 | size_t output_length = ~0; |
| 3969 | psa_status_t actual_status; |
| 3970 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3971 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3972 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3973 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3974 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3975 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3976 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3977 | psa_set_key_algorithm( &attributes, alg ); |
| 3978 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3979 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3980 | &handle ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3981 | |
| 3982 | /* Determine the maximum output length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3983 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3984 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3985 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3986 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3987 | |
| 3988 | /* Encrypt the input */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3989 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3990 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3991 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3992 | output, output_size, |
| 3993 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3994 | TEST_EQUAL( actual_status, expected_status ); |
| 3995 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3996 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3997 | /* If the label is empty, the test framework puts a non-null pointer |
| 3998 | * in label->x. Test that a null pointer works as well. */ |
| 3999 | if( label->len == 0 ) |
| 4000 | { |
| 4001 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4002 | if( output_size != 0 ) |
| 4003 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4004 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4005 | input_data->x, input_data->len, |
| 4006 | NULL, label->len, |
| 4007 | output, output_size, |
| 4008 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4009 | TEST_EQUAL( actual_status, expected_status ); |
| 4010 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4011 | } |
| 4012 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4013 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4014 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4015 | psa_destroy_key( handle ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4016 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4017 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4018 | } |
| 4019 | /* END_CASE */ |
| 4020 | |
| 4021 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4022 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4023 | data_t *key_data, |
| 4024 | int alg_arg, |
| 4025 | data_t *input_data, |
| 4026 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4027 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4028 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4029 | psa_key_type_t key_type = key_type_arg; |
| 4030 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4031 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4032 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4033 | size_t output_size; |
| 4034 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4035 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4036 | size_t output2_size; |
| 4037 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4038 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4039 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4040 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4041 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4042 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4043 | psa_set_key_algorithm( &attributes, alg ); |
| 4044 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4045 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4046 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4047 | &handle ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4048 | |
| 4049 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4050 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 4051 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4052 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4053 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4054 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4055 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4056 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4057 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4058 | * the original plaintext because of the non-optional random |
| 4059 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4060 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 4061 | input_data->x, input_data->len, |
| 4062 | label->x, label->len, |
| 4063 | output, output_size, |
| 4064 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4065 | /* We don't know what ciphertext length to expect, but check that |
| 4066 | * it looks sensible. */ |
| 4067 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4068 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4069 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4070 | output, output_length, |
| 4071 | label->x, label->len, |
| 4072 | output2, output2_size, |
| 4073 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4074 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4075 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4076 | |
| 4077 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4078 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4079 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4080 | mbedtls_free( output ); |
| 4081 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4082 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4083 | } |
| 4084 | /* END_CASE */ |
| 4085 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4086 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4087 | void asymmetric_decrypt( int key_type_arg, |
| 4088 | data_t *key_data, |
| 4089 | int alg_arg, |
| 4090 | data_t *input_data, |
| 4091 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4092 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4093 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4094 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4095 | psa_key_type_t key_type = key_type_arg; |
| 4096 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4097 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4098 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4099 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4100 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4101 | |
Jaeden Amero | 412654a | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4102 | output_size = expected_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4103 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4104 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4105 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4106 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4107 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4108 | psa_set_key_algorithm( &attributes, alg ); |
| 4109 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4110 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4111 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4112 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4113 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4114 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4115 | input_data->x, input_data->len, |
| 4116 | label->x, label->len, |
| 4117 | output, |
| 4118 | output_size, |
| 4119 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4120 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4121 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4122 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4123 | /* If the label is empty, the test framework puts a non-null pointer |
| 4124 | * in label->x. Test that a null pointer works as well. */ |
| 4125 | if( label->len == 0 ) |
| 4126 | { |
| 4127 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4128 | if( output_size != 0 ) |
| 4129 | memset( output, 0, output_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4130 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4131 | input_data->x, input_data->len, |
| 4132 | NULL, label->len, |
| 4133 | output, |
| 4134 | output_size, |
| 4135 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4136 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4137 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4138 | } |
| 4139 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4140 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4141 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4142 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4143 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4144 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4145 | } |
| 4146 | /* END_CASE */ |
| 4147 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4148 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4149 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4150 | data_t *key_data, |
| 4151 | int alg_arg, |
| 4152 | data_t *input_data, |
| 4153 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4154 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4155 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4156 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4157 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4158 | psa_key_type_t key_type = key_type_arg; |
| 4159 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4160 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4161 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4162 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4163 | psa_status_t actual_status; |
| 4164 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4165 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4166 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4167 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4168 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4169 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4170 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4171 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4172 | psa_set_key_algorithm( &attributes, alg ); |
| 4173 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4174 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4175 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4176 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4177 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4178 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4179 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4180 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4181 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4182 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4183 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4184 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4185 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4186 | /* If the label is empty, the test framework puts a non-null pointer |
| 4187 | * in label->x. Test that a null pointer works as well. */ |
| 4188 | if( label->len == 0 ) |
| 4189 | { |
| 4190 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4191 | if( output_size != 0 ) |
| 4192 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4193 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4194 | input_data->x, input_data->len, |
| 4195 | NULL, label->len, |
| 4196 | output, output_size, |
| 4197 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4198 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4199 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4200 | } |
| 4201 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4202 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4203 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4204 | psa_destroy_key( handle ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4205 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4206 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4207 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4208 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4209 | |
| 4210 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4211 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4212 | { |
| 4213 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4214 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4215 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4216 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4217 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4218 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4219 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4220 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4221 | |
| 4222 | memset( &zero, 0, sizeof( zero ) ); |
| 4223 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4224 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4225 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4226 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4227 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4228 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4229 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4230 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4231 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4232 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4233 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4234 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4235 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4236 | } |
| 4237 | /* END_CASE */ |
| 4238 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4239 | /* BEGIN_CASE */ |
| 4240 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4241 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4242 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4243 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4244 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4245 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4246 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4247 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4248 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4249 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4250 | |
| 4251 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4252 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4253 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4254 | } |
| 4255 | /* END_CASE */ |
| 4256 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4257 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4258 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4259 | int expected_status_arg ) |
| 4260 | { |
| 4261 | psa_algorithm_t alg = alg_arg; |
| 4262 | size_t capacity = capacity_arg; |
| 4263 | psa_status_t expected_status = expected_status_arg; |
| 4264 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4265 | |
| 4266 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4267 | |
| 4268 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4269 | |
| 4270 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4271 | expected_status ); |
| 4272 | |
| 4273 | exit: |
| 4274 | psa_key_derivation_abort( &operation ); |
| 4275 | PSA_DONE( ); |
| 4276 | } |
| 4277 | /* END_CASE */ |
| 4278 | |
| 4279 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4280 | void derive_input( int alg_arg, |
| 4281 | int key_type_arg, |
| 4282 | int step1_arg, data_t *input1, |
| 4283 | int step2_arg, data_t *input2, |
| 4284 | int step3_arg, data_t *input3, |
| 4285 | int expected_status_arg1, |
| 4286 | int expected_status_arg2, |
| 4287 | int expected_status_arg3 ) |
| 4288 | { |
| 4289 | psa_algorithm_t alg = alg_arg; |
| 4290 | size_t key_type = key_type_arg; |
| 4291 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4292 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4293 | expected_status_arg2, |
| 4294 | expected_status_arg3}; |
| 4295 | data_t *inputs[] = {input1, input2, input3}; |
| 4296 | psa_key_handle_t handles[] = {0, 0, 0}; |
| 4297 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4298 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4299 | size_t i; |
| 4300 | |
| 4301 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4302 | |
| 4303 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4304 | psa_set_key_algorithm( &attributes, alg ); |
| 4305 | psa_set_key_type( &attributes, key_type ); |
| 4306 | |
| 4307 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4308 | |
| 4309 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4310 | { |
| 4311 | switch( steps[i] ) |
| 4312 | { |
| 4313 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4314 | PSA_ASSERT( psa_import_key( &attributes, |
| 4315 | inputs[i]->x, inputs[i]->len, |
| 4316 | &handles[i] ) ); |
| 4317 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
| 4318 | handles[i] ), |
| 4319 | expected_statuses[i] ); |
| 4320 | break; |
| 4321 | default: |
| 4322 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4323 | &operation, steps[i], |
| 4324 | inputs[i]->x, inputs[i]->len ), |
| 4325 | expected_statuses[i] ); |
| 4326 | break; |
| 4327 | } |
| 4328 | } |
| 4329 | |
| 4330 | exit: |
| 4331 | psa_key_derivation_abort( &operation ); |
| 4332 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4333 | psa_destroy_key( handles[i] ); |
| 4334 | PSA_DONE( ); |
| 4335 | } |
| 4336 | /* END_CASE */ |
| 4337 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4338 | /* BEGIN_CASE */ |
| 4339 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4340 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4341 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4342 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4343 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4344 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4345 | unsigned char input1[] = "Input 1"; |
| 4346 | size_t input1_length = sizeof( input1 ); |
| 4347 | unsigned char input2[] = "Input 2"; |
| 4348 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4349 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4350 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4351 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4352 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4353 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4354 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4355 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4356 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4357 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4358 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4359 | psa_set_key_algorithm( &attributes, alg ); |
| 4360 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4361 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4362 | PSA_ASSERT( psa_import_key( &attributes, |
| 4363 | key_data, sizeof( key_data ), |
| 4364 | &handle ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4365 | |
| 4366 | /* valid key derivation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4367 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4368 | input1, input1_length, |
| 4369 | input2, input2_length, |
| 4370 | capacity ) ) |
| 4371 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4372 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4373 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4374 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4375 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4376 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4377 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4378 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4379 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4380 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4381 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4382 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4383 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4384 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4385 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4386 | } |
| 4387 | /* END_CASE */ |
| 4388 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4389 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4390 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4391 | { |
| 4392 | uint8_t output_buffer[16]; |
| 4393 | size_t buffer_size = 16; |
| 4394 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4395 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4396 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4397 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4398 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4399 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4400 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4401 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4402 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4403 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4404 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4405 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4406 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4407 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4408 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4409 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4410 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4411 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4412 | |
| 4413 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4414 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4415 | } |
| 4416 | /* END_CASE */ |
| 4417 | |
| 4418 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4419 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4420 | int step1_arg, data_t *input1, |
| 4421 | int step2_arg, data_t *input2, |
| 4422 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4423 | int requested_capacity_arg, |
| 4424 | data_t *expected_output1, |
| 4425 | data_t *expected_output2 ) |
| 4426 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4427 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4428 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4429 | data_t *inputs[] = {input1, input2, input3}; |
| 4430 | psa_key_handle_t handles[] = {0, 0, 0}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4431 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4432 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4433 | uint8_t *expected_outputs[2] = |
| 4434 | {expected_output1->x, expected_output2->x}; |
| 4435 | size_t output_sizes[2] = |
| 4436 | {expected_output1->len, expected_output2->len}; |
| 4437 | size_t output_buffer_size = 0; |
| 4438 | uint8_t *output_buffer = NULL; |
| 4439 | size_t expected_capacity; |
| 4440 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4441 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4442 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4443 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4444 | |
| 4445 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4446 | { |
| 4447 | if( output_sizes[i] > output_buffer_size ) |
| 4448 | output_buffer_size = output_sizes[i]; |
| 4449 | if( output_sizes[i] == 0 ) |
| 4450 | expected_outputs[i] = NULL; |
| 4451 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4452 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4453 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4454 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4455 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4456 | psa_set_key_algorithm( &attributes, alg ); |
| 4457 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4458 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4459 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4460 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4461 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4462 | requested_capacity ) ); |
| 4463 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4464 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4465 | switch( steps[i] ) |
| 4466 | { |
| 4467 | case 0: |
| 4468 | break; |
| 4469 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4470 | PSA_ASSERT( psa_import_key( &attributes, |
| 4471 | inputs[i]->x, inputs[i]->len, |
| 4472 | &handles[i] ) ); |
| 4473 | PSA_ASSERT( psa_key_derivation_input_key( |
| 4474 | &operation, steps[i], |
| 4475 | handles[i] ) ); |
| 4476 | break; |
| 4477 | default: |
| 4478 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4479 | &operation, steps[i], |
| 4480 | inputs[i]->x, inputs[i]->len ) ); |
| 4481 | break; |
| 4482 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4483 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4484 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4485 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4486 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4487 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4488 | expected_capacity = requested_capacity; |
| 4489 | |
| 4490 | /* Expansion phase. */ |
| 4491 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4492 | { |
| 4493 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4494 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4495 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4496 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4497 | { |
| 4498 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4499 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4500 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4501 | continue; |
| 4502 | } |
| 4503 | else if( expected_capacity == 0 || |
| 4504 | output_sizes[i] > expected_capacity ) |
| 4505 | { |
| 4506 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4507 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4508 | expected_capacity = 0; |
| 4509 | continue; |
| 4510 | } |
| 4511 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4512 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4513 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4514 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4515 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4516 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4517 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4518 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4519 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4520 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4521 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4522 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4523 | |
| 4524 | exit: |
| 4525 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4526 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4527 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4528 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4529 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4530 | } |
| 4531 | /* END_CASE */ |
| 4532 | |
| 4533 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4534 | void derive_full( int alg_arg, |
| 4535 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4536 | data_t *input1, |
| 4537 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4538 | int requested_capacity_arg ) |
| 4539 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4540 | psa_key_handle_t handle = 0; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4541 | psa_algorithm_t alg = alg_arg; |
| 4542 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4543 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4544 | unsigned char output_buffer[16]; |
| 4545 | size_t expected_capacity = requested_capacity; |
| 4546 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4547 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4548 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4549 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4550 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4551 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4552 | psa_set_key_algorithm( &attributes, alg ); |
| 4553 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4554 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4555 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4556 | &handle ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4557 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4558 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4559 | input1->x, input1->len, |
| 4560 | input2->x, input2->len, |
| 4561 | requested_capacity ) ) |
| 4562 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4563 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4564 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4565 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4566 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4567 | |
| 4568 | /* Expansion phase. */ |
| 4569 | while( current_capacity > 0 ) |
| 4570 | { |
| 4571 | size_t read_size = sizeof( output_buffer ); |
| 4572 | if( read_size > current_capacity ) |
| 4573 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4574 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4575 | output_buffer, |
| 4576 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4577 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4578 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4579 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4580 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4581 | } |
| 4582 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4583 | /* Check that the operation refuses to go over capacity. */ |
| 4584 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4585 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4586 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4587 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4588 | |
| 4589 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4590 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4591 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4592 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4593 | } |
| 4594 | /* END_CASE */ |
| 4595 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4596 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4597 | void derive_key_exercise( int alg_arg, |
| 4598 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4599 | data_t *input1, |
| 4600 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4601 | int derived_type_arg, |
| 4602 | int derived_bits_arg, |
| 4603 | int derived_usage_arg, |
| 4604 | int derived_alg_arg ) |
| 4605 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4606 | psa_key_handle_t base_handle = 0; |
| 4607 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4608 | psa_algorithm_t alg = alg_arg; |
| 4609 | psa_key_type_t derived_type = derived_type_arg; |
| 4610 | size_t derived_bits = derived_bits_arg; |
| 4611 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4612 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4613 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4614 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4615 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4616 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4617 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4618 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4619 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4620 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4621 | psa_set_key_algorithm( &attributes, alg ); |
| 4622 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4623 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4624 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4625 | |
| 4626 | /* Derive a key. */ |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4627 | if ( setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4628 | input1->x, input1->len, |
| 4629 | input2->x, input2->len, capacity ) ) |
| 4630 | goto exit; |
| 4631 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4632 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4633 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4634 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4635 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4636 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4637 | &derived_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4638 | |
| 4639 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4640 | PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) ); |
| 4641 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4642 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4643 | |
| 4644 | /* Exercise the derived key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4645 | if( ! exercise_key( derived_handle, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4646 | goto exit; |
| 4647 | |
| 4648 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4649 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4650 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4651 | psa_destroy_key( base_handle ); |
| 4652 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4653 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4654 | } |
| 4655 | /* END_CASE */ |
| 4656 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4657 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4658 | void derive_key_export( int alg_arg, |
| 4659 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4660 | data_t *input1, |
| 4661 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4662 | int bytes1_arg, |
| 4663 | int bytes2_arg ) |
| 4664 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4665 | psa_key_handle_t base_handle = 0; |
| 4666 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4667 | psa_algorithm_t alg = alg_arg; |
| 4668 | size_t bytes1 = bytes1_arg; |
| 4669 | size_t bytes2 = bytes2_arg; |
| 4670 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4671 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4672 | uint8_t *output_buffer = NULL; |
| 4673 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4674 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4675 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4676 | size_t length; |
| 4677 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4678 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4679 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4680 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4681 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4682 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4683 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4684 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4685 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 4686 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4687 | |
| 4688 | /* Derive some material and output it. */ |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4689 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4690 | input1->x, input1->len, |
| 4691 | input2->x, input2->len, capacity ) ) |
| 4692 | goto exit; |
| 4693 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4694 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4695 | output_buffer, |
| 4696 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4697 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4698 | |
| 4699 | /* Derive the same output again, but this time store it in key objects. */ |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4700 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4701 | input1->x, input1->len, |
| 4702 | input2->x, input2->len, capacity ) ) |
| 4703 | goto exit; |
| 4704 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4705 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4706 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 4707 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4708 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4709 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4710 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4711 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4712 | export_buffer, bytes1, |
| 4713 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4714 | TEST_EQUAL( length, bytes1 ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4715 | PSA_ASSERT( psa_destroy_key( derived_handle ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4716 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4717 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4718 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4719 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4720 | export_buffer + bytes1, bytes2, |
| 4721 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4722 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4723 | |
| 4724 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4725 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 4726 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4727 | |
| 4728 | exit: |
| 4729 | mbedtls_free( output_buffer ); |
| 4730 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4731 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4732 | psa_destroy_key( base_handle ); |
| 4733 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4734 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4735 | } |
| 4736 | /* END_CASE */ |
| 4737 | |
| 4738 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4739 | void derive_key( int alg_arg, |
| 4740 | data_t *key_data, data_t *input1, data_t *input2, |
| 4741 | int type_arg, int bits_arg, |
| 4742 | int expected_status_arg ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4743 | { |
| 4744 | psa_key_handle_t base_handle = 0; |
| 4745 | psa_key_handle_t derived_handle = 0; |
| 4746 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4747 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4748 | size_t bits = bits_arg; |
| 4749 | psa_status_t expected_status = expected_status_arg; |
| 4750 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4751 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4752 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4753 | |
| 4754 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4755 | |
| 4756 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4757 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4758 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 4759 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 4760 | &base_handle ) ); |
| 4761 | |
| 4762 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4763 | input1->x, input1->len, |
| 4764 | input2->x, input2->len, SIZE_MAX ) ) |
| 4765 | goto exit; |
| 4766 | |
| 4767 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4768 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4769 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4770 | psa_set_key_bits( &derived_attributes, bits ); |
| 4771 | TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 4772 | &derived_handle ), |
| 4773 | expected_status ); |
| 4774 | |
| 4775 | exit: |
| 4776 | psa_key_derivation_abort( &operation ); |
| 4777 | psa_destroy_key( base_handle ); |
| 4778 | psa_destroy_key( derived_handle ); |
| 4779 | PSA_DONE( ); |
| 4780 | } |
| 4781 | /* END_CASE */ |
| 4782 | |
| 4783 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4784 | void key_agreement_setup( int alg_arg, |
| 4785 | int our_key_type_arg, data_t *our_key_data, |
| 4786 | data_t *peer_key_data, |
| 4787 | int expected_status_arg ) |
| 4788 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4789 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4790 | psa_algorithm_t alg = alg_arg; |
| 4791 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4792 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4793 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4794 | psa_status_t expected_status = expected_status_arg; |
| 4795 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4796 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4797 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4798 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4799 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4800 | psa_set_key_algorithm( &attributes, alg ); |
| 4801 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4802 | PSA_ASSERT( psa_import_key( &attributes, |
| 4803 | our_key_data->x, our_key_data->len, |
| 4804 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4805 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4806 | /* The tests currently include inputs that should fail at either step. |
| 4807 | * Test cases that fail at the setup step should be changed to call |
| 4808 | * key_derivation_setup instead, and this function should be renamed |
| 4809 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4810 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4811 | if( status == PSA_SUCCESS ) |
| 4812 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4813 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 4814 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 4815 | our_key, |
| 4816 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4817 | expected_status ); |
| 4818 | } |
| 4819 | else |
| 4820 | { |
| 4821 | TEST_ASSERT( status == expected_status ); |
| 4822 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4823 | |
| 4824 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4825 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4826 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4827 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4828 | } |
| 4829 | /* END_CASE */ |
| 4830 | |
| 4831 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4832 | void raw_key_agreement( int alg_arg, |
| 4833 | int our_key_type_arg, data_t *our_key_data, |
| 4834 | data_t *peer_key_data, |
| 4835 | data_t *expected_output ) |
| 4836 | { |
| 4837 | psa_key_handle_t our_key = 0; |
| 4838 | psa_algorithm_t alg = alg_arg; |
| 4839 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4840 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4841 | unsigned char *output = NULL; |
| 4842 | size_t output_length = ~0; |
| 4843 | |
| 4844 | ASSERT_ALLOC( output, expected_output->len ); |
| 4845 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4846 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4847 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4848 | psa_set_key_algorithm( &attributes, alg ); |
| 4849 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4850 | PSA_ASSERT( psa_import_key( &attributes, |
| 4851 | our_key_data->x, our_key_data->len, |
| 4852 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4853 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4854 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 4855 | peer_key_data->x, peer_key_data->len, |
| 4856 | output, expected_output->len, |
| 4857 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4858 | ASSERT_COMPARE( output, output_length, |
| 4859 | expected_output->x, expected_output->len ); |
| 4860 | |
| 4861 | exit: |
| 4862 | mbedtls_free( output ); |
| 4863 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4864 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4865 | } |
| 4866 | /* END_CASE */ |
| 4867 | |
| 4868 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4869 | void key_agreement_capacity( int alg_arg, |
| 4870 | int our_key_type_arg, data_t *our_key_data, |
| 4871 | data_t *peer_key_data, |
| 4872 | int expected_capacity_arg ) |
| 4873 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4874 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4875 | psa_algorithm_t alg = alg_arg; |
| 4876 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4877 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4878 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4879 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4880 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4881 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4882 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4883 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4884 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4885 | psa_set_key_algorithm( &attributes, alg ); |
| 4886 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4887 | PSA_ASSERT( psa_import_key( &attributes, |
| 4888 | our_key_data->x, our_key_data->len, |
| 4889 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4890 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4891 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4892 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4893 | &operation, |
| 4894 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4895 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4896 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4897 | { |
| 4898 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4899 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4900 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4901 | NULL, 0 ) ); |
| 4902 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4903 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4904 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4905 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4906 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4907 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4908 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4909 | /* Test the actual capacity by reading the output. */ |
| 4910 | while( actual_capacity > sizeof( output ) ) |
| 4911 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4912 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4913 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4914 | actual_capacity -= sizeof( output ); |
| 4915 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4916 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4917 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4918 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4919 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4920 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4921 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4922 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4923 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4924 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4925 | } |
| 4926 | /* END_CASE */ |
| 4927 | |
| 4928 | /* BEGIN_CASE */ |
| 4929 | void key_agreement_output( int alg_arg, |
| 4930 | int our_key_type_arg, data_t *our_key_data, |
| 4931 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4932 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4933 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4934 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4935 | psa_algorithm_t alg = alg_arg; |
| 4936 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4937 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4938 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4939 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4940 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4941 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 4942 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4943 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4944 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4945 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4946 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4947 | psa_set_key_algorithm( &attributes, alg ); |
| 4948 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4949 | PSA_ASSERT( psa_import_key( &attributes, |
| 4950 | our_key_data->x, our_key_data->len, |
| 4951 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4952 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4953 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4954 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4955 | &operation, |
| 4956 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4957 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4958 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4959 | { |
| 4960 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4961 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4962 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4963 | NULL, 0 ) ); |
| 4964 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4965 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4966 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4967 | actual_output, |
| 4968 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4969 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 4970 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4971 | if( expected_output2->len != 0 ) |
| 4972 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4973 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4974 | actual_output, |
| 4975 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4976 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 4977 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4978 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4979 | |
| 4980 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4981 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4982 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4983 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4984 | mbedtls_free( actual_output ); |
| 4985 | } |
| 4986 | /* END_CASE */ |
| 4987 | |
| 4988 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4989 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4990 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4991 | size_t bytes = bytes_arg; |
| 4992 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4993 | unsigned char *output = NULL; |
| 4994 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4995 | size_t i; |
| 4996 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4997 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4998 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 4999 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5000 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5001 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5002 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5003 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5004 | /* Run several times, to ensure that every output byte will be |
| 5005 | * nonzero at least once with overwhelming probability |
| 5006 | * (2^(-8*number_of_runs)). */ |
| 5007 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5008 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5009 | if( bytes != 0 ) |
| 5010 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5011 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5012 | |
| 5013 | /* Check that no more than bytes have been overwritten */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5014 | ASSERT_COMPARE( output + bytes, sizeof( trail ), |
| 5015 | trail, sizeof( trail ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5016 | |
| 5017 | for( i = 0; i < bytes; i++ ) |
| 5018 | { |
| 5019 | if( output[i] != 0 ) |
| 5020 | ++changed[i]; |
| 5021 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5022 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5023 | |
| 5024 | /* Check that every byte was changed to nonzero at least once. This |
| 5025 | * validates that psa_generate_random is overwriting every byte of |
| 5026 | * the output buffer. */ |
| 5027 | for( i = 0; i < bytes; i++ ) |
| 5028 | { |
| 5029 | TEST_ASSERT( changed[i] != 0 ); |
| 5030 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5031 | |
| 5032 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5033 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5034 | mbedtls_free( output ); |
| 5035 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5036 | } |
| 5037 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5038 | |
| 5039 | /* BEGIN_CASE */ |
| 5040 | void generate_key( int type_arg, |
| 5041 | int bits_arg, |
| 5042 | int usage_arg, |
| 5043 | int alg_arg, |
| 5044 | int expected_status_arg ) |
| 5045 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5046 | psa_key_handle_t handle = 0; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5047 | psa_key_type_t type = type_arg; |
| 5048 | psa_key_usage_t usage = usage_arg; |
| 5049 | size_t bits = bits_arg; |
| 5050 | psa_algorithm_t alg = alg_arg; |
| 5051 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5052 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5053 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5054 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5055 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5056 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5057 | psa_set_key_usage_flags( &attributes, usage ); |
| 5058 | psa_set_key_algorithm( &attributes, alg ); |
| 5059 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5060 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5061 | |
| 5062 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5063 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5064 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5065 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5066 | |
| 5067 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5068 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 5069 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5070 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5071 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5072 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5073 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5074 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5075 | |
| 5076 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5077 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5078 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5079 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5080 | } |
| 5081 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5082 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5083 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */ |
| 5084 | void generate_key_rsa( int bits_arg, |
| 5085 | data_t *e_arg, |
| 5086 | int expected_status_arg ) |
| 5087 | { |
| 5088 | psa_key_handle_t handle = 0; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5089 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5090 | size_t bits = bits_arg; |
| 5091 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5092 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5093 | psa_status_t expected_status = expected_status_arg; |
| 5094 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5095 | uint8_t *exported = NULL; |
| 5096 | size_t exported_size = |
| 5097 | PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
| 5098 | size_t exported_length = SIZE_MAX; |
| 5099 | uint8_t *e_read_buffer = NULL; |
| 5100 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5101 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5102 | size_t e_read_length = SIZE_MAX; |
| 5103 | |
| 5104 | if( e_arg->len == 0 || |
| 5105 | ( e_arg->len == 3 && |
| 5106 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5107 | { |
| 5108 | is_default_public_exponent = 1; |
| 5109 | e_read_size = 0; |
| 5110 | } |
| 5111 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5112 | ASSERT_ALLOC( exported, exported_size ); |
| 5113 | |
| 5114 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5115 | |
| 5116 | psa_set_key_usage_flags( &attributes, usage ); |
| 5117 | psa_set_key_algorithm( &attributes, alg ); |
| 5118 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5119 | e_arg->x, e_arg->len ) ); |
| 5120 | psa_set_key_bits( &attributes, bits ); |
| 5121 | |
| 5122 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5123 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5124 | if( expected_status != PSA_SUCCESS ) |
| 5125 | goto exit; |
| 5126 | |
| 5127 | /* Test the key information */ |
| 5128 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5129 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5130 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5131 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5132 | e_read_buffer, e_read_size, |
| 5133 | &e_read_length ) ); |
| 5134 | if( is_default_public_exponent ) |
| 5135 | TEST_EQUAL( e_read_length, 0 ); |
| 5136 | else |
| 5137 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5138 | |
| 5139 | /* Do something with the key according to its type and permitted usage. */ |
| 5140 | if( ! exercise_key( handle, usage, alg ) ) |
| 5141 | goto exit; |
| 5142 | |
| 5143 | /* Export the key and check the public exponent. */ |
| 5144 | PSA_ASSERT( psa_export_public_key( handle, |
| 5145 | exported, exported_size, |
| 5146 | &exported_length ) ); |
| 5147 | { |
| 5148 | uint8_t *p = exported; |
| 5149 | uint8_t *end = exported + exported_length; |
| 5150 | size_t len; |
| 5151 | /* RSAPublicKey ::= SEQUENCE { |
| 5152 | * modulus INTEGER, -- n |
| 5153 | * publicExponent INTEGER } -- e |
| 5154 | */ |
| 5155 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5156 | MBEDTLS_ASN1_SEQUENCE | |
| 5157 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5158 | TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
| 5159 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5160 | MBEDTLS_ASN1_INTEGER ) ); |
| 5161 | if( len >= 1 && p[0] == 0 ) |
| 5162 | { |
| 5163 | ++p; |
| 5164 | --len; |
| 5165 | } |
| 5166 | if( e_arg->len == 0 ) |
| 5167 | { |
| 5168 | TEST_EQUAL( len, 3 ); |
| 5169 | TEST_EQUAL( p[0], 1 ); |
| 5170 | TEST_EQUAL( p[1], 0 ); |
| 5171 | TEST_EQUAL( p[2], 1 ); |
| 5172 | } |
| 5173 | else |
| 5174 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5175 | } |
| 5176 | |
| 5177 | exit: |
| 5178 | psa_reset_key_attributes( &attributes ); |
| 5179 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5180 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5181 | mbedtls_free( e_read_buffer ); |
| 5182 | mbedtls_free( exported ); |
| 5183 | } |
| 5184 | /* END_CASE */ |
| 5185 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5186 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5187 | void persistent_key_load_key_from_storage( data_t *data, |
| 5188 | int type_arg, int bits_arg, |
| 5189 | int usage_flags_arg, int alg_arg, |
| 5190 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5191 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5192 | psa_key_id_t key_id = 1; |
| 5193 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5194 | psa_key_handle_t handle = 0; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5195 | psa_key_handle_t base_key = 0; |
| 5196 | psa_key_type_t type = type_arg; |
| 5197 | size_t bits = bits_arg; |
| 5198 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5199 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5200 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5201 | unsigned char *first_export = NULL; |
| 5202 | unsigned char *second_export = NULL; |
| 5203 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 5204 | size_t first_exported_length; |
| 5205 | size_t second_exported_length; |
| 5206 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5207 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5208 | { |
| 5209 | ASSERT_ALLOC( first_export, export_size ); |
| 5210 | ASSERT_ALLOC( second_export, export_size ); |
| 5211 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5212 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5213 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5214 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5215 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5216 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5217 | psa_set_key_algorithm( &attributes, alg ); |
| 5218 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5219 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5220 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5221 | switch( generation_method ) |
| 5222 | { |
| 5223 | case IMPORT_KEY: |
| 5224 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5225 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
| 5226 | &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5227 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5228 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5229 | case GENERATE_KEY: |
| 5230 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5231 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5232 | break; |
| 5233 | |
| 5234 | case DERIVE_KEY: |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5235 | { |
| 5236 | /* Create base key */ |
| 5237 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5238 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5239 | psa_set_key_usage_flags( &base_attributes, |
| 5240 | PSA_KEY_USAGE_DERIVE ); |
| 5241 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5242 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5243 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5244 | data->x, data->len, |
| 5245 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5246 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5247 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5248 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5249 | &operation, |
| 5250 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5251 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5252 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5253 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5254 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5255 | &operation, |
| 5256 | &handle ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5257 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5258 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
| 5259 | base_key = 0; |
| 5260 | } |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5261 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5262 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5263 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5264 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5265 | /* Export the key if permitted by the key policy. */ |
| 5266 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5267 | { |
| 5268 | PSA_ASSERT( psa_export_key( handle, |
| 5269 | first_export, export_size, |
| 5270 | &first_exported_length ) ); |
| 5271 | if( generation_method == IMPORT_KEY ) |
| 5272 | ASSERT_COMPARE( data->x, data->len, |
| 5273 | first_export, first_exported_length ); |
| 5274 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5275 | |
| 5276 | /* Shutdown and restart */ |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 5277 | PSA_ASSERT( psa_close_key( handle ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5278 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5279 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5280 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5281 | /* Check key slot still contains key data */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5282 | PSA_ASSERT( psa_open_key( key_id, &handle ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5283 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5284 | TEST_EQUAL( psa_get_key_id( &attributes ), key_id ); |
| 5285 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5286 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5287 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5288 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5289 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 5290 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5291 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5292 | /* Export the key again if permitted by the key policy. */ |
| 5293 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5294 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5295 | PSA_ASSERT( psa_export_key( handle, |
| 5296 | second_export, export_size, |
| 5297 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5298 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5299 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5303 | if( ! exercise_key( handle, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5304 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5305 | |
| 5306 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5307 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5308 | mbedtls_free( first_export ); |
| 5309 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5310 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5311 | psa_destroy_key( base_key ); |
| 5312 | if( handle == 0 ) |
| 5313 | { |
| 5314 | /* In case there was a test failure after creating the persistent key |
| 5315 | * but while it was not open, try to re-open the persistent key |
| 5316 | * to delete it. */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5317 | psa_open_key( key_id, &handle ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5318 | } |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5319 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5320 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5321 | } |
| 5322 | /* END_CASE */ |