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