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