blob: ae61ea99ae5276ba7d2bb1dab201ec82043ee567 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Ronald Cron02c78b72020-05-27 09:22:32 +020012#include "test/psa_crypto_helpers.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinec744d992019-07-30 17:26:54 +020014/* Tests that require more than 128kB of RAM plus change have this symbol
15 * as a dependency. Currently we always define this symbol, so the tests
16 * are always executed. In the future we should make this conditional
17 * so that tests that require a lot of memory are skipped on constrained
18 * platforms. */
Gilles Peskine49232e82019-08-07 11:01:30 +020019#define HAVE_RAM_AVAILABLE_128K
Gilles Peskinec744d992019-07-30 17:26:54 +020020
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020021#include "psa/crypto.h"
22
Jaeden Amerof24c7f82018-06-27 17:20:43 +010023/** An invalid export length that will never be set by psa_export_key(). */
24static const size_t INVALID_EXPORT_LENGTH = ~0U;
25
Gilles Peskinef426e0f2019-02-25 17:42:03 +010026/* A hash algorithm that is known to be supported.
27 *
28 * This is used in some smoke tests.
29 */
30#if defined(MBEDTLS_MD2_C)
31#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
32#elif defined(MBEDTLS_MD4_C)
33#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
34#elif defined(MBEDTLS_MD5_C)
35#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
36/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
37 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
38 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
39 * implausible anyway. */
40#elif defined(MBEDTLS_SHA1_C)
41#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
42#elif defined(MBEDTLS_SHA256_C)
43#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
44#elif defined(MBEDTLS_SHA512_C)
45#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
46#elif defined(MBEDTLS_SHA3_C)
47#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
48#else
49#undef KNOWN_SUPPORTED_HASH_ALG
50#endif
51
52/* A block cipher that is known to be supported.
53 *
54 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
55 */
56#if defined(MBEDTLS_AES_C)
57#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
58#elif defined(MBEDTLS_ARIA_C)
59#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
60#elif defined(MBEDTLS_CAMELLIA_C)
61#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
62#undef KNOWN_SUPPORTED_BLOCK_CIPHER
63#endif
64
65/* A MAC mode that is known to be supported.
66 *
67 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
68 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
69 *
70 * This is used in some smoke tests.
71 */
72#if defined(KNOWN_SUPPORTED_HASH_ALG)
73#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
74#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
75#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
76#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
77#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
78#else
79#undef KNOWN_SUPPORTED_MAC_ALG
80#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
81#endif
82
83/* A cipher algorithm and key type that are known to be supported.
84 *
85 * This is used in some smoke tests.
86 */
87#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
89#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
90#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
91#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
92#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
93#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
94#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
95#else
96#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
97#endif
98#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
99#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
100#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
101#elif defined(MBEDTLS_RC4_C)
102#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
103#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
104#else
105#undef KNOWN_SUPPORTED_CIPHER_ALG
106#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
107#endif
108
Gilles Peskine667c1112019-12-03 19:03:20 +0100109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
110int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
111{
112 /* At the moment, anything that isn't a built-in lifetime is either
113 * a secure element or unassigned. */
114 return( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
115 lifetime != PSA_KEY_LIFETIME_PERSISTENT );
116}
117#else
118int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
119{
120 (void) lifetime;
121 return( 0 );
122}
123#endif
124
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200125/** Test if a buffer contains a constant byte value.
126 *
127 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200128 *
129 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200130 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200131 * \param size Size of the buffer in bytes.
132 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200133 * \return 1 if the buffer is all-bits-zero.
134 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200135 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200136static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200137{
138 size_t i;
139 for( i = 0; i < size; i++ )
140 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200141 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200142 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200143 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200144 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200145}
Gilles Peskine818ca122018-06-20 18:16:48 +0200146
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200147/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
148static int asn1_write_10x( unsigned char **p,
149 unsigned char *start,
150 size_t bits,
151 unsigned char x )
152{
153 int ret;
154 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200155 if( bits == 0 )
156 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
157 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200158 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300159 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200160 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
161 *p -= len;
162 ( *p )[len-1] = x;
163 if( bits % 8 == 0 )
164 ( *p )[1] |= 1;
165 else
166 ( *p )[0] |= 1 << ( bits % 8 );
167 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
168 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
169 MBEDTLS_ASN1_INTEGER ) );
170 return( len );
171}
172
173static int construct_fake_rsa_key( unsigned char *buffer,
174 size_t buffer_size,
175 unsigned char **p,
176 size_t bits,
177 int keypair )
178{
179 size_t half_bits = ( bits + 1 ) / 2;
180 int ret;
181 int len = 0;
182 /* Construct something that looks like a DER encoding of
183 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
184 * RSAPrivateKey ::= SEQUENCE {
185 * version Version,
186 * modulus INTEGER, -- n
187 * publicExponent INTEGER, -- e
188 * privateExponent INTEGER, -- d
189 * prime1 INTEGER, -- p
190 * prime2 INTEGER, -- q
191 * exponent1 INTEGER, -- d mod (p-1)
192 * exponent2 INTEGER, -- d mod (q-1)
193 * coefficient INTEGER, -- (inverse of q) mod p
194 * otherPrimeInfos OtherPrimeInfos OPTIONAL
195 * }
196 * Or, for a public key, the same structure with only
197 * version, modulus and publicExponent.
198 */
199 *p = buffer + buffer_size;
200 if( keypair )
201 {
202 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
203 asn1_write_10x( p, buffer, half_bits, 1 ) );
204 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
205 asn1_write_10x( p, buffer, half_bits, 1 ) );
206 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
207 asn1_write_10x( p, buffer, half_bits, 1 ) );
208 MBEDTLS_ASN1_CHK_ADD( len, /* q */
209 asn1_write_10x( p, buffer, half_bits, 1 ) );
210 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
211 asn1_write_10x( p, buffer, half_bits, 3 ) );
212 MBEDTLS_ASN1_CHK_ADD( len, /* d */
213 asn1_write_10x( p, buffer, bits, 1 ) );
214 }
215 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
216 asn1_write_10x( p, buffer, 17, 1 ) );
217 MBEDTLS_ASN1_CHK_ADD( len, /* n */
218 asn1_write_10x( p, buffer, bits, 1 ) );
219 if( keypair )
220 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
221 mbedtls_asn1_write_int( p, buffer, 0 ) );
222 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
223 {
224 const unsigned char tag =
225 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
226 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
227 }
228 return( len );
229}
230
Gilles Peskine667c1112019-12-03 19:03:20 +0100231int check_key_attributes_sanity( psa_key_handle_t key )
232{
233 int ok = 0;
234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
235 psa_key_lifetime_t lifetime;
236 psa_key_id_t id;
237 psa_key_type_t type;
238 psa_key_type_t bits;
239
240 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
241 lifetime = psa_get_key_lifetime( &attributes );
242 id = psa_get_key_id( &attributes );
243 type = psa_get_key_type( &attributes );
244 bits = psa_get_key_bits( &attributes );
245
246 /* Persistence */
247 if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
248 TEST_ASSERT( id == 0 );
249 else
250 {
251 TEST_ASSERT(
252 ( PSA_KEY_ID_USER_MIN <= id && id <= PSA_KEY_ID_USER_MAX ) ||
253 ( PSA_KEY_ID_USER_MIN <= id && id <= PSA_KEY_ID_USER_MAX ) );
254 }
255#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
256 /* randomly-generated 64-bit constant, should never appear in test data */
257 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
258 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
259 if( lifetime_is_secure_element( lifetime ) )
260 {
261 /* Mbed Crypto currently always exposes the slot number to
262 * applications. This is not mandated by the PSA specification
263 * and may change in future versions. */
264 TEST_EQUAL( status, 0 );
265 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
266 }
267 else
268 {
269 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
270 }
271#endif
272
273 /* Type and size */
274 TEST_ASSERT( type != 0 );
275 TEST_ASSERT( bits != 0 );
276 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
277 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
278 TEST_ASSERT( bits % 8 == 0 );
279
280 /* MAX macros concerning specific key types */
281 if( PSA_KEY_TYPE_IS_ECC( type ) )
282 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
283 else if( PSA_KEY_TYPE_IS_RSA( type ) )
284 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
285 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) <= PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE );
286
287 ok = 1;
288
289exit:
290 psa_reset_key_attributes( &attributes );
291 return( ok );
292}
293
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100294int exercise_mac_setup( psa_key_type_t key_type,
295 const unsigned char *key_bytes,
296 size_t key_length,
297 psa_algorithm_t alg,
298 psa_mac_operation_t *operation,
299 psa_status_t *status )
300{
301 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100303
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100304 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200305 psa_set_key_algorithm( &attributes, alg );
306 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200307 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
308 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100309
310 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100311 /* Whether setup succeeded or failed, abort must succeed. */
312 PSA_ASSERT( psa_mac_abort( operation ) );
313 /* If setup failed, reproduce the failure, so that the caller can
314 * test the resulting state of the operation object. */
315 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100316 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100317 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
318 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100319 }
320
321 psa_destroy_key( handle );
322 return( 1 );
323
324exit:
325 psa_destroy_key( handle );
326 return( 0 );
327}
328
329int exercise_cipher_setup( psa_key_type_t key_type,
330 const unsigned char *key_bytes,
331 size_t key_length,
332 psa_algorithm_t alg,
333 psa_cipher_operation_t *operation,
334 psa_status_t *status )
335{
336 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100338
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
340 psa_set_key_algorithm( &attributes, alg );
341 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200342 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
343 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100344
345 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100346 /* Whether setup succeeded or failed, abort must succeed. */
347 PSA_ASSERT( psa_cipher_abort( operation ) );
348 /* If setup failed, reproduce the failure, so that the caller can
349 * test the resulting state of the operation object. */
350 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100351 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100352 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
353 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100354 }
355
356 psa_destroy_key( handle );
357 return( 1 );
358
359exit:
360 psa_destroy_key( handle );
361 return( 0 );
362}
363
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100364static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200365 psa_key_usage_t usage,
366 psa_algorithm_t alg )
367{
Jaeden Amero769ce272019-01-04 11:48:03 +0000368 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200369 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200370 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 size_t mac_length = sizeof( mac );
372
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100373 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100375 PSA_ASSERT( psa_mac_sign_setup( &operation,
376 handle, alg ) );
377 PSA_ASSERT( psa_mac_update( &operation,
378 input, sizeof( input ) ) );
379 PSA_ASSERT( psa_mac_sign_finish( &operation,
380 mac, sizeof( mac ),
381 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200382 }
383
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100384 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200385 {
386 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100387 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200388 PSA_SUCCESS :
389 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100390 PSA_ASSERT( psa_mac_verify_setup( &operation,
391 handle, alg ) );
392 PSA_ASSERT( psa_mac_update( &operation,
393 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100394 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
395 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200396 }
397
398 return( 1 );
399
400exit:
401 psa_mac_abort( &operation );
402 return( 0 );
403}
404
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100405static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200406 psa_key_usage_t usage,
407 psa_algorithm_t alg )
408{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000409 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200410 unsigned char iv[16] = {0};
411 size_t iv_length = sizeof( iv );
412 const unsigned char plaintext[16] = "Hello, world...";
413 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
414 size_t ciphertext_length = sizeof( ciphertext );
415 unsigned char decrypted[sizeof( ciphertext )];
416 size_t part_length;
417
418 if( usage & PSA_KEY_USAGE_ENCRYPT )
419 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100420 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
421 handle, alg ) );
422 PSA_ASSERT( psa_cipher_generate_iv( &operation,
423 iv, sizeof( iv ),
424 &iv_length ) );
425 PSA_ASSERT( psa_cipher_update( &operation,
426 plaintext, sizeof( plaintext ),
427 ciphertext, sizeof( ciphertext ),
428 &ciphertext_length ) );
429 PSA_ASSERT( psa_cipher_finish( &operation,
430 ciphertext + ciphertext_length,
431 sizeof( ciphertext ) - ciphertext_length,
432 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200433 ciphertext_length += part_length;
434 }
435
436 if( usage & PSA_KEY_USAGE_DECRYPT )
437 {
438 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200439 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200440 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
441 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
443 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
444 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
445 * have this macro yet. */
446 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
447 psa_get_key_type( &attributes ) );
448 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200449 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100450 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
451 handle, alg ) );
452 PSA_ASSERT( psa_cipher_set_iv( &operation,
453 iv, iv_length ) );
454 PSA_ASSERT( psa_cipher_update( &operation,
455 ciphertext, ciphertext_length,
456 decrypted, sizeof( decrypted ),
457 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200458 status = psa_cipher_finish( &operation,
459 decrypted + part_length,
460 sizeof( decrypted ) - part_length,
461 &part_length );
462 /* For a stream cipher, all inputs are valid. For a block cipher,
463 * if the input is some aribtrary data rather than an actual
464 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200465 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200466 TEST_ASSERT( status == PSA_SUCCESS ||
467 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200468 else
469 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200470 }
471
472 return( 1 );
473
474exit:
475 psa_cipher_abort( &operation );
476 return( 0 );
477}
478
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100479static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200480 psa_key_usage_t usage,
481 psa_algorithm_t alg )
482{
483 unsigned char nonce[16] = {0};
484 size_t nonce_length = sizeof( nonce );
485 unsigned char plaintext[16] = "Hello, world...";
486 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
487 size_t ciphertext_length = sizeof( ciphertext );
488 size_t plaintext_length = sizeof( ciphertext );
489
490 if( usage & PSA_KEY_USAGE_ENCRYPT )
491 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100492 PSA_ASSERT( psa_aead_encrypt( handle, alg,
493 nonce, nonce_length,
494 NULL, 0,
495 plaintext, sizeof( plaintext ),
496 ciphertext, sizeof( ciphertext ),
497 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200498 }
499
500 if( usage & PSA_KEY_USAGE_DECRYPT )
501 {
502 psa_status_t verify_status =
503 ( usage & PSA_KEY_USAGE_ENCRYPT ?
504 PSA_SUCCESS :
505 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100506 TEST_EQUAL( psa_aead_decrypt( handle, alg,
507 nonce, nonce_length,
508 NULL, 0,
509 ciphertext, ciphertext_length,
510 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100511 &plaintext_length ),
512 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200513 }
514
515 return( 1 );
516
517exit:
518 return( 0 );
519}
520
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100521static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200522 psa_key_usage_t usage,
523 psa_algorithm_t alg )
524{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200525 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
526 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100527 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200528 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100529 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
530
531 /* If the policy allows signing with any hash, just pick one. */
532 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
533 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100534#if defined(KNOWN_SUPPORTED_HASH_ALG)
535 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
536 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100537#else
538 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100539 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100540#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100541 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200542
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100543 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200544 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200545 /* Some algorithms require the payload to have the size of
546 * the hash encoded in the algorithm. Use this input size
547 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200548 if( hash_alg != 0 )
549 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100550 PSA_ASSERT( psa_sign_hash( handle, alg,
551 payload, payload_length,
552 signature, sizeof( signature ),
553 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200554 }
555
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100556 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200557 {
558 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100559 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200560 PSA_SUCCESS :
561 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100562 TEST_EQUAL( psa_verify_hash( handle, alg,
563 payload, payload_length,
564 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100565 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200566 }
567
568 return( 1 );
569
570exit:
571 return( 0 );
572}
573
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100574static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200575 psa_key_usage_t usage,
576 psa_algorithm_t alg )
577{
578 unsigned char plaintext[256] = "Hello, world...";
579 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
580 size_t ciphertext_length = sizeof( ciphertext );
581 size_t plaintext_length = 16;
582
583 if( usage & PSA_KEY_USAGE_ENCRYPT )
584 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100585 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
586 plaintext, plaintext_length,
587 NULL, 0,
588 ciphertext, sizeof( ciphertext ),
589 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200590 }
591
592 if( usage & PSA_KEY_USAGE_DECRYPT )
593 {
594 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100595 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200596 ciphertext, ciphertext_length,
597 NULL, 0,
598 plaintext, sizeof( plaintext ),
599 &plaintext_length );
600 TEST_ASSERT( status == PSA_SUCCESS ||
601 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
602 ( status == PSA_ERROR_INVALID_ARGUMENT ||
603 status == PSA_ERROR_INVALID_PADDING ) ) );
604 }
605
606 return( 1 );
607
608exit:
609 return( 0 );
610}
Gilles Peskine02b75072018-07-01 22:31:34 +0200611
Janos Follathf2815ea2019-07-03 12:41:36 +0100612static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
613 psa_key_handle_t handle,
614 psa_algorithm_t alg,
615 unsigned char* input1, size_t input1_length,
616 unsigned char* input2, size_t input2_length,
617 size_t capacity )
618{
619 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
620 if( PSA_ALG_IS_HKDF( alg ) )
621 {
622 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
623 PSA_KEY_DERIVATION_INPUT_SALT,
624 input1, input1_length ) );
625 PSA_ASSERT( psa_key_derivation_input_key( operation,
626 PSA_KEY_DERIVATION_INPUT_SECRET,
627 handle ) );
628 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
629 PSA_KEY_DERIVATION_INPUT_INFO,
630 input2,
631 input2_length ) );
632 }
633 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
634 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
635 {
636 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
637 PSA_KEY_DERIVATION_INPUT_SEED,
638 input1, input1_length ) );
639 PSA_ASSERT( psa_key_derivation_input_key( operation,
640 PSA_KEY_DERIVATION_INPUT_SECRET,
641 handle ) );
642 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
643 PSA_KEY_DERIVATION_INPUT_LABEL,
644 input2, input2_length ) );
645 }
646 else
647 {
648 TEST_ASSERT( ! "Key derivation algorithm not supported" );
649 }
650
Gilles Peskinec744d992019-07-30 17:26:54 +0200651 if( capacity != SIZE_MAX )
652 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100653
654 return( 1 );
655
656exit:
657 return( 0 );
658}
659
660
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100661static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200662 psa_key_usage_t usage,
663 psa_algorithm_t alg )
664{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200665 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100666 unsigned char input1[] = "Input 1";
667 size_t input1_length = sizeof( input1 );
668 unsigned char input2[] = "Input 2";
669 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200670 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100671 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200672
673 if( usage & PSA_KEY_USAGE_DERIVE )
674 {
Janos Follathf2815ea2019-07-03 12:41:36 +0100675 if( !setup_key_derivation_wrap( &operation, handle, alg,
676 input1, input1_length,
677 input2, input2_length, capacity ) )
678 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200679
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200680 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200681 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100682 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200683 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200684 }
685
686 return( 1 );
687
688exit:
689 return( 0 );
690}
691
Gilles Peskinec7998b72018-11-07 18:45:02 +0100692/* We need two keys to exercise key agreement. Exercise the
693 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200694static psa_status_t key_agreement_with_self(
695 psa_key_derivation_operation_t *operation,
696 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100697{
698 psa_key_type_t private_key_type;
699 psa_key_type_t public_key_type;
700 size_t key_bits;
701 uint8_t *public_key = NULL;
702 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200703 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200704 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
705 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200706 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100708
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200709 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
710 private_key_type = psa_get_key_type( &attributes );
711 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200712 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100713 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
714 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100715 PSA_ASSERT( psa_export_public_key( handle,
716 public_key, public_key_length,
717 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100718
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200719 status = psa_key_derivation_key_agreement(
720 operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle,
721 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100722exit:
723 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200724 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100725 return( status );
726}
727
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200728/* We need two keys to exercise key agreement. Exercise the
729 * private key against its own public key. */
730static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
731 psa_key_handle_t handle )
732{
733 psa_key_type_t private_key_type;
734 psa_key_type_t public_key_type;
735 size_t key_bits;
736 uint8_t *public_key = NULL;
737 size_t public_key_length;
738 uint8_t output[1024];
739 size_t output_length;
740 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200741 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
742 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200743 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200745
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200746 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
747 private_key_type = psa_get_key_type( &attributes );
748 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200749 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200750 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
751 ASSERT_ALLOC( public_key, public_key_length );
752 PSA_ASSERT( psa_export_public_key( handle,
753 public_key, public_key_length,
754 &public_key_length ) );
755
Gilles Peskinebe697d82019-05-16 18:00:41 +0200756 status = psa_raw_key_agreement( alg, handle,
757 public_key, public_key_length,
758 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200759exit:
760 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200761 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200762 return( status );
763}
764
765static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
766 psa_key_usage_t usage,
767 psa_algorithm_t alg )
768{
769 int ok = 0;
770
771 if( usage & PSA_KEY_USAGE_DERIVE )
772 {
773 /* We need two keys to exercise key agreement. Exercise the
774 * private key against its own public key. */
775 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
776 }
777 ok = 1;
778
779exit:
780 return( ok );
781}
782
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100783static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200784 psa_key_usage_t usage,
785 psa_algorithm_t alg )
786{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200787 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200788 unsigned char output[1];
789 int ok = 0;
790
791 if( usage & PSA_KEY_USAGE_DERIVE )
792 {
793 /* We need two keys to exercise key agreement. Exercise the
794 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200795 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
796 PSA_ASSERT( key_agreement_with_self( &operation, handle ) );
797 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200798 output,
799 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200800 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200801 }
802 ok = 1;
803
804exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200805 return( ok );
806}
807
Jaeden Amerof7dca862019-06-27 17:31:33 +0100808int asn1_skip_integer( unsigned char **p, const unsigned char *end,
809 size_t min_bits, size_t max_bits,
810 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200811{
812 size_t len;
813 size_t actual_bits;
814 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100815 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100816 MBEDTLS_ASN1_INTEGER ),
817 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200818
819 /* Check if the retrieved length doesn't extend the actual buffer's size.
820 * It is assumed here, that end >= p, which validates casting to size_t. */
821 TEST_ASSERT( len <= (size_t)( end - *p) );
822
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200823 /* Tolerate a slight departure from DER encoding:
824 * - 0 may be represented by an empty string or a 1-byte string.
825 * - The sign bit may be used as a value bit. */
826 if( ( len == 1 && ( *p )[0] == 0 ) ||
827 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
828 {
829 ++( *p );
830 --len;
831 }
832 if( min_bits == 0 && len == 0 )
833 return( 1 );
834 msb = ( *p )[0];
835 TEST_ASSERT( msb != 0 );
836 actual_bits = 8 * ( len - 1 );
837 while( msb != 0 )
838 {
839 msb >>= 1;
840 ++actual_bits;
841 }
842 TEST_ASSERT( actual_bits >= min_bits );
843 TEST_ASSERT( actual_bits <= max_bits );
844 if( must_be_odd )
845 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
846 *p += len;
847 return( 1 );
848exit:
849 return( 0 );
850}
851
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200852static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
853 uint8_t *exported, size_t exported_length )
854{
855 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100856 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200857 else
858 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200859
860#if defined(MBEDTLS_DES_C)
861 if( type == PSA_KEY_TYPE_DES )
862 {
863 /* Check the parity bits. */
864 unsigned i;
865 for( i = 0; i < bits / 8; i++ )
866 {
867 unsigned bit_count = 0;
868 unsigned m;
869 for( m = 1; m <= 0x100; m <<= 1 )
870 {
871 if( exported[i] & m )
872 ++bit_count;
873 }
874 TEST_ASSERT( bit_count % 2 != 0 );
875 }
876 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200877 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200878#endif
879
880#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200881 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200882 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200883 uint8_t *p = exported;
884 uint8_t *end = exported + exported_length;
885 size_t len;
886 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200887 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200888 * modulus INTEGER, -- n
889 * publicExponent INTEGER, -- e
890 * privateExponent INTEGER, -- d
891 * prime1 INTEGER, -- p
892 * prime2 INTEGER, -- q
893 * exponent1 INTEGER, -- d mod (p-1)
894 * exponent2 INTEGER, -- d mod (q-1)
895 * coefficient INTEGER, -- (inverse of q) mod p
896 * }
897 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100898 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
899 MBEDTLS_ASN1_SEQUENCE |
900 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
901 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200902 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
903 goto exit;
904 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
905 goto exit;
906 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
907 goto exit;
908 /* Require d to be at least half the size of n. */
909 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
910 goto exit;
911 /* Require p and q to be at most half the size of n, rounded up. */
912 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
913 goto exit;
914 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
915 goto exit;
916 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
917 goto exit;
918 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
919 goto exit;
920 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
921 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100922 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100923 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200924 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200925#endif /* MBEDTLS_RSA_C */
926
927#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200928 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200929 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100930 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100931 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100932 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200933 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200934#endif /* MBEDTLS_ECP_C */
935
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200936 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
937 {
938 uint8_t *p = exported;
939 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200940#if defined(MBEDTLS_RSA_C)
941 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
942 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100943 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200944 /* RSAPublicKey ::= SEQUENCE {
945 * modulus INTEGER, -- n
946 * publicExponent INTEGER } -- e
947 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100948 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
949 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100950 MBEDTLS_ASN1_CONSTRUCTED ),
951 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100952 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200953 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
954 goto exit;
955 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
956 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100957 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200958 }
959 else
960#endif /* MBEDTLS_RSA_C */
961#if defined(MBEDTLS_ECP_C)
962 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
963 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000964 /* The representation of an ECC public key is:
965 * - The byte 0x04;
966 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
967 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
968 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000969 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100970 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
971 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200972 }
973 else
974#endif /* MBEDTLS_ECP_C */
975 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100976 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200977 mbedtls_snprintf( message, sizeof( message ),
978 "No sanity check for public key type=0x%08lx",
979 (unsigned long) type );
980 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200981 (void) p;
982 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200983 return( 0 );
984 }
985 }
986 else
987
988 {
989 /* No sanity checks for other types */
990 }
991
992 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200993
994exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200995 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200996}
997
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100998static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200999 psa_key_usage_t usage )
1000{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001002 uint8_t *exported = NULL;
1003 size_t exported_size = 0;
1004 size_t exported_length = 0;
1005 int ok = 0;
1006
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001007 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001008
1009 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001010 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001011 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001012 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
1013 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001014 ok = 1;
1015 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001016 }
1017
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001018 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
1019 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001020 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001021
Gilles Peskine8817f612018-12-18 00:18:46 +01001022 PSA_ASSERT( psa_export_key( handle,
1023 exported, exported_size,
1024 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001025 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1026 psa_get_key_bits( &attributes ),
1027 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001028
1029exit:
1030 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001031 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001032 return( ok );
1033}
1034
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +02001036{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001038 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001039 uint8_t *exported = NULL;
1040 size_t exported_size = 0;
1041 size_t exported_length = 0;
1042 int ok = 0;
1043
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001044 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1045 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001046 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001047 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001048 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +02001049 return( 1 );
1050 }
1051
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001052 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001053 psa_get_key_type( &attributes ) );
1054 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
1055 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001056 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001057
Gilles Peskine8817f612018-12-18 00:18:46 +01001058 PSA_ASSERT( psa_export_public_key( handle,
1059 exported, exported_size,
1060 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001061 ok = exported_key_sanity_check( public_type,
1062 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001063 exported, exported_length );
1064
1065exit:
1066 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001067 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001068 return( ok );
1069}
1070
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001071/** Do smoke tests on a key.
1072 *
1073 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1074 * sign/verify, or derivation) that is permitted according to \p usage.
1075 * \p usage and \p alg should correspond to the expected policy on the
1076 * key.
1077 *
1078 * Export the key if permitted by \p usage, and check that the output
1079 * looks sensible. If \p usage forbids export, check that
1080 * \p psa_export_key correctly rejects the attempt. If the key is
1081 * asymmetric, also check \p psa_export_public_key.
1082 *
1083 * If the key fails the tests, this function calls the test framework's
1084 * `test_fail` function and returns false. Otherwise this function returns
1085 * true. Therefore it should be used as follows:
1086 * ```
1087 * if( ! exercise_key( ... ) ) goto exit;
1088 * ```
1089 *
1090 * \param handle The key to exercise. It should be capable of performing
1091 * \p alg.
1092 * \param usage The usage flags to assume.
1093 * \param alg The algorithm to exercise.
1094 *
1095 * \retval 0 The key failed the smoke tests.
1096 * \retval 1 The key passed the smoke tests.
1097 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001098static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001099 psa_key_usage_t usage,
1100 psa_algorithm_t alg )
1101{
1102 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001103
1104 if( ! check_key_attributes_sanity( handle ) )
1105 return( 0 );
1106
Gilles Peskine02b75072018-07-01 22:31:34 +02001107 if( alg == 0 )
1108 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1109 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001110 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001111 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001112 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001113 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001114 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001115 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001116 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001117 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001118 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001119 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001120 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001121 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1122 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001123 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001124 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001125 else
1126 {
1127 char message[40];
1128 mbedtls_snprintf( message, sizeof( message ),
1129 "No code to exercise alg=0x%08lx",
1130 (unsigned long) alg );
1131 test_fail( message, __LINE__, __FILE__ );
1132 ok = 0;
1133 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001134
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001135 ok = ok && exercise_export_key( handle, usage );
1136 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001137
Gilles Peskine02b75072018-07-01 22:31:34 +02001138 return( ok );
1139}
1140
Gilles Peskine10df3412018-10-25 22:35:43 +02001141static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1142 psa_algorithm_t alg )
1143{
1144 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1145 {
1146 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001147 PSA_KEY_USAGE_VERIFY_HASH :
1148 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001149 }
1150 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1151 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1152 {
1153 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1154 PSA_KEY_USAGE_ENCRYPT :
1155 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1156 }
1157 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1158 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1159 {
1160 return( PSA_KEY_USAGE_DERIVE );
1161 }
1162 else
1163 {
1164 return( 0 );
1165 }
1166
1167}
Darryl Green0c6575a2018-11-07 16:05:30 +00001168
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001169static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1170{
1171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1172 uint8_t buffer[1];
1173 size_t length;
1174 int ok = 0;
1175
Gilles Peskinec87af662019-05-15 16:12:22 +02001176 psa_set_key_id( &attributes, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001177 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1178 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1179 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1180 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1181 PSA_ERROR_INVALID_HANDLE );
1182 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001183 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001184 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1185 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1186 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1187 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1188
1189 TEST_EQUAL( psa_export_key( handle,
1190 buffer, sizeof( buffer ), &length ),
1191 PSA_ERROR_INVALID_HANDLE );
1192 TEST_EQUAL( psa_export_public_key( handle,
1193 buffer, sizeof( buffer ), &length ),
1194 PSA_ERROR_INVALID_HANDLE );
1195
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001196 ok = 1;
1197
1198exit:
1199 psa_reset_key_attributes( &attributes );
1200 return( ok );
1201}
1202
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001203/* Assert that a key isn't reported as having a slot number. */
1204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1205#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1206 do \
1207 { \
1208 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1209 TEST_EQUAL( psa_get_key_slot_number( \
1210 attributes, \
1211 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1212 PSA_ERROR_INVALID_ARGUMENT ); \
1213 } \
1214 while( 0 )
1215#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1216#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1217 ( (void) 0 )
1218#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1219
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001220/* An overapproximation of the amount of storage needed for a key of the
1221 * given type and with the given content. The API doesn't make it easy
1222 * to find a good value for the size. The current implementation doesn't
1223 * care about the value anyway. */
1224#define KEY_BITS_FROM_DATA( type, data ) \
1225 ( data )->len
1226
Darryl Green0c6575a2018-11-07 16:05:30 +00001227typedef enum {
1228 IMPORT_KEY = 0,
1229 GENERATE_KEY = 1,
1230 DERIVE_KEY = 2
1231} generate_method;
1232
Gilles Peskinee59236f2018-01-27 23:32:46 +01001233/* END_HEADER */
1234
1235/* BEGIN_DEPENDENCIES
1236 * depends_on:MBEDTLS_PSA_CRYPTO_C
1237 * END_DEPENDENCIES
1238 */
1239
1240/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001241void static_checks( )
1242{
1243 size_t max_truncated_mac_size =
1244 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1245
1246 /* Check that the length for a truncated MAC always fits in the algorithm
1247 * encoding. The shifted mask is the maximum truncated value. The
1248 * untruncated algorithm may be one byte larger. */
1249 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001250
1251#if defined(MBEDTLS_TEST_DEPRECATED)
1252 /* Check deprecated constants. */
1253 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1254 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1255 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1256 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1257 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1258 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1259 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1260 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001261
1262 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_CURVE_SECP_K1 );
1263 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_CURVE_SECP_K1 );
1264 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_CURVE_SECP_K1 );
1265 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_CURVE_SECP_K1 );
1266 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_CURVE_SECP_R1 );
1267 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_CURVE_SECP_R1 );
1268 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_CURVE_SECP_R1 );
1269 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_CURVE_SECP_R1 );
1270 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_CURVE_SECP_R1 );
1271 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_CURVE_SECP_R1 );
1272 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_CURVE_SECP_R2 );
1273 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_CURVE_SECT_K1 );
1274 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_CURVE_SECT_K1 );
1275 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_CURVE_SECT_K1 );
1276 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_CURVE_SECT_K1 );
1277 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_CURVE_SECT_K1 );
1278 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_CURVE_SECT_K1 );
1279 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_CURVE_SECT_R1 );
1280 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_CURVE_SECT_R1 );
1281 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_CURVE_SECT_R1 );
1282 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_CURVE_SECT_R1 );
1283 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_CURVE_SECT_R1 );
1284 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_CURVE_SECT_R1 );
1285 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_CURVE_SECT_R2 );
1286 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_CURVE_SECT_R2 );
1287 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_CURVE_BRAINPOOL_P_R1 );
1288 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_CURVE_BRAINPOOL_P_R1 );
1289 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_CURVE_BRAINPOOL_P_R1 );
1290 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_CURVE_MONTGOMERY );
1291 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_CURVE_MONTGOMERY );
1292
1293 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_GROUP_RFC7919 );
1294 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_GROUP_RFC7919 );
1295 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_GROUP_RFC7919 );
1296 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_GROUP_RFC7919 );
1297 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_GROUP_RFC7919 );
1298#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001299}
1300/* END_CASE */
1301
1302/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001303void attributes_set_get( int id_arg, int lifetime_arg,
1304 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001305 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306{
Gilles Peskine4747d192019-04-17 15:05:45 +02001307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001308 psa_key_id_t id = id_arg;
1309 psa_key_lifetime_t lifetime = lifetime_arg;
1310 psa_key_usage_t usage_flags = usage_flags_arg;
1311 psa_algorithm_t alg = alg_arg;
1312 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001313 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001314
1315 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1316 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1317 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1318 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1319 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001320 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001321
Gilles Peskinec87af662019-05-15 16:12:22 +02001322 psa_set_key_id( &attributes, id );
1323 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001324 psa_set_key_usage_flags( &attributes, usage_flags );
1325 psa_set_key_algorithm( &attributes, alg );
1326 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001327 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001328
1329 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1330 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1331 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1332 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1333 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001334 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001335
1336 psa_reset_key_attributes( &attributes );
1337
1338 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1339 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1340 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1341 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1342 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001343 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001344}
1345/* END_CASE */
1346
1347/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001348void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1349 int expected_id_arg, int expected_lifetime_arg )
1350{
1351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1352 psa_key_id_t id1 = id1_arg;
1353 psa_key_lifetime_t lifetime = lifetime_arg;
1354 psa_key_id_t id2 = id2_arg;
1355 psa_key_id_t expected_id = expected_id_arg;
1356 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1357
1358 if( id1_arg != -1 )
1359 psa_set_key_id( &attributes, id1 );
1360 if( lifetime_arg != -1 )
1361 psa_set_key_lifetime( &attributes, lifetime );
1362 if( id2_arg != -1 )
1363 psa_set_key_id( &attributes, id2 );
1364
1365 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1366 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1367}
1368/* END_CASE */
1369
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001370/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1371void slot_number_attribute( )
1372{
1373 psa_key_slot_number_t slot_number = 0xdeadbeef;
1374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1375
1376 /* Initially, there is no slot number. */
1377 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1378 PSA_ERROR_INVALID_ARGUMENT );
1379
1380 /* Test setting a slot number. */
1381 psa_set_key_slot_number( &attributes, 0 );
1382 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1383 TEST_EQUAL( slot_number, 0 );
1384
1385 /* Test changing the slot number. */
1386 psa_set_key_slot_number( &attributes, 42 );
1387 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1388 TEST_EQUAL( slot_number, 42 );
1389
1390 /* Test clearing the slot number. */
1391 psa_clear_key_slot_number( &attributes );
1392 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1393 PSA_ERROR_INVALID_ARGUMENT );
1394
1395 /* Clearing again should have no effect. */
1396 psa_clear_key_slot_number( &attributes );
1397 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1398 PSA_ERROR_INVALID_ARGUMENT );
1399
1400 /* Test that reset clears the slot number. */
1401 psa_set_key_slot_number( &attributes, 42 );
1402 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1403 TEST_EQUAL( slot_number, 42 );
1404 psa_reset_key_attributes( &attributes );
1405 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1406 PSA_ERROR_INVALID_ARGUMENT );
1407}
1408/* END_CASE */
1409
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001410/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001411void import_with_policy( int type_arg,
1412 int usage_arg, int alg_arg,
1413 int expected_status_arg )
1414{
1415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1416 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1417 psa_key_handle_t handle = 0;
1418 psa_key_type_t type = type_arg;
1419 psa_key_usage_t usage = usage_arg;
1420 psa_algorithm_t alg = alg_arg;
1421 psa_status_t expected_status = expected_status_arg;
1422 const uint8_t key_material[16] = {0};
1423 psa_status_t status;
1424
1425 PSA_ASSERT( psa_crypto_init( ) );
1426
1427 psa_set_key_type( &attributes, type );
1428 psa_set_key_usage_flags( &attributes, usage );
1429 psa_set_key_algorithm( &attributes, alg );
1430
1431 status = psa_import_key( &attributes,
1432 key_material, sizeof( key_material ),
1433 &handle );
1434 TEST_EQUAL( status, expected_status );
1435 if( status != PSA_SUCCESS )
1436 goto exit;
1437
1438 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1439 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1440 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1441 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001442 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001443
1444 PSA_ASSERT( psa_destroy_key( handle ) );
1445 test_operations_on_invalid_handle( handle );
1446
1447exit:
1448 psa_destroy_key( handle );
1449 psa_reset_key_attributes( &got_attributes );
1450 PSA_DONE( );
1451}
1452/* END_CASE */
1453
1454/* BEGIN_CASE */
1455void import_with_data( data_t *data, int type_arg,
1456 int attr_bits_arg,
1457 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001458{
1459 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1460 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001461 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001462 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001463 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001464 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001465 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001466
Gilles Peskine8817f612018-12-18 00:18:46 +01001467 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001468
Gilles Peskine4747d192019-04-17 15:05:45 +02001469 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001470 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001471
Gilles Peskine73676cb2019-05-15 20:15:10 +02001472 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001473 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001474 if( status != PSA_SUCCESS )
1475 goto exit;
1476
1477 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1478 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001479 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001480 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001481 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001482
1483 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001484 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001485
1486exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001487 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001488 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001489 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001490}
1491/* END_CASE */
1492
1493/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001494void import_large_key( int type_arg, int byte_size_arg,
1495 int expected_status_arg )
1496{
1497 psa_key_type_t type = type_arg;
1498 size_t byte_size = byte_size_arg;
1499 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1500 psa_status_t expected_status = expected_status_arg;
1501 psa_key_handle_t handle = 0;
1502 psa_status_t status;
1503 uint8_t *buffer = NULL;
1504 size_t buffer_size = byte_size + 1;
1505 size_t n;
1506
1507 /* It would be better to skip the test than fail it if the allocation
1508 * fails, but the test framework doesn't support this yet. */
1509 ASSERT_ALLOC( buffer, buffer_size );
1510 memset( buffer, 'K', byte_size );
1511
1512 PSA_ASSERT( psa_crypto_init( ) );
1513
1514 /* Try importing the key */
1515 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1516 psa_set_key_type( &attributes, type );
1517 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1518 TEST_EQUAL( status, expected_status );
1519
1520 if( status == PSA_SUCCESS )
1521 {
1522 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1523 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1524 TEST_EQUAL( psa_get_key_bits( &attributes ),
1525 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001526 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001527 memset( buffer, 0, byte_size + 1 );
1528 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1529 for( n = 0; n < byte_size; n++ )
1530 TEST_EQUAL( buffer[n], 'K' );
1531 for( n = byte_size; n < buffer_size; n++ )
1532 TEST_EQUAL( buffer[n], 0 );
1533 }
1534
1535exit:
1536 psa_destroy_key( handle );
1537 PSA_DONE( );
1538 mbedtls_free( buffer );
1539}
1540/* END_CASE */
1541
1542/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001543void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1544{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001545 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001546 size_t bits = bits_arg;
1547 psa_status_t expected_status = expected_status_arg;
1548 psa_status_t status;
1549 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001550 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001551 size_t buffer_size = /* Slight overapproximations */
1552 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001553 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001554 unsigned char *p;
1555 int ret;
1556 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001558
Gilles Peskine8817f612018-12-18 00:18:46 +01001559 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001560 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001561
1562 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1563 bits, keypair ) ) >= 0 );
1564 length = ret;
1565
1566 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001567 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001568 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001569 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001570
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001571 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001572 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001573
1574exit:
1575 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001576 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001577}
1578/* END_CASE */
1579
1580/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001581void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001582 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001583 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001584 int expected_bits,
1585 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001586 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587 int canonical_input )
1588{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001589 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001590 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001591 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001592 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001593 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594 unsigned char *exported = NULL;
1595 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001596 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001597 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001598 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001600 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001601
Moran Pekercb088e72018-07-17 17:36:59 +03001602 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001603 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001604 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001605 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001606 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001607
Gilles Peskine4747d192019-04-17 15:05:45 +02001608 psa_set_key_usage_flags( &attributes, usage_arg );
1609 psa_set_key_algorithm( &attributes, alg );
1610 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001611
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001612 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001613 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001614
1615 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001616 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1617 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1618 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001619 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001620
1621 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001622 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001623 exported, export_size,
1624 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001625 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001626
1627 /* The exported length must be set by psa_export_key() to a value between 0
1628 * and export_size. On errors, the exported length must be 0. */
1629 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1630 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1631 TEST_ASSERT( exported_length <= export_size );
1632
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001633 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001634 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001635 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001636 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001637 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001638 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001639 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001640
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001641 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001642 goto exit;
1643
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001644 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001645 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001646 else
1647 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001648 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001649 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1650 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001651 PSA_ASSERT( psa_export_key( handle2,
1652 reexported,
1653 export_size,
1654 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001655 ASSERT_COMPARE( exported, exported_length,
1656 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001657 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001658 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001659 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001660
1661destroy:
1662 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001663 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001664 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001665
1666exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001667 mbedtls_free( exported );
1668 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001669 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001670 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001671}
1672/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001673
Moran Pekerf709f4a2018-06-06 17:26:04 +03001674/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001675void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001676 int type_arg,
1677 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001678 int export_size_delta,
1679 int expected_export_status_arg,
1680 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001681{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001682 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001683 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001684 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001685 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001686 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001687 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001688 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001689 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001690 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001691
Gilles Peskine8817f612018-12-18 00:18:46 +01001692 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001693
Gilles Peskine4747d192019-04-17 15:05:45 +02001694 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1695 psa_set_key_algorithm( &attributes, alg );
1696 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001697
1698 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001699 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001700
Gilles Peskine49c25912018-10-29 15:15:31 +01001701 /* Export the public key */
1702 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001703 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001704 exported, export_size,
1705 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001706 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001707 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001708 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001709 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001710 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001711 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1712 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001713 TEST_ASSERT( expected_public_key->len <=
1714 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001715 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1716 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001717 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001718
1719exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001720 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001721 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001722 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001723 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001724}
1725/* END_CASE */
1726
Gilles Peskine20035e32018-02-03 22:44:14 +01001727/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001728void import_and_exercise_key( data_t *data,
1729 int type_arg,
1730 int bits_arg,
1731 int alg_arg )
1732{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001733 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001734 psa_key_type_t type = type_arg;
1735 size_t bits = bits_arg;
1736 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001737 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001739 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001740
Gilles Peskine8817f612018-12-18 00:18:46 +01001741 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001742
Gilles Peskine4747d192019-04-17 15:05:45 +02001743 psa_set_key_usage_flags( &attributes, usage );
1744 psa_set_key_algorithm( &attributes, alg );
1745 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001746
1747 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001748 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001749
1750 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001751 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1752 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1753 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001754
1755 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001756 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001757 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001758
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001759 PSA_ASSERT( psa_destroy_key( handle ) );
1760 test_operations_on_invalid_handle( handle );
1761
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001762exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001763 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001764 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001765 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001766}
1767/* END_CASE */
1768
1769/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001770void effective_key_attributes( int type_arg, int expected_type_arg,
1771 int bits_arg, int expected_bits_arg,
1772 int usage_arg, int expected_usage_arg,
1773 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001774{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001775 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001776 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001777 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001778 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001779 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001780 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001781 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001782 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001783 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001784 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001785
Gilles Peskine8817f612018-12-18 00:18:46 +01001786 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001787
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001788 psa_set_key_usage_flags( &attributes, usage );
1789 psa_set_key_algorithm( &attributes, alg );
1790 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001791 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001792
Gilles Peskine1a960492019-11-26 17:12:21 +01001793 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1794 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001795
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001796 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001797 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1798 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1799 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1800 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001801
1802exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001803 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001804 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001805 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001806}
1807/* END_CASE */
1808
1809/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001810void check_key_policy( int type_arg, int bits_arg,
1811 int usage_arg, int alg_arg )
1812{
1813 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1814 usage_arg, usage_arg, alg_arg, alg_arg );
1815 goto exit;
1816}
1817/* END_CASE */
1818
1819/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001820void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001821{
1822 /* Test each valid way of initializing the object, except for `= {0}`, as
1823 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1824 * though it's OK by the C standard. We could test for this, but we'd need
1825 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001826 psa_key_attributes_t func = psa_key_attributes_init( );
1827 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1828 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001829
1830 memset( &zero, 0, sizeof( zero ) );
1831
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001832 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1833 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1834 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001835
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001836 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1837 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1838 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1839
1840 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1841 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1842 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1843
1844 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1845 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1846 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1847
1848 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1849 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1850 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001851}
1852/* END_CASE */
1853
1854/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001855void mac_key_policy( int policy_usage,
1856 int policy_alg,
1857 int key_type,
1858 data_t *key_data,
1859 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001860{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001861 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001862 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001863 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001864 psa_status_t status;
1865 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001866
Gilles Peskine8817f612018-12-18 00:18:46 +01001867 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001868
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001869 psa_set_key_usage_flags( &attributes, policy_usage );
1870 psa_set_key_algorithm( &attributes, policy_alg );
1871 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001872
Gilles Peskine049c7532019-05-15 20:22:09 +02001873 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1874 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001875
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001876 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001877 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001878 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001879 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001880 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001881 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001882 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001883
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001884 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001885 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001887 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001888 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001890 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001891
1892exit:
1893 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001894 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001895 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001896}
1897/* END_CASE */
1898
1899/* BEGIN_CASE */
1900void cipher_key_policy( int policy_usage,
1901 int policy_alg,
1902 int key_type,
1903 data_t *key_data,
1904 int exercise_alg )
1905{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001906 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001908 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001909 psa_status_t status;
1910
Gilles Peskine8817f612018-12-18 00:18:46 +01001911 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001912
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001913 psa_set_key_usage_flags( &attributes, policy_usage );
1914 psa_set_key_algorithm( &attributes, policy_alg );
1915 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001916
Gilles Peskine049c7532019-05-15 20:22:09 +02001917 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1918 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001919
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001920 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921 if( policy_alg == exercise_alg &&
1922 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001923 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001925 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001926 psa_cipher_abort( &operation );
1927
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001928 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001929 if( policy_alg == exercise_alg &&
1930 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001931 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001932 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001933 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934
1935exit:
1936 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001937 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001938 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001939}
1940/* END_CASE */
1941
1942/* BEGIN_CASE */
1943void aead_key_policy( int policy_usage,
1944 int policy_alg,
1945 int key_type,
1946 data_t *key_data,
1947 int nonce_length_arg,
1948 int tag_length_arg,
1949 int exercise_alg )
1950{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001951 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001952 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953 psa_status_t status;
1954 unsigned char nonce[16] = {0};
1955 size_t nonce_length = nonce_length_arg;
1956 unsigned char tag[16];
1957 size_t tag_length = tag_length_arg;
1958 size_t output_length;
1959
1960 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1961 TEST_ASSERT( tag_length <= sizeof( tag ) );
1962
Gilles Peskine8817f612018-12-18 00:18:46 +01001963 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001964
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001965 psa_set_key_usage_flags( &attributes, policy_usage );
1966 psa_set_key_algorithm( &attributes, policy_alg );
1967 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968
Gilles Peskine049c7532019-05-15 20:22:09 +02001969 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1970 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001971
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001972 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001973 nonce, nonce_length,
1974 NULL, 0,
1975 NULL, 0,
1976 tag, tag_length,
1977 &output_length );
1978 if( policy_alg == exercise_alg &&
1979 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001980 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001981 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001982 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983
1984 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001985 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986 nonce, nonce_length,
1987 NULL, 0,
1988 tag, tag_length,
1989 NULL, 0,
1990 &output_length );
1991 if( policy_alg == exercise_alg &&
1992 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001993 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001995 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001996
1997exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001998 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001999 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000}
2001/* END_CASE */
2002
2003/* BEGIN_CASE */
2004void asymmetric_encryption_key_policy( int policy_usage,
2005 int policy_alg,
2006 int key_type,
2007 data_t *key_data,
2008 int exercise_alg )
2009{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002010 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002012 psa_status_t status;
2013 size_t key_bits;
2014 size_t buffer_length;
2015 unsigned char *buffer = NULL;
2016 size_t output_length;
2017
Gilles Peskine8817f612018-12-18 00:18:46 +01002018 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002020 psa_set_key_usage_flags( &attributes, policy_usage );
2021 psa_set_key_algorithm( &attributes, policy_alg );
2022 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023
Gilles Peskine049c7532019-05-15 20:22:09 +02002024 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2025 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002027 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
2028 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002029 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2030 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002031 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002033 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034 NULL, 0,
2035 NULL, 0,
2036 buffer, buffer_length,
2037 &output_length );
2038 if( policy_alg == exercise_alg &&
2039 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002040 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002042 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002044 if( buffer_length != 0 )
2045 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002046 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047 buffer, buffer_length,
2048 NULL, 0,
2049 buffer, buffer_length,
2050 &output_length );
2051 if( policy_alg == exercise_alg &&
2052 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002053 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002054 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002055 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056
2057exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002058 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002059 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002060 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061 mbedtls_free( buffer );
2062}
2063/* END_CASE */
2064
2065/* BEGIN_CASE */
2066void asymmetric_signature_key_policy( int policy_usage,
2067 int policy_alg,
2068 int key_type,
2069 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002070 int exercise_alg,
2071 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002072{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002073 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002074 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002076 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2077 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2078 * compatible with the policy and `payload_length_arg` is supposed to be
2079 * a valid input length to sign. If `payload_length_arg <= 0`,
2080 * `exercise_alg` is supposed to be forbidden by the policy. */
2081 int compatible_alg = payload_length_arg > 0;
2082 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002083 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084 size_t signature_length;
2085
Gilles Peskine8817f612018-12-18 00:18:46 +01002086 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002088 psa_set_key_usage_flags( &attributes, policy_usage );
2089 psa_set_key_algorithm( &attributes, policy_alg );
2090 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002091
Gilles Peskine049c7532019-05-15 20:22:09 +02002092 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2093 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002095 status = psa_sign_hash( handle, exercise_alg,
2096 payload, payload_length,
2097 signature, sizeof( signature ),
2098 &signature_length );
2099 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002100 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002101 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002102 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103
2104 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002105 status = psa_verify_hash( handle, exercise_alg,
2106 payload, payload_length,
2107 signature, sizeof( signature ) );
2108 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002109 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002110 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002111 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002112
2113exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002114 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002115 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002116}
2117/* END_CASE */
2118
Janos Follathba3fab92019-06-11 14:50:16 +01002119/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002120void derive_key_policy( int policy_usage,
2121 int policy_alg,
2122 int key_type,
2123 data_t *key_data,
2124 int exercise_alg )
2125{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002126 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002127 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002128 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002129 psa_status_t status;
2130
Gilles Peskine8817f612018-12-18 00:18:46 +01002131 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002132
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002133 psa_set_key_usage_flags( &attributes, policy_usage );
2134 psa_set_key_algorithm( &attributes, policy_alg );
2135 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002136
Gilles Peskine049c7532019-05-15 20:22:09 +02002137 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2138 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002139
Janos Follathba3fab92019-06-11 14:50:16 +01002140 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2141
2142 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2143 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002144 {
Janos Follathba3fab92019-06-11 14:50:16 +01002145 PSA_ASSERT( psa_key_derivation_input_bytes(
2146 &operation,
2147 PSA_KEY_DERIVATION_INPUT_SEED,
2148 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002149 }
Janos Follathba3fab92019-06-11 14:50:16 +01002150
2151 status = psa_key_derivation_input_key( &operation,
2152 PSA_KEY_DERIVATION_INPUT_SECRET,
2153 handle );
2154
Gilles Peskineea0fb492018-07-12 17:17:20 +02002155 if( policy_alg == exercise_alg &&
2156 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002157 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002158 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002159 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002160
2161exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002162 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002163 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002164 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002165}
2166/* END_CASE */
2167
2168/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002169void agreement_key_policy( int policy_usage,
2170 int policy_alg,
2171 int key_type_arg,
2172 data_t *key_data,
2173 int exercise_alg )
2174{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002175 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002177 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002178 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002179 psa_status_t status;
2180
Gilles Peskine8817f612018-12-18 00:18:46 +01002181 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002182
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002183 psa_set_key_usage_flags( &attributes, policy_usage );
2184 psa_set_key_algorithm( &attributes, policy_alg );
2185 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002186
Gilles Peskine049c7532019-05-15 20:22:09 +02002187 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2188 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002189
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002190 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2191 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002192
Gilles Peskine01d718c2018-09-18 12:01:02 +02002193 if( policy_alg == exercise_alg &&
2194 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002195 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002196 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002197 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002198
2199exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002200 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002201 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002202 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002203}
2204/* END_CASE */
2205
2206/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002207void key_policy_alg2( int key_type_arg, data_t *key_data,
2208 int usage_arg, int alg_arg, int alg2_arg )
2209{
2210 psa_key_handle_t handle = 0;
2211 psa_key_type_t key_type = key_type_arg;
2212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2213 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2214 psa_key_usage_t usage = usage_arg;
2215 psa_algorithm_t alg = alg_arg;
2216 psa_algorithm_t alg2 = alg2_arg;
2217
2218 PSA_ASSERT( psa_crypto_init( ) );
2219
2220 psa_set_key_usage_flags( &attributes, usage );
2221 psa_set_key_algorithm( &attributes, alg );
2222 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2223 psa_set_key_type( &attributes, key_type );
2224 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2225 &handle ) );
2226
2227 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2228 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2229 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2230 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2231
2232 if( ! exercise_key( handle, usage, alg ) )
2233 goto exit;
2234 if( ! exercise_key( handle, usage, alg2 ) )
2235 goto exit;
2236
2237exit:
2238 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002239 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002240}
2241/* END_CASE */
2242
2243/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002244void raw_agreement_key_policy( int policy_usage,
2245 int policy_alg,
2246 int key_type_arg,
2247 data_t *key_data,
2248 int exercise_alg )
2249{
2250 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002252 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002253 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002254 psa_status_t status;
2255
2256 PSA_ASSERT( psa_crypto_init( ) );
2257
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002258 psa_set_key_usage_flags( &attributes, policy_usage );
2259 psa_set_key_algorithm( &attributes, policy_alg );
2260 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002261
Gilles Peskine049c7532019-05-15 20:22:09 +02002262 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2263 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002264
2265 status = raw_key_agreement_with_self( exercise_alg, handle );
2266
2267 if( policy_alg == exercise_alg &&
2268 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2269 PSA_ASSERT( status );
2270 else
2271 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2272
2273exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002274 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002275 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002276 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002277}
2278/* END_CASE */
2279
2280/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002281void copy_success( int source_usage_arg,
2282 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002283 int type_arg, data_t *material,
2284 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002285 int target_usage_arg,
2286 int target_alg_arg, int target_alg2_arg,
2287 int expected_usage_arg,
2288 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002289{
Gilles Peskineca25db92019-04-19 11:43:08 +02002290 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2291 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002292 psa_key_usage_t expected_usage = expected_usage_arg;
2293 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002294 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002295 psa_key_handle_t source_handle = 0;
2296 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002297 uint8_t *export_buffer = NULL;
2298
Gilles Peskine57ab7212019-01-28 13:03:09 +01002299 PSA_ASSERT( psa_crypto_init( ) );
2300
Gilles Peskineca25db92019-04-19 11:43:08 +02002301 /* Prepare the source key. */
2302 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2303 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002304 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002305 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002306 PSA_ASSERT( psa_import_key( &source_attributes,
2307 material->x, material->len,
2308 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002309 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002310
Gilles Peskineca25db92019-04-19 11:43:08 +02002311 /* Prepare the target attributes. */
2312 if( copy_attributes )
2313 target_attributes = source_attributes;
2314 if( target_usage_arg != -1 )
2315 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2316 if( target_alg_arg != -1 )
2317 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002318 if( target_alg2_arg != -1 )
2319 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002320
2321 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002322 PSA_ASSERT( psa_copy_key( source_handle,
2323 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002324
2325 /* Destroy the source to ensure that this doesn't affect the target. */
2326 PSA_ASSERT( psa_destroy_key( source_handle ) );
2327
2328 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002329 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2330 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2331 psa_get_key_type( &target_attributes ) );
2332 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2333 psa_get_key_bits( &target_attributes ) );
2334 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2335 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002336 TEST_EQUAL( expected_alg2,
2337 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002338 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2339 {
2340 size_t length;
2341 ASSERT_ALLOC( export_buffer, material->len );
2342 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2343 material->len, &length ) );
2344 ASSERT_COMPARE( material->x, material->len,
2345 export_buffer, length );
2346 }
2347 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2348 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002349 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2350 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002351
2352 PSA_ASSERT( psa_close_key( target_handle ) );
2353
2354exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002355 psa_reset_key_attributes( &source_attributes );
2356 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002357 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002358 mbedtls_free( export_buffer );
2359}
2360/* END_CASE */
2361
2362/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002363void copy_fail( int source_usage_arg,
2364 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002365 int type_arg, data_t *material,
2366 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002367 int target_usage_arg,
2368 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002369 int expected_status_arg )
2370{
2371 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2372 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2373 psa_key_handle_t source_handle = 0;
2374 psa_key_handle_t target_handle = 0;
2375
2376 PSA_ASSERT( psa_crypto_init( ) );
2377
2378 /* Prepare the source key. */
2379 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2380 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002381 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002382 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002383 PSA_ASSERT( psa_import_key( &source_attributes,
2384 material->x, material->len,
2385 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002386
2387 /* Prepare the target attributes. */
2388 psa_set_key_type( &target_attributes, target_type_arg );
2389 psa_set_key_bits( &target_attributes, target_bits_arg );
2390 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2391 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002392 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002393
2394 /* Try to copy the key. */
2395 TEST_EQUAL( psa_copy_key( source_handle,
2396 &target_attributes, &target_handle ),
2397 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002398
2399 PSA_ASSERT( psa_destroy_key( source_handle ) );
2400
Gilles Peskine4a644642019-05-03 17:14:08 +02002401exit:
2402 psa_reset_key_attributes( &source_attributes );
2403 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002404 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002405}
2406/* END_CASE */
2407
2408/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002409void hash_operation_init( )
2410{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002411 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002412 /* Test each valid way of initializing the object, except for `= {0}`, as
2413 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2414 * though it's OK by the C standard. We could test for this, but we'd need
2415 * to supress the Clang warning for the test. */
2416 psa_hash_operation_t func = psa_hash_operation_init( );
2417 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2418 psa_hash_operation_t zero;
2419
2420 memset( &zero, 0, sizeof( zero ) );
2421
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002422 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002423 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2424 PSA_ERROR_BAD_STATE );
2425 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2426 PSA_ERROR_BAD_STATE );
2427 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2428 PSA_ERROR_BAD_STATE );
2429
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002430 /* A default hash operation should be abortable without error. */
2431 PSA_ASSERT( psa_hash_abort( &func ) );
2432 PSA_ASSERT( psa_hash_abort( &init ) );
2433 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002434}
2435/* END_CASE */
2436
2437/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002438void hash_setup( int alg_arg,
2439 int expected_status_arg )
2440{
2441 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002442 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002443 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444 psa_status_t status;
2445
Gilles Peskine8817f612018-12-18 00:18:46 +01002446 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002447
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002448 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002449 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002450
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002451 /* Whether setup succeeded or failed, abort must succeed. */
2452 PSA_ASSERT( psa_hash_abort( &operation ) );
2453
2454 /* If setup failed, reproduce the failure, so as to
2455 * test the resulting state of the operation object. */
2456 if( status != PSA_SUCCESS )
2457 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2458
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002459 /* Now the operation object should be reusable. */
2460#if defined(KNOWN_SUPPORTED_HASH_ALG)
2461 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2462 PSA_ASSERT( psa_hash_abort( &operation ) );
2463#endif
2464
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002465exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002466 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467}
2468/* END_CASE */
2469
2470/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002471void hash_compute_fail( int alg_arg, data_t *input,
2472 int output_size_arg, int expected_status_arg )
2473{
2474 psa_algorithm_t alg = alg_arg;
2475 uint8_t *output = NULL;
2476 size_t output_size = output_size_arg;
2477 size_t output_length = INVALID_EXPORT_LENGTH;
2478 psa_status_t expected_status = expected_status_arg;
2479 psa_status_t status;
2480
2481 ASSERT_ALLOC( output, output_size );
2482
2483 PSA_ASSERT( psa_crypto_init( ) );
2484
2485 status = psa_hash_compute( alg, input->x, input->len,
2486 output, output_size, &output_length );
2487 TEST_EQUAL( status, expected_status );
2488 TEST_ASSERT( output_length <= output_size );
2489
2490exit:
2491 mbedtls_free( output );
2492 PSA_DONE( );
2493}
2494/* END_CASE */
2495
2496/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002497void hash_compare_fail( int alg_arg, data_t *input,
2498 data_t *reference_hash,
2499 int expected_status_arg )
2500{
2501 psa_algorithm_t alg = alg_arg;
2502 psa_status_t expected_status = expected_status_arg;
2503 psa_status_t status;
2504
2505 PSA_ASSERT( psa_crypto_init( ) );
2506
2507 status = psa_hash_compare( alg, input->x, input->len,
2508 reference_hash->x, reference_hash->len );
2509 TEST_EQUAL( status, expected_status );
2510
2511exit:
2512 PSA_DONE( );
2513}
2514/* END_CASE */
2515
2516/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002517void hash_compute_compare( int alg_arg, data_t *input,
2518 data_t *expected_output )
2519{
2520 psa_algorithm_t alg = alg_arg;
2521 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2522 size_t output_length = INVALID_EXPORT_LENGTH;
2523 size_t i;
2524
2525 PSA_ASSERT( psa_crypto_init( ) );
2526
2527 /* Compute with tight buffer */
2528 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2529 output, PSA_HASH_SIZE( alg ),
2530 &output_length ) );
2531 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2532 ASSERT_COMPARE( output, output_length,
2533 expected_output->x, expected_output->len );
2534
2535 /* Compute with larger buffer */
2536 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2537 output, sizeof( output ),
2538 &output_length ) );
2539 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2540 ASSERT_COMPARE( output, output_length,
2541 expected_output->x, expected_output->len );
2542
2543 /* Compare with correct hash */
2544 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2545 output, output_length ) );
2546
2547 /* Compare with trailing garbage */
2548 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2549 output, output_length + 1 ),
2550 PSA_ERROR_INVALID_SIGNATURE );
2551
2552 /* Compare with truncated hash */
2553 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2554 output, output_length - 1 ),
2555 PSA_ERROR_INVALID_SIGNATURE );
2556
2557 /* Compare with corrupted value */
2558 for( i = 0; i < output_length; i++ )
2559 {
2560 test_set_step( i );
2561 output[i] ^= 1;
2562 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2563 output, output_length ),
2564 PSA_ERROR_INVALID_SIGNATURE );
2565 output[i] ^= 1;
2566 }
2567
2568exit:
2569 PSA_DONE( );
2570}
2571/* END_CASE */
2572
2573/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002574void hash_bad_order( )
2575{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002576 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002577 unsigned char input[] = "";
2578 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002579 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002580 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2581 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2582 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002583 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002584 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002585 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002586
Gilles Peskine8817f612018-12-18 00:18:46 +01002587 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002588
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002589 /* Call setup twice in a row. */
2590 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2591 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2592 PSA_ERROR_BAD_STATE );
2593 PSA_ASSERT( psa_hash_abort( &operation ) );
2594
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002595 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002596 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002597 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002598 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002599
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002600 /* Call update after finish. */
2601 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2602 PSA_ASSERT( psa_hash_finish( &operation,
2603 hash, sizeof( hash ), &hash_len ) );
2604 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002605 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002606 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002607
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002608 /* Call verify without calling setup beforehand. */
2609 TEST_EQUAL( psa_hash_verify( &operation,
2610 valid_hash, sizeof( valid_hash ) ),
2611 PSA_ERROR_BAD_STATE );
2612 PSA_ASSERT( psa_hash_abort( &operation ) );
2613
2614 /* Call verify after finish. */
2615 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2616 PSA_ASSERT( psa_hash_finish( &operation,
2617 hash, sizeof( hash ), &hash_len ) );
2618 TEST_EQUAL( psa_hash_verify( &operation,
2619 valid_hash, sizeof( valid_hash ) ),
2620 PSA_ERROR_BAD_STATE );
2621 PSA_ASSERT( psa_hash_abort( &operation ) );
2622
2623 /* Call verify twice in a row. */
2624 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2625 PSA_ASSERT( psa_hash_verify( &operation,
2626 valid_hash, sizeof( valid_hash ) ) );
2627 TEST_EQUAL( psa_hash_verify( &operation,
2628 valid_hash, sizeof( valid_hash ) ),
2629 PSA_ERROR_BAD_STATE );
2630 PSA_ASSERT( psa_hash_abort( &operation ) );
2631
2632 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002633 TEST_EQUAL( psa_hash_finish( &operation,
2634 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002635 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002636 PSA_ASSERT( psa_hash_abort( &operation ) );
2637
2638 /* Call finish twice in a row. */
2639 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2640 PSA_ASSERT( psa_hash_finish( &operation,
2641 hash, sizeof( hash ), &hash_len ) );
2642 TEST_EQUAL( psa_hash_finish( &operation,
2643 hash, sizeof( hash ), &hash_len ),
2644 PSA_ERROR_BAD_STATE );
2645 PSA_ASSERT( psa_hash_abort( &operation ) );
2646
2647 /* Call finish after calling verify. */
2648 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2649 PSA_ASSERT( psa_hash_verify( &operation,
2650 valid_hash, sizeof( valid_hash ) ) );
2651 TEST_EQUAL( psa_hash_finish( &operation,
2652 hash, sizeof( hash ), &hash_len ),
2653 PSA_ERROR_BAD_STATE );
2654 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002655
2656exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002657 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002658}
2659/* END_CASE */
2660
itayzafrir27e69452018-11-01 14:26:34 +02002661/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2662void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002663{
2664 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002665 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2666 * appended to it */
2667 unsigned char hash[] = {
2668 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2669 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2670 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002671 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002672 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002673
Gilles Peskine8817f612018-12-18 00:18:46 +01002674 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002675
itayzafrir27e69452018-11-01 14:26:34 +02002676 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002677 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002678 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002679 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002680
itayzafrir27e69452018-11-01 14:26:34 +02002681 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002682 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002683 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002684 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002685
itayzafrir27e69452018-11-01 14:26:34 +02002686 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002687 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002688 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002689 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002690
itayzafrirec93d302018-10-18 18:01:10 +03002691exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002692 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002693}
2694/* END_CASE */
2695
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002696/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2697void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002698{
2699 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002700 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002701 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002702 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002703 size_t hash_len;
2704
Gilles Peskine8817f612018-12-18 00:18:46 +01002705 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002706
itayzafrir58028322018-10-25 10:22:01 +03002707 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002708 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002709 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002710 hash, expected_size - 1, &hash_len ),
2711 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002712
2713exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002714 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002715}
2716/* END_CASE */
2717
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002718/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2719void hash_clone_source_state( )
2720{
2721 psa_algorithm_t alg = PSA_ALG_SHA_256;
2722 unsigned char hash[PSA_HASH_MAX_SIZE];
2723 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2724 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2725 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2726 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2727 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2728 size_t hash_len;
2729
2730 PSA_ASSERT( psa_crypto_init( ) );
2731 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2732
2733 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2734 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2735 PSA_ASSERT( psa_hash_finish( &op_finished,
2736 hash, sizeof( hash ), &hash_len ) );
2737 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2738 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2739
2740 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2741 PSA_ERROR_BAD_STATE );
2742
2743 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2744 PSA_ASSERT( psa_hash_finish( &op_init,
2745 hash, sizeof( hash ), &hash_len ) );
2746 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2747 PSA_ASSERT( psa_hash_finish( &op_finished,
2748 hash, sizeof( hash ), &hash_len ) );
2749 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2750 PSA_ASSERT( psa_hash_finish( &op_aborted,
2751 hash, sizeof( hash ), &hash_len ) );
2752
2753exit:
2754 psa_hash_abort( &op_source );
2755 psa_hash_abort( &op_init );
2756 psa_hash_abort( &op_setup );
2757 psa_hash_abort( &op_finished );
2758 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002759 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002760}
2761/* END_CASE */
2762
2763/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2764void hash_clone_target_state( )
2765{
2766 psa_algorithm_t alg = PSA_ALG_SHA_256;
2767 unsigned char hash[PSA_HASH_MAX_SIZE];
2768 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2769 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2770 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2771 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2772 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2773 size_t hash_len;
2774
2775 PSA_ASSERT( psa_crypto_init( ) );
2776
2777 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2778 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2779 PSA_ASSERT( psa_hash_finish( &op_finished,
2780 hash, sizeof( hash ), &hash_len ) );
2781 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2782 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2783
2784 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2785 PSA_ASSERT( psa_hash_finish( &op_target,
2786 hash, sizeof( hash ), &hash_len ) );
2787
2788 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2789 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2790 PSA_ERROR_BAD_STATE );
2791 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2792 PSA_ERROR_BAD_STATE );
2793
2794exit:
2795 psa_hash_abort( &op_target );
2796 psa_hash_abort( &op_init );
2797 psa_hash_abort( &op_setup );
2798 psa_hash_abort( &op_finished );
2799 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002800 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002801}
2802/* END_CASE */
2803
itayzafrir58028322018-10-25 10:22:01 +03002804/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002805void mac_operation_init( )
2806{
Jaeden Amero252ef282019-02-15 14:05:35 +00002807 const uint8_t input[1] = { 0 };
2808
Jaeden Amero769ce272019-01-04 11:48:03 +00002809 /* Test each valid way of initializing the object, except for `= {0}`, as
2810 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2811 * though it's OK by the C standard. We could test for this, but we'd need
2812 * to supress the Clang warning for the test. */
2813 psa_mac_operation_t func = psa_mac_operation_init( );
2814 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2815 psa_mac_operation_t zero;
2816
2817 memset( &zero, 0, sizeof( zero ) );
2818
Jaeden Amero252ef282019-02-15 14:05:35 +00002819 /* A freshly-initialized MAC operation should not be usable. */
2820 TEST_EQUAL( psa_mac_update( &func,
2821 input, sizeof( input ) ),
2822 PSA_ERROR_BAD_STATE );
2823 TEST_EQUAL( psa_mac_update( &init,
2824 input, sizeof( input ) ),
2825 PSA_ERROR_BAD_STATE );
2826 TEST_EQUAL( psa_mac_update( &zero,
2827 input, sizeof( input ) ),
2828 PSA_ERROR_BAD_STATE );
2829
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002830 /* A default MAC operation should be abortable without error. */
2831 PSA_ASSERT( psa_mac_abort( &func ) );
2832 PSA_ASSERT( psa_mac_abort( &init ) );
2833 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002834}
2835/* END_CASE */
2836
2837/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002838void mac_setup( int key_type_arg,
2839 data_t *key,
2840 int alg_arg,
2841 int expected_status_arg )
2842{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002843 psa_key_type_t key_type = key_type_arg;
2844 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002845 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002846 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002847 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2848#if defined(KNOWN_SUPPORTED_MAC_ALG)
2849 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2850#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002851
Gilles Peskine8817f612018-12-18 00:18:46 +01002852 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002853
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002854 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2855 &operation, &status ) )
2856 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002857 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002858
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002859 /* The operation object should be reusable. */
2860#if defined(KNOWN_SUPPORTED_MAC_ALG)
2861 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2862 smoke_test_key_data,
2863 sizeof( smoke_test_key_data ),
2864 KNOWN_SUPPORTED_MAC_ALG,
2865 &operation, &status ) )
2866 goto exit;
2867 TEST_EQUAL( status, PSA_SUCCESS );
2868#endif
2869
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002870exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002871 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002872}
2873/* END_CASE */
2874
2875/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002876void mac_bad_order( )
2877{
2878 psa_key_handle_t handle = 0;
2879 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2880 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2881 const uint8_t key[] = {
2882 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2883 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2884 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002886 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2887 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2888 size_t sign_mac_length = 0;
2889 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2890 const uint8_t verify_mac[] = {
2891 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2892 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2893 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2894
2895 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002896 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002897 psa_set_key_algorithm( &attributes, alg );
2898 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002899
Gilles Peskine73676cb2019-05-15 20:15:10 +02002900 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002901
Jaeden Amero252ef282019-02-15 14:05:35 +00002902 /* Call update without calling setup beforehand. */
2903 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2904 PSA_ERROR_BAD_STATE );
2905 PSA_ASSERT( psa_mac_abort( &operation ) );
2906
2907 /* Call sign finish without calling setup beforehand. */
2908 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2909 &sign_mac_length),
2910 PSA_ERROR_BAD_STATE );
2911 PSA_ASSERT( psa_mac_abort( &operation ) );
2912
2913 /* Call verify finish without calling setup beforehand. */
2914 TEST_EQUAL( psa_mac_verify_finish( &operation,
2915 verify_mac, sizeof( verify_mac ) ),
2916 PSA_ERROR_BAD_STATE );
2917 PSA_ASSERT( psa_mac_abort( &operation ) );
2918
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002919 /* Call setup twice in a row. */
2920 PSA_ASSERT( psa_mac_sign_setup( &operation,
2921 handle, alg ) );
2922 TEST_EQUAL( psa_mac_sign_setup( &operation,
2923 handle, alg ),
2924 PSA_ERROR_BAD_STATE );
2925 PSA_ASSERT( psa_mac_abort( &operation ) );
2926
Jaeden Amero252ef282019-02-15 14:05:35 +00002927 /* Call update after sign finish. */
2928 PSA_ASSERT( psa_mac_sign_setup( &operation,
2929 handle, alg ) );
2930 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2931 PSA_ASSERT( psa_mac_sign_finish( &operation,
2932 sign_mac, sizeof( sign_mac ),
2933 &sign_mac_length ) );
2934 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2935 PSA_ERROR_BAD_STATE );
2936 PSA_ASSERT( psa_mac_abort( &operation ) );
2937
2938 /* Call update after verify finish. */
2939 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002940 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002941 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2942 PSA_ASSERT( psa_mac_verify_finish( &operation,
2943 verify_mac, sizeof( verify_mac ) ) );
2944 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2945 PSA_ERROR_BAD_STATE );
2946 PSA_ASSERT( psa_mac_abort( &operation ) );
2947
2948 /* Call sign finish twice in a row. */
2949 PSA_ASSERT( psa_mac_sign_setup( &operation,
2950 handle, alg ) );
2951 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2952 PSA_ASSERT( psa_mac_sign_finish( &operation,
2953 sign_mac, sizeof( sign_mac ),
2954 &sign_mac_length ) );
2955 TEST_EQUAL( psa_mac_sign_finish( &operation,
2956 sign_mac, sizeof( sign_mac ),
2957 &sign_mac_length ),
2958 PSA_ERROR_BAD_STATE );
2959 PSA_ASSERT( psa_mac_abort( &operation ) );
2960
2961 /* Call verify finish twice in a row. */
2962 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002963 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002964 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2965 PSA_ASSERT( psa_mac_verify_finish( &operation,
2966 verify_mac, sizeof( verify_mac ) ) );
2967 TEST_EQUAL( psa_mac_verify_finish( &operation,
2968 verify_mac, sizeof( verify_mac ) ),
2969 PSA_ERROR_BAD_STATE );
2970 PSA_ASSERT( psa_mac_abort( &operation ) );
2971
2972 /* Setup sign but try verify. */
2973 PSA_ASSERT( psa_mac_sign_setup( &operation,
2974 handle, alg ) );
2975 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2976 TEST_EQUAL( psa_mac_verify_finish( &operation,
2977 verify_mac, sizeof( verify_mac ) ),
2978 PSA_ERROR_BAD_STATE );
2979 PSA_ASSERT( psa_mac_abort( &operation ) );
2980
2981 /* Setup verify but try sign. */
2982 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002983 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002984 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2985 TEST_EQUAL( psa_mac_sign_finish( &operation,
2986 sign_mac, sizeof( sign_mac ),
2987 &sign_mac_length ),
2988 PSA_ERROR_BAD_STATE );
2989 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002990
Gilles Peskine76b29a72019-05-28 14:08:50 +02002991 PSA_ASSERT( psa_destroy_key( handle ) );
2992
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002993exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002994 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002995}
2996/* END_CASE */
2997
2998/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002999void mac_sign( int key_type_arg,
3000 data_t *key,
3001 int alg_arg,
3002 data_t *input,
3003 data_t *expected_mac )
3004{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003005 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003006 psa_key_type_t key_type = key_type_arg;
3007 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003008 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003010 /* Leave a little extra room in the output buffer. At the end of the
3011 * test, we'll check that the implementation didn't overwrite onto
3012 * this extra room. */
3013 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
3014 size_t mac_buffer_size =
3015 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
3016 size_t mac_length = 0;
3017
3018 memset( actual_mac, '+', sizeof( actual_mac ) );
3019 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
3020 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
3021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003023
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003024 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003025 psa_set_key_algorithm( &attributes, alg );
3026 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003027
Gilles Peskine73676cb2019-05-15 20:15:10 +02003028 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003029
3030 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003031 PSA_ASSERT( psa_mac_sign_setup( &operation,
3032 handle, alg ) );
3033 PSA_ASSERT( psa_mac_update( &operation,
3034 input->x, input->len ) );
3035 PSA_ASSERT( psa_mac_sign_finish( &operation,
3036 actual_mac, mac_buffer_size,
3037 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003038
3039 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003040 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3041 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003042
3043 /* Verify that the end of the buffer is untouched. */
3044 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
3045 sizeof( actual_mac ) - mac_length ) );
3046
3047exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003048 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003049 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003050}
3051/* END_CASE */
3052
3053/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003054void mac_verify( int key_type_arg,
3055 data_t *key,
3056 int alg_arg,
3057 data_t *input,
3058 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003059{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003060 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003061 psa_key_type_t key_type = key_type_arg;
3062 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003063 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003065
Gilles Peskine69c12672018-06-28 00:07:19 +02003066 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3067
Gilles Peskine8817f612018-12-18 00:18:46 +01003068 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003070 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003071 psa_set_key_algorithm( &attributes, alg );
3072 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003073
Gilles Peskine73676cb2019-05-15 20:15:10 +02003074 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003075
Gilles Peskine8817f612018-12-18 00:18:46 +01003076 PSA_ASSERT( psa_mac_verify_setup( &operation,
3077 handle, alg ) );
3078 PSA_ASSERT( psa_destroy_key( handle ) );
3079 PSA_ASSERT( psa_mac_update( &operation,
3080 input->x, input->len ) );
3081 PSA_ASSERT( psa_mac_verify_finish( &operation,
3082 expected_mac->x,
3083 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084
3085exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003086 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003087 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003088}
3089/* END_CASE */
3090
3091/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003092void cipher_operation_init( )
3093{
Jaeden Ameroab439972019-02-15 14:12:05 +00003094 const uint8_t input[1] = { 0 };
3095 unsigned char output[1] = { 0 };
3096 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003097 /* Test each valid way of initializing the object, except for `= {0}`, as
3098 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3099 * though it's OK by the C standard. We could test for this, but we'd need
3100 * to supress the Clang warning for the test. */
3101 psa_cipher_operation_t func = psa_cipher_operation_init( );
3102 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3103 psa_cipher_operation_t zero;
3104
3105 memset( &zero, 0, sizeof( zero ) );
3106
Jaeden Ameroab439972019-02-15 14:12:05 +00003107 /* A freshly-initialized cipher operation should not be usable. */
3108 TEST_EQUAL( psa_cipher_update( &func,
3109 input, sizeof( input ),
3110 output, sizeof( output ),
3111 &output_length ),
3112 PSA_ERROR_BAD_STATE );
3113 TEST_EQUAL( psa_cipher_update( &init,
3114 input, sizeof( input ),
3115 output, sizeof( output ),
3116 &output_length ),
3117 PSA_ERROR_BAD_STATE );
3118 TEST_EQUAL( psa_cipher_update( &zero,
3119 input, sizeof( input ),
3120 output, sizeof( output ),
3121 &output_length ),
3122 PSA_ERROR_BAD_STATE );
3123
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003124 /* A default cipher operation should be abortable without error. */
3125 PSA_ASSERT( psa_cipher_abort( &func ) );
3126 PSA_ASSERT( psa_cipher_abort( &init ) );
3127 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003128}
3129/* END_CASE */
3130
3131/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003132void cipher_setup( int key_type_arg,
3133 data_t *key,
3134 int alg_arg,
3135 int expected_status_arg )
3136{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003137 psa_key_type_t key_type = key_type_arg;
3138 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003139 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003140 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003141 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003142#if defined(KNOWN_SUPPORTED_MAC_ALG)
3143 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3144#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003145
Gilles Peskine8817f612018-12-18 00:18:46 +01003146 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003147
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003148 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3149 &operation, &status ) )
3150 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003151 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003152
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003153 /* The operation object should be reusable. */
3154#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3155 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3156 smoke_test_key_data,
3157 sizeof( smoke_test_key_data ),
3158 KNOWN_SUPPORTED_CIPHER_ALG,
3159 &operation, &status ) )
3160 goto exit;
3161 TEST_EQUAL( status, PSA_SUCCESS );
3162#endif
3163
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003165 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003166}
3167/* END_CASE */
3168
3169/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003170void cipher_bad_order( )
3171{
3172 psa_key_handle_t handle = 0;
3173 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3174 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003176 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3177 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3178 const uint8_t key[] = {
3179 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3180 0xaa, 0xaa, 0xaa, 0xaa };
3181 const uint8_t text[] = {
3182 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3183 0xbb, 0xbb, 0xbb, 0xbb };
3184 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3185 size_t length = 0;
3186
3187 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003188 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3189 psa_set_key_algorithm( &attributes, alg );
3190 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003191 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003192
3193
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003194 /* Call encrypt setup twice in a row. */
3195 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3196 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3197 PSA_ERROR_BAD_STATE );
3198 PSA_ASSERT( psa_cipher_abort( &operation ) );
3199
3200 /* Call decrypt setup twice in a row. */
3201 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3202 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3203 PSA_ERROR_BAD_STATE );
3204 PSA_ASSERT( psa_cipher_abort( &operation ) );
3205
Jaeden Ameroab439972019-02-15 14:12:05 +00003206 /* Generate an IV without calling setup beforehand. */
3207 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3208 buffer, sizeof( buffer ),
3209 &length ),
3210 PSA_ERROR_BAD_STATE );
3211 PSA_ASSERT( psa_cipher_abort( &operation ) );
3212
3213 /* Generate an IV twice in a row. */
3214 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3215 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3216 buffer, sizeof( buffer ),
3217 &length ) );
3218 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3219 buffer, sizeof( buffer ),
3220 &length ),
3221 PSA_ERROR_BAD_STATE );
3222 PSA_ASSERT( psa_cipher_abort( &operation ) );
3223
3224 /* Generate an IV after it's already set. */
3225 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3226 PSA_ASSERT( psa_cipher_set_iv( &operation,
3227 iv, sizeof( iv ) ) );
3228 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3229 buffer, sizeof( buffer ),
3230 &length ),
3231 PSA_ERROR_BAD_STATE );
3232 PSA_ASSERT( psa_cipher_abort( &operation ) );
3233
3234 /* Set an IV without calling setup beforehand. */
3235 TEST_EQUAL( psa_cipher_set_iv( &operation,
3236 iv, sizeof( iv ) ),
3237 PSA_ERROR_BAD_STATE );
3238 PSA_ASSERT( psa_cipher_abort( &operation ) );
3239
3240 /* Set an IV after it's already set. */
3241 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3242 PSA_ASSERT( psa_cipher_set_iv( &operation,
3243 iv, sizeof( iv ) ) );
3244 TEST_EQUAL( psa_cipher_set_iv( &operation,
3245 iv, sizeof( iv ) ),
3246 PSA_ERROR_BAD_STATE );
3247 PSA_ASSERT( psa_cipher_abort( &operation ) );
3248
3249 /* Set an IV after it's already generated. */
3250 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3251 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3252 buffer, sizeof( buffer ),
3253 &length ) );
3254 TEST_EQUAL( psa_cipher_set_iv( &operation,
3255 iv, sizeof( iv ) ),
3256 PSA_ERROR_BAD_STATE );
3257 PSA_ASSERT( psa_cipher_abort( &operation ) );
3258
3259 /* Call update without calling setup beforehand. */
3260 TEST_EQUAL( psa_cipher_update( &operation,
3261 text, sizeof( text ),
3262 buffer, sizeof( buffer ),
3263 &length ),
3264 PSA_ERROR_BAD_STATE );
3265 PSA_ASSERT( psa_cipher_abort( &operation ) );
3266
3267 /* Call update without an IV where an IV is required. */
3268 TEST_EQUAL( psa_cipher_update( &operation,
3269 text, sizeof( text ),
3270 buffer, sizeof( buffer ),
3271 &length ),
3272 PSA_ERROR_BAD_STATE );
3273 PSA_ASSERT( psa_cipher_abort( &operation ) );
3274
3275 /* Call update after finish. */
3276 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3277 PSA_ASSERT( psa_cipher_set_iv( &operation,
3278 iv, sizeof( iv ) ) );
3279 PSA_ASSERT( psa_cipher_finish( &operation,
3280 buffer, sizeof( buffer ), &length ) );
3281 TEST_EQUAL( psa_cipher_update( &operation,
3282 text, sizeof( text ),
3283 buffer, sizeof( buffer ),
3284 &length ),
3285 PSA_ERROR_BAD_STATE );
3286 PSA_ASSERT( psa_cipher_abort( &operation ) );
3287
3288 /* Call finish without calling setup beforehand. */
3289 TEST_EQUAL( psa_cipher_finish( &operation,
3290 buffer, sizeof( buffer ), &length ),
3291 PSA_ERROR_BAD_STATE );
3292 PSA_ASSERT( psa_cipher_abort( &operation ) );
3293
3294 /* Call finish without an IV where an IV is required. */
3295 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3296 /* Not calling update means we are encrypting an empty buffer, which is OK
3297 * for cipher modes with padding. */
3298 TEST_EQUAL( psa_cipher_finish( &operation,
3299 buffer, sizeof( buffer ), &length ),
3300 PSA_ERROR_BAD_STATE );
3301 PSA_ASSERT( psa_cipher_abort( &operation ) );
3302
3303 /* Call finish twice in a row. */
3304 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3305 PSA_ASSERT( psa_cipher_set_iv( &operation,
3306 iv, sizeof( iv ) ) );
3307 PSA_ASSERT( psa_cipher_finish( &operation,
3308 buffer, sizeof( buffer ), &length ) );
3309 TEST_EQUAL( psa_cipher_finish( &operation,
3310 buffer, sizeof( buffer ), &length ),
3311 PSA_ERROR_BAD_STATE );
3312 PSA_ASSERT( psa_cipher_abort( &operation ) );
3313
Gilles Peskine76b29a72019-05-28 14:08:50 +02003314 PSA_ASSERT( psa_destroy_key( handle ) );
3315
Jaeden Ameroab439972019-02-15 14:12:05 +00003316exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003317 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003318}
3319/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003320
Gilles Peskine50e586b2018-06-08 14:28:46 +02003321/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003322void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003323 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003324 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003325 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003326{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003327 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003328 psa_status_t status;
3329 psa_key_type_t key_type = key_type_arg;
3330 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003331 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003332 unsigned char *output = NULL;
3333 size_t output_buffer_size = 0;
3334 size_t function_output_length = 0;
3335 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003336 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003338
Gilles Peskine8817f612018-12-18 00:18:46 +01003339 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003340
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003341 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3342 psa_set_key_algorithm( &attributes, alg );
3343 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003344
Gilles Peskine73676cb2019-05-15 20:15:10 +02003345 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003346
Gilles Peskine8817f612018-12-18 00:18:46 +01003347 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3348 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003349
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003350 if( iv->len > 0 )
3351 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003352 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003353 }
3354
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003355 output_buffer_size = ( (size_t) input->len +
3356 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003357 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_cipher_update( &operation,
3360 input->x, input->len,
3361 output, output_buffer_size,
3362 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003363 total_output_length += function_output_length;
3364 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003365 output + total_output_length,
3366 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003367 &function_output_length );
3368 total_output_length += function_output_length;
3369
Gilles Peskinefe11b722018-12-18 00:24:04 +01003370 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003371 if( expected_status == PSA_SUCCESS )
3372 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003373 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003374 ASSERT_COMPARE( expected_output->x, expected_output->len,
3375 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003376 }
3377
3378exit:
3379 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003380 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003381 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003382}
3383/* END_CASE */
3384
3385/* BEGIN_CASE */
3386void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003387 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003388 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003389 int first_part_size_arg,
3390 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003391 data_t *expected_output )
3392{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003393 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003394 psa_key_type_t key_type = key_type_arg;
3395 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003396 size_t first_part_size = first_part_size_arg;
3397 size_t output1_length = output1_length_arg;
3398 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003399 unsigned char *output = NULL;
3400 size_t output_buffer_size = 0;
3401 size_t function_output_length = 0;
3402 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003403 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003404 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003405
Gilles Peskine8817f612018-12-18 00:18:46 +01003406 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003407
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003408 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3409 psa_set_key_algorithm( &attributes, alg );
3410 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003411
Gilles Peskine73676cb2019-05-15 20:15:10 +02003412 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003413
Gilles Peskine8817f612018-12-18 00:18:46 +01003414 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3415 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003416
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003417 if( iv->len > 0 )
3418 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003419 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003420 }
3421
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003422 output_buffer_size = ( (size_t) input->len +
3423 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003424 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003425
Gilles Peskinee0866522019-02-19 19:44:00 +01003426 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003427 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3428 output, output_buffer_size,
3429 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003430 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003431 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003432 PSA_ASSERT( psa_cipher_update( &operation,
3433 input->x + first_part_size,
3434 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003435 output + total_output_length,
3436 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003437 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003438 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003439 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003440 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003441 output + total_output_length,
3442 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003444 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003445 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003446
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003447 ASSERT_COMPARE( expected_output->x, expected_output->len,
3448 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003449
3450exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003451 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003452 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003453 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003454}
3455/* END_CASE */
3456
3457/* BEGIN_CASE */
3458void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003459 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003460 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003461 int first_part_size_arg,
3462 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003463 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003464{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003465 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003466
3467 psa_key_type_t key_type = key_type_arg;
3468 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003469 size_t first_part_size = first_part_size_arg;
3470 size_t output1_length = output1_length_arg;
3471 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003472 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003473 size_t output_buffer_size = 0;
3474 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003475 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003476 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003478
Gilles Peskine8817f612018-12-18 00:18:46 +01003479 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003480
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003481 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3482 psa_set_key_algorithm( &attributes, alg );
3483 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003484
Gilles Peskine73676cb2019-05-15 20:15:10 +02003485 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003486
Gilles Peskine8817f612018-12-18 00:18:46 +01003487 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3488 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003489
Steven Cooreman177deba2020-09-07 17:14:14 +02003490 if( iv->len > 0 )
3491 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003492 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003493 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003494
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003495 output_buffer_size = ( (size_t) input->len +
3496 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003497 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003498
Gilles Peskinee0866522019-02-19 19:44:00 +01003499 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003500 PSA_ASSERT( psa_cipher_update( &operation,
3501 input->x, first_part_size,
3502 output, output_buffer_size,
3503 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003504 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003505 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003506 PSA_ASSERT( psa_cipher_update( &operation,
3507 input->x + first_part_size,
3508 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003509 output + total_output_length,
3510 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003511 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003512 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003513 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003514 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003515 output + total_output_length,
3516 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003517 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003518 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003519 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003520
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003521 ASSERT_COMPARE( expected_output->x, expected_output->len,
3522 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003523
3524exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003525 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003526 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003527 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003528}
3529/* END_CASE */
3530
Gilles Peskine50e586b2018-06-08 14:28:46 +02003531/* BEGIN_CASE */
3532void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003533 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003534 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003535 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003536{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003537 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003538 psa_status_t status;
3539 psa_key_type_t key_type = key_type_arg;
3540 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003541 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003542 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003543 size_t output_buffer_size = 0;
3544 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003545 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003546 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003547 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003548
Gilles Peskine8817f612018-12-18 00:18:46 +01003549 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003550
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003551 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3552 psa_set_key_algorithm( &attributes, alg );
3553 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003554
Gilles Peskine73676cb2019-05-15 20:15:10 +02003555 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003556
Gilles Peskine8817f612018-12-18 00:18:46 +01003557 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3558 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003559
Steven Cooreman177deba2020-09-07 17:14:14 +02003560 if( iv->len > 0 )
3561 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003562 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003563 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003564
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003565 output_buffer_size = ( (size_t) input->len +
3566 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003567 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003568
Gilles Peskine8817f612018-12-18 00:18:46 +01003569 PSA_ASSERT( psa_cipher_update( &operation,
3570 input->x, input->len,
3571 output, output_buffer_size,
3572 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003573 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003574 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003575 output + total_output_length,
3576 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003577 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003578 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003579 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003580
3581 if( expected_status == PSA_SUCCESS )
3582 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003583 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003584 ASSERT_COMPARE( expected_output->x, expected_output->len,
3585 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003586 }
3587
Gilles Peskine50e586b2018-06-08 14:28:46 +02003588exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003589 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003590 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003591 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003592}
3593/* END_CASE */
3594
Gilles Peskine50e586b2018-06-08 14:28:46 +02003595/* BEGIN_CASE */
3596void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003597 data_t *key,
3598 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003599{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003600 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003601 psa_key_type_t key_type = key_type_arg;
3602 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003603 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003604 size_t iv_size = 16;
3605 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003606 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003607 size_t output1_size = 0;
3608 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003609 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003610 size_t output2_size = 0;
3611 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003612 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003613 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3614 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003615 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003616
Gilles Peskine8817f612018-12-18 00:18:46 +01003617 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003618
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003619 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3620 psa_set_key_algorithm( &attributes, alg );
3621 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003622
Gilles Peskine73676cb2019-05-15 20:15:10 +02003623 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003624
Gilles Peskine8817f612018-12-18 00:18:46 +01003625 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3626 handle, alg ) );
3627 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3628 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003629
Steven Cooreman177deba2020-09-07 17:14:14 +02003630 if( alg != PSA_ALG_ECB_NO_PADDING )
3631 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003632 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3633 iv, iv_size,
3634 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003635 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003636 output1_size = ( (size_t) input->len +
3637 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003638 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003639
Gilles Peskine8817f612018-12-18 00:18:46 +01003640 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3641 output1, output1_size,
3642 &output1_length ) );
3643 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003644 output1 + output1_length,
3645 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003646 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003647
Gilles Peskine048b7f02018-06-08 14:20:49 +02003648 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003649
Gilles Peskine8817f612018-12-18 00:18:46 +01003650 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003651
3652 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003653 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003654
Steven Cooreman177deba2020-09-07 17:14:14 +02003655 if( iv_length > 0 )
3656 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003657 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3658 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003659 }
3660
Gilles Peskine8817f612018-12-18 00:18:46 +01003661 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3662 output2, output2_size,
3663 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003664 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003665 PSA_ASSERT( psa_cipher_finish( &operation2,
3666 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003667 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003668 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003669
Gilles Peskine048b7f02018-06-08 14:20:49 +02003670 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003671
Gilles Peskine8817f612018-12-18 00:18:46 +01003672 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003673
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003674 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003675
3676exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003677 mbedtls_free( output1 );
3678 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003679 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003680 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003681}
3682/* END_CASE */
3683
3684/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003685void cipher_verify_output_multipart( int alg_arg,
3686 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003687 data_t *key,
3688 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003689 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003690{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003691 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003692 psa_key_type_t key_type = key_type_arg;
3693 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003694 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003695 unsigned char iv[16] = {0};
3696 size_t iv_size = 16;
3697 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003698 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003699 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003700 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003701 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003702 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003703 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003704 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003705 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3706 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003708
Gilles Peskine8817f612018-12-18 00:18:46 +01003709 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003710
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003711 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3712 psa_set_key_algorithm( &attributes, alg );
3713 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003714
Gilles Peskine73676cb2019-05-15 20:15:10 +02003715 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003716
Gilles Peskine8817f612018-12-18 00:18:46 +01003717 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3718 handle, alg ) );
3719 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3720 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003721
Steven Cooreman177deba2020-09-07 17:14:14 +02003722 if( alg != PSA_ALG_ECB_NO_PADDING )
3723 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003724 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3725 iv, iv_size,
3726 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003727 }
3728
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003729 output1_buffer_size = ( (size_t) input->len +
3730 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003731 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003732
Gilles Peskinee0866522019-02-19 19:44:00 +01003733 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003734
Gilles Peskine8817f612018-12-18 00:18:46 +01003735 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3736 output1, output1_buffer_size,
3737 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003738 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003739
Gilles Peskine8817f612018-12-18 00:18:46 +01003740 PSA_ASSERT( psa_cipher_update( &operation1,
3741 input->x + first_part_size,
3742 input->len - first_part_size,
3743 output1, output1_buffer_size,
3744 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003745 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003746
Gilles Peskine8817f612018-12-18 00:18:46 +01003747 PSA_ASSERT( psa_cipher_finish( &operation1,
3748 output1 + output1_length,
3749 output1_buffer_size - output1_length,
3750 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003751 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003752
Gilles Peskine8817f612018-12-18 00:18:46 +01003753 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003754
Gilles Peskine048b7f02018-06-08 14:20:49 +02003755 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003756 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003757
Steven Cooreman177deba2020-09-07 17:14:14 +02003758 if( iv_length > 0 )
3759 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003760 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3761 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003762 }
Moran Pekerded84402018-06-06 16:36:50 +03003763
Gilles Peskine8817f612018-12-18 00:18:46 +01003764 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3765 output2, output2_buffer_size,
3766 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003767 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003768
Gilles Peskine8817f612018-12-18 00:18:46 +01003769 PSA_ASSERT( psa_cipher_update( &operation2,
3770 output1 + first_part_size,
3771 output1_length - first_part_size,
3772 output2, output2_buffer_size,
3773 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003774 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003775
Gilles Peskine8817f612018-12-18 00:18:46 +01003776 PSA_ASSERT( psa_cipher_finish( &operation2,
3777 output2 + output2_length,
3778 output2_buffer_size - output2_length,
3779 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003780 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003781
Gilles Peskine8817f612018-12-18 00:18:46 +01003782 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003783
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003784 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003785
3786exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003787 mbedtls_free( output1 );
3788 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003789 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003790 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003791}
3792/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003793
Gilles Peskine20035e32018-02-03 22:44:14 +01003794/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003795void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003796 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003797 data_t *nonce,
3798 data_t *additional_data,
3799 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003800 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003801{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003802 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003803 psa_key_type_t key_type = key_type_arg;
3804 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003805 unsigned char *output_data = NULL;
3806 size_t output_size = 0;
3807 size_t output_length = 0;
3808 unsigned char *output_data2 = NULL;
3809 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003810 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003811 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003812 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003813
Gilles Peskine4abf7412018-06-18 16:35:34 +02003814 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003815 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3816 * should be exact. */
3817 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3818 TEST_EQUAL( output_size,
3819 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003820 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003821
Gilles Peskine8817f612018-12-18 00:18:46 +01003822 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003823
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003824 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3825 psa_set_key_algorithm( &attributes, alg );
3826 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003827
Gilles Peskine049c7532019-05-15 20:22:09 +02003828 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3829 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003830
Gilles Peskinefe11b722018-12-18 00:24:04 +01003831 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3832 nonce->x, nonce->len,
3833 additional_data->x,
3834 additional_data->len,
3835 input_data->x, input_data->len,
3836 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003837 &output_length ),
3838 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003839
3840 if( PSA_SUCCESS == expected_result )
3841 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003842 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003843
Gilles Peskine003a4a92019-05-14 16:09:40 +02003844 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3845 * should be exact. */
3846 TEST_EQUAL( input_data->len,
3847 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3848
Gilles Peskinefe11b722018-12-18 00:24:04 +01003849 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3850 nonce->x, nonce->len,
3851 additional_data->x,
3852 additional_data->len,
3853 output_data, output_length,
3854 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003855 &output_length2 ),
3856 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003857
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003858 ASSERT_COMPARE( input_data->x, input_data->len,
3859 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003860 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003861
Gilles Peskinea1cac842018-06-11 19:33:02 +02003862exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003863 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003864 mbedtls_free( output_data );
3865 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003866 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003867}
3868/* END_CASE */
3869
3870/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003871void aead_encrypt( int key_type_arg, data_t *key_data,
3872 int alg_arg,
3873 data_t *nonce,
3874 data_t *additional_data,
3875 data_t *input_data,
3876 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003877{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003878 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003879 psa_key_type_t key_type = key_type_arg;
3880 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003881 unsigned char *output_data = NULL;
3882 size_t output_size = 0;
3883 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003884 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003886
Gilles Peskine4abf7412018-06-18 16:35:34 +02003887 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003888 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3889 * should be exact. */
3890 TEST_EQUAL( output_size,
3891 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003892 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003893
Gilles Peskine8817f612018-12-18 00:18:46 +01003894 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003895
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003896 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3897 psa_set_key_algorithm( &attributes, alg );
3898 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003899
Gilles Peskine049c7532019-05-15 20:22:09 +02003900 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3901 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003902
Gilles Peskine8817f612018-12-18 00:18:46 +01003903 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3904 nonce->x, nonce->len,
3905 additional_data->x, additional_data->len,
3906 input_data->x, input_data->len,
3907 output_data, output_size,
3908 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003909
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003910 ASSERT_COMPARE( expected_result->x, expected_result->len,
3911 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003912
Gilles Peskinea1cac842018-06-11 19:33:02 +02003913exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003914 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003915 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003916 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003917}
3918/* END_CASE */
3919
3920/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003921void aead_decrypt( int key_type_arg, data_t *key_data,
3922 int alg_arg,
3923 data_t *nonce,
3924 data_t *additional_data,
3925 data_t *input_data,
3926 data_t *expected_data,
3927 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003928{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003929 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003930 psa_key_type_t key_type = key_type_arg;
3931 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003932 unsigned char *output_data = NULL;
3933 size_t output_size = 0;
3934 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003935 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003937 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003938
Gilles Peskine003a4a92019-05-14 16:09:40 +02003939 output_size = input_data->len - tag_length;
3940 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3941 * should be exact. */
3942 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3943 TEST_EQUAL( output_size,
3944 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003945 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003946
Gilles Peskine8817f612018-12-18 00:18:46 +01003947 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003948
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003949 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3950 psa_set_key_algorithm( &attributes, alg );
3951 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003952
Gilles Peskine049c7532019-05-15 20:22:09 +02003953 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3954 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003955
Gilles Peskinefe11b722018-12-18 00:24:04 +01003956 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3957 nonce->x, nonce->len,
3958 additional_data->x,
3959 additional_data->len,
3960 input_data->x, input_data->len,
3961 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003962 &output_length ),
3963 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003964
Gilles Peskine2d277862018-06-18 15:41:12 +02003965 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003966 ASSERT_COMPARE( expected_data->x, expected_data->len,
3967 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003968
Gilles Peskinea1cac842018-06-11 19:33:02 +02003969exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003970 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003971 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003972 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003973}
3974/* END_CASE */
3975
3976/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003977void signature_size( int type_arg,
3978 int bits,
3979 int alg_arg,
3980 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003981{
3982 psa_key_type_t type = type_arg;
3983 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003984 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003985
Gilles Peskinefe11b722018-12-18 00:24:04 +01003986 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003987#if defined(MBEDTLS_TEST_DEPRECATED)
3988 TEST_EQUAL( actual_size,
3989 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3990#endif /* MBEDTLS_TEST_DEPRECATED */
3991
Gilles Peskinee59236f2018-01-27 23:32:46 +01003992exit:
3993 ;
3994}
3995/* END_CASE */
3996
3997/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003998void sign_deterministic( int key_type_arg, data_t *key_data,
3999 int alg_arg, data_t *input_data,
4000 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004001{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004002 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004003 psa_key_type_t key_type = key_type_arg;
4004 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004005 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004006 unsigned char *signature = NULL;
4007 size_t signature_size;
4008 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004010
Gilles Peskine8817f612018-12-18 00:18:46 +01004011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004012
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004013 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004014 psa_set_key_algorithm( &attributes, alg );
4015 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004016
Gilles Peskine049c7532019-05-15 20:22:09 +02004017 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4018 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004019 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4020 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004021
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004022 /* Allocate a buffer which has the size advertized by the
4023 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004024 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004025 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004026 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004027 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004028 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004029
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004030 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004031 PSA_ASSERT( psa_sign_hash( handle, alg,
4032 input_data->x, input_data->len,
4033 signature, signature_size,
4034 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004035 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004036 ASSERT_COMPARE( output_data->x, output_data->len,
4037 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004038
Gilles Peskine0627f982019-11-26 19:12:16 +01004039#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004040 memset( signature, 0, signature_size );
4041 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01004042 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
4043 input_data->x, input_data->len,
4044 signature, signature_size,
4045 &signature_length ) );
4046 ASSERT_COMPARE( output_data->x, output_data->len,
4047 signature, signature_length );
4048#endif /* MBEDTLS_TEST_DEPRECATED */
4049
Gilles Peskine20035e32018-02-03 22:44:14 +01004050exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004051 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004052 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01004053 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004054 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004055}
4056/* END_CASE */
4057
4058/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004059void sign_fail( int key_type_arg, data_t *key_data,
4060 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004061 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004062{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004063 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01004064 psa_key_type_t key_type = key_type_arg;
4065 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004066 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004067 psa_status_t actual_status;
4068 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004069 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004070 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004072
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004073 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004074
Gilles Peskine8817f612018-12-18 00:18:46 +01004075 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004076
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004077 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004078 psa_set_key_algorithm( &attributes, alg );
4079 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004080
Gilles Peskine049c7532019-05-15 20:22:09 +02004081 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4082 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004083
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004084 actual_status = psa_sign_hash( handle, alg,
4085 input_data->x, input_data->len,
4086 signature, signature_size,
4087 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004088 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004089 /* The value of *signature_length is unspecified on error, but
4090 * whatever it is, it should be less than signature_size, so that
4091 * if the caller tries to read *signature_length bytes without
4092 * checking the error code then they don't overflow a buffer. */
4093 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004094
Gilles Peskine895242b2019-11-29 12:15:40 +01004095#if defined(MBEDTLS_TEST_DEPRECATED)
4096 signature_length = INVALID_EXPORT_LENGTH;
4097 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
4098 input_data->x, input_data->len,
4099 signature, signature_size,
4100 &signature_length ),
4101 expected_status );
4102 TEST_ASSERT( signature_length <= signature_size );
4103#endif /* MBEDTLS_TEST_DEPRECATED */
4104
Gilles Peskine20035e32018-02-03 22:44:14 +01004105exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004106 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004107 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01004108 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004109 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004110}
4111/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004112
4113/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004114void sign_verify( int key_type_arg, data_t *key_data,
4115 int alg_arg, data_t *input_data )
4116{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004117 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02004118 psa_key_type_t key_type = key_type_arg;
4119 psa_algorithm_t alg = alg_arg;
4120 size_t key_bits;
4121 unsigned char *signature = NULL;
4122 size_t signature_size;
4123 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004125
Gilles Peskine8817f612018-12-18 00:18:46 +01004126 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004127
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004128 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004129 psa_set_key_algorithm( &attributes, alg );
4130 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004131
Gilles Peskine049c7532019-05-15 20:22:09 +02004132 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4133 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004134 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4135 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004136
4137 /* Allocate a buffer which has the size advertized by the
4138 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004139 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004140 key_bits, alg );
4141 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004142 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004143 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004144
4145 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004146 PSA_ASSERT( psa_sign_hash( handle, alg,
4147 input_data->x, input_data->len,
4148 signature, signature_size,
4149 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004150 /* Check that the signature length looks sensible. */
4151 TEST_ASSERT( signature_length <= signature_size );
4152 TEST_ASSERT( signature_length > 0 );
4153
4154 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004155 PSA_ASSERT( psa_verify_hash( handle, alg,
4156 input_data->x, input_data->len,
4157 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004158
4159 if( input_data->len != 0 )
4160 {
4161 /* Flip a bit in the input and verify that the signature is now
4162 * detected as invalid. Flip a bit at the beginning, not at the end,
4163 * because ECDSA may ignore the last few bits of the input. */
4164 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004165 TEST_EQUAL( psa_verify_hash( handle, alg,
4166 input_data->x, input_data->len,
4167 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004168 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004169 }
4170
4171exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004172 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004173 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004174 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004175 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004176}
4177/* END_CASE */
4178
4179/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004180void asymmetric_verify( int key_type_arg, data_t *key_data,
4181 int alg_arg, data_t *hash_data,
4182 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004183{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004184 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004185 psa_key_type_t key_type = key_type_arg;
4186 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004188
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004189 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004190
Gilles Peskine8817f612018-12-18 00:18:46 +01004191 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004192
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004193 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004194 psa_set_key_algorithm( &attributes, alg );
4195 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004196
Gilles Peskine049c7532019-05-15 20:22:09 +02004197 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4198 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004199
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004200 PSA_ASSERT( psa_verify_hash( handle, alg,
4201 hash_data->x, hash_data->len,
4202 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004203
4204#if defined(MBEDTLS_TEST_DEPRECATED)
4205 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4206 hash_data->x, hash_data->len,
4207 signature_data->x,
4208 signature_data->len ) );
4209
4210#endif /* MBEDTLS_TEST_DEPRECATED */
4211
itayzafrir5c753392018-05-08 11:18:38 +03004212exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004213 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004214 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004215 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004216}
4217/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004218
4219/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004220void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4221 int alg_arg, data_t *hash_data,
4222 data_t *signature_data,
4223 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004224{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004225 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004226 psa_key_type_t key_type = key_type_arg;
4227 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004228 psa_status_t actual_status;
4229 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004231
Gilles Peskine8817f612018-12-18 00:18:46 +01004232 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004233
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004235 psa_set_key_algorithm( &attributes, alg );
4236 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004237
Gilles Peskine049c7532019-05-15 20:22:09 +02004238 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4239 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004240
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004241 actual_status = psa_verify_hash( handle, alg,
4242 hash_data->x, hash_data->len,
4243 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004244 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004245
Gilles Peskine895242b2019-11-29 12:15:40 +01004246#if defined(MBEDTLS_TEST_DEPRECATED)
4247 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4248 hash_data->x, hash_data->len,
4249 signature_data->x, signature_data->len ),
4250 expected_status );
4251#endif /* MBEDTLS_TEST_DEPRECATED */
4252
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004253exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004254 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004255 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004256 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004257}
4258/* END_CASE */
4259
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004260/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004261void asymmetric_encrypt( int key_type_arg,
4262 data_t *key_data,
4263 int alg_arg,
4264 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004265 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004266 int expected_output_length_arg,
4267 int expected_status_arg )
4268{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004269 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004270 psa_key_type_t key_type = key_type_arg;
4271 psa_algorithm_t alg = alg_arg;
4272 size_t expected_output_length = expected_output_length_arg;
4273 size_t key_bits;
4274 unsigned char *output = NULL;
4275 size_t output_size;
4276 size_t output_length = ~0;
4277 psa_status_t actual_status;
4278 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004280
Gilles Peskine8817f612018-12-18 00:18:46 +01004281 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004282
Gilles Peskine656896e2018-06-29 19:12:28 +02004283 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004284 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4285 psa_set_key_algorithm( &attributes, alg );
4286 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004287 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4288 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004289
4290 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004291 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4292 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004293 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004294 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004295
4296 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004297 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004298 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004299 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004300 output, output_size,
4301 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004302 TEST_EQUAL( actual_status, expected_status );
4303 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004304
Gilles Peskine68428122018-06-30 18:42:41 +02004305 /* If the label is empty, the test framework puts a non-null pointer
4306 * in label->x. Test that a null pointer works as well. */
4307 if( label->len == 0 )
4308 {
4309 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004310 if( output_size != 0 )
4311 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004312 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004313 input_data->x, input_data->len,
4314 NULL, label->len,
4315 output, output_size,
4316 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004317 TEST_EQUAL( actual_status, expected_status );
4318 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004319 }
4320
Gilles Peskine656896e2018-06-29 19:12:28 +02004321exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004322 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004323 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004324 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004325 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004326}
4327/* END_CASE */
4328
4329/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004330void asymmetric_encrypt_decrypt( int key_type_arg,
4331 data_t *key_data,
4332 int alg_arg,
4333 data_t *input_data,
4334 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004335{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004336 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004337 psa_key_type_t key_type = key_type_arg;
4338 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004339 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004341 size_t output_size;
4342 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004343 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004344 size_t output2_size;
4345 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004347
Gilles Peskine8817f612018-12-18 00:18:46 +01004348 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004349
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004350 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4351 psa_set_key_algorithm( &attributes, alg );
4352 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004353
Gilles Peskine049c7532019-05-15 20:22:09 +02004354 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4355 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004356
4357 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004358 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4359 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004360 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004361 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004362 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004363 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004364
Gilles Peskineeebd7382018-06-08 18:11:54 +02004365 /* We test encryption by checking that encrypt-then-decrypt gives back
4366 * the original plaintext because of the non-optional random
4367 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004368 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4369 input_data->x, input_data->len,
4370 label->x, label->len,
4371 output, output_size,
4372 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004373 /* We don't know what ciphertext length to expect, but check that
4374 * it looks sensible. */
4375 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004376
Gilles Peskine8817f612018-12-18 00:18:46 +01004377 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4378 output, output_length,
4379 label->x, label->len,
4380 output2, output2_size,
4381 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004382 ASSERT_COMPARE( input_data->x, input_data->len,
4383 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004384
4385exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004386 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004387 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004388 mbedtls_free( output );
4389 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004390 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004391}
4392/* END_CASE */
4393
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004394/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004395void asymmetric_decrypt( int key_type_arg,
4396 data_t *key_data,
4397 int alg_arg,
4398 data_t *input_data,
4399 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004400 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004401{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004402 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004403 psa_key_type_t key_type = key_type_arg;
4404 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004405 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004406 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004407 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004409
Jaeden Amero412654a2019-02-06 12:57:46 +00004410 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004411 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004412
Gilles Peskine8817f612018-12-18 00:18:46 +01004413 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004414
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4416 psa_set_key_algorithm( &attributes, alg );
4417 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004418
Gilles Peskine049c7532019-05-15 20:22:09 +02004419 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4420 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004421
Gilles Peskine8817f612018-12-18 00:18:46 +01004422 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4423 input_data->x, input_data->len,
4424 label->x, label->len,
4425 output,
4426 output_size,
4427 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004428 ASSERT_COMPARE( expected_data->x, expected_data->len,
4429 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004430
Gilles Peskine68428122018-06-30 18:42:41 +02004431 /* If the label is empty, the test framework puts a non-null pointer
4432 * in label->x. Test that a null pointer works as well. */
4433 if( label->len == 0 )
4434 {
4435 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004436 if( output_size != 0 )
4437 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004438 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4439 input_data->x, input_data->len,
4440 NULL, label->len,
4441 output,
4442 output_size,
4443 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004444 ASSERT_COMPARE( expected_data->x, expected_data->len,
4445 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004446 }
4447
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004448exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004449 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004450 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004451 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004452 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004453}
4454/* END_CASE */
4455
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004456/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004457void asymmetric_decrypt_fail( int key_type_arg,
4458 data_t *key_data,
4459 int alg_arg,
4460 data_t *input_data,
4461 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004462 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004463 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004464{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004465 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004466 psa_key_type_t key_type = key_type_arg;
4467 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004468 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004469 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004470 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004471 psa_status_t actual_status;
4472 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004474
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004475 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004476
Gilles Peskine8817f612018-12-18 00:18:46 +01004477 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004478
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004479 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4480 psa_set_key_algorithm( &attributes, alg );
4481 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004482
Gilles Peskine049c7532019-05-15 20:22:09 +02004483 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4484 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004485
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004486 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004487 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004488 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004489 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004490 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004491 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004492 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004493
Gilles Peskine68428122018-06-30 18:42:41 +02004494 /* If the label is empty, the test framework puts a non-null pointer
4495 * in label->x. Test that a null pointer works as well. */
4496 if( label->len == 0 )
4497 {
4498 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004499 if( output_size != 0 )
4500 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004501 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004502 input_data->x, input_data->len,
4503 NULL, label->len,
4504 output, output_size,
4505 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004506 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004507 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004508 }
4509
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004510exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004511 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004512 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004513 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004514 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004515}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004516/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004517
4518/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004519void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004520{
4521 /* Test each valid way of initializing the object, except for `= {0}`, as
4522 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4523 * though it's OK by the C standard. We could test for this, but we'd need
4524 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004525 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004526 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4527 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4528 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004529
4530 memset( &zero, 0, sizeof( zero ) );
4531
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004533 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004534 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004535 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004536 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004537 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004538 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004539
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004540 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004541 PSA_ASSERT( psa_key_derivation_abort(&func) );
4542 PSA_ASSERT( psa_key_derivation_abort(&init) );
4543 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004544}
4545/* END_CASE */
4546
Janos Follath16de4a42019-06-13 16:32:24 +01004547/* BEGIN_CASE */
4548void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004549{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004550 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004551 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004552 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004553
Gilles Peskine8817f612018-12-18 00:18:46 +01004554 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004555
Janos Follath16de4a42019-06-13 16:32:24 +01004556 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004557 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004558
4559exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004560 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004561 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004562}
4563/* END_CASE */
4564
Janos Follathaf3c2a02019-06-12 12:34:34 +01004565/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004566void derive_set_capacity( int alg_arg, int capacity_arg,
4567 int expected_status_arg )
4568{
4569 psa_algorithm_t alg = alg_arg;
4570 size_t capacity = capacity_arg;
4571 psa_status_t expected_status = expected_status_arg;
4572 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4573
4574 PSA_ASSERT( psa_crypto_init( ) );
4575
4576 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4577
4578 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4579 expected_status );
4580
4581exit:
4582 psa_key_derivation_abort( &operation );
4583 PSA_DONE( );
4584}
4585/* END_CASE */
4586
4587/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004588void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004589 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004590 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004591 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004592 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004593 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004594 int expected_status_arg3,
4595 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004596{
4597 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004598 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4599 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004600 psa_status_t expected_statuses[] = {expected_status_arg1,
4601 expected_status_arg2,
4602 expected_status_arg3};
4603 data_t *inputs[] = {input1, input2, input3};
4604 psa_key_handle_t handles[] = {0, 0, 0};
4605 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4606 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4607 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004608 psa_key_type_t output_key_type = output_key_type_arg;
4609 psa_key_handle_t output_handle = 0;
4610 psa_status_t expected_output_status = expected_output_status_arg;
4611 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004612
4613 PSA_ASSERT( psa_crypto_init( ) );
4614
4615 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4616 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004617
4618 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4619
4620 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4621 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004622 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004623 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004624 psa_set_key_type( &attributes, key_types[i] );
4625 PSA_ASSERT( psa_import_key( &attributes,
4626 inputs[i]->x, inputs[i]->len,
4627 &handles[i] ) );
4628 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4629 handles[i] ),
4630 expected_statuses[i] );
4631 }
4632 else
4633 {
4634 TEST_EQUAL( psa_key_derivation_input_bytes(
4635 &operation, steps[i],
4636 inputs[i]->x, inputs[i]->len ),
4637 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004638 }
4639 }
4640
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004641 if( output_key_type != PSA_KEY_TYPE_NONE )
4642 {
4643 psa_reset_key_attributes( &attributes );
4644 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4645 psa_set_key_bits( &attributes, 8 );
4646 actual_output_status =
4647 psa_key_derivation_output_key( &attributes, &operation,
4648 &output_handle );
4649 }
4650 else
4651 {
4652 uint8_t buffer[1];
4653 actual_output_status =
4654 psa_key_derivation_output_bytes( &operation,
4655 buffer, sizeof( buffer ) );
4656 }
4657 TEST_EQUAL( actual_output_status, expected_output_status );
4658
Janos Follathaf3c2a02019-06-12 12:34:34 +01004659exit:
4660 psa_key_derivation_abort( &operation );
4661 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4662 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004663 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004664 PSA_DONE( );
4665}
4666/* END_CASE */
4667
Janos Follathd958bb72019-07-03 15:02:16 +01004668/* BEGIN_CASE */
4669void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004670{
Janos Follathd958bb72019-07-03 15:02:16 +01004671 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004672 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004673 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004675 unsigned char input1[] = "Input 1";
4676 size_t input1_length = sizeof( input1 );
4677 unsigned char input2[] = "Input 2";
4678 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004679 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004680 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004681 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4682 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4683 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004685
Gilles Peskine8817f612018-12-18 00:18:46 +01004686 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004687
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004688 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4689 psa_set_key_algorithm( &attributes, alg );
4690 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004691
Gilles Peskine73676cb2019-05-15 20:15:10 +02004692 PSA_ASSERT( psa_import_key( &attributes,
4693 key_data, sizeof( key_data ),
4694 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004695
4696 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004697 if( !setup_key_derivation_wrap( &operation, handle, alg,
4698 input1, input1_length,
4699 input2, input2_length,
4700 capacity ) )
4701 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004702
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004704 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004705 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004706
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004707 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004708
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004710 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004711
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004712exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004713 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004714 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004715 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004716}
4717/* END_CASE */
4718
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004719/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004720void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004721{
4722 uint8_t output_buffer[16];
4723 size_t buffer_size = 16;
4724 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004726
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004727 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4728 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004729 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004730
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004732 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004733
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004735
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004736 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4737 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004738 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004739
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004741 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004742
4743exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004745}
4746/* END_CASE */
4747
4748/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004749void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004750 int step1_arg, data_t *input1,
4751 int step2_arg, data_t *input2,
4752 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004753 int requested_capacity_arg,
4754 data_t *expected_output1,
4755 data_t *expected_output2 )
4756{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004757 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004758 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4759 data_t *inputs[] = {input1, input2, input3};
4760 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004761 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004762 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004763 uint8_t *expected_outputs[2] =
4764 {expected_output1->x, expected_output2->x};
4765 size_t output_sizes[2] =
4766 {expected_output1->len, expected_output2->len};
4767 size_t output_buffer_size = 0;
4768 uint8_t *output_buffer = NULL;
4769 size_t expected_capacity;
4770 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004772 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004773 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004774
4775 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4776 {
4777 if( output_sizes[i] > output_buffer_size )
4778 output_buffer_size = output_sizes[i];
4779 if( output_sizes[i] == 0 )
4780 expected_outputs[i] = NULL;
4781 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004782 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004783 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004784
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004785 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4786 psa_set_key_algorithm( &attributes, alg );
4787 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004788
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004789 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004790 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4791 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4792 requested_capacity ) );
4793 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004794 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004795 switch( steps[i] )
4796 {
4797 case 0:
4798 break;
4799 case PSA_KEY_DERIVATION_INPUT_SECRET:
4800 PSA_ASSERT( psa_import_key( &attributes,
4801 inputs[i]->x, inputs[i]->len,
4802 &handles[i] ) );
4803 PSA_ASSERT( psa_key_derivation_input_key(
4804 &operation, steps[i],
4805 handles[i] ) );
4806 break;
4807 default:
4808 PSA_ASSERT( psa_key_derivation_input_bytes(
4809 &operation, steps[i],
4810 inputs[i]->x, inputs[i]->len ) );
4811 break;
4812 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004813 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004814
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004816 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004817 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004818 expected_capacity = requested_capacity;
4819
4820 /* Expansion phase. */
4821 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4822 {
4823 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004825 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004826 if( expected_capacity == 0 && output_sizes[i] == 0 )
4827 {
4828 /* Reading 0 bytes when 0 bytes are available can go either way. */
4829 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004830 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004831 continue;
4832 }
4833 else if( expected_capacity == 0 ||
4834 output_sizes[i] > expected_capacity )
4835 {
4836 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004837 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004838 expected_capacity = 0;
4839 continue;
4840 }
4841 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004842 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004843 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004844 ASSERT_COMPARE( output_buffer, output_sizes[i],
4845 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004847 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004849 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004850 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004851 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004853
4854exit:
4855 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004857 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4858 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004859 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004860}
4861/* END_CASE */
4862
4863/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004864void derive_full( int alg_arg,
4865 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004866 data_t *input1,
4867 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004868 int requested_capacity_arg )
4869{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004870 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004871 psa_algorithm_t alg = alg_arg;
4872 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004873 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004874 unsigned char output_buffer[16];
4875 size_t expected_capacity = requested_capacity;
4876 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004878
Gilles Peskine8817f612018-12-18 00:18:46 +01004879 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004880
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004881 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4882 psa_set_key_algorithm( &attributes, alg );
4883 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004884
Gilles Peskine049c7532019-05-15 20:22:09 +02004885 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4886 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004887
Janos Follathf2815ea2019-07-03 12:41:36 +01004888 if( !setup_key_derivation_wrap( &operation, handle, alg,
4889 input1->x, input1->len,
4890 input2->x, input2->len,
4891 requested_capacity ) )
4892 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004893
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004895 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004896 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004897
4898 /* Expansion phase. */
4899 while( current_capacity > 0 )
4900 {
4901 size_t read_size = sizeof( output_buffer );
4902 if( read_size > current_capacity )
4903 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004905 output_buffer,
4906 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004907 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004909 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004910 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004911 }
4912
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 /* Check that the operation refuses to go over capacity. */
4914 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004915 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004916
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004918
4919exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004921 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004922 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004923}
4924/* END_CASE */
4925
Janos Follathe60c9052019-07-03 13:51:30 +01004926/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004927void derive_key_exercise( int alg_arg,
4928 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004929 data_t *input1,
4930 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004931 int derived_type_arg,
4932 int derived_bits_arg,
4933 int derived_usage_arg,
4934 int derived_alg_arg )
4935{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004936 psa_key_handle_t base_handle = 0;
4937 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004938 psa_algorithm_t alg = alg_arg;
4939 psa_key_type_t derived_type = derived_type_arg;
4940 size_t derived_bits = derived_bits_arg;
4941 psa_key_usage_t derived_usage = derived_usage_arg;
4942 psa_algorithm_t derived_alg = derived_alg_arg;
4943 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004944 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004946 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004947
Gilles Peskine8817f612018-12-18 00:18:46 +01004948 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004949
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004950 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4951 psa_set_key_algorithm( &attributes, alg );
4952 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004953 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4954 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004955
4956 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004957 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4958 input1->x, input1->len,
4959 input2->x, input2->len, capacity ) )
4960 goto exit;
4961
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004962 psa_set_key_usage_flags( &attributes, derived_usage );
4963 psa_set_key_algorithm( &attributes, derived_alg );
4964 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004965 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004967 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004968
4969 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004970 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4971 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4972 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004973
4974 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004975 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004976 goto exit;
4977
4978exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004980 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004981 psa_destroy_key( base_handle );
4982 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004983 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004984}
4985/* END_CASE */
4986
Janos Follath42fd8882019-07-03 14:17:09 +01004987/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004988void derive_key_export( int alg_arg,
4989 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004990 data_t *input1,
4991 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004992 int bytes1_arg,
4993 int bytes2_arg )
4994{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004995 psa_key_handle_t base_handle = 0;
4996 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004997 psa_algorithm_t alg = alg_arg;
4998 size_t bytes1 = bytes1_arg;
4999 size_t bytes2 = bytes2_arg;
5000 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005002 uint8_t *output_buffer = NULL;
5003 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005004 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5005 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005006 size_t length;
5007
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005008 ASSERT_ALLOC( output_buffer, capacity );
5009 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005010 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005011
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005012 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5013 psa_set_key_algorithm( &base_attributes, alg );
5014 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005015 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5016 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005017
5018 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01005019 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5020 input1->x, input1->len,
5021 input2->x, input2->len, capacity ) )
5022 goto exit;
5023
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005025 output_buffer,
5026 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005028
5029 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01005030 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5031 input1->x, input1->len,
5032 input2->x, input2->len, capacity ) )
5033 goto exit;
5034
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005035 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5036 psa_set_key_algorithm( &derived_attributes, 0 );
5037 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005038 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005039 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005040 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005041 PSA_ASSERT( psa_export_key( derived_handle,
5042 export_buffer, bytes1,
5043 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005044 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01005045 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005046 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005047 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005048 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005049 PSA_ASSERT( psa_export_key( derived_handle,
5050 export_buffer + bytes1, bytes2,
5051 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005052 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005053
5054 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005055 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5056 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005057
5058exit:
5059 mbedtls_free( output_buffer );
5060 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005061 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005062 psa_destroy_key( base_handle );
5063 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005064 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005065}
5066/* END_CASE */
5067
5068/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005069void derive_key( int alg_arg,
5070 data_t *key_data, data_t *input1, data_t *input2,
5071 int type_arg, int bits_arg,
5072 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005073{
5074 psa_key_handle_t base_handle = 0;
5075 psa_key_handle_t derived_handle = 0;
5076 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005077 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005078 size_t bits = bits_arg;
5079 psa_status_t expected_status = expected_status_arg;
5080 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5081 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5082 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5083
5084 PSA_ASSERT( psa_crypto_init( ) );
5085
5086 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5087 psa_set_key_algorithm( &base_attributes, alg );
5088 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5089 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5090 &base_handle ) );
5091
5092 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5093 input1->x, input1->len,
5094 input2->x, input2->len, SIZE_MAX ) )
5095 goto exit;
5096
5097 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5098 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005099 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005100 psa_set_key_bits( &derived_attributes, bits );
5101 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
5102 &derived_handle ),
5103 expected_status );
5104
5105exit:
5106 psa_key_derivation_abort( &operation );
5107 psa_destroy_key( base_handle );
5108 psa_destroy_key( derived_handle );
5109 PSA_DONE( );
5110}
5111/* END_CASE */
5112
5113/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005114void key_agreement_setup( int alg_arg,
5115 int our_key_type_arg, data_t *our_key_data,
5116 data_t *peer_key_data,
5117 int expected_status_arg )
5118{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005119 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005120 psa_algorithm_t alg = alg_arg;
5121 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005122 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005124 psa_status_t expected_status = expected_status_arg;
5125 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005126
Gilles Peskine8817f612018-12-18 00:18:46 +01005127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005128
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005129 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5130 psa_set_key_algorithm( &attributes, alg );
5131 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005132 PSA_ASSERT( psa_import_key( &attributes,
5133 our_key_data->x, our_key_data->len,
5134 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005135
Gilles Peskine77f40d82019-04-11 21:27:06 +02005136 /* The tests currently include inputs that should fail at either step.
5137 * Test cases that fail at the setup step should be changed to call
5138 * key_derivation_setup instead, and this function should be renamed
5139 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005141 if( status == PSA_SUCCESS )
5142 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005143 TEST_EQUAL( psa_key_derivation_key_agreement(
5144 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5145 our_key,
5146 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005147 expected_status );
5148 }
5149 else
5150 {
5151 TEST_ASSERT( status == expected_status );
5152 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005153
5154exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005156 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005157 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005158}
5159/* END_CASE */
5160
5161/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005162void raw_key_agreement( int alg_arg,
5163 int our_key_type_arg, data_t *our_key_data,
5164 data_t *peer_key_data,
5165 data_t *expected_output )
5166{
5167 psa_key_handle_t our_key = 0;
5168 psa_algorithm_t alg = alg_arg;
5169 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005171 unsigned char *output = NULL;
5172 size_t output_length = ~0;
5173
5174 ASSERT_ALLOC( output, expected_output->len );
5175 PSA_ASSERT( psa_crypto_init( ) );
5176
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005177 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5178 psa_set_key_algorithm( &attributes, alg );
5179 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005180 PSA_ASSERT( psa_import_key( &attributes,
5181 our_key_data->x, our_key_data->len,
5182 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005183
Gilles Peskinebe697d82019-05-16 18:00:41 +02005184 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5185 peer_key_data->x, peer_key_data->len,
5186 output, expected_output->len,
5187 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005188 ASSERT_COMPARE( output, output_length,
5189 expected_output->x, expected_output->len );
5190
5191exit:
5192 mbedtls_free( output );
5193 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005194 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005195}
5196/* END_CASE */
5197
5198/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005199void key_agreement_capacity( int alg_arg,
5200 int our_key_type_arg, data_t *our_key_data,
5201 data_t *peer_key_data,
5202 int expected_capacity_arg )
5203{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005204 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005205 psa_algorithm_t alg = alg_arg;
5206 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005209 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005210 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005211
Gilles Peskine8817f612018-12-18 00:18:46 +01005212 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005213
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005214 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5215 psa_set_key_algorithm( &attributes, alg );
5216 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005217 PSA_ASSERT( psa_import_key( &attributes,
5218 our_key_data->x, our_key_data->len,
5219 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005220
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005222 PSA_ASSERT( psa_key_derivation_key_agreement(
5223 &operation,
5224 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5225 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005226 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5227 {
5228 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005229 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005230 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005231 NULL, 0 ) );
5232 }
Gilles Peskine59685592018-09-18 12:11:34 +02005233
Gilles Peskinebf491972018-10-25 22:36:12 +02005234 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005235 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005236 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005237 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005238
Gilles Peskinebf491972018-10-25 22:36:12 +02005239 /* Test the actual capacity by reading the output. */
5240 while( actual_capacity > sizeof( output ) )
5241 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005243 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005244 actual_capacity -= sizeof( output );
5245 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005246 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005247 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005248 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005249 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005250
Gilles Peskine59685592018-09-18 12:11:34 +02005251exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005253 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005254 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005255}
5256/* END_CASE */
5257
5258/* BEGIN_CASE */
5259void key_agreement_output( int alg_arg,
5260 int our_key_type_arg, data_t *our_key_data,
5261 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005262 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005263{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005264 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005265 psa_algorithm_t alg = alg_arg;
5266 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005269 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005270
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005271 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5272 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005273
Gilles Peskine8817f612018-12-18 00:18:46 +01005274 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005275
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005276 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5277 psa_set_key_algorithm( &attributes, alg );
5278 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005279 PSA_ASSERT( psa_import_key( &attributes,
5280 our_key_data->x, our_key_data->len,
5281 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005282
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005283 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005284 PSA_ASSERT( psa_key_derivation_key_agreement(
5285 &operation,
5286 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5287 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005288 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5289 {
5290 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005291 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005292 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005293 NULL, 0 ) );
5294 }
Gilles Peskine59685592018-09-18 12:11:34 +02005295
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005296 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005297 actual_output,
5298 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005299 ASSERT_COMPARE( actual_output, expected_output1->len,
5300 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005301 if( expected_output2->len != 0 )
5302 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005303 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005304 actual_output,
5305 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005306 ASSERT_COMPARE( actual_output, expected_output2->len,
5307 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005308 }
Gilles Peskine59685592018-09-18 12:11:34 +02005309
5310exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005312 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005313 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005314 mbedtls_free( actual_output );
5315}
5316/* END_CASE */
5317
5318/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005319void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005320{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005321 size_t bytes = bytes_arg;
5322 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005323 unsigned char *output = NULL;
5324 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005325 size_t i;
5326 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005327
Simon Butcher49f8e312020-03-03 15:51:50 +00005328 TEST_ASSERT( bytes_arg >= 0 );
5329
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005330 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5331 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005332 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005333
Gilles Peskine8817f612018-12-18 00:18:46 +01005334 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005335
Gilles Peskinea50d7392018-06-21 10:22:13 +02005336 /* Run several times, to ensure that every output byte will be
5337 * nonzero at least once with overwhelming probability
5338 * (2^(-8*number_of_runs)). */
5339 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005340 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005341 if( bytes != 0 )
5342 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005343 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005344
5345 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005346 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5347 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005348
5349 for( i = 0; i < bytes; i++ )
5350 {
5351 if( output[i] != 0 )
5352 ++changed[i];
5353 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005354 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005355
5356 /* Check that every byte was changed to nonzero at least once. This
5357 * validates that psa_generate_random is overwriting every byte of
5358 * the output buffer. */
5359 for( i = 0; i < bytes; i++ )
5360 {
5361 TEST_ASSERT( changed[i] != 0 );
5362 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005363
5364exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005365 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005366 mbedtls_free( output );
5367 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005368}
5369/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005370
5371/* BEGIN_CASE */
5372void generate_key( int type_arg,
5373 int bits_arg,
5374 int usage_arg,
5375 int alg_arg,
5376 int expected_status_arg )
5377{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005378 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005379 psa_key_type_t type = type_arg;
5380 psa_key_usage_t usage = usage_arg;
5381 size_t bits = bits_arg;
5382 psa_algorithm_t alg = alg_arg;
5383 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005385 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005386
Gilles Peskine8817f612018-12-18 00:18:46 +01005387 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005388
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005389 psa_set_key_usage_flags( &attributes, usage );
5390 psa_set_key_algorithm( &attributes, alg );
5391 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005392 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005393
5394 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005395 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005396 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005397 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005398
5399 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005400 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5401 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5402 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005403
Gilles Peskine818ca122018-06-20 18:16:48 +02005404 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005405 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005406 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005407
5408exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005409 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005410 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005411 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005412}
5413/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005414
Gilles Peskinee56e8782019-04-26 17:34:02 +02005415/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5416void generate_key_rsa( int bits_arg,
5417 data_t *e_arg,
5418 int expected_status_arg )
5419{
5420 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005421 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005422 size_t bits = bits_arg;
5423 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5424 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5425 psa_status_t expected_status = expected_status_arg;
5426 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5427 uint8_t *exported = NULL;
5428 size_t exported_size =
5429 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5430 size_t exported_length = SIZE_MAX;
5431 uint8_t *e_read_buffer = NULL;
5432 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005433 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005434 size_t e_read_length = SIZE_MAX;
5435
5436 if( e_arg->len == 0 ||
5437 ( e_arg->len == 3 &&
5438 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5439 {
5440 is_default_public_exponent = 1;
5441 e_read_size = 0;
5442 }
5443 ASSERT_ALLOC( e_read_buffer, e_read_size );
5444 ASSERT_ALLOC( exported, exported_size );
5445
5446 PSA_ASSERT( psa_crypto_init( ) );
5447
5448 psa_set_key_usage_flags( &attributes, usage );
5449 psa_set_key_algorithm( &attributes, alg );
5450 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5451 e_arg->x, e_arg->len ) );
5452 psa_set_key_bits( &attributes, bits );
5453
5454 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005455 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005456 if( expected_status != PSA_SUCCESS )
5457 goto exit;
5458
5459 /* Test the key information */
5460 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5461 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5462 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5463 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5464 e_read_buffer, e_read_size,
5465 &e_read_length ) );
5466 if( is_default_public_exponent )
5467 TEST_EQUAL( e_read_length, 0 );
5468 else
5469 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5470
5471 /* Do something with the key according to its type and permitted usage. */
5472 if( ! exercise_key( handle, usage, alg ) )
5473 goto exit;
5474
5475 /* Export the key and check the public exponent. */
5476 PSA_ASSERT( psa_export_public_key( handle,
5477 exported, exported_size,
5478 &exported_length ) );
5479 {
5480 uint8_t *p = exported;
5481 uint8_t *end = exported + exported_length;
5482 size_t len;
5483 /* RSAPublicKey ::= SEQUENCE {
5484 * modulus INTEGER, -- n
5485 * publicExponent INTEGER } -- e
5486 */
5487 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005488 MBEDTLS_ASN1_SEQUENCE |
5489 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005490 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5491 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5492 MBEDTLS_ASN1_INTEGER ) );
5493 if( len >= 1 && p[0] == 0 )
5494 {
5495 ++p;
5496 --len;
5497 }
5498 if( e_arg->len == 0 )
5499 {
5500 TEST_EQUAL( len, 3 );
5501 TEST_EQUAL( p[0], 1 );
5502 TEST_EQUAL( p[1], 0 );
5503 TEST_EQUAL( p[2], 1 );
5504 }
5505 else
5506 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5507 }
5508
5509exit:
5510 psa_reset_key_attributes( &attributes );
5511 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005512 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005513 mbedtls_free( e_read_buffer );
5514 mbedtls_free( exported );
5515}
5516/* END_CASE */
5517
Darryl Greend49a4992018-06-18 17:27:26 +01005518/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005519void persistent_key_load_key_from_storage( data_t *data,
5520 int type_arg, int bits_arg,
5521 int usage_flags_arg, int alg_arg,
5522 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005523{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005524 psa_key_id_t key_id = 1;
5525 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005526 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005527 psa_key_handle_t base_key = 0;
5528 psa_key_type_t type = type_arg;
5529 size_t bits = bits_arg;
5530 psa_key_usage_t usage_flags = usage_flags_arg;
5531 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005532 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005533 unsigned char *first_export = NULL;
5534 unsigned char *second_export = NULL;
5535 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5536 size_t first_exported_length;
5537 size_t second_exported_length;
5538
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005539 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5540 {
5541 ASSERT_ALLOC( first_export, export_size );
5542 ASSERT_ALLOC( second_export, export_size );
5543 }
Darryl Greend49a4992018-06-18 17:27:26 +01005544
Gilles Peskine8817f612018-12-18 00:18:46 +01005545 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005546
Gilles Peskinec87af662019-05-15 16:12:22 +02005547 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005548 psa_set_key_usage_flags( &attributes, usage_flags );
5549 psa_set_key_algorithm( &attributes, alg );
5550 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005551 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005552
Darryl Green0c6575a2018-11-07 16:05:30 +00005553 switch( generation_method )
5554 {
5555 case IMPORT_KEY:
5556 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005557 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5558 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005559 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005560
Darryl Green0c6575a2018-11-07 16:05:30 +00005561 case GENERATE_KEY:
5562 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005563 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005564 break;
5565
5566 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005567 {
5568 /* Create base key */
5569 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5570 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5571 psa_set_key_usage_flags( &base_attributes,
5572 PSA_KEY_USAGE_DERIVE );
5573 psa_set_key_algorithm( &base_attributes, derive_alg );
5574 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005575 PSA_ASSERT( psa_import_key( &base_attributes,
5576 data->x, data->len,
5577 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005578 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005579 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005580 PSA_ASSERT( psa_key_derivation_input_key(
5581 &operation,
5582 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005583 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005584 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005585 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005586 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5587 &operation,
5588 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005589 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005590 PSA_ASSERT( psa_destroy_key( base_key ) );
5591 base_key = 0;
5592 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005593 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005594 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005595 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005596
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005597 /* Export the key if permitted by the key policy. */
5598 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5599 {
5600 PSA_ASSERT( psa_export_key( handle,
5601 first_export, export_size,
5602 &first_exported_length ) );
5603 if( generation_method == IMPORT_KEY )
5604 ASSERT_COMPARE( data->x, data->len,
5605 first_export, first_exported_length );
5606 }
Darryl Greend49a4992018-06-18 17:27:26 +01005607
5608 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005609 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005610 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005611 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005612
Darryl Greend49a4992018-06-18 17:27:26 +01005613 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005614 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005615 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5616 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5617 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5618 PSA_KEY_LIFETIME_PERSISTENT );
5619 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5620 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5621 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5622 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005623
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005624 /* Export the key again if permitted by the key policy. */
5625 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005626 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005627 PSA_ASSERT( psa_export_key( handle,
5628 second_export, export_size,
5629 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005630 ASSERT_COMPARE( first_export, first_exported_length,
5631 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005632 }
5633
5634 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005635 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005636 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005637
5638exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005639 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005640 mbedtls_free( first_export );
5641 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005642 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005643 psa_destroy_key( base_key );
5644 if( handle == 0 )
5645 {
5646 /* In case there was a test failure after creating the persistent key
5647 * but while it was not open, try to re-open the persistent key
5648 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005649 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005650 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005651 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005652 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005653}
5654/* END_CASE */