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 | |
Gilles Peskine | fc411f1 | 2018-10-25 22:34:48 +0200 | [diff] [blame] | 14 | #define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) ) |
| 15 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 16 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 17 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 18 | #if(UINT32_MAX > SIZE_MAX) |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 19 | #define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX ) |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 20 | #else |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 21 | #define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1 |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 22 | #endif |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 23 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 24 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 25 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 26 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 27 | /** Test if a buffer contains a constant byte value. |
| 28 | * |
| 29 | * `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] | 30 | * |
| 31 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 32 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 33 | * \param size Size of the buffer in bytes. |
| 34 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 35 | * \return 1 if the buffer is all-bits-zero. |
| 36 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 37 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 38 | 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] | 39 | { |
| 40 | size_t i; |
| 41 | for( i = 0; i < size; i++ ) |
| 42 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 43 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 44 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 45 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 46 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 47 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 48 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 49 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 50 | static int asn1_write_10x( unsigned char **p, |
| 51 | unsigned char *start, |
| 52 | size_t bits, |
| 53 | unsigned char x ) |
| 54 | { |
| 55 | int ret; |
| 56 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 57 | if( bits == 0 ) |
| 58 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 59 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 60 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 61 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 62 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 63 | *p -= len; |
| 64 | ( *p )[len-1] = x; |
| 65 | if( bits % 8 == 0 ) |
| 66 | ( *p )[1] |= 1; |
| 67 | else |
| 68 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 69 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 70 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 71 | MBEDTLS_ASN1_INTEGER ) ); |
| 72 | return( len ); |
| 73 | } |
| 74 | |
| 75 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 76 | size_t buffer_size, |
| 77 | unsigned char **p, |
| 78 | size_t bits, |
| 79 | int keypair ) |
| 80 | { |
| 81 | size_t half_bits = ( bits + 1 ) / 2; |
| 82 | int ret; |
| 83 | int len = 0; |
| 84 | /* Construct something that looks like a DER encoding of |
| 85 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 86 | * RSAPrivateKey ::= SEQUENCE { |
| 87 | * version Version, |
| 88 | * modulus INTEGER, -- n |
| 89 | * publicExponent INTEGER, -- e |
| 90 | * privateExponent INTEGER, -- d |
| 91 | * prime1 INTEGER, -- p |
| 92 | * prime2 INTEGER, -- q |
| 93 | * exponent1 INTEGER, -- d mod (p-1) |
| 94 | * exponent2 INTEGER, -- d mod (q-1) |
| 95 | * coefficient INTEGER, -- (inverse of q) mod p |
| 96 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 97 | * } |
| 98 | * Or, for a public key, the same structure with only |
| 99 | * version, modulus and publicExponent. |
| 100 | */ |
| 101 | *p = buffer + buffer_size; |
| 102 | if( keypair ) |
| 103 | { |
| 104 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 105 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 106 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 107 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 108 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 109 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 110 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 111 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 112 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 113 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 114 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 115 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 116 | } |
| 117 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 118 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 119 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 120 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 121 | if( keypair ) |
| 122 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 123 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 124 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 125 | { |
| 126 | const unsigned char tag = |
| 127 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 128 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 129 | } |
| 130 | return( len ); |
| 131 | } |
| 132 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 133 | static int exercise_mac_key( psa_key_slot_t key, |
| 134 | psa_key_usage_t usage, |
| 135 | psa_algorithm_t alg ) |
| 136 | { |
| 137 | psa_mac_operation_t operation; |
| 138 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 139 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 140 | size_t mac_length = sizeof( mac ); |
| 141 | |
| 142 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 143 | { |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 144 | TEST_ASSERT( psa_mac_sign_setup( &operation, |
| 145 | key, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 146 | TEST_ASSERT( psa_mac_update( &operation, |
| 147 | input, sizeof( input ) ) == PSA_SUCCESS ); |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 148 | TEST_ASSERT( psa_mac_sign_finish( &operation, |
Gilles Peskine | ef0cb40 | 2018-07-12 16:55:59 +0200 | [diff] [blame] | 149 | mac, sizeof( mac ), |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 150 | &mac_length ) == PSA_SUCCESS ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 154 | { |
| 155 | psa_status_t verify_status = |
| 156 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 157 | PSA_SUCCESS : |
| 158 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 159 | TEST_ASSERT( psa_mac_verify_setup( &operation, |
| 160 | key, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 161 | TEST_ASSERT( psa_mac_update( &operation, |
| 162 | input, sizeof( input ) ) == PSA_SUCCESS ); |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 163 | TEST_ASSERT( psa_mac_verify_finish( &operation, |
| 164 | mac, |
| 165 | mac_length ) == verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return( 1 ); |
| 169 | |
| 170 | exit: |
| 171 | psa_mac_abort( &operation ); |
| 172 | return( 0 ); |
| 173 | } |
| 174 | |
| 175 | static int exercise_cipher_key( psa_key_slot_t key, |
| 176 | psa_key_usage_t usage, |
| 177 | psa_algorithm_t alg ) |
| 178 | { |
| 179 | psa_cipher_operation_t operation; |
| 180 | unsigned char iv[16] = {0}; |
| 181 | size_t iv_length = sizeof( iv ); |
| 182 | const unsigned char plaintext[16] = "Hello, world..."; |
| 183 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 184 | size_t ciphertext_length = sizeof( ciphertext ); |
| 185 | unsigned char decrypted[sizeof( ciphertext )]; |
| 186 | size_t part_length; |
| 187 | |
| 188 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 189 | { |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 190 | TEST_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 191 | key, alg ) == PSA_SUCCESS ); |
| 192 | TEST_ASSERT( psa_cipher_generate_iv( &operation, |
| 193 | iv, sizeof( iv ), |
| 194 | &iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 195 | TEST_ASSERT( psa_cipher_update( &operation, |
| 196 | plaintext, sizeof( plaintext ), |
| 197 | ciphertext, sizeof( ciphertext ), |
| 198 | &ciphertext_length ) == PSA_SUCCESS ); |
| 199 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 200 | ciphertext + ciphertext_length, |
| 201 | sizeof( ciphertext ) - ciphertext_length, |
| 202 | &part_length ) == PSA_SUCCESS ); |
| 203 | ciphertext_length += part_length; |
| 204 | } |
| 205 | |
| 206 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 207 | { |
| 208 | psa_status_t status; |
Mohammad AboMokh | adb9b23 | 2018-06-28 01:52:54 -0700 | [diff] [blame] | 209 | psa_key_type_t type = PSA_KEY_TYPE_NONE; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 210 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 211 | { |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 212 | size_t bits; |
| 213 | TEST_ASSERT( psa_get_key_information( key, &type, &bits ) ); |
| 214 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type ); |
| 215 | } |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 216 | TEST_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 217 | key, alg ) == PSA_SUCCESS ); |
| 218 | TEST_ASSERT( psa_cipher_set_iv( &operation, |
| 219 | iv, iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 220 | TEST_ASSERT( psa_cipher_update( &operation, |
| 221 | ciphertext, ciphertext_length, |
| 222 | decrypted, sizeof( decrypted ), |
| 223 | &part_length ) == PSA_SUCCESS ); |
| 224 | status = psa_cipher_finish( &operation, |
| 225 | decrypted + part_length, |
| 226 | sizeof( decrypted ) - part_length, |
| 227 | &part_length ); |
| 228 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 229 | * if the input is some aribtrary data rather than an actual |
| 230 | ciphertext, a padding error is likely. */ |
Mohammad AboMokh | 65fa0b8 | 2018-06-28 02:14:00 -0700 | [diff] [blame] | 231 | if( ( usage & PSA_KEY_USAGE_ENCRYPT ) || |
Mohammad AboMokh | adb9b23 | 2018-06-28 01:52:54 -0700 | [diff] [blame] | 232 | PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 233 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 234 | else |
| 235 | TEST_ASSERT( status == PSA_SUCCESS || |
| 236 | status == PSA_ERROR_INVALID_PADDING ); |
| 237 | } |
| 238 | |
| 239 | return( 1 ); |
| 240 | |
| 241 | exit: |
| 242 | psa_cipher_abort( &operation ); |
| 243 | return( 0 ); |
| 244 | } |
| 245 | |
| 246 | static int exercise_aead_key( psa_key_slot_t key, |
| 247 | psa_key_usage_t usage, |
| 248 | psa_algorithm_t alg ) |
| 249 | { |
| 250 | unsigned char nonce[16] = {0}; |
| 251 | size_t nonce_length = sizeof( nonce ); |
| 252 | unsigned char plaintext[16] = "Hello, world..."; |
| 253 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 254 | size_t ciphertext_length = sizeof( ciphertext ); |
| 255 | size_t plaintext_length = sizeof( ciphertext ); |
| 256 | |
| 257 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 258 | { |
| 259 | TEST_ASSERT( psa_aead_encrypt( key, alg, |
| 260 | nonce, nonce_length, |
| 261 | NULL, 0, |
| 262 | plaintext, sizeof( plaintext ), |
| 263 | ciphertext, sizeof( ciphertext ), |
| 264 | &ciphertext_length ) == PSA_SUCCESS ); |
| 265 | } |
| 266 | |
| 267 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 268 | { |
| 269 | psa_status_t verify_status = |
| 270 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 271 | PSA_SUCCESS : |
| 272 | PSA_ERROR_INVALID_SIGNATURE ); |
| 273 | TEST_ASSERT( psa_aead_decrypt( key, alg, |
| 274 | nonce, nonce_length, |
| 275 | NULL, 0, |
| 276 | ciphertext, ciphertext_length, |
| 277 | plaintext, sizeof( plaintext ), |
| 278 | &plaintext_length ) == verify_status ); |
| 279 | } |
| 280 | |
| 281 | return( 1 ); |
| 282 | |
| 283 | exit: |
| 284 | return( 0 ); |
| 285 | } |
| 286 | |
| 287 | static int exercise_signature_key( psa_key_slot_t key, |
| 288 | psa_key_usage_t usage, |
| 289 | psa_algorithm_t alg ) |
| 290 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 291 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 292 | size_t payload_length = 16; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 293 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 294 | size_t signature_length = sizeof( signature ); |
| 295 | |
| 296 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 297 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 298 | /* Some algorithms require the payload to have the size of |
| 299 | * the hash encoded in the algorithm. Use this input size |
| 300 | * even for algorithms that allow other input sizes. */ |
| 301 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 302 | if( hash_alg != 0 ) |
| 303 | payload_length = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 304 | TEST_ASSERT( psa_asymmetric_sign( key, alg, |
| 305 | payload, payload_length, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 306 | signature, sizeof( signature ), |
| 307 | &signature_length ) == PSA_SUCCESS ); |
| 308 | } |
| 309 | |
| 310 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 311 | { |
| 312 | psa_status_t verify_status = |
| 313 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 314 | PSA_SUCCESS : |
| 315 | PSA_ERROR_INVALID_SIGNATURE ); |
| 316 | TEST_ASSERT( psa_asymmetric_verify( key, alg, |
| 317 | payload, payload_length, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 318 | signature, signature_length ) == |
| 319 | verify_status ); |
| 320 | } |
| 321 | |
| 322 | return( 1 ); |
| 323 | |
| 324 | exit: |
| 325 | return( 0 ); |
| 326 | } |
| 327 | |
| 328 | static int exercise_asymmetric_encryption_key( psa_key_slot_t key, |
| 329 | psa_key_usage_t usage, |
| 330 | psa_algorithm_t alg ) |
| 331 | { |
| 332 | unsigned char plaintext[256] = "Hello, world..."; |
| 333 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 334 | size_t ciphertext_length = sizeof( ciphertext ); |
| 335 | size_t plaintext_length = 16; |
| 336 | |
| 337 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 338 | { |
| 339 | TEST_ASSERT( |
| 340 | psa_asymmetric_encrypt( key, alg, |
| 341 | plaintext, plaintext_length, |
| 342 | NULL, 0, |
| 343 | ciphertext, sizeof( ciphertext ), |
| 344 | &ciphertext_length ) == PSA_SUCCESS ); |
| 345 | } |
| 346 | |
| 347 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 348 | { |
| 349 | psa_status_t status = |
| 350 | psa_asymmetric_decrypt( key, alg, |
| 351 | ciphertext, ciphertext_length, |
| 352 | NULL, 0, |
| 353 | plaintext, sizeof( plaintext ), |
| 354 | &plaintext_length ); |
| 355 | TEST_ASSERT( status == PSA_SUCCESS || |
| 356 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 357 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 358 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 359 | } |
| 360 | |
| 361 | return( 1 ); |
| 362 | |
| 363 | exit: |
| 364 | return( 0 ); |
| 365 | } |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 366 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 367 | static int exercise_key_derivation_key( psa_key_slot_t key, |
| 368 | psa_key_usage_t usage, |
| 369 | psa_algorithm_t alg ) |
| 370 | { |
| 371 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 372 | unsigned char label[16] = "This is a label."; |
| 373 | size_t label_length = sizeof( label ); |
| 374 | unsigned char seed[16] = "abcdefghijklmnop"; |
| 375 | size_t seed_length = sizeof( seed ); |
| 376 | unsigned char output[1]; |
| 377 | |
| 378 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 379 | { |
| 380 | TEST_ASSERT( psa_key_derivation( &generator, |
| 381 | key, alg, |
| 382 | label, label_length, |
| 383 | seed, seed_length, |
| 384 | sizeof( output ) ) == PSA_SUCCESS ); |
| 385 | TEST_ASSERT( psa_generator_read( &generator, |
| 386 | output, |
| 387 | sizeof( output ) ) == PSA_SUCCESS ); |
| 388 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
| 389 | } |
| 390 | |
| 391 | return( 1 ); |
| 392 | |
| 393 | exit: |
| 394 | return( 0 ); |
| 395 | } |
| 396 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 397 | /* We need two keys to exercise key agreement. Exercise the |
| 398 | * private key against its own public key. */ |
| 399 | static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator, |
| 400 | psa_key_type_t key_slot, |
| 401 | psa_algorithm_t alg ) |
| 402 | { |
| 403 | psa_key_type_t private_key_type; |
| 404 | psa_key_type_t public_key_type; |
| 405 | size_t key_bits; |
| 406 | uint8_t *public_key = NULL; |
| 407 | size_t public_key_length; |
| 408 | /* Return UNKNOWN_ERROR if something other than the final call to |
| 409 | * psa_key_agreement fails. This isn't fully satisfactory, but it's |
| 410 | * good enough: callers will report it as a failed test anyway. */ |
| 411 | psa_status_t status = PSA_ERROR_UNKNOWN_ERROR; |
| 412 | |
| 413 | TEST_ASSERT( psa_get_key_information( key_slot, |
| 414 | &private_key_type, |
| 415 | &key_bits ) == PSA_SUCCESS ); |
| 416 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type ); |
| 417 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 418 | ASSERT_ALLOC( public_key, public_key_length ); |
| 419 | TEST_ASSERT( public_key != NULL ); |
| 420 | TEST_ASSERT( psa_export_public_key( key_slot, |
| 421 | public_key, public_key_length, |
| 422 | &public_key_length ) == PSA_SUCCESS ); |
| 423 | |
| 424 | status = psa_key_agreement( generator, key_slot, |
| 425 | public_key, public_key_length, |
| 426 | alg ); |
| 427 | exit: |
| 428 | mbedtls_free( public_key ); |
| 429 | return( status ); |
| 430 | } |
| 431 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 432 | static int exercise_key_agreement_key( psa_key_slot_t key, |
| 433 | psa_key_usage_t usage, |
| 434 | psa_algorithm_t alg ) |
| 435 | { |
| 436 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 437 | unsigned char output[1]; |
| 438 | int ok = 0; |
| 439 | |
| 440 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 441 | { |
| 442 | /* We need two keys to exercise key agreement. Exercise the |
| 443 | * private key against its own public key. */ |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 444 | TEST_ASSERT( key_agreement_with_self( &generator, key, alg ) == |
| 445 | PSA_SUCCESS ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 446 | TEST_ASSERT( psa_generator_read( &generator, |
| 447 | output, |
| 448 | sizeof( output ) ) == PSA_SUCCESS ); |
| 449 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
| 450 | } |
| 451 | ok = 1; |
| 452 | |
| 453 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 454 | return( ok ); |
| 455 | } |
| 456 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 457 | static int is_oid_of_key_type( psa_key_type_t type, |
| 458 | const uint8_t *oid, size_t oid_length ) |
| 459 | { |
Gilles Peskine | ae3d2a2 | 2018-08-13 14:14:22 +0200 | [diff] [blame] | 460 | const uint8_t *expected_oid = NULL; |
| 461 | size_t expected_oid_length = 0; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 462 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | ae3d2a2 | 2018-08-13 14:14:22 +0200 | [diff] [blame] | 463 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
| 464 | { |
| 465 | expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA; |
| 466 | expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1; |
| 467 | } |
| 468 | else |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 469 | #endif /* MBEDTLS_RSA_C */ |
| 470 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | ae3d2a2 | 2018-08-13 14:14:22 +0200 | [diff] [blame] | 471 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 472 | { |
| 473 | expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED; |
| 474 | expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1; |
| 475 | } |
| 476 | else |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 477 | #endif /* MBEDTLS_ECP_C */ |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 478 | { |
| 479 | char message[40]; |
| 480 | mbedtls_snprintf( message, sizeof( message ), |
| 481 | "OID not known for key type=0x%08lx", |
| 482 | (unsigned long) type ); |
| 483 | test_fail( message, __LINE__, __FILE__ ); |
| 484 | return( 0 ); |
| 485 | } |
| 486 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 487 | ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 488 | return( 1 ); |
| 489 | |
| 490 | exit: |
| 491 | return( 0 ); |
| 492 | } |
| 493 | |
| 494 | static int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 495 | size_t min_bits, size_t max_bits, |
| 496 | int must_be_odd ) |
| 497 | { |
| 498 | size_t len; |
| 499 | size_t actual_bits; |
| 500 | unsigned char msb; |
| 501 | TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len, |
| 502 | MBEDTLS_ASN1_INTEGER ) == 0 ); |
| 503 | /* Tolerate a slight departure from DER encoding: |
| 504 | * - 0 may be represented by an empty string or a 1-byte string. |
| 505 | * - The sign bit may be used as a value bit. */ |
| 506 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 507 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 508 | { |
| 509 | ++( *p ); |
| 510 | --len; |
| 511 | } |
| 512 | if( min_bits == 0 && len == 0 ) |
| 513 | return( 1 ); |
| 514 | msb = ( *p )[0]; |
| 515 | TEST_ASSERT( msb != 0 ); |
| 516 | actual_bits = 8 * ( len - 1 ); |
| 517 | while( msb != 0 ) |
| 518 | { |
| 519 | msb >>= 1; |
| 520 | ++actual_bits; |
| 521 | } |
| 522 | TEST_ASSERT( actual_bits >= min_bits ); |
| 523 | TEST_ASSERT( actual_bits <= max_bits ); |
| 524 | if( must_be_odd ) |
| 525 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 526 | *p += len; |
| 527 | return( 1 ); |
| 528 | exit: |
| 529 | return( 0 ); |
| 530 | } |
| 531 | |
| 532 | static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end, |
| 533 | size_t *len, |
| 534 | unsigned char n, unsigned char tag ) |
| 535 | { |
| 536 | int ret; |
| 537 | ret = mbedtls_asn1_get_tag( p, end, len, |
| 538 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 539 | MBEDTLS_ASN1_CONSTRUCTED | ( n ) ); |
| 540 | if( ret != 0 ) |
| 541 | return( ret ); |
| 542 | end = *p + *len; |
| 543 | ret = mbedtls_asn1_get_tag( p, end, len, tag ); |
| 544 | if( ret != 0 ) |
| 545 | return( ret ); |
| 546 | if( *p + *len != end ) |
| 547 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
| 548 | return( 0 ); |
| 549 | } |
| 550 | |
| 551 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 552 | uint8_t *exported, size_t exported_length ) |
| 553 | { |
| 554 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 555 | TEST_ASSERT( exported_length == ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 556 | else |
| 557 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 558 | |
| 559 | #if defined(MBEDTLS_DES_C) |
| 560 | if( type == PSA_KEY_TYPE_DES ) |
| 561 | { |
| 562 | /* Check the parity bits. */ |
| 563 | unsigned i; |
| 564 | for( i = 0; i < bits / 8; i++ ) |
| 565 | { |
| 566 | unsigned bit_count = 0; |
| 567 | unsigned m; |
| 568 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 569 | { |
| 570 | if( exported[i] & m ) |
| 571 | ++bit_count; |
| 572 | } |
| 573 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 574 | } |
| 575 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 576 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 577 | #endif |
| 578 | |
| 579 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
| 580 | if( type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 581 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 582 | uint8_t *p = exported; |
| 583 | uint8_t *end = exported + exported_length; |
| 584 | size_t len; |
| 585 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 586 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 587 | * modulus INTEGER, -- n |
| 588 | * publicExponent INTEGER, -- e |
| 589 | * privateExponent INTEGER, -- d |
| 590 | * prime1 INTEGER, -- p |
| 591 | * prime2 INTEGER, -- q |
| 592 | * exponent1 INTEGER, -- d mod (p-1) |
| 593 | * exponent2 INTEGER, -- d mod (q-1) |
| 594 | * coefficient INTEGER, -- (inverse of q) mod p |
| 595 | * } |
| 596 | */ |
| 597 | TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len, |
| 598 | MBEDTLS_ASN1_SEQUENCE | |
| 599 | MBEDTLS_ASN1_CONSTRUCTED ) == 0 ); |
| 600 | TEST_ASSERT( p + len == end ); |
| 601 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 602 | goto exit; |
| 603 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 604 | goto exit; |
| 605 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 606 | goto exit; |
| 607 | /* Require d to be at least half the size of n. */ |
| 608 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 609 | goto exit; |
| 610 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 611 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 612 | goto exit; |
| 613 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 614 | goto exit; |
| 615 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 616 | goto exit; |
| 617 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 618 | goto exit; |
| 619 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 620 | goto exit; |
| 621 | TEST_ASSERT( p == end ); |
| 622 | } |
| 623 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 624 | #endif /* MBEDTLS_RSA_C */ |
| 625 | |
| 626 | #if defined(MBEDTLS_ECP_C) |
| 627 | if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) ) |
| 628 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 629 | /* Just the secret value */ |
| 630 | TEST_ASSERT( exported_length == PSA_BITS_TO_BYTES( bits ) ); |
| 631 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 632 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 633 | #endif /* MBEDTLS_ECP_C */ |
| 634 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 635 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 636 | { |
| 637 | uint8_t *p = exported; |
| 638 | uint8_t *end = exported + exported_length; |
| 639 | size_t len; |
| 640 | mbedtls_asn1_buf alg; |
| 641 | mbedtls_asn1_buf params; |
| 642 | mbedtls_asn1_bitstring bitstring; |
| 643 | /* SubjectPublicKeyInfo ::= SEQUENCE { |
| 644 | * algorithm AlgorithmIdentifier, |
| 645 | * subjectPublicKey BIT STRING } |
| 646 | * AlgorithmIdentifier ::= SEQUENCE { |
| 647 | * algorithm OBJECT IDENTIFIER, |
| 648 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 649 | */ |
| 650 | TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len, |
| 651 | MBEDTLS_ASN1_SEQUENCE | |
| 652 | MBEDTLS_ASN1_CONSTRUCTED ) == 0 ); |
| 653 | TEST_ASSERT( p + len == end ); |
| 654 | TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, ¶ms ) == 0 ); |
| 655 | if( ! is_oid_of_key_type( type, alg.p, alg.len ) ) |
| 656 | goto exit; |
| 657 | TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 ); |
| 658 | TEST_ASSERT( p == end ); |
| 659 | p = bitstring.p; |
| 660 | #if defined(MBEDTLS_RSA_C) |
| 661 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 662 | { |
| 663 | /* RSAPublicKey ::= SEQUENCE { |
| 664 | * modulus INTEGER, -- n |
| 665 | * publicExponent INTEGER } -- e |
| 666 | */ |
| 667 | TEST_ASSERT( bitstring.unused_bits == 0 ); |
| 668 | TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len, |
| 669 | MBEDTLS_ASN1_SEQUENCE | |
| 670 | MBEDTLS_ASN1_CONSTRUCTED ) == 0 ); |
| 671 | TEST_ASSERT( p + len == end ); |
| 672 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 673 | goto exit; |
| 674 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 675 | goto exit; |
| 676 | TEST_ASSERT( p == end ); |
| 677 | } |
| 678 | else |
| 679 | #endif /* MBEDTLS_RSA_C */ |
| 680 | #if defined(MBEDTLS_ECP_C) |
| 681 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 682 | { |
| 683 | /* ECPoint ::= ... |
Gilles Peskine | c6290c0 | 2018-08-13 17:24:59 +0200 | [diff] [blame] | 684 | * -- first 8 bits: 0x04 (uncompressed representation); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 685 | * -- then x_P as an n-bit string, big endian; |
| 686 | * -- then y_P as a n-bit string, big endian, |
| 687 | * -- where n is the order of the curve. |
| 688 | */ |
| 689 | TEST_ASSERT( bitstring.unused_bits == 0 ); |
| 690 | TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end ); |
| 691 | TEST_ASSERT( p[0] == 4 ); |
| 692 | } |
| 693 | else |
| 694 | #endif /* MBEDTLS_ECP_C */ |
| 695 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 696 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 697 | mbedtls_snprintf( message, sizeof( message ), |
| 698 | "No sanity check for public key type=0x%08lx", |
| 699 | (unsigned long) type ); |
| 700 | test_fail( message, __LINE__, __FILE__ ); |
| 701 | return( 0 ); |
| 702 | } |
| 703 | } |
| 704 | else |
| 705 | |
| 706 | { |
| 707 | /* No sanity checks for other types */ |
| 708 | } |
| 709 | |
| 710 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 711 | |
| 712 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 713 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | static int exercise_export_key( psa_key_slot_t slot, |
| 717 | psa_key_usage_t usage ) |
| 718 | { |
| 719 | psa_key_type_t type; |
| 720 | size_t bits; |
| 721 | uint8_t *exported = NULL; |
| 722 | size_t exported_size = 0; |
| 723 | size_t exported_length = 0; |
| 724 | int ok = 0; |
| 725 | |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 726 | TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS ); |
| 727 | |
| 728 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
| 729 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 730 | { |
| 731 | TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) == |
| 732 | PSA_ERROR_NOT_PERMITTED ); |
| 733 | return( 1 ); |
| 734 | } |
| 735 | |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 736 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 737 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 738 | |
| 739 | TEST_ASSERT( psa_export_key( slot, |
| 740 | exported, exported_size, |
| 741 | &exported_length ) == PSA_SUCCESS ); |
| 742 | ok = exported_key_sanity_check( type, bits, exported, exported_length ); |
| 743 | |
| 744 | exit: |
| 745 | mbedtls_free( exported ); |
| 746 | return( ok ); |
| 747 | } |
| 748 | |
| 749 | static int exercise_export_public_key( psa_key_slot_t slot ) |
| 750 | { |
| 751 | psa_key_type_t type; |
| 752 | psa_key_type_t public_type; |
| 753 | size_t bits; |
| 754 | uint8_t *exported = NULL; |
| 755 | size_t exported_size = 0; |
| 756 | size_t exported_length = 0; |
| 757 | int ok = 0; |
| 758 | |
| 759 | TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS ); |
| 760 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
| 761 | { |
| 762 | TEST_ASSERT( psa_export_public_key( slot, |
| 763 | NULL, 0, &exported_length ) == |
| 764 | PSA_ERROR_INVALID_ARGUMENT ); |
| 765 | return( 1 ); |
| 766 | } |
| 767 | |
| 768 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type ); |
| 769 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 770 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 771 | |
| 772 | TEST_ASSERT( psa_export_public_key( slot, |
| 773 | exported, exported_size, |
| 774 | &exported_length ) == PSA_SUCCESS ); |
| 775 | ok = exported_key_sanity_check( public_type, bits, |
| 776 | exported, exported_length ); |
| 777 | |
| 778 | exit: |
| 779 | mbedtls_free( exported ); |
| 780 | return( ok ); |
| 781 | } |
| 782 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 783 | static int exercise_key( psa_key_slot_t slot, |
| 784 | psa_key_usage_t usage, |
| 785 | psa_algorithm_t alg ) |
| 786 | { |
| 787 | int ok; |
| 788 | if( alg == 0 ) |
| 789 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 790 | else if( PSA_ALG_IS_MAC( alg ) ) |
| 791 | ok = exercise_mac_key( slot, usage, alg ); |
| 792 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
| 793 | ok = exercise_cipher_key( slot, usage, alg ); |
| 794 | else if( PSA_ALG_IS_AEAD( alg ) ) |
| 795 | ok = exercise_aead_key( slot, usage, alg ); |
| 796 | else if( PSA_ALG_IS_SIGN( alg ) ) |
| 797 | ok = exercise_signature_key( slot, usage, alg ); |
| 798 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 799 | ok = exercise_asymmetric_encryption_key( slot, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 800 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 801 | ok = exercise_key_derivation_key( slot, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 802 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 803 | ok = exercise_key_agreement_key( slot, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 804 | else |
| 805 | { |
| 806 | char message[40]; |
| 807 | mbedtls_snprintf( message, sizeof( message ), |
| 808 | "No code to exercise alg=0x%08lx", |
| 809 | (unsigned long) alg ); |
| 810 | test_fail( message, __LINE__, __FILE__ ); |
| 811 | ok = 0; |
| 812 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 813 | |
| 814 | ok = ok && exercise_export_key( slot, usage ); |
| 815 | ok = ok && exercise_export_public_key( slot ); |
| 816 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 817 | return( ok ); |
| 818 | } |
| 819 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 820 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 821 | psa_algorithm_t alg ) |
| 822 | { |
| 823 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 824 | { |
| 825 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 826 | PSA_KEY_USAGE_VERIFY : |
| 827 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 828 | } |
| 829 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 830 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 831 | { |
| 832 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 833 | PSA_KEY_USAGE_ENCRYPT : |
| 834 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 835 | } |
| 836 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 837 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 838 | { |
| 839 | return( PSA_KEY_USAGE_DERIVE ); |
| 840 | } |
| 841 | else |
| 842 | { |
| 843 | return( 0 ); |
| 844 | } |
| 845 | |
| 846 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 847 | |
| 848 | typedef enum { |
| 849 | IMPORT_KEY = 0, |
| 850 | GENERATE_KEY = 1, |
| 851 | DERIVE_KEY = 2 |
| 852 | } generate_method; |
| 853 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 854 | /* END_HEADER */ |
| 855 | |
| 856 | /* BEGIN_DEPENDENCIES |
| 857 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 858 | * END_DEPENDENCIES |
| 859 | */ |
| 860 | |
| 861 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 862 | void static_checks( ) |
| 863 | { |
| 864 | size_t max_truncated_mac_size = |
| 865 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 866 | |
| 867 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 868 | * encoding. The shifted mask is the maximum truncated value. The |
| 869 | * untruncated algorithm may be one byte larger. */ |
| 870 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 871 | } |
| 872 | /* END_CASE */ |
| 873 | |
| 874 | /* BEGIN_CASE */ |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 875 | void import( data_t *data, int type, int expected_status_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 876 | { |
| 877 | int slot = 1; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 878 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 879 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 880 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 881 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 882 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 883 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 884 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 885 | status = psa_import_key( slot, type, data->x, data->len ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 886 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 887 | if( status == PSA_SUCCESS ) |
| 888 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 889 | |
| 890 | exit: |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 891 | mbedtls_psa_crypto_free( ); |
| 892 | } |
| 893 | /* END_CASE */ |
| 894 | |
| 895 | /* BEGIN_CASE */ |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 896 | void import_twice( int alg_arg, int usage_arg, |
| 897 | int type1_arg, data_t *data1, |
| 898 | int expected_import1_status_arg, |
| 899 | int type2_arg, data_t *data2, |
| 900 | int expected_import2_status_arg ) |
| 901 | { |
| 902 | int slot = 1; |
| 903 | psa_algorithm_t alg = alg_arg; |
| 904 | psa_key_usage_t usage = usage_arg; |
| 905 | psa_key_type_t type1 = type1_arg; |
| 906 | psa_status_t expected_import1_status = expected_import1_status_arg; |
| 907 | psa_key_type_t type2 = type2_arg; |
| 908 | psa_status_t expected_import2_status = expected_import2_status_arg; |
| 909 | psa_key_policy_t policy; |
| 910 | psa_status_t status; |
| 911 | |
| 912 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 913 | |
| 914 | psa_key_policy_init( &policy ); |
| 915 | psa_key_policy_set_usage( &policy, usage, alg ); |
| 916 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 917 | |
| 918 | status = psa_import_key( slot, type1, data1->x, data1->len ); |
| 919 | TEST_ASSERT( status == expected_import1_status ); |
| 920 | status = psa_import_key( slot, type2, data2->x, data2->len ); |
| 921 | TEST_ASSERT( status == expected_import2_status ); |
| 922 | |
| 923 | if( expected_import1_status == PSA_SUCCESS || |
| 924 | expected_import2_status == PSA_SUCCESS ) |
| 925 | { |
| 926 | TEST_ASSERT( exercise_key( slot, usage, alg ) ); |
| 927 | } |
| 928 | |
| 929 | exit: |
| 930 | mbedtls_psa_crypto_free( ); |
| 931 | } |
| 932 | /* END_CASE */ |
| 933 | |
| 934 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 935 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 936 | { |
| 937 | int slot = 1; |
| 938 | size_t bits = bits_arg; |
| 939 | psa_status_t expected_status = expected_status_arg; |
| 940 | psa_status_t status; |
| 941 | psa_key_type_t type = |
| 942 | keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY; |
| 943 | size_t buffer_size = /* Slight overapproximations */ |
| 944 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 945 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 946 | unsigned char *p; |
| 947 | int ret; |
| 948 | size_t length; |
| 949 | |
| 950 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 951 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 952 | |
| 953 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 954 | bits, keypair ) ) >= 0 ); |
| 955 | length = ret; |
| 956 | |
| 957 | /* Try importing the key */ |
| 958 | status = psa_import_key( slot, type, p, length ); |
| 959 | TEST_ASSERT( status == expected_status ); |
| 960 | if( status == PSA_SUCCESS ) |
| 961 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 962 | |
| 963 | exit: |
| 964 | mbedtls_free( buffer ); |
| 965 | mbedtls_psa_crypto_free( ); |
| 966 | } |
| 967 | /* END_CASE */ |
| 968 | |
| 969 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 970 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 971 | int type_arg, |
| 972 | int alg_arg, |
| 973 | int usage_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 974 | int expected_bits, |
| 975 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 976 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 977 | int canonical_input ) |
| 978 | { |
| 979 | int slot = 1; |
| 980 | int slot2 = slot + 1; |
| 981 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 982 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 983 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 984 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 985 | unsigned char *exported = NULL; |
| 986 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 987 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 988 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 989 | size_t reexported_length; |
| 990 | psa_key_type_t got_type; |
| 991 | size_t got_bits; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 992 | psa_key_policy_t policy; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 993 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 994 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 995 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 996 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 997 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 998 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 999 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1000 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1001 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1002 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1003 | psa_key_policy_set_usage( &policy, usage_arg, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1004 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1005 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1006 | /* Import the key */ |
| 1007 | TEST_ASSERT( psa_import_key( slot, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1008 | data->x, data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1009 | |
| 1010 | /* Test the key information */ |
| 1011 | TEST_ASSERT( psa_get_key_information( slot, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1012 | &got_type, |
| 1013 | &got_bits ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1014 | TEST_ASSERT( got_type == type ); |
| 1015 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 1016 | |
| 1017 | /* Export the key */ |
| 1018 | status = psa_export_key( slot, |
| 1019 | exported, export_size, |
| 1020 | &exported_length ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1021 | TEST_ASSERT( status == expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1022 | |
| 1023 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1024 | * and export_size. On errors, the exported length must be 0. */ |
| 1025 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1026 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1027 | TEST_ASSERT( exported_length <= export_size ); |
| 1028 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1029 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1030 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1031 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1032 | { |
| 1033 | TEST_ASSERT( exported_length == 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1034 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1035 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1036 | |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1037 | if( ! exercise_export_key( slot, usage_arg ) ) |
| 1038 | goto exit; |
| 1039 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1040 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1041 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1042 | else |
| 1043 | { |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1044 | TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS ); |
| 1045 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1046 | TEST_ASSERT( psa_import_key( slot2, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1047 | exported, |
Gilles Peskine | b67f308 | 2018-08-07 15:33:10 +0200 | [diff] [blame] | 1048 | exported_length ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1049 | TEST_ASSERT( psa_export_key( slot2, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1050 | reexported, |
| 1051 | export_size, |
| 1052 | &reexported_length ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1053 | ASSERT_COMPARE( exported, exported_length, |
| 1054 | reexported, reexported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1055 | } |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1056 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1057 | |
| 1058 | destroy: |
| 1059 | /* Destroy the key */ |
| 1060 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 1061 | TEST_ASSERT( psa_get_key_information( |
| 1062 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 1063 | |
| 1064 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1065 | mbedtls_free( exported ); |
| 1066 | mbedtls_free( reexported ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1067 | mbedtls_psa_crypto_free( ); |
| 1068 | } |
| 1069 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1070 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1071 | /* BEGIN_CASE */ |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1072 | void import_key_nonempty_slot( ) |
| 1073 | { |
| 1074 | int slot = 1; |
| 1075 | psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA; |
| 1076 | psa_status_t status; |
| 1077 | const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 }; |
| 1078 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1079 | |
| 1080 | /* Import the key */ |
| 1081 | TEST_ASSERT( psa_import_key( slot, type, |
| 1082 | data, sizeof( data ) ) == PSA_SUCCESS ); |
| 1083 | |
| 1084 | /* Import the key again */ |
| 1085 | status = psa_import_key( slot, type, data, sizeof( data ) ); |
| 1086 | TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT ); |
| 1087 | |
| 1088 | exit: |
| 1089 | mbedtls_psa_crypto_free( ); |
| 1090 | } |
| 1091 | /* END_CASE */ |
| 1092 | |
| 1093 | /* BEGIN_CASE */ |
| 1094 | void export_invalid_slot( int slot, int expected_export_status_arg ) |
| 1095 | { |
| 1096 | psa_status_t status; |
| 1097 | unsigned char *exported = NULL; |
| 1098 | size_t export_size = 0; |
| 1099 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1100 | psa_status_t expected_export_status = expected_export_status_arg; |
| 1101 | |
| 1102 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1103 | |
| 1104 | /* Export the key */ |
| 1105 | status = psa_export_key( slot, |
| 1106 | exported, export_size, |
| 1107 | &exported_length ); |
| 1108 | TEST_ASSERT( status == expected_export_status ); |
| 1109 | |
| 1110 | exit: |
| 1111 | mbedtls_psa_crypto_free( ); |
| 1112 | } |
| 1113 | /* END_CASE */ |
| 1114 | |
| 1115 | /* BEGIN_CASE */ |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1116 | void export_with_no_key_activity( ) |
| 1117 | { |
| 1118 | int slot = 1; |
| 1119 | psa_algorithm_t alg = PSA_ALG_CTR; |
| 1120 | psa_status_t status; |
| 1121 | psa_key_policy_t policy; |
| 1122 | unsigned char *exported = NULL; |
| 1123 | size_t export_size = 0; |
| 1124 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1125 | |
| 1126 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1127 | |
| 1128 | psa_key_policy_init( &policy ); |
| 1129 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
| 1130 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1131 | |
| 1132 | /* Export the key */ |
| 1133 | status = psa_export_key( slot, |
| 1134 | exported, export_size, |
| 1135 | &exported_length ); |
| 1136 | TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT ); |
| 1137 | |
| 1138 | exit: |
| 1139 | mbedtls_psa_crypto_free( ); |
| 1140 | } |
| 1141 | /* END_CASE */ |
| 1142 | |
| 1143 | /* BEGIN_CASE */ |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1144 | void cipher_with_no_key_activity( ) |
| 1145 | { |
| 1146 | int slot = 1; |
| 1147 | psa_status_t status; |
| 1148 | psa_key_policy_t policy; |
| 1149 | psa_cipher_operation_t operation; |
| 1150 | int exercise_alg = PSA_ALG_CTR; |
| 1151 | |
| 1152 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1153 | |
| 1154 | psa_key_policy_init( &policy ); |
| 1155 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg ); |
| 1156 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1157 | |
| 1158 | status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg ); |
| 1159 | TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT ); |
| 1160 | |
| 1161 | exit: |
| 1162 | psa_cipher_abort( &operation ); |
| 1163 | mbedtls_psa_crypto_free( ); |
| 1164 | } |
| 1165 | /* END_CASE */ |
| 1166 | |
| 1167 | /* BEGIN_CASE */ |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1168 | void export_after_import_failure( data_t *data, int type_arg, |
| 1169 | int expected_import_status_arg ) |
| 1170 | { |
| 1171 | int slot = 1; |
| 1172 | psa_key_type_t type = type_arg; |
| 1173 | psa_status_t status; |
| 1174 | unsigned char *exported = NULL; |
| 1175 | size_t export_size = 0; |
| 1176 | psa_status_t expected_import_status = expected_import_status_arg; |
| 1177 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1178 | |
| 1179 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1180 | |
| 1181 | /* Import the key - expect failure */ |
| 1182 | status = psa_import_key( slot, type, |
| 1183 | data->x, data->len ); |
| 1184 | TEST_ASSERT( status == expected_import_status ); |
| 1185 | |
| 1186 | /* Export the key */ |
| 1187 | status = psa_export_key( slot, |
| 1188 | exported, export_size, |
| 1189 | &exported_length ); |
| 1190 | TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT ); |
| 1191 | |
| 1192 | exit: |
| 1193 | mbedtls_psa_crypto_free( ); |
| 1194 | } |
| 1195 | /* END_CASE */ |
| 1196 | |
| 1197 | /* BEGIN_CASE */ |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1198 | void cipher_after_import_failure( data_t *data, int type_arg, |
| 1199 | int expected_import_status_arg ) |
| 1200 | { |
| 1201 | int slot = 1; |
| 1202 | psa_cipher_operation_t operation; |
| 1203 | psa_key_type_t type = type_arg; |
| 1204 | psa_status_t status; |
| 1205 | psa_status_t expected_import_status = expected_import_status_arg; |
| 1206 | int exercise_alg = PSA_ALG_CTR; |
| 1207 | |
| 1208 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1209 | |
| 1210 | /* Import the key - expect failure */ |
| 1211 | status = psa_import_key( slot, type, |
| 1212 | data->x, data->len ); |
| 1213 | TEST_ASSERT( status == expected_import_status ); |
| 1214 | |
| 1215 | status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg ); |
| 1216 | TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT ); |
| 1217 | |
| 1218 | exit: |
| 1219 | psa_cipher_abort( &operation ); |
| 1220 | mbedtls_psa_crypto_free( ); |
| 1221 | } |
| 1222 | /* END_CASE */ |
| 1223 | |
| 1224 | /* BEGIN_CASE */ |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1225 | void export_after_destroy_key( data_t *data, int type_arg ) |
| 1226 | { |
| 1227 | int slot = 1; |
| 1228 | psa_key_type_t type = type_arg; |
| 1229 | psa_status_t status; |
| 1230 | psa_key_policy_t policy; |
| 1231 | psa_algorithm_t alg = PSA_ALG_CTR; |
| 1232 | unsigned char *exported = NULL; |
| 1233 | size_t export_size = 0; |
| 1234 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1235 | |
| 1236 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1237 | |
| 1238 | psa_key_policy_init( &policy ); |
| 1239 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
| 1240 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1241 | export_size = (ptrdiff_t) data->len; |
| 1242 | ASSERT_ALLOC( exported, export_size ); |
| 1243 | |
| 1244 | /* Import the key */ |
| 1245 | TEST_ASSERT( psa_import_key( slot, type, |
| 1246 | data->x, data->len ) == PSA_SUCCESS ); |
| 1247 | |
| 1248 | TEST_ASSERT( psa_export_key( slot, exported, export_size, |
| 1249 | &exported_length ) == PSA_SUCCESS ); |
| 1250 | |
| 1251 | /* Destroy the key */ |
| 1252 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 1253 | |
| 1254 | /* Export the key */ |
| 1255 | status = psa_export_key( slot, exported, export_size, |
| 1256 | &exported_length ); |
| 1257 | TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT ); |
| 1258 | |
| 1259 | exit: |
| 1260 | mbedtls_free( exported ); |
| 1261 | mbedtls_psa_crypto_free( ); |
| 1262 | } |
| 1263 | /* END_CASE */ |
| 1264 | |
| 1265 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1266 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1267 | int type_arg, |
| 1268 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1269 | int export_size_delta, |
| 1270 | int expected_export_status_arg, |
| 1271 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1272 | { |
| 1273 | int slot = 1; |
| 1274 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1275 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1276 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1277 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1278 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1279 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1280 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1281 | psa_key_policy_t policy; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1282 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1283 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1284 | |
| 1285 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1286 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1287 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1288 | |
| 1289 | /* Import the key */ |
| 1290 | TEST_ASSERT( psa_import_key( slot, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1291 | data->x, data->len ) == PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1292 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1293 | /* Export the public key */ |
| 1294 | ASSERT_ALLOC( exported, export_size ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1295 | status = psa_export_public_key( slot, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1296 | exported, export_size, |
| 1297 | &exported_length ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1298 | TEST_ASSERT( status == expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1299 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1300 | { |
| 1301 | psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type ); |
| 1302 | size_t bits; |
| 1303 | TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) == |
| 1304 | PSA_SUCCESS ); |
| 1305 | TEST_ASSERT( expected_public_key->len <= |
| 1306 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1307 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1308 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1309 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1310 | |
| 1311 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1312 | mbedtls_free( exported ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1313 | psa_destroy_key( slot ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1314 | mbedtls_psa_crypto_free( ); |
| 1315 | } |
| 1316 | /* END_CASE */ |
| 1317 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1318 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1319 | void import_and_exercise_key( data_t *data, |
| 1320 | int type_arg, |
| 1321 | int bits_arg, |
| 1322 | int alg_arg ) |
| 1323 | { |
| 1324 | int slot = 1; |
| 1325 | psa_key_type_t type = type_arg; |
| 1326 | size_t bits = bits_arg; |
| 1327 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1328 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1329 | psa_key_policy_t policy; |
| 1330 | psa_key_type_t got_type; |
| 1331 | size_t got_bits; |
| 1332 | psa_status_t status; |
| 1333 | |
| 1334 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1335 | |
| 1336 | psa_key_policy_init( &policy ); |
| 1337 | psa_key_policy_set_usage( &policy, usage, alg ); |
| 1338 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1339 | |
| 1340 | /* Import the key */ |
| 1341 | status = psa_import_key( slot, type, data->x, data->len ); |
| 1342 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1343 | |
| 1344 | /* Test the key information */ |
| 1345 | TEST_ASSERT( psa_get_key_information( slot, |
| 1346 | &got_type, |
| 1347 | &got_bits ) == PSA_SUCCESS ); |
| 1348 | TEST_ASSERT( got_type == type ); |
| 1349 | TEST_ASSERT( got_bits == bits ); |
| 1350 | |
| 1351 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1352 | if( ! exercise_key( slot, usage, alg ) ) |
| 1353 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1354 | |
| 1355 | exit: |
| 1356 | psa_destroy_key( slot ); |
| 1357 | mbedtls_psa_crypto_free( ); |
| 1358 | } |
| 1359 | /* END_CASE */ |
| 1360 | |
| 1361 | /* BEGIN_CASE */ |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1362 | void key_policy( int usage_arg, int alg_arg ) |
| 1363 | { |
| 1364 | int key_slot = 1; |
| 1365 | psa_algorithm_t alg = alg_arg; |
| 1366 | psa_key_usage_t usage = usage_arg; |
| 1367 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 1368 | unsigned char key[32] = {0}; |
| 1369 | psa_key_policy_t policy_set; |
| 1370 | psa_key_policy_t policy_get; |
| 1371 | |
| 1372 | memset( key, 0x2a, sizeof( key ) ); |
| 1373 | |
| 1374 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1375 | |
| 1376 | psa_key_policy_init( &policy_set ); |
| 1377 | psa_key_policy_init( &policy_get ); |
| 1378 | |
| 1379 | psa_key_policy_set_usage( &policy_set, usage, alg ); |
| 1380 | |
| 1381 | TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage ); |
| 1382 | TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg ); |
| 1383 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); |
| 1384 | |
| 1385 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1386 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 1387 | |
| 1388 | TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); |
| 1389 | |
| 1390 | TEST_ASSERT( policy_get.usage == policy_set.usage ); |
| 1391 | TEST_ASSERT( policy_get.alg == policy_set.alg ); |
| 1392 | |
| 1393 | exit: |
| 1394 | psa_destroy_key( key_slot ); |
| 1395 | mbedtls_psa_crypto_free( ); |
| 1396 | } |
| 1397 | /* END_CASE */ |
| 1398 | |
| 1399 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1400 | void mac_key_policy( int policy_usage, |
| 1401 | int policy_alg, |
| 1402 | int key_type, |
| 1403 | data_t *key_data, |
| 1404 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1405 | { |
| 1406 | int key_slot = 1; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1407 | psa_key_policy_t policy; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1408 | psa_mac_operation_t operation; |
| 1409 | psa_status_t status; |
| 1410 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1411 | |
| 1412 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1413 | |
| 1414 | psa_key_policy_init( &policy ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1415 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1416 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1417 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1418 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1419 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1420 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1421 | status = psa_mac_sign_setup( &operation, key_slot, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1422 | if( policy_alg == exercise_alg && |
| 1423 | ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
| 1424 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1425 | else |
| 1426 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1427 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1428 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1429 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1430 | status = psa_mac_verify_setup( &operation, key_slot, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1431 | if( policy_alg == exercise_alg && |
| 1432 | ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1433 | TEST_ASSERT( status == PSA_SUCCESS ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1434 | else |
| 1435 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1436 | |
| 1437 | exit: |
| 1438 | psa_mac_abort( &operation ); |
| 1439 | psa_destroy_key( key_slot ); |
| 1440 | mbedtls_psa_crypto_free( ); |
| 1441 | } |
| 1442 | /* END_CASE */ |
| 1443 | |
| 1444 | /* BEGIN_CASE */ |
| 1445 | void cipher_key_policy( int policy_usage, |
| 1446 | int policy_alg, |
| 1447 | int key_type, |
| 1448 | data_t *key_data, |
| 1449 | int exercise_alg ) |
| 1450 | { |
| 1451 | int key_slot = 1; |
| 1452 | psa_key_policy_t policy; |
| 1453 | psa_cipher_operation_t operation; |
| 1454 | psa_status_t status; |
| 1455 | |
| 1456 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1457 | |
| 1458 | psa_key_policy_init( &policy ); |
| 1459 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
| 1460 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1461 | |
| 1462 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1463 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1464 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1465 | status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1466 | if( policy_alg == exercise_alg && |
| 1467 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1468 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1469 | else |
| 1470 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1471 | psa_cipher_abort( &operation ); |
| 1472 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1473 | status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1474 | if( policy_alg == exercise_alg && |
| 1475 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1476 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1477 | else |
| 1478 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1479 | |
| 1480 | exit: |
| 1481 | psa_cipher_abort( &operation ); |
| 1482 | psa_destroy_key( key_slot ); |
| 1483 | mbedtls_psa_crypto_free( ); |
| 1484 | } |
| 1485 | /* END_CASE */ |
| 1486 | |
| 1487 | /* BEGIN_CASE */ |
| 1488 | void aead_key_policy( int policy_usage, |
| 1489 | int policy_alg, |
| 1490 | int key_type, |
| 1491 | data_t *key_data, |
| 1492 | int nonce_length_arg, |
| 1493 | int tag_length_arg, |
| 1494 | int exercise_alg ) |
| 1495 | { |
| 1496 | int key_slot = 1; |
| 1497 | psa_key_policy_t policy; |
| 1498 | psa_status_t status; |
| 1499 | unsigned char nonce[16] = {0}; |
| 1500 | size_t nonce_length = nonce_length_arg; |
| 1501 | unsigned char tag[16]; |
| 1502 | size_t tag_length = tag_length_arg; |
| 1503 | size_t output_length; |
| 1504 | |
| 1505 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1506 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1507 | |
| 1508 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1509 | |
| 1510 | psa_key_policy_init( &policy ); |
| 1511 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
| 1512 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1513 | |
| 1514 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1515 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1516 | |
| 1517 | status = psa_aead_encrypt( key_slot, exercise_alg, |
| 1518 | nonce, nonce_length, |
| 1519 | NULL, 0, |
| 1520 | NULL, 0, |
| 1521 | tag, tag_length, |
| 1522 | &output_length ); |
| 1523 | if( policy_alg == exercise_alg && |
| 1524 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1525 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1526 | else |
| 1527 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1528 | |
| 1529 | memset( tag, 0, sizeof( tag ) ); |
| 1530 | status = psa_aead_decrypt( key_slot, exercise_alg, |
| 1531 | nonce, nonce_length, |
| 1532 | NULL, 0, |
| 1533 | tag, tag_length, |
| 1534 | NULL, 0, |
| 1535 | &output_length ); |
| 1536 | if( policy_alg == exercise_alg && |
| 1537 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1538 | TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE ); |
| 1539 | else |
| 1540 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1541 | |
| 1542 | exit: |
| 1543 | psa_destroy_key( key_slot ); |
| 1544 | mbedtls_psa_crypto_free( ); |
| 1545 | } |
| 1546 | /* END_CASE */ |
| 1547 | |
| 1548 | /* BEGIN_CASE */ |
| 1549 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1550 | int policy_alg, |
| 1551 | int key_type, |
| 1552 | data_t *key_data, |
| 1553 | int exercise_alg ) |
| 1554 | { |
| 1555 | int key_slot = 1; |
| 1556 | psa_key_policy_t policy; |
| 1557 | psa_status_t status; |
| 1558 | size_t key_bits; |
| 1559 | size_t buffer_length; |
| 1560 | unsigned char *buffer = NULL; |
| 1561 | size_t output_length; |
| 1562 | |
| 1563 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1564 | |
| 1565 | psa_key_policy_init( &policy ); |
| 1566 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
| 1567 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1568 | |
| 1569 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1570 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1571 | |
| 1572 | TEST_ASSERT( psa_get_key_information( key_slot, |
| 1573 | NULL, |
| 1574 | &key_bits ) == PSA_SUCCESS ); |
| 1575 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1576 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1577 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1578 | |
| 1579 | status = psa_asymmetric_encrypt( key_slot, exercise_alg, |
| 1580 | NULL, 0, |
| 1581 | NULL, 0, |
| 1582 | buffer, buffer_length, |
| 1583 | &output_length ); |
| 1584 | if( policy_alg == exercise_alg && |
| 1585 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1586 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1587 | else |
| 1588 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1589 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1590 | if( buffer_length != 0 ) |
| 1591 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1592 | status = psa_asymmetric_decrypt( key_slot, exercise_alg, |
| 1593 | buffer, buffer_length, |
| 1594 | NULL, 0, |
| 1595 | buffer, buffer_length, |
| 1596 | &output_length ); |
| 1597 | if( policy_alg == exercise_alg && |
| 1598 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1599 | TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING ); |
| 1600 | else |
| 1601 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1602 | |
| 1603 | exit: |
| 1604 | psa_destroy_key( key_slot ); |
| 1605 | mbedtls_psa_crypto_free( ); |
| 1606 | mbedtls_free( buffer ); |
| 1607 | } |
| 1608 | /* END_CASE */ |
| 1609 | |
| 1610 | /* BEGIN_CASE */ |
| 1611 | void asymmetric_signature_key_policy( int policy_usage, |
| 1612 | int policy_alg, |
| 1613 | int key_type, |
| 1614 | data_t *key_data, |
| 1615 | int exercise_alg ) |
| 1616 | { |
| 1617 | int key_slot = 1; |
| 1618 | psa_key_policy_t policy; |
| 1619 | psa_status_t status; |
| 1620 | unsigned char payload[16] = {1}; |
| 1621 | size_t payload_length = sizeof( payload ); |
| 1622 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
| 1623 | size_t signature_length; |
| 1624 | |
| 1625 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1626 | |
| 1627 | psa_key_policy_init( &policy ); |
| 1628 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
| 1629 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1630 | |
| 1631 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1632 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1633 | |
| 1634 | status = psa_asymmetric_sign( key_slot, exercise_alg, |
| 1635 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1636 | signature, sizeof( signature ), |
| 1637 | &signature_length ); |
| 1638 | if( policy_alg == exercise_alg && |
| 1639 | ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
| 1640 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1641 | else |
| 1642 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1643 | |
| 1644 | memset( signature, 0, sizeof( signature ) ); |
| 1645 | status = psa_asymmetric_verify( key_slot, exercise_alg, |
| 1646 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1647 | signature, sizeof( signature ) ); |
| 1648 | if( policy_alg == exercise_alg && |
| 1649 | ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
| 1650 | TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE ); |
| 1651 | else |
| 1652 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1653 | |
| 1654 | exit: |
| 1655 | psa_destroy_key( key_slot ); |
| 1656 | mbedtls_psa_crypto_free( ); |
| 1657 | } |
| 1658 | /* END_CASE */ |
| 1659 | |
| 1660 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1661 | void derive_key_policy( int policy_usage, |
| 1662 | int policy_alg, |
| 1663 | int key_type, |
| 1664 | data_t *key_data, |
| 1665 | int exercise_alg ) |
| 1666 | { |
| 1667 | int key_slot = 1; |
| 1668 | psa_key_policy_t policy; |
| 1669 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 1670 | psa_status_t status; |
| 1671 | |
| 1672 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1673 | |
| 1674 | psa_key_policy_init( &policy ); |
| 1675 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
| 1676 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1677 | |
| 1678 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1679 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1680 | |
| 1681 | status = psa_key_derivation( &generator, key_slot, |
| 1682 | exercise_alg, |
| 1683 | NULL, 0, |
| 1684 | NULL, 0, |
| 1685 | 1 ); |
| 1686 | if( policy_alg == exercise_alg && |
| 1687 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 1688 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1689 | else |
| 1690 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1691 | |
| 1692 | exit: |
| 1693 | psa_generator_abort( &generator ); |
| 1694 | psa_destroy_key( key_slot ); |
| 1695 | mbedtls_psa_crypto_free( ); |
| 1696 | } |
| 1697 | /* END_CASE */ |
| 1698 | |
| 1699 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1700 | void agreement_key_policy( int policy_usage, |
| 1701 | int policy_alg, |
| 1702 | int key_type_arg, |
| 1703 | data_t *key_data, |
| 1704 | int exercise_alg ) |
| 1705 | { |
| 1706 | int key_slot = 1; |
| 1707 | psa_key_policy_t policy; |
| 1708 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1709 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 1710 | psa_status_t status; |
| 1711 | |
| 1712 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1713 | |
| 1714 | psa_key_policy_init( &policy ); |
| 1715 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
| 1716 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1717 | |
| 1718 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1719 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1720 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 1721 | status = key_agreement_with_self( &generator, key_slot, exercise_alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1722 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1723 | if( policy_alg == exercise_alg && |
| 1724 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 1725 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1726 | else |
| 1727 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1728 | |
| 1729 | exit: |
| 1730 | psa_generator_abort( &generator ); |
| 1731 | psa_destroy_key( key_slot ); |
| 1732 | mbedtls_psa_crypto_free( ); |
| 1733 | } |
| 1734 | /* END_CASE */ |
| 1735 | |
| 1736 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1737 | void hash_setup( int alg_arg, |
| 1738 | int expected_status_arg ) |
| 1739 | { |
| 1740 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1741 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1742 | psa_hash_operation_t operation; |
| 1743 | psa_status_t status; |
| 1744 | |
| 1745 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1746 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1747 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1748 | psa_hash_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1749 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1750 | |
| 1751 | exit: |
| 1752 | mbedtls_psa_crypto_free( ); |
| 1753 | } |
| 1754 | /* END_CASE */ |
| 1755 | |
| 1756 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1757 | void hash_bad_order( ) |
| 1758 | { |
| 1759 | unsigned char input[] = ""; |
| 1760 | /* SHA-256 hash of an empty string */ |
| 1761 | unsigned char hash[] = { |
| 1762 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1763 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1764 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
| 1765 | size_t hash_len; |
| 1766 | psa_hash_operation_t operation; |
| 1767 | |
| 1768 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1769 | |
| 1770 | /* psa_hash_update without calling psa_hash_setup beforehand */ |
| 1771 | memset( &operation, 0, sizeof( operation ) ); |
| 1772 | TEST_ASSERT( psa_hash_update( &operation, |
| 1773 | input, sizeof( input ) ) == |
| 1774 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1775 | |
| 1776 | /* psa_hash_verify without calling psa_hash_setup beforehand */ |
| 1777 | memset( &operation, 0, sizeof( operation ) ); |
| 1778 | TEST_ASSERT( psa_hash_verify( &operation, |
| 1779 | hash, sizeof( hash ) ) == |
| 1780 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1781 | |
| 1782 | /* psa_hash_finish without calling psa_hash_setup beforehand */ |
| 1783 | memset( &operation, 0, sizeof( operation ) ); |
| 1784 | TEST_ASSERT( psa_hash_finish( &operation, |
| 1785 | hash, sizeof( hash ), &hash_len ) == |
| 1786 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1787 | |
| 1788 | exit: |
| 1789 | mbedtls_psa_crypto_free( ); |
| 1790 | } |
| 1791 | /* END_CASE */ |
| 1792 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1793 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 1794 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1795 | { |
| 1796 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1797 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1798 | * appended to it */ |
| 1799 | unsigned char hash[] = { |
| 1800 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1801 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1802 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1803 | size_t expected_size = PSA_HASH_SIZE( alg ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1804 | psa_hash_operation_t operation; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1805 | |
| 1806 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1807 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1808 | /* psa_hash_verify with a smaller hash than expected */ |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1809 | TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); |
| 1810 | TEST_ASSERT( psa_hash_verify( &operation, |
| 1811 | hash, expected_size - 1 ) == |
| 1812 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1813 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1814 | /* psa_hash_verify with a non-matching hash */ |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1815 | TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1816 | TEST_ASSERT( psa_hash_verify( &operation, |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1817 | hash + 1, expected_size ) == |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1818 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1819 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1820 | /* psa_hash_verify with a hash longer than expected */ |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1821 | TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1822 | TEST_ASSERT( psa_hash_verify( &operation, |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1823 | hash, sizeof( hash ) ) == |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1824 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1825 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1826 | exit: |
| 1827 | mbedtls_psa_crypto_free( ); |
| 1828 | } |
| 1829 | /* END_CASE */ |
| 1830 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1831 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 1832 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1833 | { |
| 1834 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1835 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1836 | size_t expected_size = PSA_HASH_SIZE( alg ); |
| 1837 | psa_hash_operation_t operation; |
| 1838 | size_t hash_len; |
| 1839 | |
| 1840 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1841 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1842 | /* psa_hash_finish with a smaller hash buffer than expected */ |
| 1843 | TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); |
| 1844 | TEST_ASSERT( psa_hash_finish( &operation, |
| 1845 | hash, expected_size - 1, |
| 1846 | &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1847 | |
| 1848 | exit: |
| 1849 | mbedtls_psa_crypto_free( ); |
| 1850 | } |
| 1851 | /* END_CASE */ |
| 1852 | |
| 1853 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1854 | void mac_setup( int key_type_arg, |
| 1855 | data_t *key, |
| 1856 | int alg_arg, |
| 1857 | int expected_status_arg ) |
| 1858 | { |
| 1859 | int key_slot = 1; |
| 1860 | psa_key_type_t key_type = key_type_arg; |
| 1861 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1862 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1863 | psa_mac_operation_t operation; |
| 1864 | psa_key_policy_t policy; |
| 1865 | psa_status_t status; |
| 1866 | |
| 1867 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1868 | |
| 1869 | psa_key_policy_init( &policy ); |
| 1870 | psa_key_policy_set_usage( &policy, |
| 1871 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY, |
| 1872 | alg ); |
| 1873 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1874 | |
| 1875 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1876 | key->x, key->len ) == PSA_SUCCESS ); |
| 1877 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1878 | status = psa_mac_sign_setup( &operation, key_slot, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1879 | psa_mac_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1880 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1881 | |
| 1882 | exit: |
| 1883 | psa_destroy_key( key_slot ); |
| 1884 | mbedtls_psa_crypto_free( ); |
| 1885 | } |
| 1886 | /* END_CASE */ |
| 1887 | |
| 1888 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1889 | void mac_sign( int key_type_arg, |
| 1890 | data_t *key, |
| 1891 | int alg_arg, |
| 1892 | data_t *input, |
| 1893 | data_t *expected_mac ) |
| 1894 | { |
| 1895 | int key_slot = 1; |
| 1896 | psa_key_type_t key_type = key_type_arg; |
| 1897 | psa_algorithm_t alg = alg_arg; |
| 1898 | psa_mac_operation_t operation; |
| 1899 | psa_key_policy_t policy; |
| 1900 | /* Leave a little extra room in the output buffer. At the end of the |
| 1901 | * test, we'll check that the implementation didn't overwrite onto |
| 1902 | * this extra room. */ |
| 1903 | uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10]; |
| 1904 | size_t mac_buffer_size = |
| 1905 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 1906 | size_t mac_length = 0; |
| 1907 | |
| 1908 | memset( actual_mac, '+', sizeof( actual_mac ) ); |
| 1909 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
| 1910 | TEST_ASSERT( expected_mac->len <= mac_buffer_size ); |
| 1911 | |
| 1912 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1913 | |
| 1914 | psa_key_policy_init( &policy ); |
| 1915 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
| 1916 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1917 | |
| 1918 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1919 | key->x, key->len ) == PSA_SUCCESS ); |
| 1920 | |
| 1921 | /* Calculate the MAC. */ |
| 1922 | TEST_ASSERT( psa_mac_sign_setup( &operation, |
| 1923 | key_slot, alg ) == PSA_SUCCESS ); |
| 1924 | TEST_ASSERT( psa_mac_update( &operation, |
| 1925 | input->x, input->len ) == PSA_SUCCESS ); |
| 1926 | TEST_ASSERT( psa_mac_sign_finish( &operation, |
| 1927 | actual_mac, mac_buffer_size, |
| 1928 | &mac_length ) == PSA_SUCCESS ); |
| 1929 | |
| 1930 | /* Compare with the expected value. */ |
| 1931 | TEST_ASSERT( mac_length == expected_mac->len ); |
| 1932 | TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 ); |
| 1933 | |
| 1934 | /* Verify that the end of the buffer is untouched. */ |
| 1935 | TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+', |
| 1936 | sizeof( actual_mac ) - mac_length ) ); |
| 1937 | |
| 1938 | exit: |
| 1939 | psa_destroy_key( key_slot ); |
| 1940 | mbedtls_psa_crypto_free( ); |
| 1941 | } |
| 1942 | /* END_CASE */ |
| 1943 | |
| 1944 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 1945 | void mac_verify( int key_type_arg, |
| 1946 | data_t *key, |
| 1947 | int alg_arg, |
| 1948 | data_t *input, |
| 1949 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1950 | { |
| 1951 | int key_slot = 1; |
| 1952 | psa_key_type_t key_type = key_type_arg; |
| 1953 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1954 | psa_mac_operation_t operation; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1955 | psa_key_policy_t policy; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1956 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 1957 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 1958 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1959 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1960 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1961 | TEST_ASSERT( expected_mac != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1962 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1963 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 1964 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1965 | |
| 1966 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1967 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1968 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1969 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1970 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1971 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1972 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1973 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 1974 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1975 | TEST_ASSERT( psa_mac_verify_setup( &operation, |
| 1976 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1977 | TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS ); |
| 1978 | TEST_ASSERT( psa_mac_update( &operation, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1979 | input->x, input->len ) == PSA_SUCCESS ); |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 1980 | TEST_ASSERT( psa_mac_verify_finish( &operation, |
| 1981 | expected_mac->x, |
| 1982 | expected_mac->len ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1983 | |
| 1984 | exit: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1985 | psa_destroy_key( key_slot ); |
| 1986 | mbedtls_psa_crypto_free( ); |
| 1987 | } |
| 1988 | /* END_CASE */ |
| 1989 | |
| 1990 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1991 | void cipher_setup( int key_type_arg, |
| 1992 | data_t *key, |
| 1993 | int alg_arg, |
| 1994 | int expected_status_arg ) |
| 1995 | { |
| 1996 | int key_slot = 1; |
| 1997 | psa_key_type_t key_type = key_type_arg; |
| 1998 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1999 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2000 | psa_cipher_operation_t operation; |
| 2001 | psa_key_policy_t policy; |
| 2002 | psa_status_t status; |
| 2003 | |
| 2004 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2005 | |
| 2006 | psa_key_policy_init( &policy ); |
| 2007 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
| 2008 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 2009 | |
| 2010 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 2011 | key->x, key->len ) == PSA_SUCCESS ); |
| 2012 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2013 | status = psa_cipher_encrypt_setup( &operation, key_slot, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2014 | psa_cipher_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2015 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2016 | |
| 2017 | exit: |
| 2018 | psa_destroy_key( key_slot ); |
| 2019 | mbedtls_psa_crypto_free( ); |
| 2020 | } |
| 2021 | /* END_CASE */ |
| 2022 | |
| 2023 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2024 | void cipher_encrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2025 | data_t *key, |
| 2026 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2027 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2028 | { |
| 2029 | int key_slot = 1; |
| 2030 | psa_status_t status; |
| 2031 | psa_key_type_t key_type = key_type_arg; |
| 2032 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2033 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2034 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2035 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2036 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2037 | size_t output_buffer_size = 0; |
| 2038 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2039 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2040 | psa_cipher_operation_t operation; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2041 | psa_key_policy_t policy; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2042 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2043 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2044 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2045 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2046 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2047 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 2048 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2049 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2050 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2051 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2052 | |
| 2053 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2054 | |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2055 | psa_key_policy_init( &policy ); |
| 2056 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
| 2057 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 2058 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2059 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2060 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2061 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2062 | TEST_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 2063 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2064 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2065 | TEST_ASSERT( psa_cipher_set_iv( &operation, |
| 2066 | iv, iv_size ) == PSA_SUCCESS ); |
mohammad1603 | 3d91abe | 2018-07-03 13:15:54 +0300 | [diff] [blame] | 2067 | output_buffer_size = (size_t) input->len + |
| 2068 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2069 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2070 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2071 | TEST_ASSERT( psa_cipher_update( &operation, |
| 2072 | input->x, input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2073 | output, output_buffer_size, |
| 2074 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2075 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2076 | status = psa_cipher_finish( &operation, |
| 2077 | output + function_output_length, |
| 2078 | output_buffer_size, |
| 2079 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2080 | total_output_length += function_output_length; |
| 2081 | |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2082 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2083 | if( expected_status == PSA_SUCCESS ) |
| 2084 | { |
| 2085 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2086 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2087 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2088 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2089 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2090 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2091 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2092 | psa_destroy_key( key_slot ); |
| 2093 | mbedtls_psa_crypto_free( ); |
| 2094 | } |
| 2095 | /* END_CASE */ |
| 2096 | |
| 2097 | /* BEGIN_CASE */ |
| 2098 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2099 | data_t *key, |
| 2100 | data_t *input, |
| 2101 | int first_part_size, |
| 2102 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2103 | { |
| 2104 | int key_slot = 1; |
| 2105 | psa_key_type_t key_type = key_type_arg; |
| 2106 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2107 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2108 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2109 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2110 | size_t output_buffer_size = 0; |
| 2111 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2112 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2113 | psa_cipher_operation_t operation; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2114 | psa_key_policy_t policy; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2115 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2116 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2117 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2118 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2119 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2120 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 2121 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2122 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2123 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2124 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2125 | |
| 2126 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2127 | |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2128 | psa_key_policy_init( &policy ); |
| 2129 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
| 2130 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 2131 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2132 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2133 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2134 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2135 | TEST_ASSERT( psa_cipher_encrypt_setup( &operation, |
| 2136 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2137 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2138 | TEST_ASSERT( psa_cipher_set_iv( &operation, |
| 2139 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
mohammad1603 | 3d91abe | 2018-07-03 13:15:54 +0300 | [diff] [blame] | 2140 | output_buffer_size = (size_t) input->len + |
| 2141 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2142 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2143 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2144 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2145 | TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2146 | output, output_buffer_size, |
| 2147 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2148 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2149 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2150 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2151 | input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2152 | output, output_buffer_size, |
| 2153 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2154 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2155 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 2156 | output + function_output_length, |
| 2157 | output_buffer_size, |
| 2158 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2159 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2160 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 2161 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2162 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2163 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2164 | |
| 2165 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2166 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2167 | psa_destroy_key( key_slot ); |
| 2168 | mbedtls_psa_crypto_free( ); |
| 2169 | } |
| 2170 | /* END_CASE */ |
| 2171 | |
| 2172 | /* BEGIN_CASE */ |
| 2173 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2174 | data_t *key, |
| 2175 | data_t *input, |
| 2176 | int first_part_size, |
| 2177 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2178 | { |
| 2179 | int key_slot = 1; |
| 2180 | |
| 2181 | psa_key_type_t key_type = key_type_arg; |
| 2182 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2183 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2184 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2185 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2186 | size_t output_buffer_size = 0; |
| 2187 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2188 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2189 | psa_cipher_operation_t operation; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2190 | psa_key_policy_t policy; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2191 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2192 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2193 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2194 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2195 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2196 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 2197 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2198 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2199 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2200 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2201 | |
| 2202 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2203 | |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2204 | psa_key_policy_init( &policy ); |
| 2205 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
| 2206 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 2207 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2208 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2209 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2210 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2211 | TEST_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 2212 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2213 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2214 | TEST_ASSERT( psa_cipher_set_iv( &operation, |
| 2215 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2216 | |
mohammad1603 | 3d91abe | 2018-07-03 13:15:54 +0300 | [diff] [blame] | 2217 | output_buffer_size = (size_t) input->len + |
| 2218 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2219 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2220 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2221 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
| 2222 | TEST_ASSERT( psa_cipher_update( &operation, |
| 2223 | input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2224 | output, output_buffer_size, |
| 2225 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2226 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2227 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2228 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2229 | input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2230 | output, output_buffer_size, |
| 2231 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2232 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2233 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 2234 | output + function_output_length, |
| 2235 | output_buffer_size, |
| 2236 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2237 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2238 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 2239 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2240 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2241 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2242 | |
| 2243 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2244 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2245 | psa_destroy_key( key_slot ); |
| 2246 | mbedtls_psa_crypto_free( ); |
| 2247 | } |
| 2248 | /* END_CASE */ |
| 2249 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2250 | /* BEGIN_CASE */ |
| 2251 | void cipher_decrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2252 | data_t *key, |
| 2253 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2254 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2255 | { |
| 2256 | int key_slot = 1; |
| 2257 | psa_status_t status; |
| 2258 | psa_key_type_t key_type = key_type_arg; |
| 2259 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2260 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2261 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2262 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2263 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2264 | size_t output_buffer_size = 0; |
| 2265 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2266 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2267 | psa_cipher_operation_t operation; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2268 | psa_key_policy_t policy; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2269 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2270 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2271 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2272 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2273 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2274 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 2275 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2276 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2277 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2278 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2279 | |
| 2280 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2281 | |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2282 | psa_key_policy_init( &policy ); |
| 2283 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
| 2284 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 2285 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2286 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2287 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2288 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2289 | TEST_ASSERT( psa_cipher_decrypt_setup( &operation, |
| 2290 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2291 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2292 | TEST_ASSERT( psa_cipher_set_iv( &operation, |
| 2293 | iv, iv_size ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2294 | |
mohammad1603 | 3d91abe | 2018-07-03 13:15:54 +0300 | [diff] [blame] | 2295 | output_buffer_size = (size_t) input->len + |
| 2296 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2297 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2298 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2299 | TEST_ASSERT( psa_cipher_update( &operation, |
| 2300 | input->x, input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2301 | output, output_buffer_size, |
| 2302 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2303 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2304 | status = psa_cipher_finish( &operation, |
| 2305 | output + function_output_length, |
| 2306 | output_buffer_size, |
| 2307 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2308 | total_output_length += function_output_length; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2309 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2310 | |
| 2311 | if( expected_status == PSA_SUCCESS ) |
| 2312 | { |
| 2313 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2314 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2315 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2316 | } |
| 2317 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2318 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2319 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2320 | psa_destroy_key( key_slot ); |
| 2321 | mbedtls_psa_crypto_free( ); |
| 2322 | } |
| 2323 | /* END_CASE */ |
| 2324 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2325 | /* BEGIN_CASE */ |
| 2326 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2327 | data_t *key, |
| 2328 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2329 | { |
| 2330 | int key_slot = 1; |
| 2331 | psa_key_type_t key_type = key_type_arg; |
| 2332 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 2333 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2334 | size_t iv_size = 16; |
| 2335 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2336 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2337 | size_t output1_size = 0; |
| 2338 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2339 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2340 | size_t output2_size = 0; |
| 2341 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2342 | size_t function_output_length = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2343 | psa_cipher_operation_t operation1; |
| 2344 | psa_cipher_operation_t operation2; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2345 | psa_key_policy_t policy; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2346 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2347 | TEST_ASSERT( key != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2348 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2349 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2350 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2351 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2352 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2353 | |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2354 | psa_key_policy_init( &policy ); |
| 2355 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg ); |
| 2356 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 2357 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2358 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2359 | key->x, key->len ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2360 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2361 | TEST_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 2362 | key_slot, alg ) == PSA_SUCCESS ); |
| 2363 | TEST_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 2364 | key_slot, alg ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2365 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2366 | TEST_ASSERT( psa_cipher_generate_iv( &operation1, |
| 2367 | iv, iv_size, |
| 2368 | &iv_length ) == PSA_SUCCESS ); |
mohammad1603 | 3d91abe | 2018-07-03 13:15:54 +0300 | [diff] [blame] | 2369 | output1_size = (size_t) input->len + |
| 2370 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2371 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2372 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2373 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2374 | output1, output1_size, |
| 2375 | &output1_length ) == PSA_SUCCESS ); |
| 2376 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 2377 | output1 + output1_length, output1_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2378 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2379 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2380 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2381 | |
| 2382 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 2383 | |
| 2384 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2385 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2386 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2387 | TEST_ASSERT( psa_cipher_set_iv( &operation2, |
| 2388 | iv, iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2389 | TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 2390 | output2, output2_size, |
| 2391 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2392 | function_output_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2393 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 2394 | output2 + output2_length, |
| 2395 | output2_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2396 | &function_output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2397 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2398 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2399 | |
Janos Follath | 25c4fa8 | 2018-07-06 16:23:25 +0100 | [diff] [blame] | 2400 | TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2401 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2402 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2403 | |
| 2404 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2405 | mbedtls_free( output1 ); |
| 2406 | mbedtls_free( output2 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2407 | psa_destroy_key( key_slot ); |
| 2408 | mbedtls_psa_crypto_free( ); |
| 2409 | } |
| 2410 | /* END_CASE */ |
| 2411 | |
| 2412 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2413 | void cipher_verify_output_multipart( int alg_arg, |
| 2414 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2415 | data_t *key, |
| 2416 | data_t *input, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2417 | int first_part_size ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2418 | { |
| 2419 | int key_slot = 1; |
| 2420 | psa_key_type_t key_type = key_type_arg; |
| 2421 | psa_algorithm_t alg = alg_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2422 | unsigned char iv[16] = {0}; |
| 2423 | size_t iv_size = 16; |
| 2424 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2425 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2426 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2427 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2428 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2429 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2430 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2431 | size_t function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2432 | psa_cipher_operation_t operation1; |
| 2433 | psa_cipher_operation_t operation2; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2434 | psa_key_policy_t policy; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2435 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2436 | TEST_ASSERT( key != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2437 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2438 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2439 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2440 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2441 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2442 | |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2443 | psa_key_policy_init( &policy ); |
| 2444 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg ); |
| 2445 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 2446 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2447 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2448 | key->x, key->len ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2449 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2450 | TEST_ASSERT( psa_cipher_encrypt_setup( &operation1, |
| 2451 | key_slot, alg ) == PSA_SUCCESS ); |
| 2452 | TEST_ASSERT( psa_cipher_decrypt_setup( &operation2, |
| 2453 | key_slot, alg ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2454 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2455 | TEST_ASSERT( psa_cipher_generate_iv( &operation1, |
| 2456 | iv, iv_size, |
| 2457 | &iv_length ) == PSA_SUCCESS ); |
mohammad1603 | 3d91abe | 2018-07-03 13:15:54 +0300 | [diff] [blame] | 2458 | output1_buffer_size = (size_t) input->len + |
| 2459 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2460 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2461 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2462 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2463 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2464 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2465 | output1, output1_buffer_size, |
| 2466 | &function_output_length ) == PSA_SUCCESS ); |
| 2467 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2468 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2469 | TEST_ASSERT( psa_cipher_update( &operation1, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2470 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2471 | input->len - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2472 | output1, output1_buffer_size, |
| 2473 | &function_output_length ) == PSA_SUCCESS ); |
| 2474 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2475 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2476 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 2477 | output1 + output1_length, |
| 2478 | output1_buffer_size - output1_length, |
| 2479 | &function_output_length ) == PSA_SUCCESS ); |
| 2480 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2481 | |
| 2482 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 2483 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2484 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2485 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2486 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2487 | TEST_ASSERT( psa_cipher_set_iv( &operation2, |
| 2488 | iv, iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2489 | |
| 2490 | TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2491 | output2, output2_buffer_size, |
| 2492 | &function_output_length ) == PSA_SUCCESS ); |
| 2493 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2494 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2495 | TEST_ASSERT( psa_cipher_update( &operation2, |
| 2496 | output1 + first_part_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2497 | output1_length - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2498 | output2, output2_buffer_size, |
| 2499 | &function_output_length ) == PSA_SUCCESS ); |
| 2500 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2501 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2502 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 2503 | output2 + output2_length, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2504 | output2_buffer_size - output2_length, |
| 2505 | &function_output_length ) == PSA_SUCCESS ); |
| 2506 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2507 | |
Janos Follath | 25c4fa8 | 2018-07-06 16:23:25 +0100 | [diff] [blame] | 2508 | TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2509 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2510 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2511 | |
| 2512 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2513 | mbedtls_free( output1 ); |
| 2514 | mbedtls_free( output2 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2515 | psa_destroy_key( key_slot ); |
| 2516 | mbedtls_psa_crypto_free( ); |
| 2517 | } |
| 2518 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 2519 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2520 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 2521 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2522 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 2523 | data_t *nonce, |
| 2524 | data_t *additional_data, |
| 2525 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2526 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2527 | { |
| 2528 | int slot = 1; |
| 2529 | psa_key_type_t key_type = key_type_arg; |
| 2530 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2531 | unsigned char *output_data = NULL; |
| 2532 | size_t output_size = 0; |
| 2533 | size_t output_length = 0; |
| 2534 | unsigned char *output_data2 = NULL; |
| 2535 | size_t output_length2 = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2536 | size_t tag_length = 16; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2537 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2538 | psa_key_policy_t policy; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2539 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2540 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2541 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2542 | TEST_ASSERT( nonce != NULL ); |
| 2543 | TEST_ASSERT( additional_data != NULL ); |
| 2544 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2545 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2546 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 2547 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 2548 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2549 | output_size = input_data->len + tag_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2550 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2551 | |
| 2552 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2553 | |
| 2554 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2555 | psa_key_policy_set_usage( &policy, |
| 2556 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
| 2557 | alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2558 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2559 | |
| 2560 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2561 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2562 | |
| 2563 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2564 | nonce->x, nonce->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2565 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2566 | additional_data->len, |
| 2567 | input_data->x, input_data->len, |
| 2568 | output_data, output_size, |
| 2569 | &output_length ) == expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2570 | |
| 2571 | if( PSA_SUCCESS == expected_result ) |
| 2572 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2573 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2574 | |
| 2575 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2576 | nonce->x, nonce->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2577 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2578 | additional_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2579 | output_data, output_length, |
| 2580 | output_data2, output_length, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2581 | &output_length2 ) == expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2582 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2583 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 2584 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2585 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2586 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2587 | exit: |
| 2588 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2589 | mbedtls_free( output_data ); |
| 2590 | mbedtls_free( output_data2 ); |
| 2591 | mbedtls_psa_crypto_free( ); |
| 2592 | } |
| 2593 | /* END_CASE */ |
| 2594 | |
| 2595 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 2596 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 2597 | int alg_arg, |
| 2598 | data_t *nonce, |
| 2599 | data_t *additional_data, |
| 2600 | data_t *input_data, |
| 2601 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2602 | { |
| 2603 | int slot = 1; |
| 2604 | psa_key_type_t key_type = key_type_arg; |
| 2605 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2606 | unsigned char *output_data = NULL; |
| 2607 | size_t output_size = 0; |
| 2608 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2609 | size_t tag_length = 16; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2610 | psa_key_policy_t policy; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2611 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2612 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2613 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2614 | TEST_ASSERT( additional_data != NULL ); |
| 2615 | TEST_ASSERT( nonce != NULL ); |
| 2616 | TEST_ASSERT( expected_result != NULL ); |
| 2617 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2618 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2619 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 2620 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 2621 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) ); |
| 2622 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2623 | output_size = input_data->len + tag_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2624 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2625 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2626 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2627 | |
| 2628 | psa_key_policy_init( &policy ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2629 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2630 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2631 | |
| 2632 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2633 | key_data->x, |
| 2634 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2635 | |
| 2636 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2637 | nonce->x, nonce->len, |
| 2638 | additional_data->x, additional_data->len, |
| 2639 | input_data->x, input_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2640 | output_data, output_size, |
| 2641 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2642 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2643 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 2644 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2645 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2646 | exit: |
| 2647 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2648 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2649 | mbedtls_psa_crypto_free( ); |
| 2650 | } |
| 2651 | /* END_CASE */ |
| 2652 | |
| 2653 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 2654 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 2655 | int alg_arg, |
| 2656 | data_t *nonce, |
| 2657 | data_t *additional_data, |
| 2658 | data_t *input_data, |
| 2659 | data_t *expected_data, |
| 2660 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2661 | { |
| 2662 | int slot = 1; |
| 2663 | psa_key_type_t key_type = key_type_arg; |
| 2664 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2665 | unsigned char *output_data = NULL; |
| 2666 | size_t output_size = 0; |
| 2667 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2668 | size_t tag_length = 16; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2669 | psa_key_policy_t policy; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2670 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2671 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2672 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2673 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2674 | TEST_ASSERT( additional_data != NULL ); |
| 2675 | TEST_ASSERT( nonce != NULL ); |
| 2676 | TEST_ASSERT( expected_data != NULL ); |
| 2677 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2678 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2679 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 2680 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 2681 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 2682 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2683 | output_size = input_data->len + tag_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2684 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2685 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2686 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2687 | |
| 2688 | psa_key_policy_init( &policy ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2689 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2690 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2691 | |
| 2692 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2693 | key_data->x, |
| 2694 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2695 | |
| 2696 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2697 | nonce->x, nonce->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2698 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2699 | additional_data->len, |
| 2700 | input_data->x, input_data->len, |
| 2701 | output_data, output_size, |
| 2702 | &output_length ) == expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2703 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2704 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2705 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 2706 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2707 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2708 | exit: |
| 2709 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2710 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2711 | mbedtls_psa_crypto_free( ); |
| 2712 | } |
| 2713 | /* END_CASE */ |
| 2714 | |
| 2715 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2716 | void signature_size( int type_arg, |
| 2717 | int bits, |
| 2718 | int alg_arg, |
| 2719 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2720 | { |
| 2721 | psa_key_type_t type = type_arg; |
| 2722 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2723 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2724 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 2725 | exit: |
| 2726 | ; |
| 2727 | } |
| 2728 | /* END_CASE */ |
| 2729 | |
| 2730 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2731 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 2732 | int alg_arg, data_t *input_data, |
| 2733 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2734 | { |
| 2735 | int slot = 1; |
| 2736 | psa_key_type_t key_type = key_type_arg; |
| 2737 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2738 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2739 | unsigned char *signature = NULL; |
| 2740 | size_t signature_size; |
| 2741 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2742 | psa_key_policy_t policy; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2743 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2744 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2745 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2746 | TEST_ASSERT( output_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2747 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2748 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2749 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2750 | |
| 2751 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2752 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 2753 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2754 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 2755 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2756 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2757 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2758 | key_data->x, |
| 2759 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2760 | TEST_ASSERT( psa_get_key_information( slot, |
| 2761 | NULL, |
| 2762 | &key_bits ) == PSA_SUCCESS ); |
| 2763 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 2764 | /* Allocate a buffer which has the size advertized by the |
| 2765 | * library. */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2766 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 2767 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2768 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2769 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2770 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2771 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 2772 | /* Perform the signature. */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2773 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2774 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2775 | signature, signature_size, |
| 2776 | &signature_length ) == PSA_SUCCESS ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2777 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2778 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 2779 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2780 | |
| 2781 | exit: |
| 2782 | psa_destroy_key( slot ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2783 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2784 | mbedtls_psa_crypto_free( ); |
| 2785 | } |
| 2786 | /* END_CASE */ |
| 2787 | |
| 2788 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2789 | void sign_fail( int key_type_arg, data_t *key_data, |
| 2790 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 2791 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2792 | { |
| 2793 | int slot = 1; |
| 2794 | psa_key_type_t key_type = key_type_arg; |
| 2795 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 2796 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2797 | psa_status_t actual_status; |
| 2798 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 2799 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 2800 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2801 | psa_key_policy_t policy; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2802 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2803 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2804 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2805 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2806 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2807 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2808 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2809 | |
| 2810 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2811 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 2812 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2813 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 2814 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2815 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2816 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2817 | key_data->x, |
| 2818 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2819 | |
| 2820 | actual_status = psa_asymmetric_sign( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2821 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2822 | signature, signature_size, |
| 2823 | &signature_length ); |
| 2824 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 2825 | /* The value of *signature_length is unspecified on error, but |
| 2826 | * whatever it is, it should be less than signature_size, so that |
| 2827 | * if the caller tries to read *signature_length bytes without |
| 2828 | * checking the error code then they don't overflow a buffer. */ |
| 2829 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2830 | |
| 2831 | exit: |
| 2832 | psa_destroy_key( slot ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2833 | mbedtls_free( signature ); |
| 2834 | mbedtls_psa_crypto_free( ); |
| 2835 | } |
| 2836 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 2837 | |
| 2838 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2839 | void sign_verify( int key_type_arg, data_t *key_data, |
| 2840 | int alg_arg, data_t *input_data ) |
| 2841 | { |
| 2842 | int slot = 1; |
| 2843 | psa_key_type_t key_type = key_type_arg; |
| 2844 | psa_algorithm_t alg = alg_arg; |
| 2845 | size_t key_bits; |
| 2846 | unsigned char *signature = NULL; |
| 2847 | size_t signature_size; |
| 2848 | size_t signature_length = 0xdeadbeef; |
| 2849 | psa_key_policy_t policy; |
| 2850 | |
| 2851 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2852 | |
| 2853 | psa_key_policy_init( &policy ); |
| 2854 | psa_key_policy_set_usage( &policy, |
| 2855 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY, |
| 2856 | alg ); |
| 2857 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2858 | |
| 2859 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 2860 | key_data->x, |
| 2861 | key_data->len ) == PSA_SUCCESS ); |
| 2862 | TEST_ASSERT( psa_get_key_information( slot, |
| 2863 | NULL, |
| 2864 | &key_bits ) == PSA_SUCCESS ); |
| 2865 | |
| 2866 | /* Allocate a buffer which has the size advertized by the |
| 2867 | * library. */ |
| 2868 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 2869 | key_bits, alg ); |
| 2870 | TEST_ASSERT( signature_size != 0 ); |
| 2871 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2872 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2873 | |
| 2874 | /* Perform the signature. */ |
| 2875 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
| 2876 | input_data->x, input_data->len, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2877 | signature, signature_size, |
| 2878 | &signature_length ) == PSA_SUCCESS ); |
| 2879 | /* Check that the signature length looks sensible. */ |
| 2880 | TEST_ASSERT( signature_length <= signature_size ); |
| 2881 | TEST_ASSERT( signature_length > 0 ); |
| 2882 | |
| 2883 | /* Use the library to verify that the signature is correct. */ |
| 2884 | TEST_ASSERT( psa_asymmetric_verify( |
| 2885 | slot, alg, |
| 2886 | input_data->x, input_data->len, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2887 | signature, signature_length ) == PSA_SUCCESS ); |
| 2888 | |
| 2889 | if( input_data->len != 0 ) |
| 2890 | { |
| 2891 | /* Flip a bit in the input and verify that the signature is now |
| 2892 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 2893 | * because ECDSA may ignore the last few bits of the input. */ |
| 2894 | input_data->x[0] ^= 1; |
| 2895 | TEST_ASSERT( psa_asymmetric_verify( |
| 2896 | slot, alg, |
| 2897 | input_data->x, input_data->len, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2898 | signature, |
| 2899 | signature_length ) == PSA_ERROR_INVALID_SIGNATURE ); |
| 2900 | } |
| 2901 | |
| 2902 | exit: |
| 2903 | psa_destroy_key( slot ); |
| 2904 | mbedtls_free( signature ); |
| 2905 | mbedtls_psa_crypto_free( ); |
| 2906 | } |
| 2907 | /* END_CASE */ |
| 2908 | |
| 2909 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2910 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 2911 | int alg_arg, data_t *hash_data, |
| 2912 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2913 | { |
| 2914 | int slot = 1; |
| 2915 | psa_key_type_t key_type = key_type_arg; |
| 2916 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2917 | psa_key_policy_t policy; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2918 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2919 | TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
| 2920 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2921 | TEST_ASSERT( key_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2922 | TEST_ASSERT( hash_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2923 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2924 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2925 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 2926 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2927 | |
| 2928 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2929 | |
| 2930 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2931 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2932 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2933 | |
| 2934 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2935 | key_data->x, |
| 2936 | key_data->len ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2937 | |
| 2938 | TEST_ASSERT( psa_asymmetric_verify( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2939 | hash_data->x, hash_data->len, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2940 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2941 | signature_data->len ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2942 | exit: |
| 2943 | psa_destroy_key( slot ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2944 | mbedtls_psa_crypto_free( ); |
| 2945 | } |
| 2946 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2947 | |
| 2948 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2949 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 2950 | int alg_arg, data_t *hash_data, |
| 2951 | data_t *signature_data, |
| 2952 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2953 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2954 | int slot = 1; |
| 2955 | psa_key_type_t key_type = key_type_arg; |
| 2956 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2957 | psa_status_t actual_status; |
| 2958 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2959 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2960 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2961 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2962 | TEST_ASSERT( hash_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2963 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2964 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2965 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 2966 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2967 | |
| 2968 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2969 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 2970 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2971 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 2972 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2973 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2974 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2975 | key_data->x, |
| 2976 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2977 | |
| 2978 | actual_status = psa_asymmetric_verify( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2979 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2980 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2981 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2982 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2983 | TEST_ASSERT( actual_status == expected_status ); |
| 2984 | |
| 2985 | exit: |
| 2986 | psa_destroy_key( slot ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2987 | mbedtls_psa_crypto_free( ); |
| 2988 | } |
| 2989 | /* END_CASE */ |
| 2990 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2991 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 2992 | void asymmetric_encrypt( int key_type_arg, |
| 2993 | data_t *key_data, |
| 2994 | int alg_arg, |
| 2995 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 2996 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 2997 | int expected_output_length_arg, |
| 2998 | int expected_status_arg ) |
| 2999 | { |
| 3000 | int slot = 1; |
| 3001 | psa_key_type_t key_type = key_type_arg; |
| 3002 | psa_algorithm_t alg = alg_arg; |
| 3003 | size_t expected_output_length = expected_output_length_arg; |
| 3004 | size_t key_bits; |
| 3005 | unsigned char *output = NULL; |
| 3006 | size_t output_size; |
| 3007 | size_t output_length = ~0; |
| 3008 | psa_status_t actual_status; |
| 3009 | psa_status_t expected_status = expected_status_arg; |
| 3010 | psa_key_policy_t policy; |
| 3011 | |
| 3012 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3013 | |
| 3014 | /* Import the key */ |
| 3015 | psa_key_policy_init( &policy ); |
| 3016 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
| 3017 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 3018 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 3019 | key_data->x, |
| 3020 | key_data->len ) == PSA_SUCCESS ); |
| 3021 | |
| 3022 | /* Determine the maximum output length */ |
| 3023 | TEST_ASSERT( psa_get_key_information( slot, |
| 3024 | NULL, |
| 3025 | &key_bits ) == PSA_SUCCESS ); |
| 3026 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3027 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3028 | |
| 3029 | /* Encrypt the input */ |
| 3030 | actual_status = psa_asymmetric_encrypt( slot, alg, |
| 3031 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3032 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3033 | output, output_size, |
| 3034 | &output_length ); |
| 3035 | TEST_ASSERT( actual_status == expected_status ); |
| 3036 | TEST_ASSERT( output_length == expected_output_length ); |
| 3037 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3038 | /* If the label is empty, the test framework puts a non-null pointer |
| 3039 | * in label->x. Test that a null pointer works as well. */ |
| 3040 | if( label->len == 0 ) |
| 3041 | { |
| 3042 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3043 | if( output_size != 0 ) |
| 3044 | memset( output, 0, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3045 | actual_status = psa_asymmetric_encrypt( slot, alg, |
| 3046 | input_data->x, input_data->len, |
| 3047 | NULL, label->len, |
| 3048 | output, output_size, |
| 3049 | &output_length ); |
| 3050 | TEST_ASSERT( actual_status == expected_status ); |
| 3051 | TEST_ASSERT( output_length == expected_output_length ); |
| 3052 | } |
| 3053 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3054 | exit: |
| 3055 | psa_destroy_key( slot ); |
| 3056 | mbedtls_free( output ); |
| 3057 | mbedtls_psa_crypto_free( ); |
| 3058 | } |
| 3059 | /* END_CASE */ |
| 3060 | |
| 3061 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3062 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 3063 | data_t *key_data, |
| 3064 | int alg_arg, |
| 3065 | data_t *input_data, |
| 3066 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3067 | { |
| 3068 | int slot = 1; |
| 3069 | psa_key_type_t key_type = key_type_arg; |
| 3070 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3071 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3072 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3073 | size_t output_size; |
| 3074 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3075 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3076 | size_t output2_size; |
| 3077 | size_t output2_length = ~0; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 3078 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3079 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3080 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3081 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3082 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 3083 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 3084 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3085 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3086 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3087 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3088 | psa_key_policy_set_usage( &policy, |
| 3089 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3090 | alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3091 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 3092 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3093 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3094 | key_data->x, |
| 3095 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3096 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3097 | |
| 3098 | /* Determine the maximum ciphertext length */ |
| 3099 | TEST_ASSERT( psa_get_key_information( slot, |
| 3100 | NULL, |
| 3101 | &key_bits ) == PSA_SUCCESS ); |
| 3102 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3103 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3104 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3105 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3106 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 3107 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 3108 | * the original plaintext because of the non-optional random |
| 3109 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3110 | TEST_ASSERT( psa_asymmetric_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3111 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3112 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3113 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3114 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3115 | /* We don't know what ciphertext length to expect, but check that |
| 3116 | * it looks sensible. */ |
| 3117 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3118 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3119 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3120 | output, output_length, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3121 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3122 | output2, output2_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3123 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3124 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3125 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3126 | |
| 3127 | exit: |
| 3128 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3129 | mbedtls_free( output ); |
| 3130 | mbedtls_free( output2 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3131 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3132 | } |
| 3133 | /* END_CASE */ |
| 3134 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3135 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3136 | void asymmetric_decrypt( int key_type_arg, |
| 3137 | data_t *key_data, |
| 3138 | int alg_arg, |
| 3139 | data_t *input_data, |
| 3140 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 3141 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3142 | { |
| 3143 | int slot = 1; |
| 3144 | psa_key_type_t key_type = key_type_arg; |
| 3145 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3146 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 3147 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3148 | size_t output_length = ~0; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 3149 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3150 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3151 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3152 | TEST_ASSERT( input_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3153 | TEST_ASSERT( expected_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3154 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 3155 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 3156 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 3157 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3158 | output_size = key_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3159 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3160 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3161 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3162 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3163 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3164 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3165 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 3166 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3167 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3168 | key_data->x, |
| 3169 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3170 | |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3171 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3172 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3173 | label->x, label->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3174 | output, |
| 3175 | output_size, |
| 3176 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3177 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3178 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3179 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3180 | /* If the label is empty, the test framework puts a non-null pointer |
| 3181 | * in label->x. Test that a null pointer works as well. */ |
| 3182 | if( label->len == 0 ) |
| 3183 | { |
| 3184 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3185 | if( output_size != 0 ) |
| 3186 | memset( output, 0, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3187 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
| 3188 | input_data->x, input_data->len, |
| 3189 | NULL, label->len, |
| 3190 | output, |
| 3191 | output_size, |
| 3192 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3193 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3194 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3195 | } |
| 3196 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3197 | exit: |
| 3198 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3199 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3200 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3201 | } |
| 3202 | /* END_CASE */ |
| 3203 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3204 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3205 | void asymmetric_decrypt_fail( int key_type_arg, |
| 3206 | data_t *key_data, |
| 3207 | int alg_arg, |
| 3208 | data_t *input_data, |
| 3209 | data_t *label, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3210 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3211 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3212 | int slot = 1; |
| 3213 | psa_key_type_t key_type = key_type_arg; |
| 3214 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3215 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 3216 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3217 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3218 | psa_status_t actual_status; |
| 3219 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 3220 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3221 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3222 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3223 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3224 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 3225 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 3226 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3227 | output_size = key_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3228 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3229 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3230 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3231 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3232 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3233 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3234 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 3235 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3236 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3237 | key_data->x, |
| 3238 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3239 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3240 | actual_status = psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3241 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3242 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3243 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3244 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3245 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3246 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3247 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3248 | /* If the label is empty, the test framework puts a non-null pointer |
| 3249 | * in label->x. Test that a null pointer works as well. */ |
| 3250 | if( label->len == 0 ) |
| 3251 | { |
| 3252 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3253 | if( output_size != 0 ) |
| 3254 | memset( output, 0, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3255 | actual_status = psa_asymmetric_decrypt( slot, alg, |
| 3256 | input_data->x, input_data->len, |
| 3257 | NULL, label->len, |
| 3258 | output, output_size, |
| 3259 | &output_length ); |
| 3260 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3261 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3262 | } |
| 3263 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3264 | exit: |
| 3265 | psa_destroy_key( slot ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3266 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3267 | mbedtls_psa_crypto_free( ); |
| 3268 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3269 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3270 | |
| 3271 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3272 | void derive_setup( int key_type_arg, |
| 3273 | data_t *key_data, |
| 3274 | int alg_arg, |
| 3275 | data_t *salt, |
| 3276 | data_t *label, |
| 3277 | int requested_capacity_arg, |
| 3278 | int expected_status_arg ) |
| 3279 | { |
| 3280 | psa_key_slot_t slot = 1; |
| 3281 | size_t key_type = key_type_arg; |
| 3282 | psa_algorithm_t alg = alg_arg; |
| 3283 | size_t requested_capacity = requested_capacity_arg; |
| 3284 | psa_status_t expected_status = expected_status_arg; |
| 3285 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3286 | psa_key_policy_t policy; |
| 3287 | |
| 3288 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3289 | |
| 3290 | psa_key_policy_init( &policy ); |
| 3291 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3292 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 3293 | |
| 3294 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 3295 | key_data->x, |
| 3296 | key_data->len ) == PSA_SUCCESS ); |
| 3297 | |
| 3298 | TEST_ASSERT( psa_key_derivation( &generator, slot, alg, |
| 3299 | salt->x, salt->len, |
| 3300 | label->x, label->len, |
| 3301 | requested_capacity ) == expected_status ); |
| 3302 | |
| 3303 | exit: |
| 3304 | psa_generator_abort( &generator ); |
| 3305 | psa_destroy_key( slot ); |
| 3306 | mbedtls_psa_crypto_free( ); |
| 3307 | } |
| 3308 | /* END_CASE */ |
| 3309 | |
| 3310 | /* BEGIN_CASE */ |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 3311 | void test_derive_invalid_generator_state( ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3312 | { |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3313 | psa_key_slot_t base_key = 1; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 3314 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3315 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3316 | psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3317 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3318 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 3319 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 3320 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 3321 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3322 | psa_key_policy_t policy; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3323 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3324 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3325 | |
| 3326 | psa_key_policy_init( &policy ); |
| 3327 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3328 | TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS ); |
| 3329 | |
| 3330 | TEST_ASSERT( psa_import_key( base_key, key_type, |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 3331 | key_data, |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3332 | sizeof( key_data ) ) == PSA_SUCCESS ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3333 | |
| 3334 | /* valid key derivation */ |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3335 | TEST_ASSERT( psa_key_derivation( &generator, base_key, alg, |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3336 | NULL, 0, |
| 3337 | NULL, 0, |
| 3338 | capacity ) == PSA_SUCCESS ); |
| 3339 | |
| 3340 | /* state of generator shouldn't allow additional generation */ |
| 3341 | TEST_ASSERT( psa_key_derivation( &generator, base_key, alg, |
| 3342 | NULL, 0, |
| 3343 | NULL, 0, |
| 3344 | capacity ) == PSA_ERROR_BAD_STATE ); |
| 3345 | |
| 3346 | TEST_ASSERT( psa_generator_read( &generator, buffer, capacity ) |
| 3347 | == PSA_SUCCESS ); |
| 3348 | |
| 3349 | TEST_ASSERT( psa_generator_read( &generator, buffer, capacity ) |
| 3350 | == PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3351 | |
| 3352 | |
| 3353 | exit: |
| 3354 | psa_generator_abort( &generator ); |
| 3355 | psa_destroy_key( base_key ); |
| 3356 | mbedtls_psa_crypto_free( ); |
| 3357 | } |
| 3358 | /* END_CASE */ |
| 3359 | |
| 3360 | |
| 3361 | /* BEGIN_CASE */ |
| 3362 | void test_derive_invalid_generator_tests( ) |
| 3363 | { |
| 3364 | uint8_t output_buffer[16]; |
| 3365 | size_t buffer_size = 16; |
| 3366 | size_t capacity = 0; |
| 3367 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3368 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3369 | TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3370 | == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3371 | |
| 3372 | TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3373 | == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3374 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3375 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3376 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3377 | TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3378 | == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3379 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3380 | TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3381 | == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3382 | |
| 3383 | exit: |
| 3384 | psa_generator_abort( &generator ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3385 | } |
| 3386 | /* END_CASE */ |
| 3387 | |
| 3388 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 3389 | void derive_output( int alg_arg, |
| 3390 | data_t *key_data, |
| 3391 | data_t *salt, |
| 3392 | data_t *label, |
| 3393 | int requested_capacity_arg, |
| 3394 | data_t *expected_output1, |
| 3395 | data_t *expected_output2 ) |
| 3396 | { |
| 3397 | psa_key_slot_t slot = 1; |
| 3398 | psa_algorithm_t alg = alg_arg; |
| 3399 | size_t requested_capacity = requested_capacity_arg; |
| 3400 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3401 | uint8_t *expected_outputs[2] = |
| 3402 | {expected_output1->x, expected_output2->x}; |
| 3403 | size_t output_sizes[2] = |
| 3404 | {expected_output1->len, expected_output2->len}; |
| 3405 | size_t output_buffer_size = 0; |
| 3406 | uint8_t *output_buffer = NULL; |
| 3407 | size_t expected_capacity; |
| 3408 | size_t current_capacity; |
| 3409 | psa_key_policy_t policy; |
| 3410 | psa_status_t status; |
| 3411 | unsigned i; |
| 3412 | |
| 3413 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 3414 | { |
| 3415 | if( output_sizes[i] > output_buffer_size ) |
| 3416 | output_buffer_size = output_sizes[i]; |
| 3417 | if( output_sizes[i] == 0 ) |
| 3418 | expected_outputs[i] = NULL; |
| 3419 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3420 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 3421 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3422 | |
| 3423 | psa_key_policy_init( &policy ); |
| 3424 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3425 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 3426 | |
| 3427 | TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE, |
| 3428 | key_data->x, |
| 3429 | key_data->len ) == PSA_SUCCESS ); |
| 3430 | |
| 3431 | /* Extraction phase. */ |
| 3432 | TEST_ASSERT( psa_key_derivation( &generator, slot, alg, |
| 3433 | salt->x, salt->len, |
| 3434 | label->x, label->len, |
| 3435 | requested_capacity ) == PSA_SUCCESS ); |
| 3436 | TEST_ASSERT( psa_get_generator_capacity( &generator, |
| 3437 | ¤t_capacity ) == |
| 3438 | PSA_SUCCESS ); |
| 3439 | TEST_ASSERT( current_capacity == requested_capacity ); |
| 3440 | expected_capacity = requested_capacity; |
| 3441 | |
| 3442 | /* Expansion phase. */ |
| 3443 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 3444 | { |
| 3445 | /* Read some bytes. */ |
| 3446 | status = psa_generator_read( &generator, |
| 3447 | output_buffer, output_sizes[i] ); |
| 3448 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 3449 | { |
| 3450 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 3451 | TEST_ASSERT( status == PSA_SUCCESS || |
| 3452 | status == PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3453 | continue; |
| 3454 | } |
| 3455 | else if( expected_capacity == 0 || |
| 3456 | output_sizes[i] > expected_capacity ) |
| 3457 | { |
| 3458 | /* Capacity exceeded. */ |
| 3459 | TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3460 | expected_capacity = 0; |
| 3461 | continue; |
| 3462 | } |
| 3463 | /* Success. Check the read data. */ |
| 3464 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 3465 | if( output_sizes[i] != 0 ) |
| 3466 | TEST_ASSERT( memcmp( output_buffer, expected_outputs[i], |
| 3467 | output_sizes[i] ) == 0 ); |
| 3468 | /* Check the generator status. */ |
| 3469 | expected_capacity -= output_sizes[i]; |
| 3470 | TEST_ASSERT( psa_get_generator_capacity( &generator, |
| 3471 | ¤t_capacity ) == |
| 3472 | PSA_SUCCESS ); |
| 3473 | TEST_ASSERT( expected_capacity == current_capacity ); |
| 3474 | } |
| 3475 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
| 3476 | |
| 3477 | exit: |
| 3478 | mbedtls_free( output_buffer ); |
| 3479 | psa_generator_abort( &generator ); |
| 3480 | psa_destroy_key( slot ); |
| 3481 | mbedtls_psa_crypto_free( ); |
| 3482 | } |
| 3483 | /* END_CASE */ |
| 3484 | |
| 3485 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3486 | void derive_full( int alg_arg, |
| 3487 | data_t *key_data, |
| 3488 | data_t *salt, |
| 3489 | data_t *label, |
| 3490 | int requested_capacity_arg ) |
| 3491 | { |
| 3492 | psa_key_slot_t slot = 1; |
| 3493 | psa_algorithm_t alg = alg_arg; |
| 3494 | size_t requested_capacity = requested_capacity_arg; |
| 3495 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3496 | unsigned char output_buffer[16]; |
| 3497 | size_t expected_capacity = requested_capacity; |
| 3498 | size_t current_capacity; |
| 3499 | psa_key_policy_t policy; |
| 3500 | |
| 3501 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3502 | |
| 3503 | psa_key_policy_init( &policy ); |
| 3504 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3505 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 3506 | |
| 3507 | TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE, |
| 3508 | key_data->x, |
| 3509 | key_data->len ) == PSA_SUCCESS ); |
| 3510 | |
| 3511 | /* Extraction phase. */ |
| 3512 | TEST_ASSERT( psa_key_derivation( &generator, slot, alg, |
| 3513 | salt->x, salt->len, |
| 3514 | label->x, label->len, |
| 3515 | requested_capacity ) == PSA_SUCCESS ); |
| 3516 | TEST_ASSERT( psa_get_generator_capacity( &generator, |
| 3517 | ¤t_capacity ) == |
| 3518 | PSA_SUCCESS ); |
| 3519 | TEST_ASSERT( current_capacity == expected_capacity ); |
| 3520 | |
| 3521 | /* Expansion phase. */ |
| 3522 | while( current_capacity > 0 ) |
| 3523 | { |
| 3524 | size_t read_size = sizeof( output_buffer ); |
| 3525 | if( read_size > current_capacity ) |
| 3526 | read_size = current_capacity; |
| 3527 | TEST_ASSERT( psa_generator_read( &generator, |
| 3528 | output_buffer, |
| 3529 | read_size ) == PSA_SUCCESS ); |
| 3530 | expected_capacity -= read_size; |
| 3531 | TEST_ASSERT( psa_get_generator_capacity( &generator, |
| 3532 | ¤t_capacity ) == |
| 3533 | PSA_SUCCESS ); |
| 3534 | TEST_ASSERT( current_capacity == expected_capacity ); |
| 3535 | } |
| 3536 | |
| 3537 | /* Check that the generator refuses to go over capacity. */ |
| 3538 | TEST_ASSERT( psa_generator_read( &generator, |
| 3539 | output_buffer, |
| 3540 | 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3541 | |
| 3542 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
| 3543 | |
| 3544 | exit: |
| 3545 | psa_generator_abort( &generator ); |
| 3546 | psa_destroy_key( slot ); |
| 3547 | mbedtls_psa_crypto_free( ); |
| 3548 | } |
| 3549 | /* END_CASE */ |
| 3550 | |
| 3551 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3552 | void derive_key_exercise( int alg_arg, |
| 3553 | data_t *key_data, |
| 3554 | data_t *salt, |
| 3555 | data_t *label, |
| 3556 | int derived_type_arg, |
| 3557 | int derived_bits_arg, |
| 3558 | int derived_usage_arg, |
| 3559 | int derived_alg_arg ) |
| 3560 | { |
| 3561 | psa_key_slot_t base_key = 1; |
| 3562 | psa_key_slot_t derived_key = 2; |
| 3563 | psa_algorithm_t alg = alg_arg; |
| 3564 | psa_key_type_t derived_type = derived_type_arg; |
| 3565 | size_t derived_bits = derived_bits_arg; |
| 3566 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 3567 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 3568 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
| 3569 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3570 | psa_key_policy_t policy; |
| 3571 | psa_key_type_t got_type; |
| 3572 | size_t got_bits; |
| 3573 | |
| 3574 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3575 | |
| 3576 | psa_key_policy_init( &policy ); |
| 3577 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3578 | TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS ); |
| 3579 | TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE, |
| 3580 | key_data->x, |
| 3581 | key_data->len ) == PSA_SUCCESS ); |
| 3582 | |
| 3583 | /* Derive a key. */ |
| 3584 | TEST_ASSERT( psa_key_derivation( &generator, base_key, alg, |
| 3585 | salt->x, salt->len, |
| 3586 | label->x, label->len, |
| 3587 | capacity ) == PSA_SUCCESS ); |
| 3588 | psa_key_policy_set_usage( &policy, derived_usage, derived_alg ); |
| 3589 | TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS ); |
| 3590 | TEST_ASSERT( psa_generator_import_key( derived_key, |
| 3591 | derived_type, |
| 3592 | derived_bits, |
| 3593 | &generator ) == PSA_SUCCESS ); |
| 3594 | |
| 3595 | /* Test the key information */ |
| 3596 | TEST_ASSERT( psa_get_key_information( derived_key, |
| 3597 | &got_type, |
| 3598 | &got_bits ) == PSA_SUCCESS ); |
| 3599 | TEST_ASSERT( got_type == derived_type ); |
| 3600 | TEST_ASSERT( got_bits == derived_bits ); |
| 3601 | |
| 3602 | /* Exercise the derived key. */ |
| 3603 | if( ! exercise_key( derived_key, derived_usage, derived_alg ) ) |
| 3604 | goto exit; |
| 3605 | |
| 3606 | exit: |
| 3607 | psa_generator_abort( &generator ); |
| 3608 | psa_destroy_key( base_key ); |
| 3609 | psa_destroy_key( derived_key ); |
| 3610 | mbedtls_psa_crypto_free( ); |
| 3611 | } |
| 3612 | /* END_CASE */ |
| 3613 | |
| 3614 | /* BEGIN_CASE */ |
| 3615 | void derive_key_export( int alg_arg, |
| 3616 | data_t *key_data, |
| 3617 | data_t *salt, |
| 3618 | data_t *label, |
| 3619 | int bytes1_arg, |
| 3620 | int bytes2_arg ) |
| 3621 | { |
| 3622 | psa_key_slot_t base_key = 1; |
| 3623 | psa_key_slot_t derived_key = 2; |
| 3624 | psa_algorithm_t alg = alg_arg; |
| 3625 | size_t bytes1 = bytes1_arg; |
| 3626 | size_t bytes2 = bytes2_arg; |
| 3627 | size_t capacity = bytes1 + bytes2; |
| 3628 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3629 | uint8_t *output_buffer = NULL; |
| 3630 | uint8_t *export_buffer = NULL; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3631 | psa_key_policy_t policy; |
| 3632 | size_t length; |
| 3633 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3634 | ASSERT_ALLOC( output_buffer, capacity ); |
| 3635 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3636 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3637 | |
| 3638 | psa_key_policy_init( &policy ); |
| 3639 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3640 | TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS ); |
| 3641 | TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE, |
| 3642 | key_data->x, |
| 3643 | key_data->len ) == PSA_SUCCESS ); |
| 3644 | |
| 3645 | /* Derive some material and output it. */ |
| 3646 | TEST_ASSERT( psa_key_derivation( &generator, base_key, alg, |
| 3647 | salt->x, salt->len, |
| 3648 | label->x, label->len, |
| 3649 | capacity ) == PSA_SUCCESS ); |
| 3650 | TEST_ASSERT( psa_generator_read( &generator, |
| 3651 | output_buffer, |
| 3652 | capacity ) == PSA_SUCCESS ); |
| 3653 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
| 3654 | |
| 3655 | /* Derive the same output again, but this time store it in key objects. */ |
| 3656 | TEST_ASSERT( psa_key_derivation( &generator, base_key, alg, |
| 3657 | salt->x, salt->len, |
| 3658 | label->x, label->len, |
| 3659 | capacity ) == PSA_SUCCESS ); |
| 3660 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 ); |
| 3661 | TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS ); |
| 3662 | TEST_ASSERT( psa_generator_import_key( derived_key, |
| 3663 | PSA_KEY_TYPE_RAW_DATA, |
| 3664 | PSA_BYTES_TO_BITS( bytes1 ), |
| 3665 | &generator ) == PSA_SUCCESS ); |
| 3666 | TEST_ASSERT( psa_export_key( derived_key, |
| 3667 | export_buffer, bytes1, |
| 3668 | &length ) == PSA_SUCCESS ); |
| 3669 | TEST_ASSERT( length == bytes1 ); |
| 3670 | TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS ); |
| 3671 | TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS ); |
| 3672 | TEST_ASSERT( psa_generator_import_key( derived_key, |
| 3673 | PSA_KEY_TYPE_RAW_DATA, |
| 3674 | PSA_BYTES_TO_BITS( bytes2 ), |
| 3675 | &generator ) == PSA_SUCCESS ); |
| 3676 | TEST_ASSERT( psa_export_key( derived_key, |
| 3677 | export_buffer + bytes1, bytes2, |
| 3678 | &length ) == PSA_SUCCESS ); |
| 3679 | TEST_ASSERT( length == bytes2 ); |
| 3680 | |
| 3681 | /* Compare the outputs from the two runs. */ |
| 3682 | TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 ); |
| 3683 | |
| 3684 | exit: |
| 3685 | mbedtls_free( output_buffer ); |
| 3686 | mbedtls_free( export_buffer ); |
| 3687 | psa_generator_abort( &generator ); |
| 3688 | psa_destroy_key( base_key ); |
| 3689 | psa_destroy_key( derived_key ); |
| 3690 | mbedtls_psa_crypto_free( ); |
| 3691 | } |
| 3692 | /* END_CASE */ |
| 3693 | |
| 3694 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 3695 | void key_agreement_setup( int alg_arg, |
| 3696 | int our_key_type_arg, data_t *our_key_data, |
| 3697 | data_t *peer_key_data, |
| 3698 | int expected_status_arg ) |
| 3699 | { |
| 3700 | psa_key_slot_t our_key = 1; |
| 3701 | psa_algorithm_t alg = alg_arg; |
| 3702 | psa_key_type_t our_key_type = our_key_type_arg; |
| 3703 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3704 | psa_key_policy_t policy; |
| 3705 | |
| 3706 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3707 | |
| 3708 | psa_key_policy_init( &policy ); |
| 3709 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3710 | TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS ); |
| 3711 | TEST_ASSERT( psa_import_key( our_key, our_key_type, |
| 3712 | our_key_data->x, |
| 3713 | our_key_data->len ) == PSA_SUCCESS ); |
| 3714 | |
| 3715 | TEST_ASSERT( psa_key_agreement( &generator, |
| 3716 | our_key, |
| 3717 | peer_key_data->x, peer_key_data->len, |
| 3718 | alg ) == expected_status_arg ); |
| 3719 | |
| 3720 | exit: |
| 3721 | psa_generator_abort( &generator ); |
| 3722 | psa_destroy_key( our_key ); |
| 3723 | mbedtls_psa_crypto_free( ); |
| 3724 | } |
| 3725 | /* END_CASE */ |
| 3726 | |
| 3727 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3728 | void key_agreement_capacity( int alg_arg, |
| 3729 | int our_key_type_arg, data_t *our_key_data, |
| 3730 | data_t *peer_key_data, |
| 3731 | int expected_capacity_arg ) |
| 3732 | { |
| 3733 | psa_key_slot_t our_key = 1; |
| 3734 | psa_algorithm_t alg = alg_arg; |
| 3735 | psa_key_type_t our_key_type = our_key_type_arg; |
| 3736 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3737 | psa_key_policy_t policy; |
| 3738 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 3739 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3740 | |
| 3741 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3742 | |
| 3743 | psa_key_policy_init( &policy ); |
| 3744 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3745 | TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS ); |
| 3746 | TEST_ASSERT( psa_import_key( our_key, our_key_type, |
| 3747 | our_key_data->x, |
| 3748 | our_key_data->len ) == PSA_SUCCESS ); |
| 3749 | |
| 3750 | TEST_ASSERT( psa_key_agreement( &generator, |
| 3751 | our_key, |
| 3752 | peer_key_data->x, peer_key_data->len, |
| 3753 | alg ) == PSA_SUCCESS ); |
| 3754 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 3755 | /* Test the advertized capacity. */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3756 | TEST_ASSERT( psa_get_generator_capacity( |
| 3757 | &generator, &actual_capacity ) == PSA_SUCCESS ); |
| 3758 | TEST_ASSERT( actual_capacity == (size_t) expected_capacity_arg ); |
| 3759 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 3760 | /* Test the actual capacity by reading the output. */ |
| 3761 | while( actual_capacity > sizeof( output ) ) |
| 3762 | { |
| 3763 | TEST_ASSERT( psa_generator_read( &generator, |
| 3764 | output, sizeof( output ) ) == |
| 3765 | PSA_SUCCESS ); |
| 3766 | actual_capacity -= sizeof( output ); |
| 3767 | } |
| 3768 | TEST_ASSERT( psa_generator_read( &generator, |
| 3769 | output, actual_capacity ) == |
| 3770 | PSA_SUCCESS ); |
| 3771 | TEST_ASSERT( psa_generator_read( &generator, output, 1 ) == |
| 3772 | PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3773 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3774 | exit: |
| 3775 | psa_generator_abort( &generator ); |
| 3776 | psa_destroy_key( our_key ); |
| 3777 | mbedtls_psa_crypto_free( ); |
| 3778 | } |
| 3779 | /* END_CASE */ |
| 3780 | |
| 3781 | /* BEGIN_CASE */ |
| 3782 | void key_agreement_output( int alg_arg, |
| 3783 | int our_key_type_arg, data_t *our_key_data, |
| 3784 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 3785 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3786 | { |
| 3787 | psa_key_slot_t our_key = 1; |
| 3788 | psa_algorithm_t alg = alg_arg; |
| 3789 | psa_key_type_t our_key_type = our_key_type_arg; |
| 3790 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3791 | psa_key_policy_t policy; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 3792 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3793 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 3794 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 3795 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3796 | |
| 3797 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3798 | |
| 3799 | psa_key_policy_init( &policy ); |
| 3800 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3801 | TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS ); |
| 3802 | TEST_ASSERT( psa_import_key( our_key, our_key_type, |
| 3803 | our_key_data->x, |
| 3804 | our_key_data->len ) == PSA_SUCCESS ); |
| 3805 | |
| 3806 | TEST_ASSERT( psa_key_agreement( &generator, |
| 3807 | our_key, |
| 3808 | peer_key_data->x, peer_key_data->len, |
| 3809 | alg ) == PSA_SUCCESS ); |
| 3810 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 3811 | TEST_ASSERT( |
| 3812 | psa_generator_read( &generator, |
| 3813 | actual_output, |
| 3814 | expected_output1->len ) == PSA_SUCCESS ); |
| 3815 | TEST_ASSERT( memcmp( actual_output, expected_output1->x, |
| 3816 | expected_output1->len ) == 0 ); |
| 3817 | if( expected_output2->len != 0 ) |
| 3818 | { |
| 3819 | TEST_ASSERT( |
| 3820 | psa_generator_read( &generator, |
| 3821 | actual_output, |
| 3822 | expected_output2->len ) == PSA_SUCCESS ); |
| 3823 | TEST_ASSERT( memcmp( actual_output, expected_output2->x, |
| 3824 | expected_output2->len ) == 0 ); |
| 3825 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3826 | |
| 3827 | exit: |
| 3828 | psa_generator_abort( &generator ); |
| 3829 | psa_destroy_key( our_key ); |
| 3830 | mbedtls_psa_crypto_free( ); |
| 3831 | mbedtls_free( actual_output ); |
| 3832 | } |
| 3833 | /* END_CASE */ |
| 3834 | |
| 3835 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3836 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3837 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3838 | size_t bytes = bytes_arg; |
| 3839 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3840 | unsigned char *output = NULL; |
| 3841 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3842 | size_t i; |
| 3843 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3844 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3845 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 3846 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3847 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3848 | |
| 3849 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3850 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3851 | /* Run several times, to ensure that every output byte will be |
| 3852 | * nonzero at least once with overwhelming probability |
| 3853 | * (2^(-8*number_of_runs)). */ |
| 3854 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3855 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3856 | if( bytes != 0 ) |
| 3857 | memset( output, 0, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3858 | TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS ); |
| 3859 | |
| 3860 | /* Check that no more than bytes have been overwritten */ |
| 3861 | TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 ); |
| 3862 | |
| 3863 | for( i = 0; i < bytes; i++ ) |
| 3864 | { |
| 3865 | if( output[i] != 0 ) |
| 3866 | ++changed[i]; |
| 3867 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3868 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3869 | |
| 3870 | /* Check that every byte was changed to nonzero at least once. This |
| 3871 | * validates that psa_generate_random is overwriting every byte of |
| 3872 | * the output buffer. */ |
| 3873 | for( i = 0; i < bytes; i++ ) |
| 3874 | { |
| 3875 | TEST_ASSERT( changed[i] != 0 ); |
| 3876 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3877 | |
| 3878 | exit: |
| 3879 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3880 | mbedtls_free( output ); |
| 3881 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3882 | } |
| 3883 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 3884 | |
| 3885 | /* BEGIN_CASE */ |
| 3886 | void generate_key( int type_arg, |
| 3887 | int bits_arg, |
| 3888 | int usage_arg, |
| 3889 | int alg_arg, |
| 3890 | int expected_status_arg ) |
| 3891 | { |
| 3892 | int slot = 1; |
| 3893 | psa_key_type_t type = type_arg; |
| 3894 | psa_key_usage_t usage = usage_arg; |
| 3895 | size_t bits = bits_arg; |
| 3896 | psa_algorithm_t alg = alg_arg; |
| 3897 | psa_status_t expected_status = expected_status_arg; |
| 3898 | psa_key_type_t got_type; |
| 3899 | size_t got_bits; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 3900 | psa_status_t expected_info_status = |
| 3901 | expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT; |
| 3902 | psa_key_policy_t policy; |
| 3903 | |
| 3904 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3905 | |
| 3906 | psa_key_policy_init( &policy ); |
| 3907 | psa_key_policy_set_usage( &policy, usage, alg ); |
| 3908 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 3909 | |
| 3910 | /* Generate a key */ |
| 3911 | TEST_ASSERT( psa_generate_key( slot, type, bits, |
| 3912 | NULL, 0 ) == expected_status ); |
| 3913 | |
| 3914 | /* Test the key information */ |
| 3915 | TEST_ASSERT( psa_get_key_information( slot, |
| 3916 | &got_type, |
| 3917 | &got_bits ) == expected_info_status ); |
| 3918 | if( expected_info_status != PSA_SUCCESS ) |
| 3919 | goto exit; |
| 3920 | TEST_ASSERT( got_type == type ); |
| 3921 | TEST_ASSERT( got_bits == bits ); |
| 3922 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 3923 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 3924 | if( ! exercise_key( slot, usage, alg ) ) |
| 3925 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 3926 | |
| 3927 | exit: |
| 3928 | psa_destroy_key( slot ); |
| 3929 | mbedtls_psa_crypto_free( ); |
| 3930 | } |
| 3931 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 3932 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 3933 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 3934 | void persistent_key_load_key_from_storage( data_t *data, int type_arg, |
| 3935 | int bits, int usage_arg, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 3936 | int alg_arg, int generation_method, |
| 3937 | int export_status ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 3938 | { |
| 3939 | psa_key_slot_t slot = 1; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 3940 | psa_key_slot_t base_key = 2; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 3941 | psa_key_type_t type = (psa_key_type_t) type_arg; |
| 3942 | psa_key_type_t type_get; |
| 3943 | size_t bits_get; |
| 3944 | psa_key_policy_t policy_set; |
| 3945 | psa_key_policy_t policy_get; |
| 3946 | psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg; |
| 3947 | psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 3948 | psa_key_policy_t base_policy_set; |
| 3949 | psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 3950 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 3951 | unsigned char *first_export = NULL; |
| 3952 | unsigned char *second_export = NULL; |
| 3953 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 3954 | size_t first_exported_length; |
| 3955 | size_t second_exported_length; |
| 3956 | |
| 3957 | ASSERT_ALLOC( first_export, export_size ); |
| 3958 | ASSERT_ALLOC( second_export, export_size ); |
| 3959 | |
| 3960 | TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS ); |
| 3961 | |
| 3962 | TEST_ASSERT( psa_set_key_lifetime( |
| 3963 | slot, PSA_KEY_LIFETIME_PERSISTENT ) == PSA_SUCCESS ); |
| 3964 | |
| 3965 | psa_key_policy_init( &policy_set ); |
| 3966 | |
| 3967 | psa_key_policy_set_usage( &policy_set, policy_usage, |
| 3968 | policy_alg ); |
| 3969 | |
| 3970 | TEST_ASSERT( psa_set_key_policy( slot, &policy_set ) == PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 3971 | switch( generation_method ) |
| 3972 | { |
| 3973 | case IMPORT_KEY: |
| 3974 | /* Import the key */ |
| 3975 | TEST_ASSERT( psa_import_key( slot, type, |
| 3976 | data->x, data->len ) == PSA_SUCCESS ); |
| 3977 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 3978 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 3979 | case GENERATE_KEY: |
| 3980 | /* Generate a key */ |
| 3981 | TEST_ASSERT( psa_generate_key( slot, type, bits, |
| 3982 | NULL, 0 ) == PSA_SUCCESS ); |
| 3983 | break; |
| 3984 | |
| 3985 | case DERIVE_KEY: |
| 3986 | /* Create base key */ |
| 3987 | psa_key_policy_init( &base_policy_set ); |
| 3988 | |
| 3989 | psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE, |
| 3990 | base_policy_alg ); |
| 3991 | TEST_ASSERT( psa_set_key_policy( |
| 3992 | base_key, &base_policy_set ) == PSA_SUCCESS ); |
| 3993 | TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE, |
| 3994 | data->x, data->len ) == PSA_SUCCESS ); |
| 3995 | /* Derive a key. */ |
| 3996 | TEST_ASSERT( psa_key_derivation( &generator, base_key, |
| 3997 | base_policy_alg, |
| 3998 | NULL, 0, NULL, 0, |
| 3999 | export_size ) == PSA_SUCCESS ); |
| 4000 | TEST_ASSERT( psa_generator_import_key( |
| 4001 | slot, PSA_KEY_TYPE_RAW_DATA, |
| 4002 | bits, &generator ) == PSA_SUCCESS ); |
| 4003 | break; |
| 4004 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4005 | |
| 4006 | /* Export the key */ |
| 4007 | TEST_ASSERT( psa_export_key( slot, first_export, export_size, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4008 | &first_exported_length ) == export_status ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4009 | |
| 4010 | /* Shutdown and restart */ |
| 4011 | mbedtls_psa_crypto_free(); |
| 4012 | |
| 4013 | TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS ); |
| 4014 | |
| 4015 | /* Mark slot as persistent again */ |
| 4016 | TEST_ASSERT( psa_set_key_lifetime( |
| 4017 | slot, PSA_KEY_LIFETIME_PERSISTENT ) == PSA_SUCCESS ); |
| 4018 | |
| 4019 | /* Check key slot still contains key data */ |
| 4020 | TEST_ASSERT( psa_get_key_information( |
| 4021 | slot, &type_get, &bits_get ) == PSA_SUCCESS ); |
| 4022 | TEST_ASSERT( type_get == type ); |
| 4023 | TEST_ASSERT( bits_get == (size_t) bits ); |
| 4024 | |
| 4025 | TEST_ASSERT( psa_get_key_policy( slot, &policy_get ) == PSA_SUCCESS ); |
| 4026 | TEST_ASSERT( psa_key_policy_get_usage( |
| 4027 | &policy_get ) == policy_usage ); |
| 4028 | TEST_ASSERT( psa_key_policy_get_algorithm( |
| 4029 | &policy_get ) == policy_alg ); |
| 4030 | |
| 4031 | /* Export the key again */ |
| 4032 | TEST_ASSERT( psa_export_key( slot, second_export, export_size, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4033 | &second_exported_length ) == export_status ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4034 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4035 | if( export_status == PSA_SUCCESS ) |
| 4036 | { |
| 4037 | ASSERT_COMPARE( first_export, first_exported_length, |
| 4038 | second_export, second_exported_length ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4039 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4040 | switch( generation_method ) |
| 4041 | { |
| 4042 | case IMPORT_KEY: |
| 4043 | ASSERT_COMPARE( data->x, data->len, |
| 4044 | first_export, first_exported_length ); |
| 4045 | break; |
| 4046 | default: |
| 4047 | break; |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | /* Do something with the key according to its type and permitted usage. */ |
| 4052 | if( ! exercise_key( slot, policy_usage, policy_alg ) ) |
| 4053 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4054 | |
| 4055 | exit: |
| 4056 | mbedtls_free( first_export ); |
| 4057 | mbedtls_free( second_export ); |
| 4058 | psa_destroy_key( slot ); |
| 4059 | mbedtls_psa_crypto_free(); |
| 4060 | } |
| 4061 | /* END_CASE */ |