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