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