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