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