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