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