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