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