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