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 | 1838e82 | 2019-06-20 12:40:56 +0200 | [diff] [blame] | 8 | #include "psa_crypto_helpers.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 9 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 10 | /* Tests that require more than 128kB of RAM plus change have this symbol |
| 11 | * as a dependency. Currently we always define this symbol, so the tests |
| 12 | * are always executed. In the future we should make this conditional |
| 13 | * so that tests that require a lot of memory are skipped on constrained |
| 14 | * platforms. */ |
| 15 | #define HAVE_RAM_AVAILABLE_128k |
| 16 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 17 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 18 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 19 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 20 | /* A hash algorithm that is known to be supported. |
| 21 | * |
| 22 | * This is used in some smoke tests. |
| 23 | */ |
| 24 | #if defined(MBEDTLS_MD2_C) |
| 25 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2 |
| 26 | #elif defined(MBEDTLS_MD4_C) |
| 27 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4 |
| 28 | #elif defined(MBEDTLS_MD5_C) |
| 29 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5 |
| 30 | /* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of |
| 31 | * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160 |
| 32 | * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be |
| 33 | * implausible anyway. */ |
| 34 | #elif defined(MBEDTLS_SHA1_C) |
| 35 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1 |
| 36 | #elif defined(MBEDTLS_SHA256_C) |
| 37 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256 |
| 38 | #elif defined(MBEDTLS_SHA512_C) |
| 39 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384 |
| 40 | #elif defined(MBEDTLS_SHA3_C) |
| 41 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256 |
| 42 | #else |
| 43 | #undef KNOWN_SUPPORTED_HASH_ALG |
| 44 | #endif |
| 45 | |
| 46 | /* A block cipher that is known to be supported. |
| 47 | * |
| 48 | * For simplicity's sake, stick to block ciphers with 16-byte blocks. |
| 49 | */ |
| 50 | #if defined(MBEDTLS_AES_C) |
| 51 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES |
| 52 | #elif defined(MBEDTLS_ARIA_C) |
| 53 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA |
| 54 | #elif defined(MBEDTLS_CAMELLIA_C) |
| 55 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA |
| 56 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER |
| 57 | #endif |
| 58 | |
| 59 | /* A MAC mode that is known to be supported. |
| 60 | * |
| 61 | * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or |
| 62 | * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER. |
| 63 | * |
| 64 | * This is used in some smoke tests. |
| 65 | */ |
| 66 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 67 | #define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) ) |
| 68 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC |
| 69 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C) |
| 70 | #define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC |
| 71 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 72 | #else |
| 73 | #undef KNOWN_SUPPORTED_MAC_ALG |
| 74 | #undef KNOWN_SUPPORTED_MAC_KEY_TYPE |
| 75 | #endif |
| 76 | |
| 77 | /* A cipher algorithm and key type that are known to be supported. |
| 78 | * |
| 79 | * This is used in some smoke tests. |
| 80 | */ |
| 81 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR) |
| 82 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR |
| 83 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 84 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING |
| 85 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB) |
| 86 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB |
| 87 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB) |
| 88 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB |
| 89 | #else |
| 90 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 91 | #endif |
| 92 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG) |
| 93 | #define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 94 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 95 | #elif defined(MBEDTLS_RC4_C) |
| 96 | #define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4 |
| 97 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4 |
| 98 | #else |
| 99 | #undef KNOWN_SUPPORTED_CIPHER_ALG |
| 100 | #undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE |
| 101 | #endif |
| 102 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 103 | /** Test if a buffer contains a constant byte value. |
| 104 | * |
| 105 | * `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] | 106 | * |
| 107 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 108 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 109 | * \param size Size of the buffer in bytes. |
| 110 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 111 | * \return 1 if the buffer is all-bits-zero. |
| 112 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 113 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 114 | 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] | 115 | { |
| 116 | size_t i; |
| 117 | for( i = 0; i < size; i++ ) |
| 118 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 119 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 120 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 121 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 122 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 123 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 124 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 125 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 126 | static int asn1_write_10x( unsigned char **p, |
| 127 | unsigned char *start, |
| 128 | size_t bits, |
| 129 | unsigned char x ) |
| 130 | { |
| 131 | int ret; |
| 132 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 133 | if( bits == 0 ) |
| 134 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 135 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 136 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 137 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 138 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 139 | *p -= len; |
| 140 | ( *p )[len-1] = x; |
| 141 | if( bits % 8 == 0 ) |
| 142 | ( *p )[1] |= 1; |
| 143 | else |
| 144 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 145 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 146 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 147 | MBEDTLS_ASN1_INTEGER ) ); |
| 148 | return( len ); |
| 149 | } |
| 150 | |
| 151 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 152 | size_t buffer_size, |
| 153 | unsigned char **p, |
| 154 | size_t bits, |
| 155 | int keypair ) |
| 156 | { |
| 157 | size_t half_bits = ( bits + 1 ) / 2; |
| 158 | int ret; |
| 159 | int len = 0; |
| 160 | /* Construct something that looks like a DER encoding of |
| 161 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 162 | * RSAPrivateKey ::= SEQUENCE { |
| 163 | * version Version, |
| 164 | * modulus INTEGER, -- n |
| 165 | * publicExponent INTEGER, -- e |
| 166 | * privateExponent INTEGER, -- d |
| 167 | * prime1 INTEGER, -- p |
| 168 | * prime2 INTEGER, -- q |
| 169 | * exponent1 INTEGER, -- d mod (p-1) |
| 170 | * exponent2 INTEGER, -- d mod (q-1) |
| 171 | * coefficient INTEGER, -- (inverse of q) mod p |
| 172 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 173 | * } |
| 174 | * Or, for a public key, the same structure with only |
| 175 | * version, modulus and publicExponent. |
| 176 | */ |
| 177 | *p = buffer + buffer_size; |
| 178 | if( keypair ) |
| 179 | { |
| 180 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 181 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 182 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 183 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 184 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 185 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 186 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 187 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 188 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 189 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 190 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 191 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 192 | } |
| 193 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 194 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 195 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 196 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 197 | if( keypair ) |
| 198 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 199 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 200 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 201 | { |
| 202 | const unsigned char tag = |
| 203 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 204 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 205 | } |
| 206 | return( len ); |
| 207 | } |
| 208 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 209 | int exercise_mac_setup( psa_key_type_t key_type, |
| 210 | const unsigned char *key_bytes, |
| 211 | size_t key_length, |
| 212 | psa_algorithm_t alg, |
| 213 | psa_mac_operation_t *operation, |
| 214 | psa_status_t *status ) |
| 215 | { |
| 216 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 217 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 218 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 219 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 220 | psa_set_key_algorithm( &attributes, alg ); |
| 221 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 222 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 223 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 224 | |
| 225 | *status = psa_mac_sign_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 226 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 227 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 228 | /* If setup failed, reproduce the failure, so that the caller can |
| 229 | * test the resulting state of the operation object. */ |
| 230 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 231 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 232 | TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ), |
| 233 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | psa_destroy_key( handle ); |
| 237 | return( 1 ); |
| 238 | |
| 239 | exit: |
| 240 | psa_destroy_key( handle ); |
| 241 | return( 0 ); |
| 242 | } |
| 243 | |
| 244 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 245 | const unsigned char *key_bytes, |
| 246 | size_t key_length, |
| 247 | psa_algorithm_t alg, |
| 248 | psa_cipher_operation_t *operation, |
| 249 | psa_status_t *status ) |
| 250 | { |
| 251 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 252 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 253 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 254 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 255 | psa_set_key_algorithm( &attributes, alg ); |
| 256 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 257 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, |
| 258 | &handle ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 259 | |
| 260 | *status = psa_cipher_encrypt_setup( operation, handle, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 261 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 262 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 263 | /* If setup failed, reproduce the failure, so that the caller can |
| 264 | * test the resulting state of the operation object. */ |
| 265 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 266 | { |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 267 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ), |
| 268 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | psa_destroy_key( handle ); |
| 272 | return( 1 ); |
| 273 | |
| 274 | exit: |
| 275 | psa_destroy_key( handle ); |
| 276 | return( 0 ); |
| 277 | } |
| 278 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 279 | static int exercise_mac_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 280 | psa_key_usage_t usage, |
| 281 | psa_algorithm_t alg ) |
| 282 | { |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 283 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 284 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 285 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 286 | size_t mac_length = sizeof( mac ); |
| 287 | |
| 288 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 289 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 290 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 291 | handle, alg ) ); |
| 292 | PSA_ASSERT( psa_mac_update( &operation, |
| 293 | input, sizeof( input ) ) ); |
| 294 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 295 | mac, sizeof( mac ), |
| 296 | &mac_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 300 | { |
| 301 | psa_status_t verify_status = |
| 302 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 303 | PSA_SUCCESS : |
| 304 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 305 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 306 | handle, alg ) ); |
| 307 | PSA_ASSERT( psa_mac_update( &operation, |
| 308 | input, sizeof( input ) ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 309 | TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ), |
| 310 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | return( 1 ); |
| 314 | |
| 315 | exit: |
| 316 | psa_mac_abort( &operation ); |
| 317 | return( 0 ); |
| 318 | } |
| 319 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 320 | static int exercise_cipher_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 321 | psa_key_usage_t usage, |
| 322 | psa_algorithm_t alg ) |
| 323 | { |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 324 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 325 | unsigned char iv[16] = {0}; |
| 326 | size_t iv_length = sizeof( iv ); |
| 327 | const unsigned char plaintext[16] = "Hello, world..."; |
| 328 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 329 | size_t ciphertext_length = sizeof( ciphertext ); |
| 330 | unsigned char decrypted[sizeof( ciphertext )]; |
| 331 | size_t part_length; |
| 332 | |
| 333 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 334 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 335 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 336 | handle, alg ) ); |
| 337 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 338 | iv, sizeof( iv ), |
| 339 | &iv_length ) ); |
| 340 | PSA_ASSERT( psa_cipher_update( &operation, |
| 341 | plaintext, sizeof( plaintext ), |
| 342 | ciphertext, sizeof( ciphertext ), |
| 343 | &ciphertext_length ) ); |
| 344 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 345 | ciphertext + ciphertext_length, |
| 346 | sizeof( ciphertext ) - ciphertext_length, |
| 347 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 348 | ciphertext_length += part_length; |
| 349 | } |
| 350 | |
| 351 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 352 | { |
| 353 | psa_status_t status; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 354 | int maybe_invalid_padding = 0; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 355 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 356 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 357 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 358 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 359 | /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't |
| 360 | * have this macro yet. */ |
| 361 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( |
| 362 | psa_get_key_type( &attributes ) ); |
| 363 | maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 364 | } |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 365 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 366 | handle, alg ) ); |
| 367 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 368 | iv, iv_length ) ); |
| 369 | PSA_ASSERT( psa_cipher_update( &operation, |
| 370 | ciphertext, ciphertext_length, |
| 371 | decrypted, sizeof( decrypted ), |
| 372 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 373 | status = psa_cipher_finish( &operation, |
| 374 | decrypted + part_length, |
| 375 | sizeof( decrypted ) - part_length, |
| 376 | &part_length ); |
| 377 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 378 | * if the input is some aribtrary data rather than an actual |
| 379 | ciphertext, a padding error is likely. */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 380 | if( maybe_invalid_padding ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 381 | TEST_ASSERT( status == PSA_SUCCESS || |
| 382 | status == PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 383 | else |
| 384 | PSA_ASSERT( status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | return( 1 ); |
| 388 | |
| 389 | exit: |
| 390 | psa_cipher_abort( &operation ); |
| 391 | return( 0 ); |
| 392 | } |
| 393 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 394 | static int exercise_aead_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 395 | psa_key_usage_t usage, |
| 396 | psa_algorithm_t alg ) |
| 397 | { |
| 398 | unsigned char nonce[16] = {0}; |
| 399 | size_t nonce_length = sizeof( nonce ); |
| 400 | unsigned char plaintext[16] = "Hello, world..."; |
| 401 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 402 | size_t ciphertext_length = sizeof( ciphertext ); |
| 403 | size_t plaintext_length = sizeof( ciphertext ); |
| 404 | |
| 405 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 406 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 407 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 408 | nonce, nonce_length, |
| 409 | NULL, 0, |
| 410 | plaintext, sizeof( plaintext ), |
| 411 | ciphertext, sizeof( ciphertext ), |
| 412 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 416 | { |
| 417 | psa_status_t verify_status = |
| 418 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 419 | PSA_SUCCESS : |
| 420 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 421 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 422 | nonce, nonce_length, |
| 423 | NULL, 0, |
| 424 | ciphertext, ciphertext_length, |
| 425 | plaintext, sizeof( plaintext ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 426 | &plaintext_length ), |
| 427 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | return( 1 ); |
| 431 | |
| 432 | exit: |
| 433 | return( 0 ); |
| 434 | } |
| 435 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 436 | static int exercise_signature_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 437 | psa_key_usage_t usage, |
| 438 | psa_algorithm_t alg ) |
| 439 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 440 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 441 | size_t payload_length = 16; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 442 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 443 | size_t signature_length = sizeof( signature ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 444 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 445 | |
| 446 | /* If the policy allows signing with any hash, just pick one. */ |
| 447 | if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH ) |
| 448 | { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 449 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 450 | hash_alg = KNOWN_SUPPORTED_HASH_ALG; |
| 451 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 452 | #else |
| 453 | test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 454 | return( 1 ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 455 | #endif |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 456 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 457 | |
| 458 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 459 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 460 | /* Some algorithms require the payload to have the size of |
| 461 | * the hash encoded in the algorithm. Use this input size |
| 462 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 463 | if( hash_alg != 0 ) |
| 464 | payload_length = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 465 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 466 | payload, payload_length, |
| 467 | signature, sizeof( signature ), |
| 468 | &signature_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 472 | { |
| 473 | psa_status_t verify_status = |
| 474 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 475 | PSA_SUCCESS : |
| 476 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 477 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 478 | payload, payload_length, |
| 479 | signature, signature_length ), |
| 480 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | return( 1 ); |
| 484 | |
| 485 | exit: |
| 486 | return( 0 ); |
| 487 | } |
| 488 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 489 | static int exercise_asymmetric_encryption_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 490 | psa_key_usage_t usage, |
| 491 | psa_algorithm_t alg ) |
| 492 | { |
| 493 | unsigned char plaintext[256] = "Hello, world..."; |
| 494 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 495 | size_t ciphertext_length = sizeof( ciphertext ); |
| 496 | size_t plaintext_length = 16; |
| 497 | |
| 498 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 499 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 500 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 501 | plaintext, plaintext_length, |
| 502 | NULL, 0, |
| 503 | ciphertext, sizeof( ciphertext ), |
| 504 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 508 | { |
| 509 | psa_status_t status = |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 510 | psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 511 | ciphertext, ciphertext_length, |
| 512 | NULL, 0, |
| 513 | plaintext, sizeof( plaintext ), |
| 514 | &plaintext_length ); |
| 515 | TEST_ASSERT( status == PSA_SUCCESS || |
| 516 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 517 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 518 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 519 | } |
| 520 | |
| 521 | return( 1 ); |
| 522 | |
| 523 | exit: |
| 524 | return( 0 ); |
| 525 | } |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 526 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 527 | static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation, |
| 528 | psa_key_handle_t handle, |
| 529 | psa_algorithm_t alg, |
| 530 | unsigned char* input1, size_t input1_length, |
| 531 | unsigned char* input2, size_t input2_length, |
| 532 | size_t capacity ) |
| 533 | { |
| 534 | PSA_ASSERT( psa_key_derivation_setup( operation, alg ) ); |
| 535 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 536 | { |
| 537 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 538 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 539 | input1, input1_length ) ); |
| 540 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 541 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 542 | handle ) ); |
| 543 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 544 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 545 | input2, |
| 546 | input2_length ) ); |
| 547 | } |
| 548 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 549 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 550 | { |
| 551 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 552 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 553 | input1, input1_length ) ); |
| 554 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 555 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 556 | handle ) ); |
| 557 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 558 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 559 | input2, input2_length ) ); |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | TEST_ASSERT( ! "Key derivation algorithm not supported" ); |
| 564 | } |
| 565 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 566 | if( capacity != SIZE_MAX ) |
| 567 | PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) ); |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 568 | |
| 569 | return( 1 ); |
| 570 | |
| 571 | exit: |
| 572 | return( 0 ); |
| 573 | } |
| 574 | |
| 575 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 576 | static int exercise_key_derivation_key( psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 577 | psa_key_usage_t usage, |
| 578 | psa_algorithm_t alg ) |
| 579 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 580 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 581 | unsigned char input1[] = "Input 1"; |
| 582 | size_t input1_length = sizeof( input1 ); |
| 583 | unsigned char input2[] = "Input 2"; |
| 584 | size_t input2_length = sizeof( input2 ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 585 | unsigned char output[1]; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 586 | size_t capacity = sizeof( output ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 587 | |
| 588 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 589 | { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 590 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 591 | input1, input1_length, |
| 592 | input2, input2_length, capacity ) ) |
| 593 | goto exit; |
Gilles Peskine | 7607cd6 | 2019-05-29 17:35:00 +0200 | [diff] [blame] | 594 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 595 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 596 | output, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 597 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 598 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | return( 1 ); |
| 602 | |
| 603 | exit: |
| 604 | return( 0 ); |
| 605 | } |
| 606 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 607 | /* We need two keys to exercise key agreement. Exercise the |
| 608 | * private key against its own public key. */ |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 609 | static psa_status_t key_agreement_with_self( |
| 610 | psa_key_derivation_operation_t *operation, |
| 611 | psa_key_handle_t handle ) |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 612 | { |
| 613 | psa_key_type_t private_key_type; |
| 614 | psa_key_type_t public_key_type; |
| 615 | size_t key_bits; |
| 616 | uint8_t *public_key = NULL; |
| 617 | size_t public_key_length; |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 618 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 619 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 620 | * 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] | 621 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 622 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 623 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 624 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 625 | private_key_type = psa_get_key_type( &attributes ); |
| 626 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 627 | 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] | 628 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 629 | ASSERT_ALLOC( public_key, public_key_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 630 | PSA_ASSERT( psa_export_public_key( handle, |
| 631 | public_key, public_key_length, |
| 632 | &public_key_length ) ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 633 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 634 | status = psa_key_derivation_key_agreement( |
| 635 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle, |
| 636 | public_key, public_key_length ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 637 | exit: |
| 638 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 639 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 640 | return( status ); |
| 641 | } |
| 642 | |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 643 | /* We need two keys to exercise key agreement. Exercise the |
| 644 | * private key against its own public key. */ |
| 645 | static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg, |
| 646 | psa_key_handle_t handle ) |
| 647 | { |
| 648 | psa_key_type_t private_key_type; |
| 649 | psa_key_type_t public_key_type; |
| 650 | size_t key_bits; |
| 651 | uint8_t *public_key = NULL; |
| 652 | size_t public_key_length; |
| 653 | uint8_t output[1024]; |
| 654 | size_t output_length; |
| 655 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 656 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 657 | * 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] | 658 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 659 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 660 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 661 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 662 | private_key_type = psa_get_key_type( &attributes ); |
| 663 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 664 | 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] | 665 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 666 | ASSERT_ALLOC( public_key, public_key_length ); |
| 667 | PSA_ASSERT( psa_export_public_key( handle, |
| 668 | public_key, public_key_length, |
| 669 | &public_key_length ) ); |
| 670 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 671 | status = psa_raw_key_agreement( alg, handle, |
| 672 | public_key, public_key_length, |
| 673 | output, sizeof( output ), &output_length ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 674 | exit: |
| 675 | mbedtls_free( public_key ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 676 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 677 | return( status ); |
| 678 | } |
| 679 | |
| 680 | static int exercise_raw_key_agreement_key( psa_key_handle_t handle, |
| 681 | psa_key_usage_t usage, |
| 682 | psa_algorithm_t alg ) |
| 683 | { |
| 684 | int ok = 0; |
| 685 | |
| 686 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 687 | { |
| 688 | /* We need two keys to exercise key agreement. Exercise the |
| 689 | * private key against its own public key. */ |
| 690 | PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) ); |
| 691 | } |
| 692 | ok = 1; |
| 693 | |
| 694 | exit: |
| 695 | return( ok ); |
| 696 | } |
| 697 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 698 | static int exercise_key_agreement_key( psa_key_handle_t handle, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 699 | psa_key_usage_t usage, |
| 700 | psa_algorithm_t alg ) |
| 701 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 702 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 703 | unsigned char output[1]; |
| 704 | int ok = 0; |
| 705 | |
| 706 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 707 | { |
| 708 | /* We need two keys to exercise key agreement. Exercise the |
| 709 | * private key against its own public key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 710 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 711 | PSA_ASSERT( key_agreement_with_self( &operation, handle ) ); |
| 712 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 713 | output, |
| 714 | sizeof( output ) ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 715 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 716 | } |
| 717 | ok = 1; |
| 718 | |
| 719 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 720 | return( ok ); |
| 721 | } |
| 722 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 723 | static int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 724 | size_t min_bits, size_t max_bits, |
| 725 | int must_be_odd ) |
| 726 | { |
| 727 | size_t len; |
| 728 | size_t actual_bits; |
| 729 | unsigned char msb; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 730 | TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 731 | MBEDTLS_ASN1_INTEGER ), |
| 732 | 0 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 733 | /* Tolerate a slight departure from DER encoding: |
| 734 | * - 0 may be represented by an empty string or a 1-byte string. |
| 735 | * - The sign bit may be used as a value bit. */ |
| 736 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 737 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 738 | { |
| 739 | ++( *p ); |
| 740 | --len; |
| 741 | } |
| 742 | if( min_bits == 0 && len == 0 ) |
| 743 | return( 1 ); |
| 744 | msb = ( *p )[0]; |
| 745 | TEST_ASSERT( msb != 0 ); |
| 746 | actual_bits = 8 * ( len - 1 ); |
| 747 | while( msb != 0 ) |
| 748 | { |
| 749 | msb >>= 1; |
| 750 | ++actual_bits; |
| 751 | } |
| 752 | TEST_ASSERT( actual_bits >= min_bits ); |
| 753 | TEST_ASSERT( actual_bits <= max_bits ); |
| 754 | if( must_be_odd ) |
| 755 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 756 | *p += len; |
| 757 | return( 1 ); |
| 758 | exit: |
| 759 | return( 0 ); |
| 760 | } |
| 761 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 762 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 763 | uint8_t *exported, size_t exported_length ) |
| 764 | { |
| 765 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 766 | TEST_EQUAL( exported_length, ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 767 | else |
| 768 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 769 | |
| 770 | #if defined(MBEDTLS_DES_C) |
| 771 | if( type == PSA_KEY_TYPE_DES ) |
| 772 | { |
| 773 | /* Check the parity bits. */ |
| 774 | unsigned i; |
| 775 | for( i = 0; i < bits / 8; i++ ) |
| 776 | { |
| 777 | unsigned bit_count = 0; |
| 778 | unsigned m; |
| 779 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 780 | { |
| 781 | if( exported[i] & m ) |
| 782 | ++bit_count; |
| 783 | } |
| 784 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 785 | } |
| 786 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 787 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 788 | #endif |
| 789 | |
| 790 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 791 | if( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 792 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 793 | uint8_t *p = exported; |
| 794 | uint8_t *end = exported + exported_length; |
| 795 | size_t len; |
| 796 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 797 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 798 | * modulus INTEGER, -- n |
| 799 | * publicExponent INTEGER, -- e |
| 800 | * privateExponent INTEGER, -- d |
| 801 | * prime1 INTEGER, -- p |
| 802 | * prime2 INTEGER, -- q |
| 803 | * exponent1 INTEGER, -- d mod (p-1) |
| 804 | * exponent2 INTEGER, -- d mod (q-1) |
| 805 | * coefficient INTEGER, -- (inverse of q) mod p |
| 806 | * } |
| 807 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 808 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 809 | MBEDTLS_ASN1_SEQUENCE | |
| 810 | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); |
| 811 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 812 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 813 | goto exit; |
| 814 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 815 | goto exit; |
| 816 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 817 | goto exit; |
| 818 | /* Require d to be at least half the size of n. */ |
| 819 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 820 | goto exit; |
| 821 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 822 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 823 | goto exit; |
| 824 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 825 | goto exit; |
| 826 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 827 | goto exit; |
| 828 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 829 | goto exit; |
| 830 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 831 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 832 | TEST_EQUAL( p, end ); |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 833 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 834 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 835 | #endif /* MBEDTLS_RSA_C */ |
| 836 | |
| 837 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 838 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 839 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 840 | /* Just the secret value */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 841 | TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 842 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 843 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 844 | #endif /* MBEDTLS_ECP_C */ |
| 845 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 846 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 847 | { |
| 848 | uint8_t *p = exported; |
| 849 | uint8_t *end = exported + exported_length; |
| 850 | size_t len; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 851 | #if defined(MBEDTLS_RSA_C) |
| 852 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 853 | { |
| 854 | /* RSAPublicKey ::= SEQUENCE { |
| 855 | * modulus INTEGER, -- n |
| 856 | * publicExponent INTEGER } -- e |
| 857 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 858 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 859 | MBEDTLS_ASN1_SEQUENCE | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 860 | MBEDTLS_ASN1_CONSTRUCTED ), |
| 861 | 0 ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 862 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 863 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 864 | goto exit; |
| 865 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 866 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 867 | TEST_EQUAL( p, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 868 | } |
| 869 | else |
| 870 | #endif /* MBEDTLS_RSA_C */ |
| 871 | #if defined(MBEDTLS_ECP_C) |
| 872 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 873 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 874 | /* The representation of an ECC public key is: |
| 875 | * - The byte 0x04; |
| 876 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 877 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 878 | * - where m is the bit size associated with the curve. |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 879 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 880 | TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end ); |
| 881 | TEST_EQUAL( p[0], 4 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 882 | } |
| 883 | else |
| 884 | #endif /* MBEDTLS_ECP_C */ |
| 885 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 886 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 887 | mbedtls_snprintf( message, sizeof( message ), |
| 888 | "No sanity check for public key type=0x%08lx", |
| 889 | (unsigned long) type ); |
| 890 | test_fail( message, __LINE__, __FILE__ ); |
| 891 | return( 0 ); |
| 892 | } |
| 893 | } |
| 894 | else |
| 895 | |
| 896 | { |
| 897 | /* No sanity checks for other types */ |
| 898 | } |
| 899 | |
| 900 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 901 | |
| 902 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 903 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 904 | } |
| 905 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 906 | static int exercise_export_key( psa_key_handle_t handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 907 | psa_key_usage_t usage ) |
| 908 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 909 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 910 | uint8_t *exported = NULL; |
| 911 | size_t exported_size = 0; |
| 912 | size_t exported_length = 0; |
| 913 | int ok = 0; |
| 914 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 915 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 916 | |
| 917 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 918 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 919 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 920 | TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ), |
| 921 | PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 922 | ok = 1; |
| 923 | goto exit; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 926 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ), |
| 927 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 928 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 929 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 930 | PSA_ASSERT( psa_export_key( handle, |
| 931 | exported, exported_size, |
| 932 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 933 | ok = exported_key_sanity_check( psa_get_key_type( &attributes ), |
| 934 | psa_get_key_bits( &attributes ), |
| 935 | exported, exported_length ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 936 | |
| 937 | exit: |
| 938 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 939 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 940 | return( ok ); |
| 941 | } |
| 942 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 943 | static int exercise_export_public_key( psa_key_handle_t handle ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 944 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 945 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 946 | psa_key_type_t public_type; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 947 | uint8_t *exported = NULL; |
| 948 | size_t exported_size = 0; |
| 949 | size_t exported_length = 0; |
| 950 | int ok = 0; |
| 951 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 952 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 953 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 954 | { |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 955 | TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 956 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 957 | return( 1 ); |
| 958 | } |
| 959 | |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 960 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 961 | psa_get_key_type( &attributes ) ); |
| 962 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, |
| 963 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 964 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 965 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 966 | PSA_ASSERT( psa_export_public_key( handle, |
| 967 | exported, exported_size, |
| 968 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 969 | ok = exported_key_sanity_check( public_type, |
| 970 | psa_get_key_bits( &attributes ), |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 971 | exported, exported_length ); |
| 972 | |
| 973 | exit: |
| 974 | mbedtls_free( exported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 975 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 976 | return( ok ); |
| 977 | } |
| 978 | |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 979 | /** Do smoke tests on a key. |
| 980 | * |
| 981 | * Perform one of each operation indicated by \p alg (decrypt/encrypt, |
| 982 | * sign/verify, or derivation) that is permitted according to \p usage. |
| 983 | * \p usage and \p alg should correspond to the expected policy on the |
| 984 | * key. |
| 985 | * |
| 986 | * Export the key if permitted by \p usage, and check that the output |
| 987 | * looks sensible. If \p usage forbids export, check that |
| 988 | * \p psa_export_key correctly rejects the attempt. If the key is |
| 989 | * asymmetric, also check \p psa_export_public_key. |
| 990 | * |
| 991 | * If the key fails the tests, this function calls the test framework's |
| 992 | * `test_fail` function and returns false. Otherwise this function returns |
| 993 | * true. Therefore it should be used as follows: |
| 994 | * ``` |
| 995 | * if( ! exercise_key( ... ) ) goto exit; |
| 996 | * ``` |
| 997 | * |
| 998 | * \param handle The key to exercise. It should be capable of performing |
| 999 | * \p alg. |
| 1000 | * \param usage The usage flags to assume. |
| 1001 | * \param alg The algorithm to exercise. |
| 1002 | * |
| 1003 | * \retval 0 The key failed the smoke tests. |
| 1004 | * \retval 1 The key passed the smoke tests. |
| 1005 | */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1006 | static int exercise_key( psa_key_handle_t handle, |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1007 | psa_key_usage_t usage, |
| 1008 | psa_algorithm_t alg ) |
| 1009 | { |
| 1010 | int ok; |
| 1011 | if( alg == 0 ) |
| 1012 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 1013 | else if( PSA_ALG_IS_MAC( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1014 | ok = exercise_mac_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1015 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1016 | ok = exercise_cipher_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1017 | else if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1018 | ok = exercise_aead_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1019 | else if( PSA_ALG_IS_SIGN( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1020 | ok = exercise_signature_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1021 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1022 | ok = exercise_asymmetric_encryption_key( handle, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1023 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1024 | ok = exercise_key_derivation_key( handle, usage, alg ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 1025 | else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 1026 | ok = exercise_raw_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1027 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1028 | ok = exercise_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1029 | else |
| 1030 | { |
| 1031 | char message[40]; |
| 1032 | mbedtls_snprintf( message, sizeof( message ), |
| 1033 | "No code to exercise alg=0x%08lx", |
| 1034 | (unsigned long) alg ); |
| 1035 | test_fail( message, __LINE__, __FILE__ ); |
| 1036 | ok = 0; |
| 1037 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1038 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1039 | ok = ok && exercise_export_key( handle, usage ); |
| 1040 | ok = ok && exercise_export_public_key( handle ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1041 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1042 | return( ok ); |
| 1043 | } |
| 1044 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1045 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 1046 | psa_algorithm_t alg ) |
| 1047 | { |
| 1048 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 1049 | { |
| 1050 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1051 | PSA_KEY_USAGE_VERIFY : |
| 1052 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 1053 | } |
| 1054 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 1055 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 1056 | { |
| 1057 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1058 | PSA_KEY_USAGE_ENCRYPT : |
| 1059 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 1060 | } |
| 1061 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 1062 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 1063 | { |
| 1064 | return( PSA_KEY_USAGE_DERIVE ); |
| 1065 | } |
| 1066 | else |
| 1067 | { |
| 1068 | return( 0 ); |
| 1069 | } |
| 1070 | |
| 1071 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1072 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1073 | static int test_operations_on_invalid_handle( psa_key_handle_t handle ) |
| 1074 | { |
| 1075 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1076 | uint8_t buffer[1]; |
| 1077 | size_t length; |
| 1078 | int ok = 0; |
| 1079 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1080 | psa_set_key_id( &attributes, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1081 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 1082 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 1083 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
| 1084 | TEST_EQUAL( psa_get_key_attributes( handle, &attributes ), |
| 1085 | PSA_ERROR_INVALID_HANDLE ); |
| 1086 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 1087 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1088 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1089 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1090 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 1091 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 1092 | |
| 1093 | TEST_EQUAL( psa_export_key( handle, |
| 1094 | buffer, sizeof( buffer ), &length ), |
| 1095 | PSA_ERROR_INVALID_HANDLE ); |
| 1096 | TEST_EQUAL( psa_export_public_key( handle, |
| 1097 | buffer, sizeof( buffer ), &length ), |
| 1098 | PSA_ERROR_INVALID_HANDLE ); |
| 1099 | |
| 1100 | TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE ); |
| 1101 | TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE ); |
| 1102 | |
| 1103 | ok = 1; |
| 1104 | |
| 1105 | exit: |
| 1106 | psa_reset_key_attributes( &attributes ); |
| 1107 | return( ok ); |
| 1108 | } |
| 1109 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1110 | /* An overapproximation of the amount of storage needed for a key of the |
| 1111 | * given type and with the given content. The API doesn't make it easy |
| 1112 | * to find a good value for the size. The current implementation doesn't |
| 1113 | * care about the value anyway. */ |
| 1114 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 1115 | ( data )->len |
| 1116 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1117 | typedef enum { |
| 1118 | IMPORT_KEY = 0, |
| 1119 | GENERATE_KEY = 1, |
| 1120 | DERIVE_KEY = 2 |
| 1121 | } generate_method; |
| 1122 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1123 | /* END_HEADER */ |
| 1124 | |
| 1125 | /* BEGIN_DEPENDENCIES |
| 1126 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1127 | * END_DEPENDENCIES |
| 1128 | */ |
| 1129 | |
| 1130 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1131 | void static_checks( ) |
| 1132 | { |
| 1133 | size_t max_truncated_mac_size = |
| 1134 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1135 | |
| 1136 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1137 | * encoding. The shifted mask is the maximum truncated value. The |
| 1138 | * untruncated algorithm may be one byte larger. */ |
| 1139 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 1140 | } |
| 1141 | /* END_CASE */ |
| 1142 | |
| 1143 | /* BEGIN_CASE */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1144 | void attributes_set_get( int id_arg, int lifetime_arg, |
| 1145 | int usage_flags_arg, int alg_arg, |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1146 | int type_arg, int bits_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1147 | { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1148 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1149 | psa_key_id_t id = id_arg; |
| 1150 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1151 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 1152 | psa_algorithm_t alg = alg_arg; |
| 1153 | psa_key_type_t type = type_arg; |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1154 | size_t bits = bits_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1155 | |
| 1156 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1157 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1158 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1159 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1160 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1161 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1162 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1163 | psa_set_key_id( &attributes, id ); |
| 1164 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1165 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 1166 | psa_set_key_algorithm( &attributes, alg ); |
| 1167 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1168 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1169 | |
| 1170 | TEST_EQUAL( psa_get_key_id( &attributes ), id ); |
| 1171 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime ); |
| 1172 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 1173 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
| 1174 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1175 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1176 | |
| 1177 | psa_reset_key_attributes( &attributes ); |
| 1178 | |
| 1179 | TEST_EQUAL( psa_get_key_id( &attributes ), 0 ); |
| 1180 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1181 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1182 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1183 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1184 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1185 | } |
| 1186 | /* END_CASE */ |
| 1187 | |
| 1188 | /* BEGIN_CASE */ |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1189 | void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg, |
| 1190 | int expected_id_arg, int expected_lifetime_arg ) |
| 1191 | { |
| 1192 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1193 | psa_key_id_t id1 = id1_arg; |
| 1194 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1195 | psa_key_id_t id2 = id2_arg; |
| 1196 | psa_key_id_t expected_id = expected_id_arg; |
| 1197 | psa_key_lifetime_t expected_lifetime = expected_lifetime_arg; |
| 1198 | |
| 1199 | if( id1_arg != -1 ) |
| 1200 | psa_set_key_id( &attributes, id1 ); |
| 1201 | if( lifetime_arg != -1 ) |
| 1202 | psa_set_key_lifetime( &attributes, lifetime ); |
| 1203 | if( id2_arg != -1 ) |
| 1204 | psa_set_key_id( &attributes, id2 ); |
| 1205 | |
| 1206 | TEST_EQUAL( psa_get_key_id( &attributes ), expected_id ); |
| 1207 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime ); |
| 1208 | } |
| 1209 | /* END_CASE */ |
| 1210 | |
| 1211 | /* BEGIN_CASE */ |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1212 | void import( data_t *data, int type_arg, |
| 1213 | int attr_bits_arg, |
| 1214 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1215 | { |
| 1216 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1217 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1218 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1219 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1220 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1221 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1222 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1223 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1224 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1225 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1226 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1227 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1228 | status = psa_import_key( &attributes, data->x, data->len, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1229 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1230 | if( status != PSA_SUCCESS ) |
| 1231 | goto exit; |
| 1232 | |
| 1233 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1234 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1235 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1236 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1237 | |
| 1238 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1239 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1240 | |
| 1241 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1242 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1243 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1244 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1245 | } |
| 1246 | /* END_CASE */ |
| 1247 | |
| 1248 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1249 | void import_large_key( int type_arg, int byte_size_arg, |
| 1250 | int expected_status_arg ) |
| 1251 | { |
| 1252 | psa_key_type_t type = type_arg; |
| 1253 | size_t byte_size = byte_size_arg; |
| 1254 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1255 | psa_status_t expected_status = expected_status_arg; |
| 1256 | psa_key_handle_t handle = 0; |
| 1257 | psa_status_t status; |
| 1258 | uint8_t *buffer = NULL; |
| 1259 | size_t buffer_size = byte_size + 1; |
| 1260 | size_t n; |
| 1261 | |
| 1262 | /* It would be better to skip the test than fail it if the allocation |
| 1263 | * fails, but the test framework doesn't support this yet. */ |
| 1264 | ASSERT_ALLOC( buffer, buffer_size ); |
| 1265 | memset( buffer, 'K', byte_size ); |
| 1266 | |
| 1267 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1268 | |
| 1269 | /* Try importing the key */ |
| 1270 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1271 | psa_set_key_type( &attributes, type ); |
| 1272 | status = psa_import_key( &attributes, buffer, byte_size, &handle ); |
| 1273 | TEST_EQUAL( status, expected_status ); |
| 1274 | |
| 1275 | if( status == PSA_SUCCESS ) |
| 1276 | { |
| 1277 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1278 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1279 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1280 | PSA_BYTES_TO_BITS( byte_size ) ); |
| 1281 | memset( buffer, 0, byte_size + 1 ); |
| 1282 | PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) ); |
| 1283 | for( n = 0; n < byte_size; n++ ) |
| 1284 | TEST_EQUAL( buffer[n], 'K' ); |
| 1285 | for( n = byte_size; n < buffer_size; n++ ) |
| 1286 | TEST_EQUAL( buffer[n], 0 ); |
| 1287 | } |
| 1288 | |
| 1289 | exit: |
| 1290 | psa_destroy_key( handle ); |
| 1291 | PSA_DONE( ); |
| 1292 | mbedtls_free( buffer ); |
| 1293 | } |
| 1294 | /* END_CASE */ |
| 1295 | |
| 1296 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1297 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1298 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1299 | psa_key_handle_t handle = 0; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1300 | size_t bits = bits_arg; |
| 1301 | psa_status_t expected_status = expected_status_arg; |
| 1302 | psa_status_t status; |
| 1303 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1304 | 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] | 1305 | size_t buffer_size = /* Slight overapproximations */ |
| 1306 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1307 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1308 | unsigned char *p; |
| 1309 | int ret; |
| 1310 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1311 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1312 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1313 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1314 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1315 | |
| 1316 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1317 | bits, keypair ) ) >= 0 ); |
| 1318 | length = ret; |
| 1319 | |
| 1320 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1321 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1322 | status = psa_import_key( &attributes, p, length, &handle ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1323 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1324 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1325 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1326 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1327 | |
| 1328 | exit: |
| 1329 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1330 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1331 | } |
| 1332 | /* END_CASE */ |
| 1333 | |
| 1334 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1335 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1336 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1337 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1338 | int expected_bits, |
| 1339 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1340 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1341 | int canonical_input ) |
| 1342 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1343 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1344 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1345 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1346 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1347 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1348 | unsigned char *exported = NULL; |
| 1349 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1350 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1351 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1352 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1353 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1354 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1355 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1356 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1357 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1358 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1359 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1360 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1361 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1362 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1363 | psa_set_key_algorithm( &attributes, alg ); |
| 1364 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1365 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1366 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1367 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1368 | |
| 1369 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1370 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1371 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1372 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1373 | |
| 1374 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1375 | status = psa_export_key( handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1376 | exported, export_size, |
| 1377 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1378 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1379 | |
| 1380 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1381 | * and export_size. On errors, the exported length must be 0. */ |
| 1382 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1383 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1384 | TEST_ASSERT( exported_length <= export_size ); |
| 1385 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1386 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1387 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1388 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1389 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1390 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1391 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1392 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1393 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1394 | if( ! exercise_export_key( handle, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1395 | goto exit; |
| 1396 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1397 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1398 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1399 | else |
| 1400 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1401 | psa_key_handle_t handle2; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1402 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
| 1403 | &handle2 ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1404 | PSA_ASSERT( psa_export_key( handle2, |
| 1405 | reexported, |
| 1406 | export_size, |
| 1407 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1408 | ASSERT_COMPARE( exported, exported_length, |
| 1409 | reexported, reexported_length ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1410 | PSA_ASSERT( psa_close_key( handle2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1411 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1412 | 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] | 1413 | |
| 1414 | destroy: |
| 1415 | /* Destroy the key */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1416 | PSA_ASSERT( psa_destroy_key( handle ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1417 | test_operations_on_invalid_handle( handle ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1418 | |
| 1419 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1420 | mbedtls_free( exported ); |
| 1421 | mbedtls_free( reexported ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1422 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1423 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1424 | } |
| 1425 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1426 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1427 | /* BEGIN_CASE */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1428 | void invalid_handle( int handle ) |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1429 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1430 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1431 | test_operations_on_invalid_handle( handle ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1432 | |
| 1433 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1434 | PSA_DONE( ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1435 | } |
| 1436 | /* END_CASE */ |
| 1437 | |
| 1438 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1439 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1440 | int type_arg, |
| 1441 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1442 | int export_size_delta, |
| 1443 | int expected_export_status_arg, |
| 1444 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1445 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1446 | psa_key_handle_t handle = 0; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1447 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1448 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1449 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1450 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1451 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1452 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1453 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1454 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1455 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1456 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1457 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1458 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1459 | psa_set_key_algorithm( &attributes, alg ); |
| 1460 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1461 | |
| 1462 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1463 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1464 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1465 | /* Export the public key */ |
| 1466 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1467 | status = psa_export_public_key( handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1468 | exported, export_size, |
| 1469 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1470 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1471 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1472 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1473 | 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] | 1474 | size_t bits; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1475 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1476 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1477 | TEST_ASSERT( expected_public_key->len <= |
| 1478 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1479 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1480 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1481 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1482 | |
| 1483 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1484 | mbedtls_free( exported ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1485 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1486 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1487 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1488 | } |
| 1489 | /* END_CASE */ |
| 1490 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1491 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1492 | void import_and_exercise_key( data_t *data, |
| 1493 | int type_arg, |
| 1494 | int bits_arg, |
| 1495 | int alg_arg ) |
| 1496 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1497 | psa_key_handle_t handle = 0; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1498 | psa_key_type_t type = type_arg; |
| 1499 | size_t bits = bits_arg; |
| 1500 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1501 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1502 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1503 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1504 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1505 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1506 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1507 | psa_set_key_usage_flags( &attributes, usage ); |
| 1508 | psa_set_key_algorithm( &attributes, alg ); |
| 1509 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1510 | |
| 1511 | /* Import the key */ |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1512 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1513 | |
| 1514 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1515 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1516 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1517 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1518 | |
| 1519 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1520 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1521 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1522 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1523 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 1524 | test_operations_on_invalid_handle( handle ); |
| 1525 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1526 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1527 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1528 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1529 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1530 | } |
| 1531 | /* END_CASE */ |
| 1532 | |
| 1533 | /* BEGIN_CASE */ |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1534 | void key_policy( int usage_arg, int alg_arg ) |
| 1535 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1536 | psa_key_handle_t handle = 0; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1537 | psa_algorithm_t alg = alg_arg; |
| 1538 | psa_key_usage_t usage = usage_arg; |
| 1539 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 1540 | unsigned char key[32] = {0}; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1541 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1542 | |
| 1543 | memset( key, 0x2a, sizeof( key ) ); |
| 1544 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1545 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1546 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1547 | psa_set_key_usage_flags( &attributes, usage ); |
| 1548 | psa_set_key_algorithm( &attributes, alg ); |
| 1549 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1550 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1551 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1552 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1553 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1554 | TEST_EQUAL( psa_get_key_type( &attributes ), key_type ); |
| 1555 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage ); |
| 1556 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1557 | |
| 1558 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1559 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1560 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1561 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1562 | } |
| 1563 | /* END_CASE */ |
| 1564 | |
| 1565 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1566 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1567 | { |
| 1568 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1569 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1570 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1571 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1572 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1573 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1574 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1575 | |
| 1576 | memset( &zero, 0, sizeof( zero ) ); |
| 1577 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1578 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1579 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1580 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1581 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1582 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1583 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1584 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1585 | |
| 1586 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1587 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1588 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1589 | |
| 1590 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1591 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1592 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1593 | |
| 1594 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1595 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1596 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1597 | } |
| 1598 | /* END_CASE */ |
| 1599 | |
| 1600 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1601 | void mac_key_policy( int policy_usage, |
| 1602 | int policy_alg, |
| 1603 | int key_type, |
| 1604 | data_t *key_data, |
| 1605 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1606 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1607 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1608 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1609 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1610 | psa_status_t status; |
| 1611 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1612 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1613 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1614 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1615 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1616 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1617 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1618 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1619 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1620 | &handle ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1621 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1622 | status = psa_mac_sign_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1623 | if( policy_alg == exercise_alg && |
| 1624 | ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1625 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1626 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1627 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1628 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1629 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1630 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1631 | status = psa_mac_verify_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1632 | if( policy_alg == exercise_alg && |
| 1633 | ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1634 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1635 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1636 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1637 | |
| 1638 | exit: |
| 1639 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1640 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1641 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1642 | } |
| 1643 | /* END_CASE */ |
| 1644 | |
| 1645 | /* BEGIN_CASE */ |
| 1646 | void cipher_key_policy( int policy_usage, |
| 1647 | int policy_alg, |
| 1648 | int key_type, |
| 1649 | data_t *key_data, |
| 1650 | int exercise_alg ) |
| 1651 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1652 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1653 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1654 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1655 | psa_status_t status; |
| 1656 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1657 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1658 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1659 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1660 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1661 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1662 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1663 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1664 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1665 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1666 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1667 | if( policy_alg == exercise_alg && |
| 1668 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1669 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1670 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1671 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1672 | psa_cipher_abort( &operation ); |
| 1673 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1674 | status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1675 | if( policy_alg == exercise_alg && |
| 1676 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1677 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1678 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1679 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1680 | |
| 1681 | exit: |
| 1682 | psa_cipher_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1683 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1684 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1685 | } |
| 1686 | /* END_CASE */ |
| 1687 | |
| 1688 | /* BEGIN_CASE */ |
| 1689 | void aead_key_policy( int policy_usage, |
| 1690 | int policy_alg, |
| 1691 | int key_type, |
| 1692 | data_t *key_data, |
| 1693 | int nonce_length_arg, |
| 1694 | int tag_length_arg, |
| 1695 | int exercise_alg ) |
| 1696 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1697 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1698 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1699 | psa_status_t status; |
| 1700 | unsigned char nonce[16] = {0}; |
| 1701 | size_t nonce_length = nonce_length_arg; |
| 1702 | unsigned char tag[16]; |
| 1703 | size_t tag_length = tag_length_arg; |
| 1704 | size_t output_length; |
| 1705 | |
| 1706 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1707 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1708 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1709 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1710 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1711 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1712 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1713 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1714 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1715 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1716 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1717 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1718 | status = psa_aead_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1719 | nonce, nonce_length, |
| 1720 | NULL, 0, |
| 1721 | NULL, 0, |
| 1722 | tag, tag_length, |
| 1723 | &output_length ); |
| 1724 | if( policy_alg == exercise_alg && |
| 1725 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1726 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1727 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1728 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1729 | |
| 1730 | memset( tag, 0, sizeof( tag ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1731 | status = psa_aead_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1732 | nonce, nonce_length, |
| 1733 | NULL, 0, |
| 1734 | tag, tag_length, |
| 1735 | NULL, 0, |
| 1736 | &output_length ); |
| 1737 | if( policy_alg == exercise_alg && |
| 1738 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1739 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1740 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1741 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1742 | |
| 1743 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1744 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1745 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1746 | } |
| 1747 | /* END_CASE */ |
| 1748 | |
| 1749 | /* BEGIN_CASE */ |
| 1750 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1751 | int policy_alg, |
| 1752 | int key_type, |
| 1753 | data_t *key_data, |
| 1754 | int exercise_alg ) |
| 1755 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1756 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1757 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1758 | psa_status_t status; |
| 1759 | size_t key_bits; |
| 1760 | size_t buffer_length; |
| 1761 | unsigned char *buffer = NULL; |
| 1762 | size_t output_length; |
| 1763 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1764 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1765 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1766 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1767 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1768 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1769 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1770 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1771 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1772 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1773 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 1774 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1775 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1776 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1777 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1778 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1779 | status = psa_asymmetric_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1780 | NULL, 0, |
| 1781 | NULL, 0, |
| 1782 | buffer, buffer_length, |
| 1783 | &output_length ); |
| 1784 | if( policy_alg == exercise_alg && |
| 1785 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1786 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1787 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1788 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1789 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1790 | if( buffer_length != 0 ) |
| 1791 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1792 | status = psa_asymmetric_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1793 | buffer, buffer_length, |
| 1794 | NULL, 0, |
| 1795 | buffer, buffer_length, |
| 1796 | &output_length ); |
| 1797 | if( policy_alg == exercise_alg && |
| 1798 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1799 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1800 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1801 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1802 | |
| 1803 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1804 | psa_destroy_key( handle ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1805 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1806 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1807 | mbedtls_free( buffer ); |
| 1808 | } |
| 1809 | /* END_CASE */ |
| 1810 | |
| 1811 | /* BEGIN_CASE */ |
| 1812 | void asymmetric_signature_key_policy( int policy_usage, |
| 1813 | int policy_alg, |
| 1814 | int key_type, |
| 1815 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1816 | int exercise_alg, |
| 1817 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1818 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1819 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1820 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1821 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1822 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1823 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1824 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1825 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1826 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1827 | int compatible_alg = payload_length_arg > 0; |
| 1828 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1829 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
| 1830 | size_t signature_length; |
| 1831 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1832 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1833 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1834 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1835 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1836 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1837 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1838 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1839 | &handle ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1840 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1841 | status = psa_asymmetric_sign( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1842 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1843 | signature, sizeof( signature ), |
| 1844 | &signature_length ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1845 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1846 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1847 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1848 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1849 | |
| 1850 | memset( signature, 0, sizeof( signature ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1851 | status = psa_asymmetric_verify( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1852 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1853 | signature, sizeof( signature ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1854 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 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 | d5b3322 | 2018-06-18 22:20:03 +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 | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1862 | } |
| 1863 | /* END_CASE */ |
| 1864 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1865 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1866 | void derive_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 | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1874 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1875 | psa_status_t status; |
| 1876 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1877 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1878 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1879 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1880 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1881 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1882 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1883 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1884 | &handle ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1885 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1886 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1887 | |
| 1888 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1889 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1890 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1891 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1892 | &operation, |
| 1893 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1894 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1895 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1896 | |
| 1897 | status = psa_key_derivation_input_key( &operation, |
| 1898 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 1899 | handle ); |
| 1900 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1901 | if( policy_alg == exercise_alg && |
| 1902 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1903 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1904 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1905 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1906 | |
| 1907 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1908 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1909 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1910 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1911 | } |
| 1912 | /* END_CASE */ |
| 1913 | |
| 1914 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1915 | void agreement_key_policy( int policy_usage, |
| 1916 | int policy_alg, |
| 1917 | int key_type_arg, |
| 1918 | data_t *key_data, |
| 1919 | int exercise_alg ) |
| 1920 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1921 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1922 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1923 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1924 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1925 | psa_status_t status; |
| 1926 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1927 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1928 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1929 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1930 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1931 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1932 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1933 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1934 | &handle ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1935 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1936 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1937 | status = key_agreement_with_self( &operation, handle ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1938 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1939 | if( policy_alg == exercise_alg && |
| 1940 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1941 | PSA_ASSERT( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1942 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1943 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1944 | |
| 1945 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1946 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1947 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1948 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1949 | } |
| 1950 | /* END_CASE */ |
| 1951 | |
| 1952 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1953 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1954 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1955 | { |
| 1956 | psa_key_handle_t handle = 0; |
| 1957 | psa_key_type_t key_type = key_type_arg; |
| 1958 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1959 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1960 | psa_key_usage_t usage = usage_arg; |
| 1961 | psa_algorithm_t alg = alg_arg; |
| 1962 | psa_algorithm_t alg2 = alg2_arg; |
| 1963 | |
| 1964 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1965 | |
| 1966 | psa_set_key_usage_flags( &attributes, usage ); |
| 1967 | psa_set_key_algorithm( &attributes, alg ); |
| 1968 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1969 | psa_set_key_type( &attributes, key_type ); |
| 1970 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 1971 | &handle ) ); |
| 1972 | |
| 1973 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 1974 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1975 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1976 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1977 | |
| 1978 | if( ! exercise_key( handle, usage, alg ) ) |
| 1979 | goto exit; |
| 1980 | if( ! exercise_key( handle, usage, alg2 ) ) |
| 1981 | goto exit; |
| 1982 | |
| 1983 | exit: |
| 1984 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1985 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1986 | } |
| 1987 | /* END_CASE */ |
| 1988 | |
| 1989 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1990 | void raw_agreement_key_policy( int policy_usage, |
| 1991 | int policy_alg, |
| 1992 | int key_type_arg, |
| 1993 | data_t *key_data, |
| 1994 | int exercise_alg ) |
| 1995 | { |
| 1996 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1997 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1998 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1999 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2000 | psa_status_t status; |
| 2001 | |
| 2002 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2003 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2004 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2005 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2006 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2007 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2008 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2009 | &handle ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2010 | |
| 2011 | status = raw_key_agreement_with_self( exercise_alg, handle ); |
| 2012 | |
| 2013 | if( policy_alg == exercise_alg && |
| 2014 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 2015 | PSA_ASSERT( status ); |
| 2016 | else |
| 2017 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2018 | |
| 2019 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2020 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2021 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2022 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2023 | } |
| 2024 | /* END_CASE */ |
| 2025 | |
| 2026 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2027 | void copy_success( int source_usage_arg, |
| 2028 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2029 | int type_arg, data_t *material, |
| 2030 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2031 | int target_usage_arg, |
| 2032 | int target_alg_arg, int target_alg2_arg, |
| 2033 | int expected_usage_arg, |
| 2034 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2035 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2036 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2037 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2038 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2039 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2040 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2041 | psa_key_handle_t source_handle = 0; |
| 2042 | psa_key_handle_t target_handle = 0; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2043 | uint8_t *export_buffer = NULL; |
| 2044 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2045 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2046 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2047 | /* Prepare the source key. */ |
| 2048 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2049 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2050 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2051 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2052 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2053 | material->x, material->len, |
| 2054 | &source_handle ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2055 | PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2056 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2057 | /* Prepare the target attributes. */ |
| 2058 | if( copy_attributes ) |
| 2059 | target_attributes = source_attributes; |
| 2060 | if( target_usage_arg != -1 ) |
| 2061 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2062 | if( target_alg_arg != -1 ) |
| 2063 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2064 | if( target_alg2_arg != -1 ) |
| 2065 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2066 | |
| 2067 | /* Copy the key. */ |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2068 | PSA_ASSERT( psa_copy_key( source_handle, |
| 2069 | &target_attributes, &target_handle ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2070 | |
| 2071 | /* Destroy the source to ensure that this doesn't affect the target. */ |
| 2072 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2073 | |
| 2074 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2075 | PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) ); |
| 2076 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2077 | psa_get_key_type( &target_attributes ) ); |
| 2078 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2079 | psa_get_key_bits( &target_attributes ) ); |
| 2080 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2081 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2082 | TEST_EQUAL( expected_alg2, |
| 2083 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2084 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2085 | { |
| 2086 | size_t length; |
| 2087 | ASSERT_ALLOC( export_buffer, material->len ); |
| 2088 | PSA_ASSERT( psa_export_key( target_handle, export_buffer, |
| 2089 | material->len, &length ) ); |
| 2090 | ASSERT_COMPARE( material->x, material->len, |
| 2091 | export_buffer, length ); |
| 2092 | } |
| 2093 | if( ! exercise_key( target_handle, expected_usage, expected_alg ) ) |
| 2094 | goto exit; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2095 | if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) ) |
| 2096 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2097 | |
| 2098 | PSA_ASSERT( psa_close_key( target_handle ) ); |
| 2099 | |
| 2100 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2101 | psa_reset_key_attributes( &source_attributes ); |
| 2102 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2103 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2104 | mbedtls_free( export_buffer ); |
| 2105 | } |
| 2106 | /* END_CASE */ |
| 2107 | |
| 2108 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2109 | void copy_fail( int source_usage_arg, |
| 2110 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2111 | int type_arg, data_t *material, |
| 2112 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2113 | int target_usage_arg, |
| 2114 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2115 | int expected_status_arg ) |
| 2116 | { |
| 2117 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2118 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2119 | psa_key_handle_t source_handle = 0; |
| 2120 | psa_key_handle_t target_handle = 0; |
| 2121 | |
| 2122 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2123 | |
| 2124 | /* Prepare the source key. */ |
| 2125 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2126 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2127 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2128 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2129 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2130 | material->x, material->len, |
| 2131 | &source_handle ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2132 | |
| 2133 | /* Prepare the target attributes. */ |
| 2134 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2135 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2136 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2137 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2138 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2139 | |
| 2140 | /* Try to copy the key. */ |
| 2141 | TEST_EQUAL( psa_copy_key( source_handle, |
| 2142 | &target_attributes, &target_handle ), |
| 2143 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2144 | |
| 2145 | PSA_ASSERT( psa_destroy_key( source_handle ) ); |
| 2146 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2147 | exit: |
| 2148 | psa_reset_key_attributes( &source_attributes ); |
| 2149 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2150 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2151 | } |
| 2152 | /* END_CASE */ |
| 2153 | |
| 2154 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2155 | void hash_operation_init( ) |
| 2156 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2157 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2158 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2159 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2160 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2161 | * to supress the Clang warning for the test. */ |
| 2162 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2163 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2164 | psa_hash_operation_t zero; |
| 2165 | |
| 2166 | memset( &zero, 0, sizeof( zero ) ); |
| 2167 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2168 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2169 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2170 | PSA_ERROR_BAD_STATE ); |
| 2171 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2172 | PSA_ERROR_BAD_STATE ); |
| 2173 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2174 | PSA_ERROR_BAD_STATE ); |
| 2175 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2176 | /* A default hash operation should be abortable without error. */ |
| 2177 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2178 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2179 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2180 | } |
| 2181 | /* END_CASE */ |
| 2182 | |
| 2183 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2184 | void hash_setup( int alg_arg, |
| 2185 | int expected_status_arg ) |
| 2186 | { |
| 2187 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2188 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2189 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2190 | psa_status_t status; |
| 2191 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2192 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2193 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2194 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2195 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2196 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2197 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2198 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2199 | |
| 2200 | /* If setup failed, reproduce the failure, so as to |
| 2201 | * test the resulting state of the operation object. */ |
| 2202 | if( status != PSA_SUCCESS ) |
| 2203 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2204 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2205 | /* Now the operation object should be reusable. */ |
| 2206 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2207 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2208 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2209 | #endif |
| 2210 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2211 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2212 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2213 | } |
| 2214 | /* END_CASE */ |
| 2215 | |
| 2216 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2217 | void hash_bad_order( ) |
| 2218 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2219 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2220 | unsigned char input[] = ""; |
| 2221 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2222 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2223 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2224 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2225 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2226 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2227 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2228 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2229 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2230 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2231 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2232 | /* Call setup twice in a row. */ |
| 2233 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2234 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2235 | PSA_ERROR_BAD_STATE ); |
| 2236 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2237 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2238 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2239 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2240 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2241 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2242 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2243 | /* Call update after finish. */ |
| 2244 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2245 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2246 | hash, sizeof( hash ), &hash_len ) ); |
| 2247 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2248 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2249 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2250 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2251 | /* Call verify without calling setup beforehand. */ |
| 2252 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2253 | valid_hash, sizeof( valid_hash ) ), |
| 2254 | PSA_ERROR_BAD_STATE ); |
| 2255 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2256 | |
| 2257 | /* Call verify after finish. */ |
| 2258 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2259 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2260 | hash, sizeof( hash ), &hash_len ) ); |
| 2261 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2262 | valid_hash, sizeof( valid_hash ) ), |
| 2263 | PSA_ERROR_BAD_STATE ); |
| 2264 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2265 | |
| 2266 | /* Call verify twice in a row. */ |
| 2267 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2268 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2269 | valid_hash, sizeof( valid_hash ) ) ); |
| 2270 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2271 | valid_hash, sizeof( valid_hash ) ), |
| 2272 | PSA_ERROR_BAD_STATE ); |
| 2273 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2274 | |
| 2275 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2276 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2277 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2278 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2279 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2280 | |
| 2281 | /* Call finish twice in a row. */ |
| 2282 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2283 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2284 | hash, sizeof( hash ), &hash_len ) ); |
| 2285 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2286 | hash, sizeof( hash ), &hash_len ), |
| 2287 | PSA_ERROR_BAD_STATE ); |
| 2288 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2289 | |
| 2290 | /* Call finish after calling verify. */ |
| 2291 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2292 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2293 | valid_hash, sizeof( valid_hash ) ) ); |
| 2294 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2295 | hash, sizeof( hash ), &hash_len ), |
| 2296 | PSA_ERROR_BAD_STATE ); |
| 2297 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2298 | |
| 2299 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2300 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2301 | } |
| 2302 | /* END_CASE */ |
| 2303 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2304 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2305 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2306 | { |
| 2307 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2308 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2309 | * appended to it */ |
| 2310 | unsigned char hash[] = { |
| 2311 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2312 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2313 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2314 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2315 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2316 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2317 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2318 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2319 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2320 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2321 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2322 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2323 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2324 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2325 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2326 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2327 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2328 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2329 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2330 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2331 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2332 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2333 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2334 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2335 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2336 | } |
| 2337 | /* END_CASE */ |
| 2338 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2339 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2340 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2341 | { |
| 2342 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2343 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2344 | size_t expected_size = PSA_HASH_SIZE( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2345 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2346 | size_t hash_len; |
| 2347 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2348 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2349 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2350 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2351 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2352 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2353 | hash, expected_size - 1, &hash_len ), |
| 2354 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2355 | |
| 2356 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2357 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2358 | } |
| 2359 | /* END_CASE */ |
| 2360 | |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2361 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2362 | void hash_clone_source_state( ) |
| 2363 | { |
| 2364 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2365 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2366 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2367 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2368 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2369 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2370 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2371 | size_t hash_len; |
| 2372 | |
| 2373 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2374 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2375 | |
| 2376 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2377 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2378 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2379 | hash, sizeof( hash ), &hash_len ) ); |
| 2380 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2381 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2382 | |
| 2383 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2384 | PSA_ERROR_BAD_STATE ); |
| 2385 | |
| 2386 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2387 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2388 | hash, sizeof( hash ), &hash_len ) ); |
| 2389 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2390 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2391 | hash, sizeof( hash ), &hash_len ) ); |
| 2392 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2393 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2394 | hash, sizeof( hash ), &hash_len ) ); |
| 2395 | |
| 2396 | exit: |
| 2397 | psa_hash_abort( &op_source ); |
| 2398 | psa_hash_abort( &op_init ); |
| 2399 | psa_hash_abort( &op_setup ); |
| 2400 | psa_hash_abort( &op_finished ); |
| 2401 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2402 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2403 | } |
| 2404 | /* END_CASE */ |
| 2405 | |
| 2406 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2407 | void hash_clone_target_state( ) |
| 2408 | { |
| 2409 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2410 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2411 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2412 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2413 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2414 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2415 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2416 | size_t hash_len; |
| 2417 | |
| 2418 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2419 | |
| 2420 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2421 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2422 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2423 | hash, sizeof( hash ), &hash_len ) ); |
| 2424 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2425 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2426 | |
| 2427 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2428 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2429 | hash, sizeof( hash ), &hash_len ) ); |
| 2430 | |
| 2431 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2432 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2433 | PSA_ERROR_BAD_STATE ); |
| 2434 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2435 | PSA_ERROR_BAD_STATE ); |
| 2436 | |
| 2437 | exit: |
| 2438 | psa_hash_abort( &op_target ); |
| 2439 | psa_hash_abort( &op_init ); |
| 2440 | psa_hash_abort( &op_setup ); |
| 2441 | psa_hash_abort( &op_finished ); |
| 2442 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2443 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2444 | } |
| 2445 | /* END_CASE */ |
| 2446 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2447 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2448 | void mac_operation_init( ) |
| 2449 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2450 | const uint8_t input[1] = { 0 }; |
| 2451 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2452 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2453 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2454 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2455 | * to supress the Clang warning for the test. */ |
| 2456 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2457 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2458 | psa_mac_operation_t zero; |
| 2459 | |
| 2460 | memset( &zero, 0, sizeof( zero ) ); |
| 2461 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2462 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2463 | TEST_EQUAL( psa_mac_update( &func, |
| 2464 | input, sizeof( input ) ), |
| 2465 | PSA_ERROR_BAD_STATE ); |
| 2466 | TEST_EQUAL( psa_mac_update( &init, |
| 2467 | input, sizeof( input ) ), |
| 2468 | PSA_ERROR_BAD_STATE ); |
| 2469 | TEST_EQUAL( psa_mac_update( &zero, |
| 2470 | input, sizeof( input ) ), |
| 2471 | PSA_ERROR_BAD_STATE ); |
| 2472 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2473 | /* A default MAC operation should be abortable without error. */ |
| 2474 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2475 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2476 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2477 | } |
| 2478 | /* END_CASE */ |
| 2479 | |
| 2480 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2481 | void mac_setup( int key_type_arg, |
| 2482 | data_t *key, |
| 2483 | int alg_arg, |
| 2484 | int expected_status_arg ) |
| 2485 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2486 | psa_key_type_t key_type = key_type_arg; |
| 2487 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2488 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2489 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2490 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2491 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2492 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2493 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2494 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2495 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2496 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2497 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2498 | &operation, &status ) ) |
| 2499 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2500 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2501 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2502 | /* The operation object should be reusable. */ |
| 2503 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2504 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2505 | smoke_test_key_data, |
| 2506 | sizeof( smoke_test_key_data ), |
| 2507 | KNOWN_SUPPORTED_MAC_ALG, |
| 2508 | &operation, &status ) ) |
| 2509 | goto exit; |
| 2510 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2511 | #endif |
| 2512 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2513 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2514 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2515 | } |
| 2516 | /* END_CASE */ |
| 2517 | |
| 2518 | /* BEGIN_CASE */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2519 | void mac_bad_order( ) |
| 2520 | { |
| 2521 | psa_key_handle_t handle = 0; |
| 2522 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2523 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
| 2524 | const uint8_t key[] = { |
| 2525 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2526 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2527 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2528 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2529 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2530 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2531 | size_t sign_mac_length = 0; |
| 2532 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2533 | const uint8_t verify_mac[] = { |
| 2534 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2535 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2536 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2537 | |
| 2538 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2539 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 2540 | psa_set_key_algorithm( &attributes, alg ); |
| 2541 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2542 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2543 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2544 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2545 | /* Call update without calling setup beforehand. */ |
| 2546 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2547 | PSA_ERROR_BAD_STATE ); |
| 2548 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2549 | |
| 2550 | /* Call sign finish without calling setup beforehand. */ |
| 2551 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2552 | &sign_mac_length), |
| 2553 | PSA_ERROR_BAD_STATE ); |
| 2554 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2555 | |
| 2556 | /* Call verify finish without calling setup beforehand. */ |
| 2557 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2558 | verify_mac, sizeof( verify_mac ) ), |
| 2559 | PSA_ERROR_BAD_STATE ); |
| 2560 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2561 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2562 | /* Call setup twice in a row. */ |
| 2563 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2564 | handle, alg ) ); |
| 2565 | TEST_EQUAL( psa_mac_sign_setup( &operation, |
| 2566 | handle, alg ), |
| 2567 | PSA_ERROR_BAD_STATE ); |
| 2568 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2569 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2570 | /* Call update after sign finish. */ |
| 2571 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2572 | handle, alg ) ); |
| 2573 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2574 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2575 | sign_mac, sizeof( sign_mac ), |
| 2576 | &sign_mac_length ) ); |
| 2577 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2578 | PSA_ERROR_BAD_STATE ); |
| 2579 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2580 | |
| 2581 | /* Call update after verify finish. */ |
| 2582 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2583 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2584 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2585 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2586 | verify_mac, sizeof( verify_mac ) ) ); |
| 2587 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2588 | PSA_ERROR_BAD_STATE ); |
| 2589 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2590 | |
| 2591 | /* Call sign finish twice in a row. */ |
| 2592 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2593 | handle, alg ) ); |
| 2594 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2595 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2596 | sign_mac, sizeof( sign_mac ), |
| 2597 | &sign_mac_length ) ); |
| 2598 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2599 | sign_mac, sizeof( sign_mac ), |
| 2600 | &sign_mac_length ), |
| 2601 | PSA_ERROR_BAD_STATE ); |
| 2602 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2603 | |
| 2604 | /* Call verify finish twice in a row. */ |
| 2605 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2606 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2607 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2608 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2609 | verify_mac, sizeof( verify_mac ) ) ); |
| 2610 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2611 | verify_mac, sizeof( verify_mac ) ), |
| 2612 | PSA_ERROR_BAD_STATE ); |
| 2613 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2614 | |
| 2615 | /* Setup sign but try verify. */ |
| 2616 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2617 | handle, alg ) ); |
| 2618 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2619 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2620 | verify_mac, sizeof( verify_mac ) ), |
| 2621 | PSA_ERROR_BAD_STATE ); |
| 2622 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2623 | |
| 2624 | /* Setup verify but try sign. */ |
| 2625 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 2626 | handle, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2627 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2628 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2629 | sign_mac, sizeof( sign_mac ), |
| 2630 | &sign_mac_length ), |
| 2631 | PSA_ERROR_BAD_STATE ); |
| 2632 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2633 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2634 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2635 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2636 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2637 | PSA_DONE( ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2638 | } |
| 2639 | /* END_CASE */ |
| 2640 | |
| 2641 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2642 | void mac_sign( int key_type_arg, |
| 2643 | data_t *key, |
| 2644 | int alg_arg, |
| 2645 | data_t *input, |
| 2646 | data_t *expected_mac ) |
| 2647 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2648 | psa_key_handle_t handle = 0; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2649 | psa_key_type_t key_type = key_type_arg; |
| 2650 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2651 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2652 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2653 | /* Leave a little extra room in the output buffer. At the end of the |
| 2654 | * test, we'll check that the implementation didn't overwrite onto |
| 2655 | * this extra room. */ |
| 2656 | uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10]; |
| 2657 | size_t mac_buffer_size = |
| 2658 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 2659 | size_t mac_length = 0; |
| 2660 | |
| 2661 | memset( actual_mac, '+', sizeof( actual_mac ) ); |
| 2662 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
| 2663 | TEST_ASSERT( expected_mac->len <= mac_buffer_size ); |
| 2664 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2665 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2666 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2667 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 2668 | psa_set_key_algorithm( &attributes, alg ); |
| 2669 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2670 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2671 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2672 | |
| 2673 | /* Calculate the MAC. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2674 | PSA_ASSERT( psa_mac_sign_setup( &operation, |
| 2675 | handle, alg ) ); |
| 2676 | PSA_ASSERT( psa_mac_update( &operation, |
| 2677 | input->x, input->len ) ); |
| 2678 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2679 | actual_mac, mac_buffer_size, |
| 2680 | &mac_length ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2681 | |
| 2682 | /* Compare with the expected value. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 2683 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2684 | actual_mac, mac_length ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2685 | |
| 2686 | /* Verify that the end of the buffer is untouched. */ |
| 2687 | TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+', |
| 2688 | sizeof( actual_mac ) - mac_length ) ); |
| 2689 | |
| 2690 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2691 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2692 | PSA_DONE( ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2693 | } |
| 2694 | /* END_CASE */ |
| 2695 | |
| 2696 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2697 | void mac_verify( int key_type_arg, |
| 2698 | data_t *key, |
| 2699 | int alg_arg, |
| 2700 | data_t *input, |
| 2701 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2702 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2703 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2704 | psa_key_type_t key_type = key_type_arg; |
| 2705 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2706 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2707 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2708 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2709 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2710 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2711 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2712 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2713 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 2714 | psa_set_key_algorithm( &attributes, alg ); |
| 2715 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2716 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2717 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2718 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2719 | PSA_ASSERT( psa_mac_verify_setup( &operation, |
| 2720 | handle, alg ) ); |
| 2721 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2722 | PSA_ASSERT( psa_mac_update( &operation, |
| 2723 | input->x, input->len ) ); |
| 2724 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2725 | expected_mac->x, |
| 2726 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2727 | |
| 2728 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2729 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2730 | PSA_DONE( ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2731 | } |
| 2732 | /* END_CASE */ |
| 2733 | |
| 2734 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2735 | void cipher_operation_init( ) |
| 2736 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2737 | const uint8_t input[1] = { 0 }; |
| 2738 | unsigned char output[1] = { 0 }; |
| 2739 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2740 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2741 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2742 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2743 | * to supress the Clang warning for the test. */ |
| 2744 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2745 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2746 | psa_cipher_operation_t zero; |
| 2747 | |
| 2748 | memset( &zero, 0, sizeof( zero ) ); |
| 2749 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2750 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2751 | TEST_EQUAL( psa_cipher_update( &func, |
| 2752 | input, sizeof( input ), |
| 2753 | output, sizeof( output ), |
| 2754 | &output_length ), |
| 2755 | PSA_ERROR_BAD_STATE ); |
| 2756 | TEST_EQUAL( psa_cipher_update( &init, |
| 2757 | input, sizeof( input ), |
| 2758 | output, sizeof( output ), |
| 2759 | &output_length ), |
| 2760 | PSA_ERROR_BAD_STATE ); |
| 2761 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2762 | input, sizeof( input ), |
| 2763 | output, sizeof( output ), |
| 2764 | &output_length ), |
| 2765 | PSA_ERROR_BAD_STATE ); |
| 2766 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2767 | /* A default cipher operation should be abortable without error. */ |
| 2768 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2769 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2770 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2771 | } |
| 2772 | /* END_CASE */ |
| 2773 | |
| 2774 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2775 | void cipher_setup( int key_type_arg, |
| 2776 | data_t *key, |
| 2777 | int alg_arg, |
| 2778 | int expected_status_arg ) |
| 2779 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2780 | psa_key_type_t key_type = key_type_arg; |
| 2781 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2782 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2783 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2784 | psa_status_t status; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2785 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2786 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2787 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2788 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2789 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2790 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2791 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2792 | &operation, &status ) ) |
| 2793 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2794 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2795 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2796 | /* The operation object should be reusable. */ |
| 2797 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2798 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2799 | smoke_test_key_data, |
| 2800 | sizeof( smoke_test_key_data ), |
| 2801 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2802 | &operation, &status ) ) |
| 2803 | goto exit; |
| 2804 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2805 | #endif |
| 2806 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2807 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2808 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2809 | } |
| 2810 | /* END_CASE */ |
| 2811 | |
| 2812 | /* BEGIN_CASE */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2813 | void cipher_bad_order( ) |
| 2814 | { |
| 2815 | psa_key_handle_t handle = 0; |
| 2816 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2817 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2818 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2819 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2820 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2821 | const uint8_t key[] = { |
| 2822 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2823 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2824 | const uint8_t text[] = { |
| 2825 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2826 | 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2827 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 }; |
| 2828 | size_t length = 0; |
| 2829 | |
| 2830 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2831 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2832 | psa_set_key_algorithm( &attributes, alg ); |
| 2833 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2834 | PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2835 | |
| 2836 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2837 | /* Call encrypt setup twice in a row. */ |
| 2838 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2839 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ), |
| 2840 | PSA_ERROR_BAD_STATE ); |
| 2841 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2842 | |
| 2843 | /* Call decrypt setup twice in a row. */ |
| 2844 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) ); |
| 2845 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ), |
| 2846 | PSA_ERROR_BAD_STATE ); |
| 2847 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2848 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2849 | /* Generate an IV without calling setup beforehand. */ |
| 2850 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2851 | buffer, sizeof( buffer ), |
| 2852 | &length ), |
| 2853 | PSA_ERROR_BAD_STATE ); |
| 2854 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2855 | |
| 2856 | /* Generate an IV twice in a row. */ |
| 2857 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2858 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2859 | buffer, sizeof( buffer ), |
| 2860 | &length ) ); |
| 2861 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2862 | buffer, sizeof( buffer ), |
| 2863 | &length ), |
| 2864 | PSA_ERROR_BAD_STATE ); |
| 2865 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2866 | |
| 2867 | /* Generate an IV after it's already set. */ |
| 2868 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2869 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2870 | iv, sizeof( iv ) ) ); |
| 2871 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2872 | buffer, sizeof( buffer ), |
| 2873 | &length ), |
| 2874 | PSA_ERROR_BAD_STATE ); |
| 2875 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2876 | |
| 2877 | /* Set an IV without calling setup beforehand. */ |
| 2878 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2879 | iv, sizeof( iv ) ), |
| 2880 | PSA_ERROR_BAD_STATE ); |
| 2881 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2882 | |
| 2883 | /* Set an IV after it's already set. */ |
| 2884 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2885 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2886 | iv, sizeof( iv ) ) ); |
| 2887 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2888 | iv, sizeof( iv ) ), |
| 2889 | PSA_ERROR_BAD_STATE ); |
| 2890 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2891 | |
| 2892 | /* Set an IV after it's already generated. */ |
| 2893 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2894 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2895 | buffer, sizeof( buffer ), |
| 2896 | &length ) ); |
| 2897 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2898 | iv, sizeof( iv ) ), |
| 2899 | PSA_ERROR_BAD_STATE ); |
| 2900 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2901 | |
| 2902 | /* Call update without calling setup beforehand. */ |
| 2903 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2904 | text, sizeof( text ), |
| 2905 | buffer, sizeof( buffer ), |
| 2906 | &length ), |
| 2907 | PSA_ERROR_BAD_STATE ); |
| 2908 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2909 | |
| 2910 | /* Call update without an IV where an IV is required. */ |
| 2911 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2912 | text, sizeof( text ), |
| 2913 | buffer, sizeof( buffer ), |
| 2914 | &length ), |
| 2915 | PSA_ERROR_BAD_STATE ); |
| 2916 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2917 | |
| 2918 | /* Call update after finish. */ |
| 2919 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2920 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2921 | iv, sizeof( iv ) ) ); |
| 2922 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2923 | buffer, sizeof( buffer ), &length ) ); |
| 2924 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2925 | text, sizeof( text ), |
| 2926 | buffer, sizeof( buffer ), |
| 2927 | &length ), |
| 2928 | PSA_ERROR_BAD_STATE ); |
| 2929 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2930 | |
| 2931 | /* Call finish without calling setup beforehand. */ |
| 2932 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2933 | buffer, sizeof( buffer ), &length ), |
| 2934 | PSA_ERROR_BAD_STATE ); |
| 2935 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2936 | |
| 2937 | /* Call finish without an IV where an IV is required. */ |
| 2938 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2939 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2940 | * for cipher modes with padding. */ |
| 2941 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2942 | buffer, sizeof( buffer ), &length ), |
| 2943 | PSA_ERROR_BAD_STATE ); |
| 2944 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2945 | |
| 2946 | /* Call finish twice in a row. */ |
| 2947 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) ); |
| 2948 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2949 | iv, sizeof( iv ) ) ); |
| 2950 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2951 | buffer, sizeof( buffer ), &length ) ); |
| 2952 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2953 | buffer, sizeof( buffer ), &length ), |
| 2954 | PSA_ERROR_BAD_STATE ); |
| 2955 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2956 | |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2957 | PSA_ASSERT( psa_destroy_key( handle ) ); |
| 2958 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2959 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2960 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2961 | } |
| 2962 | /* END_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2963 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2964 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2965 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 2966 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2967 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2968 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2969 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2970 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2971 | psa_status_t status; |
| 2972 | psa_key_type_t key_type = key_type_arg; |
| 2973 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2974 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2975 | unsigned char *output = NULL; |
| 2976 | size_t output_buffer_size = 0; |
| 2977 | size_t function_output_length = 0; |
| 2978 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2979 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2980 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2981 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2982 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2983 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2984 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2985 | psa_set_key_algorithm( &attributes, alg ); |
| 2986 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2987 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2988 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2989 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2990 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 2991 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2992 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 2993 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 2994 | output_buffer_size = ( (size_t) input->len + |
| 2995 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2996 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2997 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2998 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2999 | input->x, input->len, |
| 3000 | output, output_buffer_size, |
| 3001 | &function_output_length ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3002 | total_output_length += function_output_length; |
| 3003 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3004 | output + total_output_length, |
| 3005 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3006 | &function_output_length ); |
| 3007 | total_output_length += function_output_length; |
| 3008 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3009 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3010 | if( expected_status == PSA_SUCCESS ) |
| 3011 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3012 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3013 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3014 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | exit: |
| 3018 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3019 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3020 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3021 | } |
| 3022 | /* END_CASE */ |
| 3023 | |
| 3024 | /* BEGIN_CASE */ |
| 3025 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3026 | data_t *key, data_t *iv, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3027 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3028 | int first_part_size_arg, |
| 3029 | int output1_length_arg, int output2_length_arg, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3030 | data_t *expected_output ) |
| 3031 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3032 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3033 | psa_key_type_t key_type = key_type_arg; |
| 3034 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3035 | size_t first_part_size = first_part_size_arg; |
| 3036 | size_t output1_length = output1_length_arg; |
| 3037 | size_t output2_length = output2_length_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3038 | unsigned char *output = NULL; |
| 3039 | size_t output_buffer_size = 0; |
| 3040 | size_t function_output_length = 0; |
| 3041 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3042 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3043 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3044 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3045 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3046 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3047 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3048 | psa_set_key_algorithm( &attributes, alg ); |
| 3049 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3050 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3051 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3052 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3053 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 3054 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3055 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3056 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3057 | output_buffer_size = ( (size_t) input->len + |
| 3058 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3059 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3060 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3061 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3062 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3063 | output, output_buffer_size, |
| 3064 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3065 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3066 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3067 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3068 | input->x + first_part_size, |
| 3069 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3070 | output + total_output_length, |
| 3071 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3072 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3073 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3074 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3075 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3076 | output + total_output_length, |
| 3077 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3078 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3079 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3080 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3081 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3082 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3083 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3084 | |
| 3085 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3086 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3087 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3088 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3089 | } |
| 3090 | /* END_CASE */ |
| 3091 | |
| 3092 | /* BEGIN_CASE */ |
| 3093 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3094 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3095 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3096 | int first_part_size_arg, |
| 3097 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3098 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3099 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3100 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3101 | |
| 3102 | psa_key_type_t key_type = key_type_arg; |
| 3103 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3104 | size_t first_part_size = first_part_size_arg; |
| 3105 | size_t output1_length = output1_length_arg; |
| 3106 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3107 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3108 | size_t output_buffer_size = 0; |
| 3109 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3110 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3111 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3112 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3115 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3116 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3117 | psa_set_key_algorithm( &attributes, alg ); |
| 3118 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3119 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3120 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3121 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3122 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3123 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3124 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3125 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3126 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3127 | output_buffer_size = ( (size_t) input->len + |
| 3128 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3129 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3130 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3131 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3132 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3133 | input->x, first_part_size, |
| 3134 | output, output_buffer_size, |
| 3135 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3136 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3137 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3138 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3139 | input->x + first_part_size, |
| 3140 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3141 | output + total_output_length, |
| 3142 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3143 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3144 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3145 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3146 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3147 | output + total_output_length, |
| 3148 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3149 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3150 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3151 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3152 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3153 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3154 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3155 | |
| 3156 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3157 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3158 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3159 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3160 | } |
| 3161 | /* END_CASE */ |
| 3162 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3163 | /* BEGIN_CASE */ |
| 3164 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3165 | data_t *key, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3166 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3167 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3168 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3169 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3170 | psa_status_t status; |
| 3171 | psa_key_type_t key_type = key_type_arg; |
| 3172 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3173 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3174 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3175 | size_t output_buffer_size = 0; |
| 3176 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3177 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3178 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3179 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3180 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3181 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3182 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3183 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3184 | psa_set_key_algorithm( &attributes, alg ); |
| 3185 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3186 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3187 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3188 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3189 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 3190 | handle, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3191 | |
Gilles Peskine | 423005e | 2019-05-06 15:22:57 +0200 | [diff] [blame] | 3192 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3193 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3194 | output_buffer_size = ( (size_t) input->len + |
| 3195 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3196 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3197 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3198 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3199 | input->x, input->len, |
| 3200 | output, output_buffer_size, |
| 3201 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3202 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3203 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3204 | output + total_output_length, |
| 3205 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3206 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3207 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3208 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3209 | |
| 3210 | if( expected_status == PSA_SUCCESS ) |
| 3211 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3212 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3213 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3214 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3215 | } |
| 3216 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3217 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3218 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3219 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3220 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3221 | } |
| 3222 | /* END_CASE */ |
| 3223 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3224 | /* BEGIN_CASE */ |
| 3225 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3226 | data_t *key, |
| 3227 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3228 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3229 | psa_key_handle_t handle = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3230 | psa_key_type_t key_type = key_type_arg; |
| 3231 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 3232 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3233 | size_t iv_size = 16; |
| 3234 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3235 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3236 | size_t output1_size = 0; |
| 3237 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3238 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3239 | size_t output2_size = 0; |
| 3240 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3241 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3242 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3243 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3244 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3245 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3246 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3247 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3248 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3249 | psa_set_key_algorithm( &attributes, alg ); |
| 3250 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3251 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3252 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3253 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3254 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3255 | handle, alg ) ); |
| 3256 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3257 | handle, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3258 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3259 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3260 | iv, iv_size, |
| 3261 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3262 | output1_size = ( (size_t) input->len + |
| 3263 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3264 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3265 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3266 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3267 | output1, output1_size, |
| 3268 | &output1_length ) ); |
| 3269 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3270 | output1 + output1_length, |
| 3271 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3272 | &function_output_length ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3273 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3274 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3275 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3276 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3277 | |
| 3278 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3279 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3280 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3281 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3282 | iv, iv_length ) ); |
| 3283 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3284 | output2, output2_size, |
| 3285 | &output2_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3286 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3287 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3288 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3289 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3290 | &function_output_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3291 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3292 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3293 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3294 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3295 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3296 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3297 | |
| 3298 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3299 | mbedtls_free( output1 ); |
| 3300 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3301 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3302 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3303 | } |
| 3304 | /* END_CASE */ |
| 3305 | |
| 3306 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3307 | void cipher_verify_output_multipart( int alg_arg, |
| 3308 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3309 | data_t *key, |
| 3310 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3311 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3312 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3313 | psa_key_handle_t handle = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3314 | psa_key_type_t key_type = key_type_arg; |
| 3315 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3316 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3317 | unsigned char iv[16] = {0}; |
| 3318 | size_t iv_size = 16; |
| 3319 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3320 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3321 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3322 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3323 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3324 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3325 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3326 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3327 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3328 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3329 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3330 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3331 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3332 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3333 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3334 | psa_set_key_algorithm( &attributes, alg ); |
| 3335 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3336 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 3337 | PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3338 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3339 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 3340 | handle, alg ) ); |
| 3341 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 3342 | handle, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3343 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3344 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3345 | iv, iv_size, |
| 3346 | &iv_length ) ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3347 | output1_buffer_size = ( (size_t) input->len + |
| 3348 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3349 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3350 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3351 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3352 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3353 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3354 | output1, output1_buffer_size, |
| 3355 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3356 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3357 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3358 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3359 | input->x + first_part_size, |
| 3360 | input->len - first_part_size, |
| 3361 | output1, output1_buffer_size, |
| 3362 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3363 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3364 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3365 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3366 | output1 + output1_length, |
| 3367 | output1_buffer_size - output1_length, |
| 3368 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3369 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3370 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3371 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3372 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3373 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3374 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3375 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3376 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3377 | iv, iv_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3378 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3379 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3380 | output2, output2_buffer_size, |
| 3381 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3382 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3383 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3384 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3385 | output1 + first_part_size, |
| 3386 | output1_length - first_part_size, |
| 3387 | output2, output2_buffer_size, |
| 3388 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3389 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3390 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3391 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3392 | output2 + output2_length, |
| 3393 | output2_buffer_size - output2_length, |
| 3394 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3395 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3396 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3397 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3398 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3399 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3400 | |
| 3401 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3402 | mbedtls_free( output1 ); |
| 3403 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3404 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3405 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3406 | } |
| 3407 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3408 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3409 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3410 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3411 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3412 | data_t *nonce, |
| 3413 | data_t *additional_data, |
| 3414 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3415 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3416 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3417 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3418 | psa_key_type_t key_type = key_type_arg; |
| 3419 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3420 | unsigned char *output_data = NULL; |
| 3421 | size_t output_size = 0; |
| 3422 | size_t output_length = 0; |
| 3423 | unsigned char *output_data2 = NULL; |
| 3424 | size_t output_length2 = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3425 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3426 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3427 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3428 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3429 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3430 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3431 | * should be exact. */ |
| 3432 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3433 | TEST_EQUAL( output_size, |
| 3434 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3435 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3436 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3437 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3438 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3439 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3440 | psa_set_key_algorithm( &attributes, alg ); |
| 3441 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3442 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3443 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3444 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3445 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3446 | TEST_EQUAL( psa_aead_encrypt( handle, alg, |
| 3447 | nonce->x, nonce->len, |
| 3448 | additional_data->x, |
| 3449 | additional_data->len, |
| 3450 | input_data->x, input_data->len, |
| 3451 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3452 | &output_length ), |
| 3453 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3454 | |
| 3455 | if( PSA_SUCCESS == expected_result ) |
| 3456 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3457 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3458 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3459 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3460 | * should be exact. */ |
| 3461 | TEST_EQUAL( input_data->len, |
| 3462 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) ); |
| 3463 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3464 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3465 | nonce->x, nonce->len, |
| 3466 | additional_data->x, |
| 3467 | additional_data->len, |
| 3468 | output_data, output_length, |
| 3469 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3470 | &output_length2 ), |
| 3471 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3472 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3473 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3474 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3475 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3476 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3477 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3478 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3479 | mbedtls_free( output_data ); |
| 3480 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3481 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3482 | } |
| 3483 | /* END_CASE */ |
| 3484 | |
| 3485 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3486 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3487 | int alg_arg, |
| 3488 | data_t *nonce, |
| 3489 | data_t *additional_data, |
| 3490 | data_t *input_data, |
| 3491 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3492 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3493 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3494 | psa_key_type_t key_type = key_type_arg; |
| 3495 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3496 | unsigned char *output_data = NULL; |
| 3497 | size_t output_size = 0; |
| 3498 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3499 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3500 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3501 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3502 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3503 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3504 | * should be exact. */ |
| 3505 | TEST_EQUAL( output_size, |
| 3506 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3507 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3508 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3509 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3510 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3511 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3512 | psa_set_key_algorithm( &attributes, alg ); |
| 3513 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3514 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3515 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3516 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3517 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3518 | PSA_ASSERT( psa_aead_encrypt( handle, alg, |
| 3519 | nonce->x, nonce->len, |
| 3520 | additional_data->x, additional_data->len, |
| 3521 | input_data->x, input_data->len, |
| 3522 | output_data, output_size, |
| 3523 | &output_length ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3524 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3525 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3526 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3527 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3528 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3529 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3530 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3531 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3532 | } |
| 3533 | /* END_CASE */ |
| 3534 | |
| 3535 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3536 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3537 | int alg_arg, |
| 3538 | data_t *nonce, |
| 3539 | data_t *additional_data, |
| 3540 | data_t *input_data, |
| 3541 | data_t *expected_data, |
| 3542 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3543 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3544 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3545 | psa_key_type_t key_type = key_type_arg; |
| 3546 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3547 | unsigned char *output_data = NULL; |
| 3548 | size_t output_size = 0; |
| 3549 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3550 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3551 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3552 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3553 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3554 | output_size = input_data->len - tag_length; |
| 3555 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3556 | * should be exact. */ |
| 3557 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3558 | TEST_EQUAL( output_size, |
| 3559 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3560 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3561 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3562 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3563 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3564 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3565 | psa_set_key_algorithm( &attributes, alg ); |
| 3566 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3567 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3568 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3569 | &handle ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3570 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3571 | TEST_EQUAL( psa_aead_decrypt( handle, alg, |
| 3572 | nonce->x, nonce->len, |
| 3573 | additional_data->x, |
| 3574 | additional_data->len, |
| 3575 | input_data->x, input_data->len, |
| 3576 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3577 | &output_length ), |
| 3578 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3579 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3580 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3581 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3582 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3583 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3584 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3585 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3586 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3587 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3588 | } |
| 3589 | /* END_CASE */ |
| 3590 | |
| 3591 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3592 | void signature_size( int type_arg, |
| 3593 | int bits, |
| 3594 | int alg_arg, |
| 3595 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3596 | { |
| 3597 | psa_key_type_t type = type_arg; |
| 3598 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3599 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3600 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3601 | exit: |
| 3602 | ; |
| 3603 | } |
| 3604 | /* END_CASE */ |
| 3605 | |
| 3606 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3607 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 3608 | int alg_arg, data_t *input_data, |
| 3609 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3610 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3611 | psa_key_handle_t handle = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3612 | psa_key_type_t key_type = key_type_arg; |
| 3613 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3614 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3615 | unsigned char *signature = NULL; |
| 3616 | size_t signature_size; |
| 3617 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3618 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3619 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3620 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3621 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3622 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 3623 | psa_set_key_algorithm( &attributes, alg ); |
| 3624 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3625 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3626 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3627 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3628 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3629 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3630 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3631 | /* Allocate a buffer which has the size advertized by the |
| 3632 | * library. */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3633 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3634 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3635 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3636 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3637 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3638 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3639 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3640 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3641 | input_data->x, input_data->len, |
| 3642 | signature, signature_size, |
| 3643 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3644 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3645 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3646 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3647 | |
| 3648 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3649 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3650 | psa_destroy_key( handle ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3651 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3652 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3653 | } |
| 3654 | /* END_CASE */ |
| 3655 | |
| 3656 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3657 | void sign_fail( int key_type_arg, data_t *key_data, |
| 3658 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3659 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3660 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3661 | psa_key_handle_t handle = 0; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3662 | psa_key_type_t key_type = key_type_arg; |
| 3663 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3664 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3665 | psa_status_t actual_status; |
| 3666 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3667 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3668 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3669 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3670 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3671 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3672 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3673 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3674 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3675 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN ); |
| 3676 | psa_set_key_algorithm( &attributes, alg ); |
| 3677 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3678 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3679 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3680 | &handle ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3681 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3682 | actual_status = psa_asymmetric_sign( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3683 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3684 | signature, signature_size, |
| 3685 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3686 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3687 | /* The value of *signature_length is unspecified on error, but |
| 3688 | * whatever it is, it should be less than signature_size, so that |
| 3689 | * if the caller tries to read *signature_length bytes without |
| 3690 | * checking the error code then they don't overflow a buffer. */ |
| 3691 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3692 | |
| 3693 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3694 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3695 | psa_destroy_key( handle ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3696 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3697 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3698 | } |
| 3699 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3700 | |
| 3701 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3702 | void sign_verify( int key_type_arg, data_t *key_data, |
| 3703 | int alg_arg, data_t *input_data ) |
| 3704 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3705 | psa_key_handle_t handle = 0; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3706 | psa_key_type_t key_type = key_type_arg; |
| 3707 | psa_algorithm_t alg = alg_arg; |
| 3708 | size_t key_bits; |
| 3709 | unsigned char *signature = NULL; |
| 3710 | size_t signature_size; |
| 3711 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3712 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3713 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3714 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3715 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3716 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 3717 | psa_set_key_algorithm( &attributes, alg ); |
| 3718 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3719 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3720 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3721 | &handle ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3722 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3723 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3724 | |
| 3725 | /* Allocate a buffer which has the size advertized by the |
| 3726 | * library. */ |
| 3727 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 3728 | key_bits, alg ); |
| 3729 | TEST_ASSERT( signature_size != 0 ); |
| 3730 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3731 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3732 | |
| 3733 | /* Perform the signature. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3734 | PSA_ASSERT( psa_asymmetric_sign( handle, alg, |
| 3735 | input_data->x, input_data->len, |
| 3736 | signature, signature_size, |
| 3737 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3738 | /* Check that the signature length looks sensible. */ |
| 3739 | TEST_ASSERT( signature_length <= signature_size ); |
| 3740 | TEST_ASSERT( signature_length > 0 ); |
| 3741 | |
| 3742 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3743 | PSA_ASSERT( psa_asymmetric_verify( |
| 3744 | handle, alg, |
| 3745 | input_data->x, input_data->len, |
| 3746 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3747 | |
| 3748 | if( input_data->len != 0 ) |
| 3749 | { |
| 3750 | /* Flip a bit in the input and verify that the signature is now |
| 3751 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3752 | * because ECDSA may ignore the last few bits of the input. */ |
| 3753 | input_data->x[0] ^= 1; |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3754 | TEST_EQUAL( psa_asymmetric_verify( handle, alg, |
| 3755 | input_data->x, input_data->len, |
| 3756 | signature, signature_length ), |
| 3757 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3761 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3762 | psa_destroy_key( handle ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3763 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3764 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3765 | } |
| 3766 | /* END_CASE */ |
| 3767 | |
| 3768 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3769 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 3770 | int alg_arg, data_t *hash_data, |
| 3771 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3772 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3773 | psa_key_handle_t handle = 0; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3774 | psa_key_type_t key_type = key_type_arg; |
| 3775 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3776 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3777 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3778 | TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
| 3779 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3780 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3781 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3782 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 3783 | psa_set_key_algorithm( &attributes, alg ); |
| 3784 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3785 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3786 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3787 | &handle ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3788 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3789 | PSA_ASSERT( psa_asymmetric_verify( handle, alg, |
| 3790 | hash_data->x, hash_data->len, |
| 3791 | signature_data->x, |
| 3792 | signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3793 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3794 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3795 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3796 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3797 | } |
| 3798 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3799 | |
| 3800 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3801 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 3802 | int alg_arg, data_t *hash_data, |
| 3803 | data_t *signature_data, |
| 3804 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3805 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3806 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3807 | psa_key_type_t key_type = key_type_arg; |
| 3808 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3809 | psa_status_t actual_status; |
| 3810 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3811 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3812 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3813 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3814 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3815 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY ); |
| 3816 | psa_set_key_algorithm( &attributes, alg ); |
| 3817 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3818 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3819 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3820 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3821 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3822 | actual_status = psa_asymmetric_verify( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3823 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3824 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3825 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3826 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3827 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3828 | |
| 3829 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3830 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3831 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3832 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3833 | } |
| 3834 | /* END_CASE */ |
| 3835 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3836 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3837 | void asymmetric_encrypt( int key_type_arg, |
| 3838 | data_t *key_data, |
| 3839 | int alg_arg, |
| 3840 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3841 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3842 | int expected_output_length_arg, |
| 3843 | int expected_status_arg ) |
| 3844 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3845 | psa_key_handle_t handle = 0; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3846 | psa_key_type_t key_type = key_type_arg; |
| 3847 | psa_algorithm_t alg = alg_arg; |
| 3848 | size_t expected_output_length = expected_output_length_arg; |
| 3849 | size_t key_bits; |
| 3850 | unsigned char *output = NULL; |
| 3851 | size_t output_size; |
| 3852 | size_t output_length = ~0; |
| 3853 | psa_status_t actual_status; |
| 3854 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3855 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3856 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3857 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3858 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3859 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3860 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3861 | psa_set_key_algorithm( &attributes, alg ); |
| 3862 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3863 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3864 | &handle ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3865 | |
| 3866 | /* Determine the maximum output length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3867 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3868 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3869 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3870 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3871 | |
| 3872 | /* Encrypt the input */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3873 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3874 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3875 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3876 | output, output_size, |
| 3877 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3878 | TEST_EQUAL( actual_status, expected_status ); |
| 3879 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3880 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3881 | /* If the label is empty, the test framework puts a non-null pointer |
| 3882 | * in label->x. Test that a null pointer works as well. */ |
| 3883 | if( label->len == 0 ) |
| 3884 | { |
| 3885 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3886 | if( output_size != 0 ) |
| 3887 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3888 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3889 | input_data->x, input_data->len, |
| 3890 | NULL, label->len, |
| 3891 | output, output_size, |
| 3892 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3893 | TEST_EQUAL( actual_status, expected_status ); |
| 3894 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3895 | } |
| 3896 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3897 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3898 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3899 | psa_destroy_key( handle ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3900 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3901 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3902 | } |
| 3903 | /* END_CASE */ |
| 3904 | |
| 3905 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3906 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 3907 | data_t *key_data, |
| 3908 | int alg_arg, |
| 3909 | data_t *input_data, |
| 3910 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3911 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3912 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3913 | psa_key_type_t key_type = key_type_arg; |
| 3914 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3915 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3916 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3917 | size_t output_size; |
| 3918 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3919 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3920 | size_t output2_size; |
| 3921 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3922 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3923 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3924 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3925 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3926 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3927 | psa_set_key_algorithm( &attributes, alg ); |
| 3928 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3929 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3930 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3931 | &handle ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3932 | |
| 3933 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3934 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 3935 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3936 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3937 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3938 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3939 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3940 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 3941 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 3942 | * the original plaintext because of the non-optional random |
| 3943 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3944 | PSA_ASSERT( psa_asymmetric_encrypt( handle, alg, |
| 3945 | input_data->x, input_data->len, |
| 3946 | label->x, label->len, |
| 3947 | output, output_size, |
| 3948 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3949 | /* We don't know what ciphertext length to expect, but check that |
| 3950 | * it looks sensible. */ |
| 3951 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3952 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3953 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 3954 | output, output_length, |
| 3955 | label->x, label->len, |
| 3956 | output2, output2_size, |
| 3957 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3958 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3959 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3960 | |
| 3961 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3962 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3963 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3964 | mbedtls_free( output ); |
| 3965 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3966 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3967 | } |
| 3968 | /* END_CASE */ |
| 3969 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3970 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3971 | void asymmetric_decrypt( int key_type_arg, |
| 3972 | data_t *key_data, |
| 3973 | int alg_arg, |
| 3974 | data_t *input_data, |
| 3975 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 3976 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3977 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3978 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3979 | psa_key_type_t key_type = key_type_arg; |
| 3980 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3981 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 3982 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3983 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3984 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3985 | |
Jaeden Amero | 412654a | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 3986 | output_size = expected_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3987 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3988 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3989 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3990 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3991 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3992 | psa_set_key_algorithm( &attributes, alg ); |
| 3993 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3994 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3995 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3996 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3997 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3998 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 3999 | input_data->x, input_data->len, |
| 4000 | label->x, label->len, |
| 4001 | output, |
| 4002 | output_size, |
| 4003 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4004 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4005 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4006 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4007 | /* If the label is empty, the test framework puts a non-null pointer |
| 4008 | * in label->x. Test that a null pointer works as well. */ |
| 4009 | if( label->len == 0 ) |
| 4010 | { |
| 4011 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4012 | if( output_size != 0 ) |
| 4013 | memset( output, 0, output_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4014 | PSA_ASSERT( psa_asymmetric_decrypt( handle, alg, |
| 4015 | input_data->x, input_data->len, |
| 4016 | NULL, label->len, |
| 4017 | output, |
| 4018 | output_size, |
| 4019 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4020 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4021 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4022 | } |
| 4023 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4024 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4025 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4026 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4027 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4028 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4029 | } |
| 4030 | /* END_CASE */ |
| 4031 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4032 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4033 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4034 | data_t *key_data, |
| 4035 | int alg_arg, |
| 4036 | data_t *input_data, |
| 4037 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4038 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4039 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4040 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4041 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4042 | psa_key_type_t key_type = key_type_arg; |
| 4043 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4044 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4045 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4046 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4047 | psa_status_t actual_status; |
| 4048 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4049 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4050 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4051 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4052 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4053 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4054 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4055 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4056 | psa_set_key_algorithm( &attributes, alg ); |
| 4057 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4058 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4059 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4060 | &handle ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4061 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4062 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4063 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4064 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4065 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4066 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4067 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4068 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4069 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4070 | /* If the label is empty, the test framework puts a non-null pointer |
| 4071 | * in label->x. Test that a null pointer works as well. */ |
| 4072 | if( label->len == 0 ) |
| 4073 | { |
| 4074 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4075 | if( output_size != 0 ) |
| 4076 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4077 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4078 | input_data->x, input_data->len, |
| 4079 | NULL, label->len, |
| 4080 | output, output_size, |
| 4081 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4082 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4083 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4084 | } |
| 4085 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4086 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4087 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4088 | psa_destroy_key( handle ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4089 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4090 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4091 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4092 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4093 | |
| 4094 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4095 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4096 | { |
| 4097 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4098 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4099 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4100 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4101 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4102 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4103 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4104 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4105 | |
| 4106 | memset( &zero, 0, sizeof( zero ) ); |
| 4107 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4108 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4109 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4110 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4111 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4112 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4113 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4114 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4115 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4116 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4117 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4118 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4119 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4120 | } |
| 4121 | /* END_CASE */ |
| 4122 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4123 | /* BEGIN_CASE */ |
| 4124 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4125 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4126 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4127 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4128 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4129 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4130 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4131 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4132 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4133 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4134 | |
| 4135 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4136 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4137 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4138 | } |
| 4139 | /* END_CASE */ |
| 4140 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4141 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4142 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4143 | int expected_status_arg ) |
| 4144 | { |
| 4145 | psa_algorithm_t alg = alg_arg; |
| 4146 | size_t capacity = capacity_arg; |
| 4147 | psa_status_t expected_status = expected_status_arg; |
| 4148 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4149 | |
| 4150 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4151 | |
| 4152 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4153 | |
| 4154 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4155 | expected_status ); |
| 4156 | |
| 4157 | exit: |
| 4158 | psa_key_derivation_abort( &operation ); |
| 4159 | PSA_DONE( ); |
| 4160 | } |
| 4161 | /* END_CASE */ |
| 4162 | |
| 4163 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4164 | void derive_input( int alg_arg, |
| 4165 | int key_type_arg, |
| 4166 | int step1_arg, data_t *input1, |
| 4167 | int step2_arg, data_t *input2, |
| 4168 | int step3_arg, data_t *input3, |
| 4169 | int expected_status_arg1, |
| 4170 | int expected_status_arg2, |
| 4171 | int expected_status_arg3 ) |
| 4172 | { |
| 4173 | psa_algorithm_t alg = alg_arg; |
| 4174 | size_t key_type = key_type_arg; |
| 4175 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4176 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4177 | expected_status_arg2, |
| 4178 | expected_status_arg3}; |
| 4179 | data_t *inputs[] = {input1, input2, input3}; |
| 4180 | psa_key_handle_t handles[] = {0, 0, 0}; |
| 4181 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4182 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4183 | size_t i; |
| 4184 | |
| 4185 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4186 | |
| 4187 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4188 | psa_set_key_algorithm( &attributes, alg ); |
| 4189 | psa_set_key_type( &attributes, key_type ); |
| 4190 | |
| 4191 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4192 | |
| 4193 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4194 | { |
| 4195 | switch( steps[i] ) |
| 4196 | { |
| 4197 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4198 | PSA_ASSERT( psa_import_key( &attributes, |
| 4199 | inputs[i]->x, inputs[i]->len, |
| 4200 | &handles[i] ) ); |
| 4201 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
| 4202 | handles[i] ), |
| 4203 | expected_statuses[i] ); |
| 4204 | break; |
| 4205 | default: |
| 4206 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4207 | &operation, steps[i], |
| 4208 | inputs[i]->x, inputs[i]->len ), |
| 4209 | expected_statuses[i] ); |
| 4210 | break; |
| 4211 | } |
| 4212 | } |
| 4213 | |
| 4214 | exit: |
| 4215 | psa_key_derivation_abort( &operation ); |
| 4216 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4217 | psa_destroy_key( handles[i] ); |
| 4218 | PSA_DONE( ); |
| 4219 | } |
| 4220 | /* END_CASE */ |
| 4221 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4222 | /* BEGIN_CASE */ |
| 4223 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4224 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4225 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4226 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4227 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4228 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4229 | unsigned char input1[] = "Input 1"; |
| 4230 | size_t input1_length = sizeof( input1 ); |
| 4231 | unsigned char input2[] = "Input 2"; |
| 4232 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4233 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4234 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4235 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4236 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4237 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4238 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4239 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4240 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4241 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4242 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4243 | psa_set_key_algorithm( &attributes, alg ); |
| 4244 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4245 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4246 | PSA_ASSERT( psa_import_key( &attributes, |
| 4247 | key_data, sizeof( key_data ), |
| 4248 | &handle ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4249 | |
| 4250 | /* valid key derivation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4251 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4252 | input1, input1_length, |
| 4253 | input2, input2_length, |
| 4254 | capacity ) ) |
| 4255 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4256 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4257 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4258 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4259 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4260 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4261 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4262 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4263 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4264 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4265 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4266 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4267 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4268 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4269 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4270 | } |
| 4271 | /* END_CASE */ |
| 4272 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4273 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4274 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4275 | { |
| 4276 | uint8_t output_buffer[16]; |
| 4277 | size_t buffer_size = 16; |
| 4278 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4279 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4280 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4281 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4282 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4283 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4284 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4285 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4286 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4287 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4288 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4289 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4290 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4291 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4292 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4293 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4294 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4295 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4296 | |
| 4297 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4298 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4299 | } |
| 4300 | /* END_CASE */ |
| 4301 | |
| 4302 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4303 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4304 | int step1_arg, data_t *input1, |
| 4305 | int step2_arg, data_t *input2, |
| 4306 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4307 | int requested_capacity_arg, |
| 4308 | data_t *expected_output1, |
| 4309 | data_t *expected_output2 ) |
| 4310 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4311 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4312 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4313 | data_t *inputs[] = {input1, input2, input3}; |
| 4314 | psa_key_handle_t handles[] = {0, 0, 0}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4315 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4316 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4317 | uint8_t *expected_outputs[2] = |
| 4318 | {expected_output1->x, expected_output2->x}; |
| 4319 | size_t output_sizes[2] = |
| 4320 | {expected_output1->len, expected_output2->len}; |
| 4321 | size_t output_buffer_size = 0; |
| 4322 | uint8_t *output_buffer = NULL; |
| 4323 | size_t expected_capacity; |
| 4324 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4325 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4326 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4327 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4328 | |
| 4329 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4330 | { |
| 4331 | if( output_sizes[i] > output_buffer_size ) |
| 4332 | output_buffer_size = output_sizes[i]; |
| 4333 | if( output_sizes[i] == 0 ) |
| 4334 | expected_outputs[i] = NULL; |
| 4335 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4336 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4337 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4338 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4339 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4340 | psa_set_key_algorithm( &attributes, alg ); |
| 4341 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4342 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4343 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4344 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4345 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4346 | requested_capacity ) ); |
| 4347 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4348 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4349 | switch( steps[i] ) |
| 4350 | { |
| 4351 | case 0: |
| 4352 | break; |
| 4353 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4354 | PSA_ASSERT( psa_import_key( &attributes, |
| 4355 | inputs[i]->x, inputs[i]->len, |
| 4356 | &handles[i] ) ); |
| 4357 | PSA_ASSERT( psa_key_derivation_input_key( |
| 4358 | &operation, steps[i], |
| 4359 | handles[i] ) ); |
| 4360 | break; |
| 4361 | default: |
| 4362 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4363 | &operation, steps[i], |
| 4364 | inputs[i]->x, inputs[i]->len ) ); |
| 4365 | break; |
| 4366 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4367 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4368 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4369 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4370 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4371 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4372 | expected_capacity = requested_capacity; |
| 4373 | |
| 4374 | /* Expansion phase. */ |
| 4375 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4376 | { |
| 4377 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4378 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4379 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4380 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4381 | { |
| 4382 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4383 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4384 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4385 | continue; |
| 4386 | } |
| 4387 | else if( expected_capacity == 0 || |
| 4388 | output_sizes[i] > expected_capacity ) |
| 4389 | { |
| 4390 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4391 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4392 | expected_capacity = 0; |
| 4393 | continue; |
| 4394 | } |
| 4395 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4396 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4397 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4398 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4399 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4400 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4401 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4402 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4403 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4404 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4405 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4406 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4407 | |
| 4408 | exit: |
| 4409 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4410 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4411 | for( i = 0; i < ARRAY_LENGTH( handles ); i++ ) |
| 4412 | psa_destroy_key( handles[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4413 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4414 | } |
| 4415 | /* END_CASE */ |
| 4416 | |
| 4417 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4418 | void derive_full( int alg_arg, |
| 4419 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4420 | data_t *input1, |
| 4421 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4422 | int requested_capacity_arg ) |
| 4423 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4424 | psa_key_handle_t handle = 0; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4425 | psa_algorithm_t alg = alg_arg; |
| 4426 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4427 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4428 | unsigned char output_buffer[16]; |
| 4429 | size_t expected_capacity = requested_capacity; |
| 4430 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4431 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4432 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4433 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4434 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4435 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4436 | psa_set_key_algorithm( &attributes, alg ); |
| 4437 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4438 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4439 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4440 | &handle ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4441 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4442 | if( !setup_key_derivation_wrap( &operation, handle, alg, |
| 4443 | input1->x, input1->len, |
| 4444 | input2->x, input2->len, |
| 4445 | requested_capacity ) ) |
| 4446 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4447 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4448 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4449 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4450 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4451 | |
| 4452 | /* Expansion phase. */ |
| 4453 | while( current_capacity > 0 ) |
| 4454 | { |
| 4455 | size_t read_size = sizeof( output_buffer ); |
| 4456 | if( read_size > current_capacity ) |
| 4457 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4458 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4459 | output_buffer, |
| 4460 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4461 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4462 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4463 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4464 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4465 | } |
| 4466 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4467 | /* Check that the operation refuses to go over capacity. */ |
| 4468 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4469 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4470 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4471 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4472 | |
| 4473 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4474 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4475 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4476 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4477 | } |
| 4478 | /* END_CASE */ |
| 4479 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4480 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4481 | void derive_key_exercise( int alg_arg, |
| 4482 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4483 | data_t *input1, |
| 4484 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4485 | int derived_type_arg, |
| 4486 | int derived_bits_arg, |
| 4487 | int derived_usage_arg, |
| 4488 | int derived_alg_arg ) |
| 4489 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4490 | psa_key_handle_t base_handle = 0; |
| 4491 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4492 | psa_algorithm_t alg = alg_arg; |
| 4493 | psa_key_type_t derived_type = derived_type_arg; |
| 4494 | size_t derived_bits = derived_bits_arg; |
| 4495 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4496 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4497 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4498 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4499 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4500 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4501 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4502 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4503 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4504 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4505 | psa_set_key_algorithm( &attributes, alg ); |
| 4506 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4507 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4508 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4509 | |
| 4510 | /* Derive a key. */ |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4511 | if ( setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4512 | input1->x, input1->len, |
| 4513 | input2->x, input2->len, capacity ) ) |
| 4514 | goto exit; |
| 4515 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4516 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4517 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4518 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4519 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4520 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4521 | &derived_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4522 | |
| 4523 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4524 | PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) ); |
| 4525 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4526 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4527 | |
| 4528 | /* Exercise the derived key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4529 | if( ! exercise_key( derived_handle, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4530 | goto exit; |
| 4531 | |
| 4532 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4533 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4534 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4535 | psa_destroy_key( base_handle ); |
| 4536 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4537 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4538 | } |
| 4539 | /* END_CASE */ |
| 4540 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4541 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4542 | void derive_key_export( int alg_arg, |
| 4543 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4544 | data_t *input1, |
| 4545 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4546 | int bytes1_arg, |
| 4547 | int bytes2_arg ) |
| 4548 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4549 | psa_key_handle_t base_handle = 0; |
| 4550 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4551 | psa_algorithm_t alg = alg_arg; |
| 4552 | size_t bytes1 = bytes1_arg; |
| 4553 | size_t bytes2 = bytes2_arg; |
| 4554 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4555 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4556 | uint8_t *output_buffer = NULL; |
| 4557 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4558 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4559 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4560 | size_t length; |
| 4561 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4562 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4563 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4564 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4565 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4566 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4567 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4568 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4569 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 4570 | &base_handle ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4571 | |
| 4572 | /* Derive some material and output it. */ |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4573 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4574 | input1->x, input1->len, |
| 4575 | input2->x, input2->len, capacity ) ) |
| 4576 | goto exit; |
| 4577 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4578 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4579 | output_buffer, |
| 4580 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4581 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4582 | |
| 4583 | /* 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] | 4584 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4585 | input1->x, input1->len, |
| 4586 | input2->x, input2->len, capacity ) ) |
| 4587 | goto exit; |
| 4588 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4589 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4590 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 4591 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4592 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4593 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4594 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4595 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4596 | export_buffer, bytes1, |
| 4597 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4598 | TEST_EQUAL( length, bytes1 ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4599 | PSA_ASSERT( psa_destroy_key( derived_handle ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4600 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4601 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4602 | &derived_handle ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4603 | PSA_ASSERT( psa_export_key( derived_handle, |
| 4604 | export_buffer + bytes1, bytes2, |
| 4605 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4606 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4607 | |
| 4608 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4609 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 4610 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4611 | |
| 4612 | exit: |
| 4613 | mbedtls_free( output_buffer ); |
| 4614 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4615 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4616 | psa_destroy_key( base_handle ); |
| 4617 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4618 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4619 | } |
| 4620 | /* END_CASE */ |
| 4621 | |
| 4622 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4623 | void derive_large_key( int alg_arg, |
| 4624 | data_t *key_data, data_t *input1, data_t *input2, |
| 4625 | int bits_arg, |
| 4626 | int expected_status_arg ) |
| 4627 | { |
| 4628 | psa_key_handle_t base_handle = 0; |
| 4629 | psa_key_handle_t derived_handle = 0; |
| 4630 | psa_algorithm_t alg = alg_arg; |
| 4631 | size_t bits = bits_arg; |
| 4632 | psa_status_t expected_status = expected_status_arg; |
| 4633 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4634 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4635 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4636 | |
| 4637 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4638 | |
| 4639 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4640 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4641 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 4642 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 4643 | &base_handle ) ); |
| 4644 | |
| 4645 | if( !setup_key_derivation_wrap( &operation, base_handle, alg, |
| 4646 | input1->x, input1->len, |
| 4647 | input2->x, input2->len, SIZE_MAX ) ) |
| 4648 | goto exit; |
| 4649 | |
| 4650 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4651 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 4652 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 4653 | psa_set_key_bits( &derived_attributes, bits ); |
| 4654 | TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 4655 | &derived_handle ), |
| 4656 | expected_status ); |
| 4657 | |
| 4658 | exit: |
| 4659 | psa_key_derivation_abort( &operation ); |
| 4660 | psa_destroy_key( base_handle ); |
| 4661 | psa_destroy_key( derived_handle ); |
| 4662 | PSA_DONE( ); |
| 4663 | } |
| 4664 | /* END_CASE */ |
| 4665 | |
| 4666 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4667 | void key_agreement_setup( int alg_arg, |
| 4668 | int our_key_type_arg, data_t *our_key_data, |
| 4669 | data_t *peer_key_data, |
| 4670 | int expected_status_arg ) |
| 4671 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4672 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4673 | psa_algorithm_t alg = alg_arg; |
| 4674 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4675 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4676 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4677 | psa_status_t expected_status = expected_status_arg; |
| 4678 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4679 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4680 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4681 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4682 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4683 | psa_set_key_algorithm( &attributes, alg ); |
| 4684 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4685 | PSA_ASSERT( psa_import_key( &attributes, |
| 4686 | our_key_data->x, our_key_data->len, |
| 4687 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4688 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4689 | /* The tests currently include inputs that should fail at either step. |
| 4690 | * Test cases that fail at the setup step should be changed to call |
| 4691 | * key_derivation_setup instead, and this function should be renamed |
| 4692 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4693 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4694 | if( status == PSA_SUCCESS ) |
| 4695 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4696 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 4697 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 4698 | our_key, |
| 4699 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4700 | expected_status ); |
| 4701 | } |
| 4702 | else |
| 4703 | { |
| 4704 | TEST_ASSERT( status == expected_status ); |
| 4705 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4706 | |
| 4707 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4708 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4709 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4710 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4711 | } |
| 4712 | /* END_CASE */ |
| 4713 | |
| 4714 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4715 | void raw_key_agreement( int alg_arg, |
| 4716 | int our_key_type_arg, data_t *our_key_data, |
| 4717 | data_t *peer_key_data, |
| 4718 | data_t *expected_output ) |
| 4719 | { |
| 4720 | psa_key_handle_t our_key = 0; |
| 4721 | psa_algorithm_t alg = alg_arg; |
| 4722 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4723 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4724 | unsigned char *output = NULL; |
| 4725 | size_t output_length = ~0; |
| 4726 | |
| 4727 | ASSERT_ALLOC( output, expected_output->len ); |
| 4728 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4729 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4730 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4731 | psa_set_key_algorithm( &attributes, alg ); |
| 4732 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4733 | PSA_ASSERT( psa_import_key( &attributes, |
| 4734 | our_key_data->x, our_key_data->len, |
| 4735 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4736 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4737 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 4738 | peer_key_data->x, peer_key_data->len, |
| 4739 | output, expected_output->len, |
| 4740 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4741 | ASSERT_COMPARE( output, output_length, |
| 4742 | expected_output->x, expected_output->len ); |
| 4743 | |
| 4744 | exit: |
| 4745 | mbedtls_free( output ); |
| 4746 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4747 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4748 | } |
| 4749 | /* END_CASE */ |
| 4750 | |
| 4751 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4752 | void key_agreement_capacity( int alg_arg, |
| 4753 | int our_key_type_arg, data_t *our_key_data, |
| 4754 | data_t *peer_key_data, |
| 4755 | int expected_capacity_arg ) |
| 4756 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4757 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4758 | psa_algorithm_t alg = alg_arg; |
| 4759 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4760 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4761 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4762 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4763 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4764 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4765 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4766 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4767 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4768 | psa_set_key_algorithm( &attributes, alg ); |
| 4769 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4770 | PSA_ASSERT( psa_import_key( &attributes, |
| 4771 | our_key_data->x, our_key_data->len, |
| 4772 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4773 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4774 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4775 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4776 | &operation, |
| 4777 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4778 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4779 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4780 | { |
| 4781 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4782 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4783 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4784 | NULL, 0 ) ); |
| 4785 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4786 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4787 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4788 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4789 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4790 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4791 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4792 | /* Test the actual capacity by reading the output. */ |
| 4793 | while( actual_capacity > sizeof( output ) ) |
| 4794 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4795 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4796 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4797 | actual_capacity -= sizeof( output ); |
| 4798 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4799 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4800 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4801 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4802 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4803 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4804 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4805 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4806 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4807 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4808 | } |
| 4809 | /* END_CASE */ |
| 4810 | |
| 4811 | /* BEGIN_CASE */ |
| 4812 | void key_agreement_output( int alg_arg, |
| 4813 | int our_key_type_arg, data_t *our_key_data, |
| 4814 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4815 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4816 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4817 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4818 | psa_algorithm_t alg = alg_arg; |
| 4819 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4820 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4821 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4822 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4823 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4824 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 4825 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4826 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4827 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4828 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4829 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4830 | psa_set_key_algorithm( &attributes, alg ); |
| 4831 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4832 | PSA_ASSERT( psa_import_key( &attributes, |
| 4833 | our_key_data->x, our_key_data->len, |
| 4834 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4835 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4836 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4837 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4838 | &operation, |
| 4839 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4840 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4841 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4842 | { |
| 4843 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4844 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4845 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4846 | NULL, 0 ) ); |
| 4847 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4848 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4849 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4850 | actual_output, |
| 4851 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4852 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 4853 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4854 | if( expected_output2->len != 0 ) |
| 4855 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4856 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4857 | actual_output, |
| 4858 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4859 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 4860 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4861 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4862 | |
| 4863 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4864 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4865 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4866 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4867 | mbedtls_free( actual_output ); |
| 4868 | } |
| 4869 | /* END_CASE */ |
| 4870 | |
| 4871 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4872 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4873 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4874 | size_t bytes = bytes_arg; |
| 4875 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4876 | unsigned char *output = NULL; |
| 4877 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4878 | size_t i; |
| 4879 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4880 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4881 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 4882 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4883 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4884 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4885 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4886 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4887 | /* Run several times, to ensure that every output byte will be |
| 4888 | * nonzero at least once with overwhelming probability |
| 4889 | * (2^(-8*number_of_runs)). */ |
| 4890 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4891 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4892 | if( bytes != 0 ) |
| 4893 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4894 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4895 | |
| 4896 | /* Check that no more than bytes have been overwritten */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4897 | ASSERT_COMPARE( output + bytes, sizeof( trail ), |
| 4898 | trail, sizeof( trail ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4899 | |
| 4900 | for( i = 0; i < bytes; i++ ) |
| 4901 | { |
| 4902 | if( output[i] != 0 ) |
| 4903 | ++changed[i]; |
| 4904 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4905 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4906 | |
| 4907 | /* Check that every byte was changed to nonzero at least once. This |
| 4908 | * validates that psa_generate_random is overwriting every byte of |
| 4909 | * the output buffer. */ |
| 4910 | for( i = 0; i < bytes; i++ ) |
| 4911 | { |
| 4912 | TEST_ASSERT( changed[i] != 0 ); |
| 4913 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4914 | |
| 4915 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4916 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4917 | mbedtls_free( output ); |
| 4918 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4919 | } |
| 4920 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4921 | |
| 4922 | /* BEGIN_CASE */ |
| 4923 | void generate_key( int type_arg, |
| 4924 | int bits_arg, |
| 4925 | int usage_arg, |
| 4926 | int alg_arg, |
| 4927 | int expected_status_arg ) |
| 4928 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4929 | psa_key_handle_t handle = 0; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4930 | psa_key_type_t type = type_arg; |
| 4931 | psa_key_usage_t usage = usage_arg; |
| 4932 | size_t bits = bits_arg; |
| 4933 | psa_algorithm_t alg = alg_arg; |
| 4934 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4935 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4936 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4937 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4938 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4939 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4940 | psa_set_key_usage_flags( &attributes, usage ); |
| 4941 | psa_set_key_algorithm( &attributes, alg ); |
| 4942 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4943 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4944 | |
| 4945 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 4946 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4947 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4948 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4949 | |
| 4950 | /* Test the key information */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4951 | PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) ); |
| 4952 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 4953 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4954 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 4955 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4956 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 4957 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4958 | |
| 4959 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4960 | psa_reset_key_attributes( &got_attributes ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4961 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4962 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4963 | } |
| 4964 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 4965 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4966 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */ |
| 4967 | void generate_key_rsa( int bits_arg, |
| 4968 | data_t *e_arg, |
| 4969 | int expected_status_arg ) |
| 4970 | { |
| 4971 | psa_key_handle_t handle = 0; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 4972 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4973 | size_t bits = bits_arg; |
| 4974 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 4975 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 4976 | psa_status_t expected_status = expected_status_arg; |
| 4977 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4978 | uint8_t *exported = NULL; |
| 4979 | size_t exported_size = |
| 4980 | PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
| 4981 | size_t exported_length = SIZE_MAX; |
| 4982 | uint8_t *e_read_buffer = NULL; |
| 4983 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 4984 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 4985 | size_t e_read_length = SIZE_MAX; |
| 4986 | |
| 4987 | if( e_arg->len == 0 || |
| 4988 | ( e_arg->len == 3 && |
| 4989 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 4990 | { |
| 4991 | is_default_public_exponent = 1; |
| 4992 | e_read_size = 0; |
| 4993 | } |
| 4994 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 4995 | ASSERT_ALLOC( exported, exported_size ); |
| 4996 | |
| 4997 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4998 | |
| 4999 | psa_set_key_usage_flags( &attributes, usage ); |
| 5000 | psa_set_key_algorithm( &attributes, alg ); |
| 5001 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5002 | e_arg->x, e_arg->len ) ); |
| 5003 | psa_set_key_bits( &attributes, bits ); |
| 5004 | |
| 5005 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5006 | TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5007 | if( expected_status != PSA_SUCCESS ) |
| 5008 | goto exit; |
| 5009 | |
| 5010 | /* Test the key information */ |
| 5011 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5012 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5013 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5014 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5015 | e_read_buffer, e_read_size, |
| 5016 | &e_read_length ) ); |
| 5017 | if( is_default_public_exponent ) |
| 5018 | TEST_EQUAL( e_read_length, 0 ); |
| 5019 | else |
| 5020 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5021 | |
| 5022 | /* Do something with the key according to its type and permitted usage. */ |
| 5023 | if( ! exercise_key( handle, usage, alg ) ) |
| 5024 | goto exit; |
| 5025 | |
| 5026 | /* Export the key and check the public exponent. */ |
| 5027 | PSA_ASSERT( psa_export_public_key( handle, |
| 5028 | exported, exported_size, |
| 5029 | &exported_length ) ); |
| 5030 | { |
| 5031 | uint8_t *p = exported; |
| 5032 | uint8_t *end = exported + exported_length; |
| 5033 | size_t len; |
| 5034 | /* RSAPublicKey ::= SEQUENCE { |
| 5035 | * modulus INTEGER, -- n |
| 5036 | * publicExponent INTEGER } -- e |
| 5037 | */ |
| 5038 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5039 | MBEDTLS_ASN1_SEQUENCE | |
| 5040 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5041 | TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
| 5042 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5043 | MBEDTLS_ASN1_INTEGER ) ); |
| 5044 | if( len >= 1 && p[0] == 0 ) |
| 5045 | { |
| 5046 | ++p; |
| 5047 | --len; |
| 5048 | } |
| 5049 | if( e_arg->len == 0 ) |
| 5050 | { |
| 5051 | TEST_EQUAL( len, 3 ); |
| 5052 | TEST_EQUAL( p[0], 1 ); |
| 5053 | TEST_EQUAL( p[1], 0 ); |
| 5054 | TEST_EQUAL( p[2], 1 ); |
| 5055 | } |
| 5056 | else |
| 5057 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5058 | } |
| 5059 | |
| 5060 | exit: |
| 5061 | psa_reset_key_attributes( &attributes ); |
| 5062 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5063 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5064 | mbedtls_free( e_read_buffer ); |
| 5065 | mbedtls_free( exported ); |
| 5066 | } |
| 5067 | /* END_CASE */ |
| 5068 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5069 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5070 | void persistent_key_load_key_from_storage( data_t *data, |
| 5071 | int type_arg, int bits_arg, |
| 5072 | int usage_flags_arg, int alg_arg, |
| 5073 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5074 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5075 | psa_key_id_t key_id = 1; |
| 5076 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5077 | psa_key_handle_t handle = 0; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5078 | psa_key_handle_t base_key = 0; |
| 5079 | psa_key_type_t type = type_arg; |
| 5080 | size_t bits = bits_arg; |
| 5081 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5082 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5083 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5084 | unsigned char *first_export = NULL; |
| 5085 | unsigned char *second_export = NULL; |
| 5086 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 5087 | size_t first_exported_length; |
| 5088 | size_t second_exported_length; |
| 5089 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5090 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5091 | { |
| 5092 | ASSERT_ALLOC( first_export, export_size ); |
| 5093 | ASSERT_ALLOC( second_export, export_size ); |
| 5094 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5095 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5096 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5097 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5098 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5099 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5100 | psa_set_key_algorithm( &attributes, alg ); |
| 5101 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5102 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5103 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5104 | switch( generation_method ) |
| 5105 | { |
| 5106 | case IMPORT_KEY: |
| 5107 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5108 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
| 5109 | &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5110 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5111 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5112 | case GENERATE_KEY: |
| 5113 | /* Generate a key */ |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5114 | PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5115 | break; |
| 5116 | |
| 5117 | case DERIVE_KEY: |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5118 | { |
| 5119 | /* Create base key */ |
| 5120 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5121 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5122 | psa_set_key_usage_flags( &base_attributes, |
| 5123 | PSA_KEY_USAGE_DERIVE ); |
| 5124 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5125 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5126 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5127 | data->x, data->len, |
| 5128 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5129 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5130 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5131 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5132 | &operation, |
| 5133 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5134 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5135 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5136 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5137 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5138 | &operation, |
| 5139 | &handle ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5140 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5141 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
| 5142 | base_key = 0; |
| 5143 | } |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5144 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5145 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5146 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5147 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5148 | /* Export the key if permitted by the key policy. */ |
| 5149 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5150 | { |
| 5151 | PSA_ASSERT( psa_export_key( handle, |
| 5152 | first_export, export_size, |
| 5153 | &first_exported_length ) ); |
| 5154 | if( generation_method == IMPORT_KEY ) |
| 5155 | ASSERT_COMPARE( data->x, data->len, |
| 5156 | first_export, first_exported_length ); |
| 5157 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5158 | |
| 5159 | /* Shutdown and restart */ |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 5160 | PSA_ASSERT( psa_close_key( handle ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5161 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5162 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5163 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5164 | /* Check key slot still contains key data */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5165 | PSA_ASSERT( psa_open_key( key_id, &handle ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5166 | PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) ); |
| 5167 | TEST_EQUAL( psa_get_key_id( &attributes ), key_id ); |
| 5168 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5169 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5170 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5171 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5172 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 5173 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5174 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5175 | /* Export the key again if permitted by the key policy. */ |
| 5176 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5177 | { |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5178 | PSA_ASSERT( psa_export_key( handle, |
| 5179 | second_export, export_size, |
| 5180 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5181 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5182 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5186 | if( ! exercise_key( handle, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5187 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5188 | |
| 5189 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5190 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5191 | mbedtls_free( first_export ); |
| 5192 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5193 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5194 | psa_destroy_key( base_key ); |
| 5195 | if( handle == 0 ) |
| 5196 | { |
| 5197 | /* In case there was a test failure after creating the persistent key |
| 5198 | * but while it was not open, try to re-open the persistent key |
| 5199 | * to delete it. */ |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 5200 | psa_open_key( key_id, &handle ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5201 | } |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5202 | psa_destroy_key( handle ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5203 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5204 | } |
| 5205 | /* END_CASE */ |