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