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