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