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