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