blob: ff79872e46189aa2c2169fff65a555e506a8aaf8 [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
Paul Elliott8ff510a2020-06-02 17:19:28 +01001262 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1263 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1264 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1265 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1266 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1267 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1268 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1269 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1270 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1271 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1272 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1273 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1274 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1275 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1276 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1277 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1278 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1279 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1280 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1281 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1282 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1283 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1284 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1285 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1286 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1287 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1288 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1289 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1290 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1291 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1292
1293 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1294 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1295 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1296 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1297 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1298 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1299 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1300 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001301
1302 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_GROUP_RFC7919 );
1303 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_GROUP_RFC7919 );
1304 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_GROUP_RFC7919 );
1305 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_GROUP_RFC7919 );
1306 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_GROUP_RFC7919 );
1307#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001308}
1309/* END_CASE */
1310
1311/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001312void attributes_set_get( int id_arg, int lifetime_arg,
1313 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001314 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315{
Gilles Peskine4747d192019-04-17 15:05:45 +02001316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001317 psa_key_id_t id = id_arg;
1318 psa_key_lifetime_t lifetime = lifetime_arg;
1319 psa_key_usage_t usage_flags = usage_flags_arg;
1320 psa_algorithm_t alg = alg_arg;
1321 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001322 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001323
1324 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1325 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1326 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1327 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1328 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001329 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001330
Gilles Peskinec87af662019-05-15 16:12:22 +02001331 psa_set_key_id( &attributes, id );
1332 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001333 psa_set_key_usage_flags( &attributes, usage_flags );
1334 psa_set_key_algorithm( &attributes, alg );
1335 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001336 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001337
1338 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1339 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1340 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1341 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1342 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001343 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001344
1345 psa_reset_key_attributes( &attributes );
1346
1347 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1348 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1349 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1350 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1351 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001352 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001353}
1354/* END_CASE */
1355
1356/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001357void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1358 int expected_id_arg, int expected_lifetime_arg )
1359{
1360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1361 psa_key_id_t id1 = id1_arg;
1362 psa_key_lifetime_t lifetime = lifetime_arg;
1363 psa_key_id_t id2 = id2_arg;
1364 psa_key_id_t expected_id = expected_id_arg;
1365 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1366
1367 if( id1_arg != -1 )
1368 psa_set_key_id( &attributes, id1 );
1369 if( lifetime_arg != -1 )
1370 psa_set_key_lifetime( &attributes, lifetime );
1371 if( id2_arg != -1 )
1372 psa_set_key_id( &attributes, id2 );
1373
1374 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1375 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1376}
1377/* END_CASE */
1378
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001379/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1380void slot_number_attribute( )
1381{
1382 psa_key_slot_number_t slot_number = 0xdeadbeef;
1383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1384
1385 /* Initially, there is no slot number. */
1386 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1387 PSA_ERROR_INVALID_ARGUMENT );
1388
1389 /* Test setting a slot number. */
1390 psa_set_key_slot_number( &attributes, 0 );
1391 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1392 TEST_EQUAL( slot_number, 0 );
1393
1394 /* Test changing the slot number. */
1395 psa_set_key_slot_number( &attributes, 42 );
1396 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1397 TEST_EQUAL( slot_number, 42 );
1398
1399 /* Test clearing the slot number. */
1400 psa_clear_key_slot_number( &attributes );
1401 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1402 PSA_ERROR_INVALID_ARGUMENT );
1403
1404 /* Clearing again should have no effect. */
1405 psa_clear_key_slot_number( &attributes );
1406 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1407 PSA_ERROR_INVALID_ARGUMENT );
1408
1409 /* Test that reset clears the slot number. */
1410 psa_set_key_slot_number( &attributes, 42 );
1411 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1412 TEST_EQUAL( slot_number, 42 );
1413 psa_reset_key_attributes( &attributes );
1414 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1415 PSA_ERROR_INVALID_ARGUMENT );
1416}
1417/* END_CASE */
1418
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001419/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001420void import_with_policy( int type_arg,
1421 int usage_arg, int alg_arg,
1422 int expected_status_arg )
1423{
1424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1425 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1426 psa_key_handle_t handle = 0;
1427 psa_key_type_t type = type_arg;
1428 psa_key_usage_t usage = usage_arg;
1429 psa_algorithm_t alg = alg_arg;
1430 psa_status_t expected_status = expected_status_arg;
1431 const uint8_t key_material[16] = {0};
1432 psa_status_t status;
1433
1434 PSA_ASSERT( psa_crypto_init( ) );
1435
1436 psa_set_key_type( &attributes, type );
1437 psa_set_key_usage_flags( &attributes, usage );
1438 psa_set_key_algorithm( &attributes, alg );
1439
1440 status = psa_import_key( &attributes,
1441 key_material, sizeof( key_material ),
1442 &handle );
1443 TEST_EQUAL( status, expected_status );
1444 if( status != PSA_SUCCESS )
1445 goto exit;
1446
1447 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1448 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1449 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1450 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001451 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001452
1453 PSA_ASSERT( psa_destroy_key( handle ) );
1454 test_operations_on_invalid_handle( handle );
1455
1456exit:
1457 psa_destroy_key( handle );
1458 psa_reset_key_attributes( &got_attributes );
1459 PSA_DONE( );
1460}
1461/* END_CASE */
1462
1463/* BEGIN_CASE */
1464void import_with_data( data_t *data, int type_arg,
1465 int attr_bits_arg,
1466 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001467{
1468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1469 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001470 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001471 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001472 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001473 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001474 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001475
Gilles Peskine8817f612018-12-18 00:18:46 +01001476 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001477
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001479 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001480
Gilles Peskine73676cb2019-05-15 20:15:10 +02001481 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001482 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001483 if( status != PSA_SUCCESS )
1484 goto exit;
1485
1486 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1487 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001488 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001489 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001490 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001491
1492 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001493 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001494
1495exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001496 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001497 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001498 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001499}
1500/* END_CASE */
1501
1502/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001503void import_large_key( int type_arg, int byte_size_arg,
1504 int expected_status_arg )
1505{
1506 psa_key_type_t type = type_arg;
1507 size_t byte_size = byte_size_arg;
1508 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1509 psa_status_t expected_status = expected_status_arg;
1510 psa_key_handle_t handle = 0;
1511 psa_status_t status;
1512 uint8_t *buffer = NULL;
1513 size_t buffer_size = byte_size + 1;
1514 size_t n;
1515
1516 /* It would be better to skip the test than fail it if the allocation
1517 * fails, but the test framework doesn't support this yet. */
1518 ASSERT_ALLOC( buffer, buffer_size );
1519 memset( buffer, 'K', byte_size );
1520
1521 PSA_ASSERT( psa_crypto_init( ) );
1522
1523 /* Try importing the key */
1524 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1525 psa_set_key_type( &attributes, type );
1526 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1527 TEST_EQUAL( status, expected_status );
1528
1529 if( status == PSA_SUCCESS )
1530 {
1531 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1532 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1533 TEST_EQUAL( psa_get_key_bits( &attributes ),
1534 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001535 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001536 memset( buffer, 0, byte_size + 1 );
1537 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1538 for( n = 0; n < byte_size; n++ )
1539 TEST_EQUAL( buffer[n], 'K' );
1540 for( n = byte_size; n < buffer_size; n++ )
1541 TEST_EQUAL( buffer[n], 0 );
1542 }
1543
1544exit:
1545 psa_destroy_key( handle );
1546 PSA_DONE( );
1547 mbedtls_free( buffer );
1548}
1549/* END_CASE */
1550
1551/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001552void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1553{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001554 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001555 size_t bits = bits_arg;
1556 psa_status_t expected_status = expected_status_arg;
1557 psa_status_t status;
1558 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001559 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001560 size_t buffer_size = /* Slight overapproximations */
1561 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001562 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001563 unsigned char *p;
1564 int ret;
1565 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001567
Gilles Peskine8817f612018-12-18 00:18:46 +01001568 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001569 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001570
1571 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1572 bits, keypair ) ) >= 0 );
1573 length = ret;
1574
1575 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001576 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001577 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001578 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001579
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001580 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001581 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001582
1583exit:
1584 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001585 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001586}
1587/* END_CASE */
1588
1589/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001590void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001591 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001592 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001593 int expected_bits,
1594 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001595 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001596 int canonical_input )
1597{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001598 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001599 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001600 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001601 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001602 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603 unsigned char *exported = NULL;
1604 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001605 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001606 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001607 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001609 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001610
Moran Pekercb088e72018-07-17 17:36:59 +03001611 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001612 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001613 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001614 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001616
Gilles Peskine4747d192019-04-17 15:05:45 +02001617 psa_set_key_usage_flags( &attributes, usage_arg );
1618 psa_set_key_algorithm( &attributes, alg );
1619 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001620
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001621 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001622 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001623
1624 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001625 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1626 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1627 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001628 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001629
1630 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001631 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001632 exported, export_size,
1633 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001634 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001635
1636 /* The exported length must be set by psa_export_key() to a value between 0
1637 * and export_size. On errors, the exported length must be 0. */
1638 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1639 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1640 TEST_ASSERT( exported_length <= export_size );
1641
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001642 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001643 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001644 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001645 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001646 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001647 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001648 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001649
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001650 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001651 goto exit;
1652
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001653 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001654 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001655 else
1656 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001657 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001658 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1659 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001660 PSA_ASSERT( psa_export_key( handle2,
1661 reexported,
1662 export_size,
1663 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001664 ASSERT_COMPARE( exported, exported_length,
1665 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001666 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001667 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001668 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001669
1670destroy:
1671 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001672 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001673 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001674
1675exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001676 mbedtls_free( exported );
1677 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001678 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001679 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001680}
1681/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001682
Moran Pekerf709f4a2018-06-06 17:26:04 +03001683/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001684void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001685 int type_arg,
1686 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001687 int export_size_delta,
1688 int expected_export_status_arg,
1689 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001690{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001691 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001692 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001693 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001694 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001695 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001696 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001697 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001698 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001700
Gilles Peskine8817f612018-12-18 00:18:46 +01001701 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001702
Gilles Peskine4747d192019-04-17 15:05:45 +02001703 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1704 psa_set_key_algorithm( &attributes, alg );
1705 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001706
1707 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001708 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001709
Gilles Peskine49c25912018-10-29 15:15:31 +01001710 /* Export the public key */
1711 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001712 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001713 exported, export_size,
1714 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001715 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001716 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001717 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001718 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001719 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001720 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1721 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001722 TEST_ASSERT( expected_public_key->len <=
1723 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001724 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1725 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001726 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001727
1728exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001729 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001730 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001731 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001732 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001733}
1734/* END_CASE */
1735
Gilles Peskine20035e32018-02-03 22:44:14 +01001736/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001737void import_and_exercise_key( data_t *data,
1738 int type_arg,
1739 int bits_arg,
1740 int alg_arg )
1741{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001742 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001743 psa_key_type_t type = type_arg;
1744 size_t bits = bits_arg;
1745 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001746 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001748 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001749
Gilles Peskine8817f612018-12-18 00:18:46 +01001750 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001751
Gilles Peskine4747d192019-04-17 15:05:45 +02001752 psa_set_key_usage_flags( &attributes, usage );
1753 psa_set_key_algorithm( &attributes, alg );
1754 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001755
1756 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001757 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001758
1759 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001760 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1761 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1762 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001763
1764 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001765 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001766 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001767
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001768 PSA_ASSERT( psa_destroy_key( handle ) );
1769 test_operations_on_invalid_handle( handle );
1770
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001771exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001772 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001773 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001774 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001775}
1776/* END_CASE */
1777
1778/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001779void effective_key_attributes( int type_arg, int expected_type_arg,
1780 int bits_arg, int expected_bits_arg,
1781 int usage_arg, int expected_usage_arg,
1782 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001783{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001784 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001785 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001786 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001787 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001788 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001789 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001790 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001791 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001792 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001793 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001794
Gilles Peskine8817f612018-12-18 00:18:46 +01001795 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001796
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001797 psa_set_key_usage_flags( &attributes, usage );
1798 psa_set_key_algorithm( &attributes, alg );
1799 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001800 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001801
Gilles Peskine1a960492019-11-26 17:12:21 +01001802 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1803 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001804
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001805 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001806 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1807 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1808 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1809 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001810
1811exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001812 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001813 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001814 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001815}
1816/* END_CASE */
1817
1818/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001819void check_key_policy( int type_arg, int bits_arg,
1820 int usage_arg, int alg_arg )
1821{
1822 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1823 usage_arg, usage_arg, alg_arg, alg_arg );
1824 goto exit;
1825}
1826/* END_CASE */
1827
1828/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001829void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001830{
1831 /* Test each valid way of initializing the object, except for `= {0}`, as
1832 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1833 * though it's OK by the C standard. We could test for this, but we'd need
1834 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001835 psa_key_attributes_t func = psa_key_attributes_init( );
1836 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1837 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001838
1839 memset( &zero, 0, sizeof( zero ) );
1840
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001841 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1842 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1843 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001844
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001845 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1846 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1847 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1848
1849 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1850 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1851 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1852
1853 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1854 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1855 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1856
1857 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1858 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1859 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001860}
1861/* END_CASE */
1862
1863/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001864void mac_key_policy( int policy_usage,
1865 int policy_alg,
1866 int key_type,
1867 data_t *key_data,
1868 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001869{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001870 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001872 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001873 psa_status_t status;
1874 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001875
Gilles Peskine8817f612018-12-18 00:18:46 +01001876 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001877
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001878 psa_set_key_usage_flags( &attributes, policy_usage );
1879 psa_set_key_algorithm( &attributes, policy_alg );
1880 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001881
Gilles Peskine049c7532019-05-15 20:22:09 +02001882 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1883 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001884
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001885 status = psa_mac_sign_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_SIGN_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 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001892
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001893 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001894 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001895 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001896 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001897 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001898 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001899 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001900
1901exit:
1902 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001903 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001904 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001905}
1906/* END_CASE */
1907
1908/* BEGIN_CASE */
1909void cipher_key_policy( int policy_usage,
1910 int policy_alg,
1911 int key_type,
1912 data_t *key_data,
1913 int exercise_alg )
1914{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001915 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001916 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001917 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918 psa_status_t status;
1919
Gilles Peskine8817f612018-12-18 00:18:46 +01001920 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001922 psa_set_key_usage_flags( &attributes, policy_usage );
1923 psa_set_key_algorithm( &attributes, policy_alg );
1924 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001925
Gilles Peskine049c7532019-05-15 20:22:09 +02001926 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1927 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001928
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001929 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001930 if( policy_alg == exercise_alg &&
1931 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001932 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001933 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001934 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001935 psa_cipher_abort( &operation );
1936
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001937 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001938 if( policy_alg == exercise_alg &&
1939 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001940 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001942 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001943
1944exit:
1945 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001946 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001947 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001948}
1949/* END_CASE */
1950
1951/* BEGIN_CASE */
1952void aead_key_policy( int policy_usage,
1953 int policy_alg,
1954 int key_type,
1955 data_t *key_data,
1956 int nonce_length_arg,
1957 int tag_length_arg,
1958 int exercise_alg )
1959{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001960 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962 psa_status_t status;
1963 unsigned char nonce[16] = {0};
1964 size_t nonce_length = nonce_length_arg;
1965 unsigned char tag[16];
1966 size_t tag_length = tag_length_arg;
1967 size_t output_length;
1968
1969 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1970 TEST_ASSERT( tag_length <= sizeof( tag ) );
1971
Gilles Peskine8817f612018-12-18 00:18:46 +01001972 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001973
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001974 psa_set_key_usage_flags( &attributes, policy_usage );
1975 psa_set_key_algorithm( &attributes, policy_alg );
1976 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977
Gilles Peskine049c7532019-05-15 20:22:09 +02001978 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1979 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001980
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001981 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001982 nonce, nonce_length,
1983 NULL, 0,
1984 NULL, 0,
1985 tag, tag_length,
1986 &output_length );
1987 if( policy_alg == exercise_alg &&
1988 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001989 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001991 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001992
1993 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001994 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995 nonce, nonce_length,
1996 NULL, 0,
1997 tag, tag_length,
1998 NULL, 0,
1999 &output_length );
2000 if( policy_alg == exercise_alg &&
2001 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002002 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002004 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002005
2006exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002007 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002008 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002009}
2010/* END_CASE */
2011
2012/* BEGIN_CASE */
2013void asymmetric_encryption_key_policy( int policy_usage,
2014 int policy_alg,
2015 int key_type,
2016 data_t *key_data,
2017 int exercise_alg )
2018{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002019 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021 psa_status_t status;
2022 size_t key_bits;
2023 size_t buffer_length;
2024 unsigned char *buffer = NULL;
2025 size_t output_length;
2026
Gilles Peskine8817f612018-12-18 00:18:46 +01002027 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002029 psa_set_key_usage_flags( &attributes, policy_usage );
2030 psa_set_key_algorithm( &attributes, policy_alg );
2031 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032
Gilles Peskine049c7532019-05-15 20:22:09 +02002033 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2034 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002036 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
2037 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2039 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002040 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002042 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043 NULL, 0,
2044 NULL, 0,
2045 buffer, buffer_length,
2046 &output_length );
2047 if( policy_alg == exercise_alg &&
2048 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002049 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002051 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002052
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002053 if( buffer_length != 0 )
2054 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002055 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056 buffer, buffer_length,
2057 NULL, 0,
2058 buffer, buffer_length,
2059 &output_length );
2060 if( policy_alg == exercise_alg &&
2061 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002062 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002063 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002064 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002065
2066exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002067 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002068 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002069 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070 mbedtls_free( buffer );
2071}
2072/* END_CASE */
2073
2074/* BEGIN_CASE */
2075void asymmetric_signature_key_policy( int policy_usage,
2076 int policy_alg,
2077 int key_type,
2078 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002079 int exercise_alg,
2080 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002082 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002085 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2086 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2087 * compatible with the policy and `payload_length_arg` is supposed to be
2088 * a valid input length to sign. If `payload_length_arg <= 0`,
2089 * `exercise_alg` is supposed to be forbidden by the policy. */
2090 int compatible_alg = payload_length_arg > 0;
2091 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002092 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002093 size_t signature_length;
2094
Gilles Peskine8817f612018-12-18 00:18:46 +01002095 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002097 psa_set_key_usage_flags( &attributes, policy_usage );
2098 psa_set_key_algorithm( &attributes, policy_alg );
2099 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100
Gilles Peskine049c7532019-05-15 20:22:09 +02002101 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2102 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002104 status = psa_sign_hash( handle, exercise_alg,
2105 payload, payload_length,
2106 signature, sizeof( signature ),
2107 &signature_length );
2108 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002109 PSA_ASSERT( status );
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 Peskine76f5c7b2018-07-06 16:53:09 +02002112
2113 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002114 status = psa_verify_hash( handle, exercise_alg,
2115 payload, payload_length,
2116 signature, sizeof( signature ) );
2117 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002118 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002119 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002120 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002121
2122exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002123 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002124 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002125}
2126/* END_CASE */
2127
Janos Follathba3fab92019-06-11 14:50:16 +01002128/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002129void derive_key_policy( int policy_usage,
2130 int policy_alg,
2131 int key_type,
2132 data_t *key_data,
2133 int exercise_alg )
2134{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002135 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002137 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002138 psa_status_t status;
2139
Gilles Peskine8817f612018-12-18 00:18:46 +01002140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002141
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002142 psa_set_key_usage_flags( &attributes, policy_usage );
2143 psa_set_key_algorithm( &attributes, policy_alg );
2144 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002145
Gilles Peskine049c7532019-05-15 20:22:09 +02002146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2147 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002148
Janos Follathba3fab92019-06-11 14:50:16 +01002149 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2150
2151 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2152 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002153 {
Janos Follathba3fab92019-06-11 14:50:16 +01002154 PSA_ASSERT( psa_key_derivation_input_bytes(
2155 &operation,
2156 PSA_KEY_DERIVATION_INPUT_SEED,
2157 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002158 }
Janos Follathba3fab92019-06-11 14:50:16 +01002159
2160 status = psa_key_derivation_input_key( &operation,
2161 PSA_KEY_DERIVATION_INPUT_SECRET,
2162 handle );
2163
Gilles Peskineea0fb492018-07-12 17:17:20 +02002164 if( policy_alg == exercise_alg &&
2165 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002166 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002167 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002168 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002169
2170exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002171 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002172 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002173 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002174}
2175/* END_CASE */
2176
2177/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002178void agreement_key_policy( int policy_usage,
2179 int policy_alg,
2180 int key_type_arg,
2181 data_t *key_data,
2182 int exercise_alg )
2183{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002184 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002186 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002187 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002188 psa_status_t status;
2189
Gilles Peskine8817f612018-12-18 00:18:46 +01002190 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002191
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002192 psa_set_key_usage_flags( &attributes, policy_usage );
2193 psa_set_key_algorithm( &attributes, policy_alg );
2194 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002195
Gilles Peskine049c7532019-05-15 20:22:09 +02002196 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2197 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002198
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002199 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2200 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002201
Gilles Peskine01d718c2018-09-18 12:01:02 +02002202 if( policy_alg == exercise_alg &&
2203 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002204 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002205 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002206 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002207
2208exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002209 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002210 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002211 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002212}
2213/* END_CASE */
2214
2215/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002216void key_policy_alg2( int key_type_arg, data_t *key_data,
2217 int usage_arg, int alg_arg, int alg2_arg )
2218{
2219 psa_key_handle_t handle = 0;
2220 psa_key_type_t key_type = key_type_arg;
2221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2222 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2223 psa_key_usage_t usage = usage_arg;
2224 psa_algorithm_t alg = alg_arg;
2225 psa_algorithm_t alg2 = alg2_arg;
2226
2227 PSA_ASSERT( psa_crypto_init( ) );
2228
2229 psa_set_key_usage_flags( &attributes, usage );
2230 psa_set_key_algorithm( &attributes, alg );
2231 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2232 psa_set_key_type( &attributes, key_type );
2233 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2234 &handle ) );
2235
2236 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2237 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2238 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2239 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2240
2241 if( ! exercise_key( handle, usage, alg ) )
2242 goto exit;
2243 if( ! exercise_key( handle, usage, alg2 ) )
2244 goto exit;
2245
2246exit:
2247 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002248 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002249}
2250/* END_CASE */
2251
2252/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002253void raw_agreement_key_policy( int policy_usage,
2254 int policy_alg,
2255 int key_type_arg,
2256 data_t *key_data,
2257 int exercise_alg )
2258{
2259 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002261 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002262 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002263 psa_status_t status;
2264
2265 PSA_ASSERT( psa_crypto_init( ) );
2266
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002267 psa_set_key_usage_flags( &attributes, policy_usage );
2268 psa_set_key_algorithm( &attributes, policy_alg );
2269 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002270
Gilles Peskine049c7532019-05-15 20:22:09 +02002271 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2272 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002273
2274 status = raw_key_agreement_with_self( exercise_alg, handle );
2275
2276 if( policy_alg == exercise_alg &&
2277 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2278 PSA_ASSERT( status );
2279 else
2280 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2281
2282exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002283 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002284 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002285 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002286}
2287/* END_CASE */
2288
2289/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002290void copy_success( int source_usage_arg,
2291 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002292 int type_arg, data_t *material,
2293 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002294 int target_usage_arg,
2295 int target_alg_arg, int target_alg2_arg,
2296 int expected_usage_arg,
2297 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002298{
Gilles Peskineca25db92019-04-19 11:43:08 +02002299 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2300 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002301 psa_key_usage_t expected_usage = expected_usage_arg;
2302 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002303 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002304 psa_key_handle_t source_handle = 0;
2305 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002306 uint8_t *export_buffer = NULL;
2307
Gilles Peskine57ab7212019-01-28 13:03:09 +01002308 PSA_ASSERT( psa_crypto_init( ) );
2309
Gilles Peskineca25db92019-04-19 11:43:08 +02002310 /* Prepare the source key. */
2311 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2312 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002313 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002314 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002315 PSA_ASSERT( psa_import_key( &source_attributes,
2316 material->x, material->len,
2317 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002318 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002319
Gilles Peskineca25db92019-04-19 11:43:08 +02002320 /* Prepare the target attributes. */
2321 if( copy_attributes )
2322 target_attributes = source_attributes;
2323 if( target_usage_arg != -1 )
2324 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2325 if( target_alg_arg != -1 )
2326 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002327 if( target_alg2_arg != -1 )
2328 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002329
2330 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002331 PSA_ASSERT( psa_copy_key( source_handle,
2332 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002333
2334 /* Destroy the source to ensure that this doesn't affect the target. */
2335 PSA_ASSERT( psa_destroy_key( source_handle ) );
2336
2337 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002338 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2339 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2340 psa_get_key_type( &target_attributes ) );
2341 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2342 psa_get_key_bits( &target_attributes ) );
2343 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2344 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002345 TEST_EQUAL( expected_alg2,
2346 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002347 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2348 {
2349 size_t length;
2350 ASSERT_ALLOC( export_buffer, material->len );
2351 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2352 material->len, &length ) );
2353 ASSERT_COMPARE( material->x, material->len,
2354 export_buffer, length );
2355 }
2356 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2357 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002358 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2359 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002360
2361 PSA_ASSERT( psa_close_key( target_handle ) );
2362
2363exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002364 psa_reset_key_attributes( &source_attributes );
2365 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002366 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002367 mbedtls_free( export_buffer );
2368}
2369/* END_CASE */
2370
2371/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002372void copy_fail( int source_usage_arg,
2373 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002374 int type_arg, data_t *material,
2375 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002376 int target_usage_arg,
2377 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002378 int expected_status_arg )
2379{
2380 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2381 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2382 psa_key_handle_t source_handle = 0;
2383 psa_key_handle_t target_handle = 0;
2384
2385 PSA_ASSERT( psa_crypto_init( ) );
2386
2387 /* Prepare the source key. */
2388 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2389 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002390 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002391 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002392 PSA_ASSERT( psa_import_key( &source_attributes,
2393 material->x, material->len,
2394 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002395
2396 /* Prepare the target attributes. */
2397 psa_set_key_type( &target_attributes, target_type_arg );
2398 psa_set_key_bits( &target_attributes, target_bits_arg );
2399 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2400 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002401 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002402
2403 /* Try to copy the key. */
2404 TEST_EQUAL( psa_copy_key( source_handle,
2405 &target_attributes, &target_handle ),
2406 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002407
2408 PSA_ASSERT( psa_destroy_key( source_handle ) );
2409
Gilles Peskine4a644642019-05-03 17:14:08 +02002410exit:
2411 psa_reset_key_attributes( &source_attributes );
2412 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002413 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002414}
2415/* END_CASE */
2416
2417/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002418void hash_operation_init( )
2419{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002420 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002421 /* Test each valid way of initializing the object, except for `= {0}`, as
2422 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2423 * though it's OK by the C standard. We could test for this, but we'd need
2424 * to supress the Clang warning for the test. */
2425 psa_hash_operation_t func = psa_hash_operation_init( );
2426 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2427 psa_hash_operation_t zero;
2428
2429 memset( &zero, 0, sizeof( zero ) );
2430
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002431 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002432 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2433 PSA_ERROR_BAD_STATE );
2434 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2435 PSA_ERROR_BAD_STATE );
2436 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2437 PSA_ERROR_BAD_STATE );
2438
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002439 /* A default hash operation should be abortable without error. */
2440 PSA_ASSERT( psa_hash_abort( &func ) );
2441 PSA_ASSERT( psa_hash_abort( &init ) );
2442 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002443}
2444/* END_CASE */
2445
2446/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002447void hash_setup( int alg_arg,
2448 int expected_status_arg )
2449{
2450 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002451 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002452 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002453 psa_status_t status;
2454
Gilles Peskine8817f612018-12-18 00:18:46 +01002455 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002456
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002457 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002458 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002460 /* Whether setup succeeded or failed, abort must succeed. */
2461 PSA_ASSERT( psa_hash_abort( &operation ) );
2462
2463 /* If setup failed, reproduce the failure, so as to
2464 * test the resulting state of the operation object. */
2465 if( status != PSA_SUCCESS )
2466 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2467
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002468 /* Now the operation object should be reusable. */
2469#if defined(KNOWN_SUPPORTED_HASH_ALG)
2470 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2471 PSA_ASSERT( psa_hash_abort( &operation ) );
2472#endif
2473
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002474exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002475 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476}
2477/* END_CASE */
2478
2479/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002480void hash_compute_fail( int alg_arg, data_t *input,
2481 int output_size_arg, int expected_status_arg )
2482{
2483 psa_algorithm_t alg = alg_arg;
2484 uint8_t *output = NULL;
2485 size_t output_size = output_size_arg;
2486 size_t output_length = INVALID_EXPORT_LENGTH;
2487 psa_status_t expected_status = expected_status_arg;
2488 psa_status_t status;
2489
2490 ASSERT_ALLOC( output, output_size );
2491
2492 PSA_ASSERT( psa_crypto_init( ) );
2493
2494 status = psa_hash_compute( alg, input->x, input->len,
2495 output, output_size, &output_length );
2496 TEST_EQUAL( status, expected_status );
2497 TEST_ASSERT( output_length <= output_size );
2498
2499exit:
2500 mbedtls_free( output );
2501 PSA_DONE( );
2502}
2503/* END_CASE */
2504
2505/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002506void hash_compare_fail( int alg_arg, data_t *input,
2507 data_t *reference_hash,
2508 int expected_status_arg )
2509{
2510 psa_algorithm_t alg = alg_arg;
2511 psa_status_t expected_status = expected_status_arg;
2512 psa_status_t status;
2513
2514 PSA_ASSERT( psa_crypto_init( ) );
2515
2516 status = psa_hash_compare( alg, input->x, input->len,
2517 reference_hash->x, reference_hash->len );
2518 TEST_EQUAL( status, expected_status );
2519
2520exit:
2521 PSA_DONE( );
2522}
2523/* END_CASE */
2524
2525/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002526void hash_compute_compare( int alg_arg, data_t *input,
2527 data_t *expected_output )
2528{
2529 psa_algorithm_t alg = alg_arg;
2530 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2531 size_t output_length = INVALID_EXPORT_LENGTH;
2532 size_t i;
2533
2534 PSA_ASSERT( psa_crypto_init( ) );
2535
2536 /* Compute with tight buffer */
2537 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2538 output, PSA_HASH_SIZE( alg ),
2539 &output_length ) );
2540 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2541 ASSERT_COMPARE( output, output_length,
2542 expected_output->x, expected_output->len );
2543
2544 /* Compute with larger buffer */
2545 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2546 output, sizeof( output ),
2547 &output_length ) );
2548 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2549 ASSERT_COMPARE( output, output_length,
2550 expected_output->x, expected_output->len );
2551
2552 /* Compare with correct hash */
2553 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2554 output, output_length ) );
2555
2556 /* Compare with trailing garbage */
2557 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2558 output, output_length + 1 ),
2559 PSA_ERROR_INVALID_SIGNATURE );
2560
2561 /* Compare with truncated hash */
2562 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2563 output, output_length - 1 ),
2564 PSA_ERROR_INVALID_SIGNATURE );
2565
2566 /* Compare with corrupted value */
2567 for( i = 0; i < output_length; i++ )
2568 {
2569 test_set_step( i );
2570 output[i] ^= 1;
2571 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2572 output, output_length ),
2573 PSA_ERROR_INVALID_SIGNATURE );
2574 output[i] ^= 1;
2575 }
2576
2577exit:
2578 PSA_DONE( );
2579}
2580/* END_CASE */
2581
2582/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002583void hash_bad_order( )
2584{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002585 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002586 unsigned char input[] = "";
2587 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002588 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002589 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2590 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2591 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002592 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002593 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002594 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002595
Gilles Peskine8817f612018-12-18 00:18:46 +01002596 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002597
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002598 /* Call setup twice in a row. */
2599 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2600 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2601 PSA_ERROR_BAD_STATE );
2602 PSA_ASSERT( psa_hash_abort( &operation ) );
2603
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002604 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002605 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002606 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002607 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002608
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002609 /* Call update after finish. */
2610 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2611 PSA_ASSERT( psa_hash_finish( &operation,
2612 hash, sizeof( hash ), &hash_len ) );
2613 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002614 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002615 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002616
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002617 /* Call verify without calling setup beforehand. */
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 after finish. */
2624 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2625 PSA_ASSERT( psa_hash_finish( &operation,
2626 hash, sizeof( hash ), &hash_len ) );
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 verify twice in a row. */
2633 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2634 PSA_ASSERT( psa_hash_verify( &operation,
2635 valid_hash, sizeof( valid_hash ) ) );
2636 TEST_EQUAL( psa_hash_verify( &operation,
2637 valid_hash, sizeof( valid_hash ) ),
2638 PSA_ERROR_BAD_STATE );
2639 PSA_ASSERT( psa_hash_abort( &operation ) );
2640
2641 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002642 TEST_EQUAL( psa_hash_finish( &operation,
2643 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002644 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002645 PSA_ASSERT( psa_hash_abort( &operation ) );
2646
2647 /* Call finish twice in a row. */
2648 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2649 PSA_ASSERT( psa_hash_finish( &operation,
2650 hash, sizeof( hash ), &hash_len ) );
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 ) );
2655
2656 /* Call finish after calling verify. */
2657 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2658 PSA_ASSERT( psa_hash_verify( &operation,
2659 valid_hash, sizeof( valid_hash ) ) );
2660 TEST_EQUAL( psa_hash_finish( &operation,
2661 hash, sizeof( hash ), &hash_len ),
2662 PSA_ERROR_BAD_STATE );
2663 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002664
2665exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002666 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002667}
2668/* END_CASE */
2669
itayzafrir27e69452018-11-01 14:26:34 +02002670/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2671void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002672{
2673 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002674 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2675 * appended to it */
2676 unsigned char hash[] = {
2677 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2678 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2679 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002680 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002681 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002682
Gilles Peskine8817f612018-12-18 00:18:46 +01002683 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002684
itayzafrir27e69452018-11-01 14:26:34 +02002685 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002686 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002687 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002688 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002689
itayzafrir27e69452018-11-01 14:26:34 +02002690 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002691 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002692 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002693 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002694
itayzafrir27e69452018-11-01 14:26:34 +02002695 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002696 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002697 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002698 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002699
itayzafrirec93d302018-10-18 18:01:10 +03002700exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002701 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002702}
2703/* END_CASE */
2704
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002705/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2706void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002707{
2708 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002709 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002710 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002711 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002712 size_t hash_len;
2713
Gilles Peskine8817f612018-12-18 00:18:46 +01002714 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002715
itayzafrir58028322018-10-25 10:22:01 +03002716 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002718 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002719 hash, expected_size - 1, &hash_len ),
2720 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002721
2722exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002723 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002724}
2725/* END_CASE */
2726
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002727/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2728void hash_clone_source_state( )
2729{
2730 psa_algorithm_t alg = PSA_ALG_SHA_256;
2731 unsigned char hash[PSA_HASH_MAX_SIZE];
2732 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2733 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2734 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2735 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2736 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2737 size_t hash_len;
2738
2739 PSA_ASSERT( psa_crypto_init( ) );
2740 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2741
2742 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2743 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2744 PSA_ASSERT( psa_hash_finish( &op_finished,
2745 hash, sizeof( hash ), &hash_len ) );
2746 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2747 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2748
2749 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2750 PSA_ERROR_BAD_STATE );
2751
2752 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2753 PSA_ASSERT( psa_hash_finish( &op_init,
2754 hash, sizeof( hash ), &hash_len ) );
2755 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2756 PSA_ASSERT( psa_hash_finish( &op_finished,
2757 hash, sizeof( hash ), &hash_len ) );
2758 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2759 PSA_ASSERT( psa_hash_finish( &op_aborted,
2760 hash, sizeof( hash ), &hash_len ) );
2761
2762exit:
2763 psa_hash_abort( &op_source );
2764 psa_hash_abort( &op_init );
2765 psa_hash_abort( &op_setup );
2766 psa_hash_abort( &op_finished );
2767 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002768 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002769}
2770/* END_CASE */
2771
2772/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2773void hash_clone_target_state( )
2774{
2775 psa_algorithm_t alg = PSA_ALG_SHA_256;
2776 unsigned char hash[PSA_HASH_MAX_SIZE];
2777 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2778 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2779 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2780 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2781 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2782 size_t hash_len;
2783
2784 PSA_ASSERT( psa_crypto_init( ) );
2785
2786 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2787 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2788 PSA_ASSERT( psa_hash_finish( &op_finished,
2789 hash, sizeof( hash ), &hash_len ) );
2790 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2791 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2792
2793 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2794 PSA_ASSERT( psa_hash_finish( &op_target,
2795 hash, sizeof( hash ), &hash_len ) );
2796
2797 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2798 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2799 PSA_ERROR_BAD_STATE );
2800 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2801 PSA_ERROR_BAD_STATE );
2802
2803exit:
2804 psa_hash_abort( &op_target );
2805 psa_hash_abort( &op_init );
2806 psa_hash_abort( &op_setup );
2807 psa_hash_abort( &op_finished );
2808 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002809 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002810}
2811/* END_CASE */
2812
itayzafrir58028322018-10-25 10:22:01 +03002813/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002814void mac_operation_init( )
2815{
Jaeden Amero252ef282019-02-15 14:05:35 +00002816 const uint8_t input[1] = { 0 };
2817
Jaeden Amero769ce272019-01-04 11:48:03 +00002818 /* Test each valid way of initializing the object, except for `= {0}`, as
2819 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2820 * though it's OK by the C standard. We could test for this, but we'd need
2821 * to supress the Clang warning for the test. */
2822 psa_mac_operation_t func = psa_mac_operation_init( );
2823 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2824 psa_mac_operation_t zero;
2825
2826 memset( &zero, 0, sizeof( zero ) );
2827
Jaeden Amero252ef282019-02-15 14:05:35 +00002828 /* A freshly-initialized MAC operation should not be usable. */
2829 TEST_EQUAL( psa_mac_update( &func,
2830 input, sizeof( input ) ),
2831 PSA_ERROR_BAD_STATE );
2832 TEST_EQUAL( psa_mac_update( &init,
2833 input, sizeof( input ) ),
2834 PSA_ERROR_BAD_STATE );
2835 TEST_EQUAL( psa_mac_update( &zero,
2836 input, sizeof( input ) ),
2837 PSA_ERROR_BAD_STATE );
2838
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002839 /* A default MAC operation should be abortable without error. */
2840 PSA_ASSERT( psa_mac_abort( &func ) );
2841 PSA_ASSERT( psa_mac_abort( &init ) );
2842 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002843}
2844/* END_CASE */
2845
2846/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002847void mac_setup( int key_type_arg,
2848 data_t *key,
2849 int alg_arg,
2850 int expected_status_arg )
2851{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002852 psa_key_type_t key_type = key_type_arg;
2853 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002854 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002855 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002856 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2857#if defined(KNOWN_SUPPORTED_MAC_ALG)
2858 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2859#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002860
Gilles Peskine8817f612018-12-18 00:18:46 +01002861 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002862
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002863 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2864 &operation, &status ) )
2865 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002866 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002867
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002868 /* The operation object should be reusable. */
2869#if defined(KNOWN_SUPPORTED_MAC_ALG)
2870 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2871 smoke_test_key_data,
2872 sizeof( smoke_test_key_data ),
2873 KNOWN_SUPPORTED_MAC_ALG,
2874 &operation, &status ) )
2875 goto exit;
2876 TEST_EQUAL( status, PSA_SUCCESS );
2877#endif
2878
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002879exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002880 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002881}
2882/* END_CASE */
2883
2884/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002885void mac_bad_order( )
2886{
2887 psa_key_handle_t handle = 0;
2888 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2889 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2890 const uint8_t key[] = {
2891 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2892 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2893 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002895 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2896 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2897 size_t sign_mac_length = 0;
2898 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2899 const uint8_t verify_mac[] = {
2900 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2901 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2902 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2903
2904 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002905 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002906 psa_set_key_algorithm( &attributes, alg );
2907 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002908
Gilles Peskine73676cb2019-05-15 20:15:10 +02002909 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002910
Jaeden Amero252ef282019-02-15 14:05:35 +00002911 /* Call update without calling setup beforehand. */
2912 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2913 PSA_ERROR_BAD_STATE );
2914 PSA_ASSERT( psa_mac_abort( &operation ) );
2915
2916 /* Call sign finish without calling setup beforehand. */
2917 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2918 &sign_mac_length),
2919 PSA_ERROR_BAD_STATE );
2920 PSA_ASSERT( psa_mac_abort( &operation ) );
2921
2922 /* Call verify finish without calling setup beforehand. */
2923 TEST_EQUAL( psa_mac_verify_finish( &operation,
2924 verify_mac, sizeof( verify_mac ) ),
2925 PSA_ERROR_BAD_STATE );
2926 PSA_ASSERT( psa_mac_abort( &operation ) );
2927
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002928 /* Call setup twice in a row. */
2929 PSA_ASSERT( psa_mac_sign_setup( &operation,
2930 handle, alg ) );
2931 TEST_EQUAL( psa_mac_sign_setup( &operation,
2932 handle, alg ),
2933 PSA_ERROR_BAD_STATE );
2934 PSA_ASSERT( psa_mac_abort( &operation ) );
2935
Jaeden Amero252ef282019-02-15 14:05:35 +00002936 /* Call update after sign finish. */
2937 PSA_ASSERT( psa_mac_sign_setup( &operation,
2938 handle, alg ) );
2939 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2940 PSA_ASSERT( psa_mac_sign_finish( &operation,
2941 sign_mac, sizeof( sign_mac ),
2942 &sign_mac_length ) );
2943 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2944 PSA_ERROR_BAD_STATE );
2945 PSA_ASSERT( psa_mac_abort( &operation ) );
2946
2947 /* Call update after verify finish. */
2948 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002949 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002950 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2951 PSA_ASSERT( psa_mac_verify_finish( &operation,
2952 verify_mac, sizeof( verify_mac ) ) );
2953 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2954 PSA_ERROR_BAD_STATE );
2955 PSA_ASSERT( psa_mac_abort( &operation ) );
2956
2957 /* Call sign finish twice in a row. */
2958 PSA_ASSERT( psa_mac_sign_setup( &operation,
2959 handle, alg ) );
2960 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2961 PSA_ASSERT( psa_mac_sign_finish( &operation,
2962 sign_mac, sizeof( sign_mac ),
2963 &sign_mac_length ) );
2964 TEST_EQUAL( psa_mac_sign_finish( &operation,
2965 sign_mac, sizeof( sign_mac ),
2966 &sign_mac_length ),
2967 PSA_ERROR_BAD_STATE );
2968 PSA_ASSERT( psa_mac_abort( &operation ) );
2969
2970 /* Call verify finish twice in a row. */
2971 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002972 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002973 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2974 PSA_ASSERT( psa_mac_verify_finish( &operation,
2975 verify_mac, sizeof( verify_mac ) ) );
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 sign but try verify. */
2982 PSA_ASSERT( psa_mac_sign_setup( &operation,
2983 handle, alg ) );
2984 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2985 TEST_EQUAL( psa_mac_verify_finish( &operation,
2986 verify_mac, sizeof( verify_mac ) ),
2987 PSA_ERROR_BAD_STATE );
2988 PSA_ASSERT( psa_mac_abort( &operation ) );
2989
2990 /* Setup verify but try sign. */
2991 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002992 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002993 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2994 TEST_EQUAL( psa_mac_sign_finish( &operation,
2995 sign_mac, sizeof( sign_mac ),
2996 &sign_mac_length ),
2997 PSA_ERROR_BAD_STATE );
2998 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002999
Gilles Peskine76b29a72019-05-28 14:08:50 +02003000 PSA_ASSERT( psa_destroy_key( handle ) );
3001
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003002exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003003 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003004}
3005/* END_CASE */
3006
3007/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003008void mac_sign( int key_type_arg,
3009 data_t *key,
3010 int alg_arg,
3011 data_t *input,
3012 data_t *expected_mac )
3013{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003014 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003015 psa_key_type_t key_type = key_type_arg;
3016 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003017 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003019 /* Leave a little extra room in the output buffer. At the end of the
3020 * test, we'll check that the implementation didn't overwrite onto
3021 * this extra room. */
3022 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
3023 size_t mac_buffer_size =
3024 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
3025 size_t mac_length = 0;
3026
3027 memset( actual_mac, '+', sizeof( actual_mac ) );
3028 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
3029 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
3030
Gilles Peskine8817f612018-12-18 00:18:46 +01003031 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003032
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003034 psa_set_key_algorithm( &attributes, alg );
3035 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003036
Gilles Peskine73676cb2019-05-15 20:15:10 +02003037 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003038
3039 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003040 PSA_ASSERT( psa_mac_sign_setup( &operation,
3041 handle, alg ) );
3042 PSA_ASSERT( psa_mac_update( &operation,
3043 input->x, input->len ) );
3044 PSA_ASSERT( psa_mac_sign_finish( &operation,
3045 actual_mac, mac_buffer_size,
3046 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003047
3048 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003049 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3050 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003051
3052 /* Verify that the end of the buffer is untouched. */
3053 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
3054 sizeof( actual_mac ) - mac_length ) );
3055
3056exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003057 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003058 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003059}
3060/* END_CASE */
3061
3062/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003063void mac_verify( int key_type_arg,
3064 data_t *key,
3065 int alg_arg,
3066 data_t *input,
3067 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003069 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003070 psa_key_type_t key_type = key_type_arg;
3071 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003072 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003074
Gilles Peskine69c12672018-06-28 00:07:19 +02003075 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3076
Gilles Peskine8817f612018-12-18 00:18:46 +01003077 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003078
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003080 psa_set_key_algorithm( &attributes, alg );
3081 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003082
Gilles Peskine73676cb2019-05-15 20:15:10 +02003083 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003084
Gilles Peskine8817f612018-12-18 00:18:46 +01003085 PSA_ASSERT( psa_mac_verify_setup( &operation,
3086 handle, alg ) );
3087 PSA_ASSERT( psa_destroy_key( handle ) );
3088 PSA_ASSERT( psa_mac_update( &operation,
3089 input->x, input->len ) );
3090 PSA_ASSERT( psa_mac_verify_finish( &operation,
3091 expected_mac->x,
3092 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003093
3094exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003095 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003096 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003097}
3098/* END_CASE */
3099
3100/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003101void cipher_operation_init( )
3102{
Jaeden Ameroab439972019-02-15 14:12:05 +00003103 const uint8_t input[1] = { 0 };
3104 unsigned char output[1] = { 0 };
3105 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003106 /* Test each valid way of initializing the object, except for `= {0}`, as
3107 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3108 * though it's OK by the C standard. We could test for this, but we'd need
3109 * to supress the Clang warning for the test. */
3110 psa_cipher_operation_t func = psa_cipher_operation_init( );
3111 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3112 psa_cipher_operation_t zero;
3113
3114 memset( &zero, 0, sizeof( zero ) );
3115
Jaeden Ameroab439972019-02-15 14:12:05 +00003116 /* A freshly-initialized cipher operation should not be usable. */
3117 TEST_EQUAL( psa_cipher_update( &func,
3118 input, sizeof( input ),
3119 output, sizeof( output ),
3120 &output_length ),
3121 PSA_ERROR_BAD_STATE );
3122 TEST_EQUAL( psa_cipher_update( &init,
3123 input, sizeof( input ),
3124 output, sizeof( output ),
3125 &output_length ),
3126 PSA_ERROR_BAD_STATE );
3127 TEST_EQUAL( psa_cipher_update( &zero,
3128 input, sizeof( input ),
3129 output, sizeof( output ),
3130 &output_length ),
3131 PSA_ERROR_BAD_STATE );
3132
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003133 /* A default cipher operation should be abortable without error. */
3134 PSA_ASSERT( psa_cipher_abort( &func ) );
3135 PSA_ASSERT( psa_cipher_abort( &init ) );
3136 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003137}
3138/* END_CASE */
3139
3140/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003141void cipher_setup( int key_type_arg,
3142 data_t *key,
3143 int alg_arg,
3144 int expected_status_arg )
3145{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003146 psa_key_type_t key_type = key_type_arg;
3147 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003148 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003149 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003150 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003151#if defined(KNOWN_SUPPORTED_MAC_ALG)
3152 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3153#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003156
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003157 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3158 &operation, &status ) )
3159 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003160 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003161
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003162 /* The operation object should be reusable. */
3163#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3164 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3165 smoke_test_key_data,
3166 sizeof( smoke_test_key_data ),
3167 KNOWN_SUPPORTED_CIPHER_ALG,
3168 &operation, &status ) )
3169 goto exit;
3170 TEST_EQUAL( status, PSA_SUCCESS );
3171#endif
3172
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003173exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003174 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003175}
3176/* END_CASE */
3177
3178/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003179void cipher_bad_order( )
3180{
3181 psa_key_handle_t handle = 0;
3182 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3183 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003185 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3186 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3187 const uint8_t key[] = {
3188 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3189 0xaa, 0xaa, 0xaa, 0xaa };
3190 const uint8_t text[] = {
3191 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3192 0xbb, 0xbb, 0xbb, 0xbb };
3193 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3194 size_t length = 0;
3195
3196 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003197 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3198 psa_set_key_algorithm( &attributes, alg );
3199 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003200 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003201
3202
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003203 /* Call encrypt setup twice in a row. */
3204 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3205 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3206 PSA_ERROR_BAD_STATE );
3207 PSA_ASSERT( psa_cipher_abort( &operation ) );
3208
3209 /* Call decrypt setup twice in a row. */
3210 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3211 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3212 PSA_ERROR_BAD_STATE );
3213 PSA_ASSERT( psa_cipher_abort( &operation ) );
3214
Jaeden Ameroab439972019-02-15 14:12:05 +00003215 /* Generate an IV without calling setup beforehand. */
3216 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3217 buffer, sizeof( buffer ),
3218 &length ),
3219 PSA_ERROR_BAD_STATE );
3220 PSA_ASSERT( psa_cipher_abort( &operation ) );
3221
3222 /* Generate an IV twice in a row. */
3223 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3224 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3225 buffer, sizeof( buffer ),
3226 &length ) );
3227 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3228 buffer, sizeof( buffer ),
3229 &length ),
3230 PSA_ERROR_BAD_STATE );
3231 PSA_ASSERT( psa_cipher_abort( &operation ) );
3232
3233 /* Generate an IV after it's already set. */
3234 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3235 PSA_ASSERT( psa_cipher_set_iv( &operation,
3236 iv, sizeof( iv ) ) );
3237 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3238 buffer, sizeof( buffer ),
3239 &length ),
3240 PSA_ERROR_BAD_STATE );
3241 PSA_ASSERT( psa_cipher_abort( &operation ) );
3242
3243 /* Set an IV without calling setup beforehand. */
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 set. */
3250 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3251 PSA_ASSERT( psa_cipher_set_iv( &operation,
3252 iv, sizeof( iv ) ) );
3253 TEST_EQUAL( psa_cipher_set_iv( &operation,
3254 iv, sizeof( iv ) ),
3255 PSA_ERROR_BAD_STATE );
3256 PSA_ASSERT( psa_cipher_abort( &operation ) );
3257
3258 /* Set an IV after it's already generated. */
3259 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3260 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3261 buffer, sizeof( buffer ),
3262 &length ) );
3263 TEST_EQUAL( psa_cipher_set_iv( &operation,
3264 iv, sizeof( iv ) ),
3265 PSA_ERROR_BAD_STATE );
3266 PSA_ASSERT( psa_cipher_abort( &operation ) );
3267
3268 /* Call update without calling setup beforehand. */
3269 TEST_EQUAL( psa_cipher_update( &operation,
3270 text, sizeof( text ),
3271 buffer, sizeof( buffer ),
3272 &length ),
3273 PSA_ERROR_BAD_STATE );
3274 PSA_ASSERT( psa_cipher_abort( &operation ) );
3275
3276 /* Call update without an IV where an IV is required. */
3277 TEST_EQUAL( psa_cipher_update( &operation,
3278 text, sizeof( text ),
3279 buffer, sizeof( buffer ),
3280 &length ),
3281 PSA_ERROR_BAD_STATE );
3282 PSA_ASSERT( psa_cipher_abort( &operation ) );
3283
3284 /* Call update after finish. */
3285 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3286 PSA_ASSERT( psa_cipher_set_iv( &operation,
3287 iv, sizeof( iv ) ) );
3288 PSA_ASSERT( psa_cipher_finish( &operation,
3289 buffer, sizeof( buffer ), &length ) );
3290 TEST_EQUAL( psa_cipher_update( &operation,
3291 text, sizeof( text ),
3292 buffer, sizeof( buffer ),
3293 &length ),
3294 PSA_ERROR_BAD_STATE );
3295 PSA_ASSERT( psa_cipher_abort( &operation ) );
3296
3297 /* Call finish without calling setup beforehand. */
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 without an IV where an IV is required. */
3304 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3305 /* Not calling update means we are encrypting an empty buffer, which is OK
3306 * for cipher modes with padding. */
3307 TEST_EQUAL( psa_cipher_finish( &operation,
3308 buffer, sizeof( buffer ), &length ),
3309 PSA_ERROR_BAD_STATE );
3310 PSA_ASSERT( psa_cipher_abort( &operation ) );
3311
3312 /* Call finish twice in a row. */
3313 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3314 PSA_ASSERT( psa_cipher_set_iv( &operation,
3315 iv, sizeof( iv ) ) );
3316 PSA_ASSERT( psa_cipher_finish( &operation,
3317 buffer, sizeof( buffer ), &length ) );
3318 TEST_EQUAL( psa_cipher_finish( &operation,
3319 buffer, sizeof( buffer ), &length ),
3320 PSA_ERROR_BAD_STATE );
3321 PSA_ASSERT( psa_cipher_abort( &operation ) );
3322
Gilles Peskine76b29a72019-05-28 14:08:50 +02003323 PSA_ASSERT( psa_destroy_key( handle ) );
3324
Jaeden Ameroab439972019-02-15 14:12:05 +00003325exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003326 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003327}
3328/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003329
Gilles Peskine50e586b2018-06-08 14:28:46 +02003330/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003331void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003332 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003333 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003334 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003335{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003336 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003337 psa_status_t status;
3338 psa_key_type_t key_type = key_type_arg;
3339 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003340 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003341 unsigned char *output = NULL;
3342 size_t output_buffer_size = 0;
3343 size_t function_output_length = 0;
3344 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003345 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003347
Gilles Peskine8817f612018-12-18 00:18:46 +01003348 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003349
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003350 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3351 psa_set_key_algorithm( &attributes, alg );
3352 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003353
Gilles Peskine73676cb2019-05-15 20:15:10 +02003354 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003355
Gilles Peskine8817f612018-12-18 00:18:46 +01003356 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3357 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003358
Gilles Peskine423005e2019-05-06 15:22:57 +02003359 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003360 output_buffer_size = ( (size_t) input->len +
3361 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003362 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003363
Gilles Peskine8817f612018-12-18 00:18:46 +01003364 PSA_ASSERT( psa_cipher_update( &operation,
3365 input->x, input->len,
3366 output, output_buffer_size,
3367 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003368 total_output_length += function_output_length;
3369 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003370 output + total_output_length,
3371 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003372 &function_output_length );
3373 total_output_length += function_output_length;
3374
Gilles Peskinefe11b722018-12-18 00:24:04 +01003375 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003376 if( expected_status == PSA_SUCCESS )
3377 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003378 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003379 ASSERT_COMPARE( expected_output->x, expected_output->len,
3380 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003381 }
3382
3383exit:
3384 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003385 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003386 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003387}
3388/* END_CASE */
3389
3390/* BEGIN_CASE */
3391void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003392 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003393 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003394 int first_part_size_arg,
3395 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003396 data_t *expected_output )
3397{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003398 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003399 psa_key_type_t key_type = key_type_arg;
3400 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003401 size_t first_part_size = first_part_size_arg;
3402 size_t output1_length = output1_length_arg;
3403 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003404 unsigned char *output = NULL;
3405 size_t output_buffer_size = 0;
3406 size_t function_output_length = 0;
3407 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003408 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003410
Gilles Peskine8817f612018-12-18 00:18:46 +01003411 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003412
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003413 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3414 psa_set_key_algorithm( &attributes, alg );
3415 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003416
Gilles Peskine73676cb2019-05-15 20:15:10 +02003417 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003418
Gilles Peskine8817f612018-12-18 00:18:46 +01003419 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3420 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003421
Gilles Peskine423005e2019-05-06 15:22:57 +02003422 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003423 output_buffer_size = ( (size_t) input->len +
3424 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003425 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003426
Gilles Peskinee0866522019-02-19 19:44:00 +01003427 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003428 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3429 output, output_buffer_size,
3430 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003431 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003432 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003433 PSA_ASSERT( psa_cipher_update( &operation,
3434 input->x + first_part_size,
3435 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003436 output + total_output_length,
3437 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003438 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003439 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003440 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003441 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003442 output + total_output_length,
3443 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003444 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003445 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003446 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003447
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003448 ASSERT_COMPARE( expected_output->x, expected_output->len,
3449 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003450
3451exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003452 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003453 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003454 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003455}
3456/* END_CASE */
3457
3458/* BEGIN_CASE */
3459void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003460 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003461 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003462 int first_part_size_arg,
3463 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003464 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003465{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003466 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003467
3468 psa_key_type_t key_type = key_type_arg;
3469 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003470 size_t first_part_size = first_part_size_arg;
3471 size_t output1_length = output1_length_arg;
3472 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003473 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003474 size_t output_buffer_size = 0;
3475 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003476 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003477 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003479
Gilles Peskine8817f612018-12-18 00:18:46 +01003480 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003481
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003482 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3483 psa_set_key_algorithm( &attributes, alg );
3484 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003485
Gilles Peskine73676cb2019-05-15 20:15:10 +02003486 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003487
Gilles Peskine8817f612018-12-18 00:18:46 +01003488 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3489 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003490
Gilles Peskine423005e2019-05-06 15:22:57 +02003491 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003492
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003493 output_buffer_size = ( (size_t) input->len +
3494 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003495 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003496
Gilles Peskinee0866522019-02-19 19:44:00 +01003497 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003498 PSA_ASSERT( psa_cipher_update( &operation,
3499 input->x, first_part_size,
3500 output, output_buffer_size,
3501 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003502 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003503 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003504 PSA_ASSERT( psa_cipher_update( &operation,
3505 input->x + first_part_size,
3506 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003507 output + total_output_length,
3508 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003509 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003510 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003511 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003513 output + total_output_length,
3514 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003515 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003516 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003517 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003518
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003519 ASSERT_COMPARE( expected_output->x, expected_output->len,
3520 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003521
3522exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003523 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003524 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003525 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003526}
3527/* END_CASE */
3528
Gilles Peskine50e586b2018-06-08 14:28:46 +02003529/* BEGIN_CASE */
3530void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003531 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003532 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003533 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003534{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003535 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003536 psa_status_t status;
3537 psa_key_type_t key_type = key_type_arg;
3538 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003539 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003540 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003541 size_t output_buffer_size = 0;
3542 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003543 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003544 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003546
Gilles Peskine8817f612018-12-18 00:18:46 +01003547 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003548
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003549 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3550 psa_set_key_algorithm( &attributes, alg );
3551 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003552
Gilles Peskine73676cb2019-05-15 20:15:10 +02003553 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003554
Gilles Peskine8817f612018-12-18 00:18:46 +01003555 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3556 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003557
Gilles Peskine423005e2019-05-06 15:22:57 +02003558 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003559
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003560 output_buffer_size = ( (size_t) input->len +
3561 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003562 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003563
Gilles Peskine8817f612018-12-18 00:18:46 +01003564 PSA_ASSERT( psa_cipher_update( &operation,
3565 input->x, input->len,
3566 output, output_buffer_size,
3567 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003568 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003569 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003570 output + total_output_length,
3571 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003572 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003573 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003574 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003575
3576 if( expected_status == PSA_SUCCESS )
3577 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003578 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003579 ASSERT_COMPARE( expected_output->x, expected_output->len,
3580 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003581 }
3582
Gilles Peskine50e586b2018-06-08 14:28:46 +02003583exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003584 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003585 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003586 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003587}
3588/* END_CASE */
3589
Gilles Peskine50e586b2018-06-08 14:28:46 +02003590/* BEGIN_CASE */
3591void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003592 data_t *key,
3593 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003594{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003595 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003596 psa_key_type_t key_type = key_type_arg;
3597 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003598 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003599 size_t iv_size = 16;
3600 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003601 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003602 size_t output1_size = 0;
3603 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003604 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003605 size_t output2_size = 0;
3606 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003607 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003608 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3609 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003611
Gilles Peskine8817f612018-12-18 00:18:46 +01003612 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003613
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003614 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3615 psa_set_key_algorithm( &attributes, alg );
3616 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003617
Gilles Peskine73676cb2019-05-15 20:15:10 +02003618 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003619
Gilles Peskine8817f612018-12-18 00:18:46 +01003620 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3621 handle, alg ) );
3622 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3623 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003624
Gilles Peskine8817f612018-12-18 00:18:46 +01003625 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3626 iv, iv_size,
3627 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003628 output1_size = ( (size_t) input->len +
3629 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003630 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003631
Gilles Peskine8817f612018-12-18 00:18:46 +01003632 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3633 output1, output1_size,
3634 &output1_length ) );
3635 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003636 output1 + output1_length,
3637 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003638 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003639
Gilles Peskine048b7f02018-06-08 14:20:49 +02003640 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003641
Gilles Peskine8817f612018-12-18 00:18:46 +01003642 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003643
3644 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003645 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003646
Gilles Peskine8817f612018-12-18 00:18:46 +01003647 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3648 iv, iv_length ) );
3649 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3650 output2, output2_size,
3651 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003652 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003653 PSA_ASSERT( psa_cipher_finish( &operation2,
3654 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003655 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003656 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003657
Gilles Peskine048b7f02018-06-08 14:20:49 +02003658 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003659
Gilles Peskine8817f612018-12-18 00:18:46 +01003660 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003661
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003662 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003663
3664exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003665 mbedtls_free( output1 );
3666 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003667 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003668 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003669}
3670/* END_CASE */
3671
3672/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003673void cipher_verify_output_multipart( int alg_arg,
3674 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003675 data_t *key,
3676 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003677 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003678{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003679 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003680 psa_key_type_t key_type = key_type_arg;
3681 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003682 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003683 unsigned char iv[16] = {0};
3684 size_t iv_size = 16;
3685 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003686 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003687 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003688 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003689 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003690 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003691 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003692 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003693 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3694 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003696
Gilles Peskine8817f612018-12-18 00:18:46 +01003697 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003698
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003699 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3700 psa_set_key_algorithm( &attributes, alg );
3701 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003702
Gilles Peskine73676cb2019-05-15 20:15:10 +02003703 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003704
Gilles Peskine8817f612018-12-18 00:18:46 +01003705 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3706 handle, alg ) );
3707 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3708 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003709
Gilles Peskine8817f612018-12-18 00:18:46 +01003710 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3711 iv, iv_size,
3712 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003713 output1_buffer_size = ( (size_t) input->len +
3714 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003715 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003716
Gilles Peskinee0866522019-02-19 19:44:00 +01003717 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003718
Gilles Peskine8817f612018-12-18 00:18:46 +01003719 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3720 output1, output1_buffer_size,
3721 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003722 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003723
Gilles Peskine8817f612018-12-18 00:18:46 +01003724 PSA_ASSERT( psa_cipher_update( &operation1,
3725 input->x + first_part_size,
3726 input->len - first_part_size,
3727 output1, output1_buffer_size,
3728 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003729 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003730
Gilles Peskine8817f612018-12-18 00:18:46 +01003731 PSA_ASSERT( psa_cipher_finish( &operation1,
3732 output1 + output1_length,
3733 output1_buffer_size - output1_length,
3734 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003735 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003736
Gilles Peskine8817f612018-12-18 00:18:46 +01003737 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003738
Gilles Peskine048b7f02018-06-08 14:20:49 +02003739 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003740 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003741
Gilles Peskine8817f612018-12-18 00:18:46 +01003742 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3743 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003744
Gilles Peskine8817f612018-12-18 00:18:46 +01003745 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3746 output2, output2_buffer_size,
3747 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003748 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003749
Gilles Peskine8817f612018-12-18 00:18:46 +01003750 PSA_ASSERT( psa_cipher_update( &operation2,
3751 output1 + first_part_size,
3752 output1_length - first_part_size,
3753 output2, output2_buffer_size,
3754 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003755 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003756
Gilles Peskine8817f612018-12-18 00:18:46 +01003757 PSA_ASSERT( psa_cipher_finish( &operation2,
3758 output2 + output2_length,
3759 output2_buffer_size - output2_length,
3760 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003761 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003762
Gilles Peskine8817f612018-12-18 00:18:46 +01003763 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003764
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003765 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003766
3767exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003768 mbedtls_free( output1 );
3769 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003770 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003771 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003772}
3773/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003774
Gilles Peskine20035e32018-02-03 22:44:14 +01003775/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003776void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003777 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003778 data_t *nonce,
3779 data_t *additional_data,
3780 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003781 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003782{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003783 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003784 psa_key_type_t key_type = key_type_arg;
3785 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003786 unsigned char *output_data = NULL;
3787 size_t output_size = 0;
3788 size_t output_length = 0;
3789 unsigned char *output_data2 = NULL;
3790 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003791 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003792 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003793 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003794
Gilles Peskine4abf7412018-06-18 16:35:34 +02003795 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003796 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3797 * should be exact. */
3798 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3799 TEST_EQUAL( output_size,
3800 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003801 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003802
Gilles Peskine8817f612018-12-18 00:18:46 +01003803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003804
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003805 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3806 psa_set_key_algorithm( &attributes, alg );
3807 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003808
Gilles Peskine049c7532019-05-15 20:22:09 +02003809 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3810 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003811
Gilles Peskinefe11b722018-12-18 00:24:04 +01003812 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3813 nonce->x, nonce->len,
3814 additional_data->x,
3815 additional_data->len,
3816 input_data->x, input_data->len,
3817 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003818 &output_length ),
3819 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003820
3821 if( PSA_SUCCESS == expected_result )
3822 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003823 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003824
Gilles Peskine003a4a92019-05-14 16:09:40 +02003825 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3826 * should be exact. */
3827 TEST_EQUAL( input_data->len,
3828 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3829
Gilles Peskinefe11b722018-12-18 00:24:04 +01003830 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3831 nonce->x, nonce->len,
3832 additional_data->x,
3833 additional_data->len,
3834 output_data, output_length,
3835 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003836 &output_length2 ),
3837 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003838
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003839 ASSERT_COMPARE( input_data->x, input_data->len,
3840 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003841 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003842
Gilles Peskinea1cac842018-06-11 19:33:02 +02003843exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003844 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003845 mbedtls_free( output_data );
3846 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003847 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003848}
3849/* END_CASE */
3850
3851/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003852void aead_encrypt( int key_type_arg, data_t *key_data,
3853 int alg_arg,
3854 data_t *nonce,
3855 data_t *additional_data,
3856 data_t *input_data,
3857 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003858{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003859 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003860 psa_key_type_t key_type = key_type_arg;
3861 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003862 unsigned char *output_data = NULL;
3863 size_t output_size = 0;
3864 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003865 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003866 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003867
Gilles Peskine4abf7412018-06-18 16:35:34 +02003868 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003869 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3870 * should be exact. */
3871 TEST_EQUAL( output_size,
3872 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003873 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003874
Gilles Peskine8817f612018-12-18 00:18:46 +01003875 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003876
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003877 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3878 psa_set_key_algorithm( &attributes, alg );
3879 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003880
Gilles Peskine049c7532019-05-15 20:22:09 +02003881 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3882 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003883
Gilles Peskine8817f612018-12-18 00:18:46 +01003884 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3885 nonce->x, nonce->len,
3886 additional_data->x, additional_data->len,
3887 input_data->x, input_data->len,
3888 output_data, output_size,
3889 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003890
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003891 ASSERT_COMPARE( expected_result->x, expected_result->len,
3892 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003893
Gilles Peskinea1cac842018-06-11 19:33:02 +02003894exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003895 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003896 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003897 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003898}
3899/* END_CASE */
3900
3901/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003902void aead_decrypt( int key_type_arg, data_t *key_data,
3903 int alg_arg,
3904 data_t *nonce,
3905 data_t *additional_data,
3906 data_t *input_data,
3907 data_t *expected_data,
3908 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003909{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003910 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003911 psa_key_type_t key_type = key_type_arg;
3912 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003913 unsigned char *output_data = NULL;
3914 size_t output_size = 0;
3915 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003916 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003918 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003919
Gilles Peskine003a4a92019-05-14 16:09:40 +02003920 output_size = input_data->len - tag_length;
3921 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3922 * should be exact. */
3923 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3924 TEST_EQUAL( output_size,
3925 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003926 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003927
Gilles Peskine8817f612018-12-18 00:18:46 +01003928 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003929
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3931 psa_set_key_algorithm( &attributes, alg );
3932 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003933
Gilles Peskine049c7532019-05-15 20:22:09 +02003934 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3935 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003936
Gilles Peskinefe11b722018-12-18 00:24:04 +01003937 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3938 nonce->x, nonce->len,
3939 additional_data->x,
3940 additional_data->len,
3941 input_data->x, input_data->len,
3942 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003943 &output_length ),
3944 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003945
Gilles Peskine2d277862018-06-18 15:41:12 +02003946 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003947 ASSERT_COMPARE( expected_data->x, expected_data->len,
3948 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003949
Gilles Peskinea1cac842018-06-11 19:33:02 +02003950exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003951 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003952 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003953 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003954}
3955/* END_CASE */
3956
3957/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003958void signature_size( int type_arg,
3959 int bits,
3960 int alg_arg,
3961 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003962{
3963 psa_key_type_t type = type_arg;
3964 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003965 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003966
Gilles Peskinefe11b722018-12-18 00:24:04 +01003967 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003968#if defined(MBEDTLS_TEST_DEPRECATED)
3969 TEST_EQUAL( actual_size,
3970 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3971#endif /* MBEDTLS_TEST_DEPRECATED */
3972
Gilles Peskinee59236f2018-01-27 23:32:46 +01003973exit:
3974 ;
3975}
3976/* END_CASE */
3977
3978/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003979void sign_deterministic( int key_type_arg, data_t *key_data,
3980 int alg_arg, data_t *input_data,
3981 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003982{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003983 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003984 psa_key_type_t key_type = key_type_arg;
3985 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003986 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003987 unsigned char *signature = NULL;
3988 size_t signature_size;
3989 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003991
Gilles Peskine8817f612018-12-18 00:18:46 +01003992 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003993
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003995 psa_set_key_algorithm( &attributes, alg );
3996 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003997
Gilles Peskine049c7532019-05-15 20:22:09 +02003998 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3999 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004000 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4001 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004002
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004003 /* Allocate a buffer which has the size advertized by the
4004 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004005 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004006 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004007 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004008 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004009 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004010
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004011 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004012 PSA_ASSERT( psa_sign_hash( handle, alg,
4013 input_data->x, input_data->len,
4014 signature, signature_size,
4015 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004016 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004017 ASSERT_COMPARE( output_data->x, output_data->len,
4018 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004019
Gilles Peskine0627f982019-11-26 19:12:16 +01004020#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004021 memset( signature, 0, signature_size );
4022 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01004023 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
4024 input_data->x, input_data->len,
4025 signature, signature_size,
4026 &signature_length ) );
4027 ASSERT_COMPARE( output_data->x, output_data->len,
4028 signature, signature_length );
4029#endif /* MBEDTLS_TEST_DEPRECATED */
4030
Gilles Peskine20035e32018-02-03 22:44:14 +01004031exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004032 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004033 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01004034 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004035 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004036}
4037/* END_CASE */
4038
4039/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004040void sign_fail( int key_type_arg, data_t *key_data,
4041 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004042 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004043{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004044 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01004045 psa_key_type_t key_type = key_type_arg;
4046 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004047 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004048 psa_status_t actual_status;
4049 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004050 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004051 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004052 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004053
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004054 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004055
Gilles Peskine8817f612018-12-18 00:18:46 +01004056 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004057
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004058 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004059 psa_set_key_algorithm( &attributes, alg );
4060 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004061
Gilles Peskine049c7532019-05-15 20:22:09 +02004062 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4063 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004064
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004065 actual_status = psa_sign_hash( handle, alg,
4066 input_data->x, input_data->len,
4067 signature, signature_size,
4068 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004069 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004070 /* The value of *signature_length is unspecified on error, but
4071 * whatever it is, it should be less than signature_size, so that
4072 * if the caller tries to read *signature_length bytes without
4073 * checking the error code then they don't overflow a buffer. */
4074 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004075
Gilles Peskine895242b2019-11-29 12:15:40 +01004076#if defined(MBEDTLS_TEST_DEPRECATED)
4077 signature_length = INVALID_EXPORT_LENGTH;
4078 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
4079 input_data->x, input_data->len,
4080 signature, signature_size,
4081 &signature_length ),
4082 expected_status );
4083 TEST_ASSERT( signature_length <= signature_size );
4084#endif /* MBEDTLS_TEST_DEPRECATED */
4085
Gilles Peskine20035e32018-02-03 22:44:14 +01004086exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004087 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004088 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01004089 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004090 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004091}
4092/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004093
4094/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004095void sign_verify( int key_type_arg, data_t *key_data,
4096 int alg_arg, data_t *input_data )
4097{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004098 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02004099 psa_key_type_t key_type = key_type_arg;
4100 psa_algorithm_t alg = alg_arg;
4101 size_t key_bits;
4102 unsigned char *signature = NULL;
4103 size_t signature_size;
4104 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004106
Gilles Peskine8817f612018-12-18 00:18:46 +01004107 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004108
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004109 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004110 psa_set_key_algorithm( &attributes, alg );
4111 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004112
Gilles Peskine049c7532019-05-15 20:22:09 +02004113 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4114 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004115 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4116 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004117
4118 /* Allocate a buffer which has the size advertized by the
4119 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004120 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004121 key_bits, alg );
4122 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004123 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004124 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004125
4126 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004127 PSA_ASSERT( psa_sign_hash( handle, alg,
4128 input_data->x, input_data->len,
4129 signature, signature_size,
4130 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004131 /* Check that the signature length looks sensible. */
4132 TEST_ASSERT( signature_length <= signature_size );
4133 TEST_ASSERT( signature_length > 0 );
4134
4135 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004136 PSA_ASSERT( psa_verify_hash( handle, alg,
4137 input_data->x, input_data->len,
4138 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004139
4140 if( input_data->len != 0 )
4141 {
4142 /* Flip a bit in the input and verify that the signature is now
4143 * detected as invalid. Flip a bit at the beginning, not at the end,
4144 * because ECDSA may ignore the last few bits of the input. */
4145 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004146 TEST_EQUAL( psa_verify_hash( handle, alg,
4147 input_data->x, input_data->len,
4148 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004149 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004150 }
4151
4152exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004153 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004154 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004155 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004156 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004157}
4158/* END_CASE */
4159
4160/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004161void asymmetric_verify( int key_type_arg, data_t *key_data,
4162 int alg_arg, data_t *hash_data,
4163 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004164{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004165 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004166 psa_key_type_t key_type = key_type_arg;
4167 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004169
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004170 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004171
Gilles Peskine8817f612018-12-18 00:18:46 +01004172 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004173
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004175 psa_set_key_algorithm( &attributes, alg );
4176 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004177
Gilles Peskine049c7532019-05-15 20:22:09 +02004178 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4179 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004180
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004181 PSA_ASSERT( psa_verify_hash( handle, alg,
4182 hash_data->x, hash_data->len,
4183 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004184
4185#if defined(MBEDTLS_TEST_DEPRECATED)
4186 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4187 hash_data->x, hash_data->len,
4188 signature_data->x,
4189 signature_data->len ) );
4190
4191#endif /* MBEDTLS_TEST_DEPRECATED */
4192
itayzafrir5c753392018-05-08 11:18:38 +03004193exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004194 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004195 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004196 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004197}
4198/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199
4200/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004201void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4202 int alg_arg, data_t *hash_data,
4203 data_t *signature_data,
4204 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004205{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004206 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004207 psa_key_type_t key_type = key_type_arg;
4208 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004209 psa_status_t actual_status;
4210 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004212
Gilles Peskine8817f612018-12-18 00:18:46 +01004213 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004214
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004215 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004216 psa_set_key_algorithm( &attributes, alg );
4217 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004218
Gilles Peskine049c7532019-05-15 20:22:09 +02004219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4220 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004221
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004222 actual_status = psa_verify_hash( handle, alg,
4223 hash_data->x, hash_data->len,
4224 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004225 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004226
Gilles Peskine895242b2019-11-29 12:15:40 +01004227#if defined(MBEDTLS_TEST_DEPRECATED)
4228 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4229 hash_data->x, hash_data->len,
4230 signature_data->x, signature_data->len ),
4231 expected_status );
4232#endif /* MBEDTLS_TEST_DEPRECATED */
4233
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004234exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004235 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004236 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004237 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004238}
4239/* END_CASE */
4240
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004241/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004242void asymmetric_encrypt( int key_type_arg,
4243 data_t *key_data,
4244 int alg_arg,
4245 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004246 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004247 int expected_output_length_arg,
4248 int expected_status_arg )
4249{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004250 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004251 psa_key_type_t key_type = key_type_arg;
4252 psa_algorithm_t alg = alg_arg;
4253 size_t expected_output_length = expected_output_length_arg;
4254 size_t key_bits;
4255 unsigned char *output = NULL;
4256 size_t output_size;
4257 size_t output_length = ~0;
4258 psa_status_t actual_status;
4259 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004261
Gilles Peskine8817f612018-12-18 00:18:46 +01004262 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004263
Gilles Peskine656896e2018-06-29 19:12:28 +02004264 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004265 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4266 psa_set_key_algorithm( &attributes, alg );
4267 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004268 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4269 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004270
4271 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004272 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4273 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004274 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004275 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004276
4277 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004278 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004279 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004280 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004281 output, output_size,
4282 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004283 TEST_EQUAL( actual_status, expected_status );
4284 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004285
Gilles Peskine68428122018-06-30 18:42:41 +02004286 /* If the label is empty, the test framework puts a non-null pointer
4287 * in label->x. Test that a null pointer works as well. */
4288 if( label->len == 0 )
4289 {
4290 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004291 if( output_size != 0 )
4292 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004293 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004294 input_data->x, input_data->len,
4295 NULL, label->len,
4296 output, output_size,
4297 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004298 TEST_EQUAL( actual_status, expected_status );
4299 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004300 }
4301
Gilles Peskine656896e2018-06-29 19:12:28 +02004302exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004303 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004304 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004305 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004306 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004307}
4308/* END_CASE */
4309
4310/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004311void asymmetric_encrypt_decrypt( int key_type_arg,
4312 data_t *key_data,
4313 int alg_arg,
4314 data_t *input_data,
4315 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004316{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004317 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004318 psa_key_type_t key_type = key_type_arg;
4319 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004320 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004321 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004322 size_t output_size;
4323 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004324 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004325 size_t output2_size;
4326 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004327 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004328
Gilles Peskine8817f612018-12-18 00:18:46 +01004329 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004330
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004331 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4332 psa_set_key_algorithm( &attributes, alg );
4333 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004334
Gilles Peskine049c7532019-05-15 20:22:09 +02004335 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4336 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004337
4338 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004339 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4340 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004341 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004342 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004343 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004344 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004345
Gilles Peskineeebd7382018-06-08 18:11:54 +02004346 /* We test encryption by checking that encrypt-then-decrypt gives back
4347 * the original plaintext because of the non-optional random
4348 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004349 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4350 input_data->x, input_data->len,
4351 label->x, label->len,
4352 output, output_size,
4353 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004354 /* We don't know what ciphertext length to expect, but check that
4355 * it looks sensible. */
4356 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004357
Gilles Peskine8817f612018-12-18 00:18:46 +01004358 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4359 output, output_length,
4360 label->x, label->len,
4361 output2, output2_size,
4362 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004363 ASSERT_COMPARE( input_data->x, input_data->len,
4364 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004365
4366exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004367 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004368 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004369 mbedtls_free( output );
4370 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004371 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004372}
4373/* END_CASE */
4374
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004375/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004376void asymmetric_decrypt( int key_type_arg,
4377 data_t *key_data,
4378 int alg_arg,
4379 data_t *input_data,
4380 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004381 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004382{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004383 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004384 psa_key_type_t key_type = key_type_arg;
4385 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004386 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004387 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004388 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004390
Jaeden Amero412654a2019-02-06 12:57:46 +00004391 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004392 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004393
Gilles Peskine8817f612018-12-18 00:18:46 +01004394 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004395
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004396 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4397 psa_set_key_algorithm( &attributes, alg );
4398 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004399
Gilles Peskine049c7532019-05-15 20:22:09 +02004400 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4401 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004402
Gilles Peskine8817f612018-12-18 00:18:46 +01004403 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4404 input_data->x, input_data->len,
4405 label->x, label->len,
4406 output,
4407 output_size,
4408 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004409 ASSERT_COMPARE( expected_data->x, expected_data->len,
4410 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004411
Gilles Peskine68428122018-06-30 18:42:41 +02004412 /* If the label is empty, the test framework puts a non-null pointer
4413 * in label->x. Test that a null pointer works as well. */
4414 if( label->len == 0 )
4415 {
4416 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004417 if( output_size != 0 )
4418 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004419 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4420 input_data->x, input_data->len,
4421 NULL, label->len,
4422 output,
4423 output_size,
4424 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004425 ASSERT_COMPARE( expected_data->x, expected_data->len,
4426 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004427 }
4428
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004429exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004430 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004431 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004432 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004433 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004434}
4435/* END_CASE */
4436
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004437/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004438void asymmetric_decrypt_fail( int key_type_arg,
4439 data_t *key_data,
4440 int alg_arg,
4441 data_t *input_data,
4442 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004443 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004444 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004446 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004447 psa_key_type_t key_type = key_type_arg;
4448 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004449 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004450 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004451 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004452 psa_status_t actual_status;
4453 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004455
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004456 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004457
Gilles Peskine8817f612018-12-18 00:18:46 +01004458 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004459
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004460 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4461 psa_set_key_algorithm( &attributes, alg );
4462 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004463
Gilles Peskine049c7532019-05-15 20:22:09 +02004464 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4465 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004466
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004467 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004468 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004469 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004470 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004471 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004472 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004473 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004474
Gilles Peskine68428122018-06-30 18:42:41 +02004475 /* If the label is empty, the test framework puts a non-null pointer
4476 * in label->x. Test that a null pointer works as well. */
4477 if( label->len == 0 )
4478 {
4479 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004480 if( output_size != 0 )
4481 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004482 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004483 input_data->x, input_data->len,
4484 NULL, label->len,
4485 output, output_size,
4486 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004487 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004488 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004489 }
4490
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004491exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004492 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004493 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004494 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004495 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004496}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004497/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004498
4499/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004500void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004501{
4502 /* Test each valid way of initializing the object, except for `= {0}`, as
4503 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4504 * though it's OK by the C standard. We could test for this, but we'd need
4505 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004506 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004507 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4508 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4509 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004510
4511 memset( &zero, 0, sizeof( zero ) );
4512
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004513 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004514 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004515 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004516 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004517 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004518 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004519 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004520
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004521 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004522 PSA_ASSERT( psa_key_derivation_abort(&func) );
4523 PSA_ASSERT( psa_key_derivation_abort(&init) );
4524 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004525}
4526/* END_CASE */
4527
Janos Follath16de4a42019-06-13 16:32:24 +01004528/* BEGIN_CASE */
4529void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004530{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004531 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004532 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004533 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004534
Gilles Peskine8817f612018-12-18 00:18:46 +01004535 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004536
Janos Follath16de4a42019-06-13 16:32:24 +01004537 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004538 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004539
4540exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004542 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004543}
4544/* END_CASE */
4545
Janos Follathaf3c2a02019-06-12 12:34:34 +01004546/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004547void derive_set_capacity( int alg_arg, int capacity_arg,
4548 int expected_status_arg )
4549{
4550 psa_algorithm_t alg = alg_arg;
4551 size_t capacity = capacity_arg;
4552 psa_status_t expected_status = expected_status_arg;
4553 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4554
4555 PSA_ASSERT( psa_crypto_init( ) );
4556
4557 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4558
4559 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4560 expected_status );
4561
4562exit:
4563 psa_key_derivation_abort( &operation );
4564 PSA_DONE( );
4565}
4566/* END_CASE */
4567
4568/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004569void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004570 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004571 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004572 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004573 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004574 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004575 int expected_status_arg3,
4576 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004577{
4578 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004579 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4580 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004581 psa_status_t expected_statuses[] = {expected_status_arg1,
4582 expected_status_arg2,
4583 expected_status_arg3};
4584 data_t *inputs[] = {input1, input2, input3};
4585 psa_key_handle_t handles[] = {0, 0, 0};
4586 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4588 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004589 psa_key_type_t output_key_type = output_key_type_arg;
4590 psa_key_handle_t output_handle = 0;
4591 psa_status_t expected_output_status = expected_output_status_arg;
4592 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004593
4594 PSA_ASSERT( psa_crypto_init( ) );
4595
4596 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4597 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004598
4599 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4600
4601 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4602 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004603 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004604 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004605 psa_set_key_type( &attributes, key_types[i] );
4606 PSA_ASSERT( psa_import_key( &attributes,
4607 inputs[i]->x, inputs[i]->len,
4608 &handles[i] ) );
4609 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4610 handles[i] ),
4611 expected_statuses[i] );
4612 }
4613 else
4614 {
4615 TEST_EQUAL( psa_key_derivation_input_bytes(
4616 &operation, steps[i],
4617 inputs[i]->x, inputs[i]->len ),
4618 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004619 }
4620 }
4621
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004622 if( output_key_type != PSA_KEY_TYPE_NONE )
4623 {
4624 psa_reset_key_attributes( &attributes );
4625 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4626 psa_set_key_bits( &attributes, 8 );
4627 actual_output_status =
4628 psa_key_derivation_output_key( &attributes, &operation,
4629 &output_handle );
4630 }
4631 else
4632 {
4633 uint8_t buffer[1];
4634 actual_output_status =
4635 psa_key_derivation_output_bytes( &operation,
4636 buffer, sizeof( buffer ) );
4637 }
4638 TEST_EQUAL( actual_output_status, expected_output_status );
4639
Janos Follathaf3c2a02019-06-12 12:34:34 +01004640exit:
4641 psa_key_derivation_abort( &operation );
4642 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4643 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004644 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004645 PSA_DONE( );
4646}
4647/* END_CASE */
4648
Janos Follathd958bb72019-07-03 15:02:16 +01004649/* BEGIN_CASE */
4650void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004651{
Janos Follathd958bb72019-07-03 15:02:16 +01004652 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004653 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004654 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004655 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004656 unsigned char input1[] = "Input 1";
4657 size_t input1_length = sizeof( input1 );
4658 unsigned char input2[] = "Input 2";
4659 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004660 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004661 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004662 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4663 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4664 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004666
Gilles Peskine8817f612018-12-18 00:18:46 +01004667 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004668
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004669 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4670 psa_set_key_algorithm( &attributes, alg );
4671 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004672
Gilles Peskine73676cb2019-05-15 20:15:10 +02004673 PSA_ASSERT( psa_import_key( &attributes,
4674 key_data, sizeof( key_data ),
4675 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004676
4677 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004678 if( !setup_key_derivation_wrap( &operation, handle, alg,
4679 input1, input1_length,
4680 input2, input2_length,
4681 capacity ) )
4682 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004683
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004684 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004685 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004686 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004687
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004688 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004689
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004690 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004691 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004692
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004693exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004694 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004695 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004696 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004697}
4698/* END_CASE */
4699
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004700/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004701void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004702{
4703 uint8_t output_buffer[16];
4704 size_t buffer_size = 16;
4705 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004706 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004707
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004708 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4709 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004710 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004711
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004712 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004713 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004714
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004715 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004716
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004717 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4718 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004719 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004720
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004722 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004723
4724exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004726}
4727/* END_CASE */
4728
4729/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004730void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004731 int step1_arg, data_t *input1,
4732 int step2_arg, data_t *input2,
4733 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004734 int requested_capacity_arg,
4735 data_t *expected_output1,
4736 data_t *expected_output2 )
4737{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004738 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004739 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4740 data_t *inputs[] = {input1, input2, input3};
4741 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004742 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004744 uint8_t *expected_outputs[2] =
4745 {expected_output1->x, expected_output2->x};
4746 size_t output_sizes[2] =
4747 {expected_output1->len, expected_output2->len};
4748 size_t output_buffer_size = 0;
4749 uint8_t *output_buffer = NULL;
4750 size_t expected_capacity;
4751 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004752 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004753 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004754 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004755
4756 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4757 {
4758 if( output_sizes[i] > output_buffer_size )
4759 output_buffer_size = output_sizes[i];
4760 if( output_sizes[i] == 0 )
4761 expected_outputs[i] = NULL;
4762 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004763 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004764 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004765
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004766 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4767 psa_set_key_algorithm( &attributes, alg );
4768 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004769
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004770 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004771 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4772 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4773 requested_capacity ) );
4774 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004775 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004776 switch( steps[i] )
4777 {
4778 case 0:
4779 break;
4780 case PSA_KEY_DERIVATION_INPUT_SECRET:
4781 PSA_ASSERT( psa_import_key( &attributes,
4782 inputs[i]->x, inputs[i]->len,
4783 &handles[i] ) );
4784 PSA_ASSERT( psa_key_derivation_input_key(
4785 &operation, steps[i],
4786 handles[i] ) );
4787 break;
4788 default:
4789 PSA_ASSERT( psa_key_derivation_input_bytes(
4790 &operation, steps[i],
4791 inputs[i]->x, inputs[i]->len ) );
4792 break;
4793 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004794 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004795
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004796 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004797 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004798 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004799 expected_capacity = requested_capacity;
4800
4801 /* Expansion phase. */
4802 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4803 {
4804 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004806 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004807 if( expected_capacity == 0 && output_sizes[i] == 0 )
4808 {
4809 /* Reading 0 bytes when 0 bytes are available can go either way. */
4810 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004811 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004812 continue;
4813 }
4814 else if( expected_capacity == 0 ||
4815 output_sizes[i] > expected_capacity )
4816 {
4817 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004818 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004819 expected_capacity = 0;
4820 continue;
4821 }
4822 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004823 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004824 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004825 ASSERT_COMPARE( output_buffer, output_sizes[i],
4826 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004828 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004830 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004831 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004832 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004834
4835exit:
4836 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004838 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4839 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004840 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004841}
4842/* END_CASE */
4843
4844/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004845void derive_full( int alg_arg,
4846 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004847 data_t *input1,
4848 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004849 int requested_capacity_arg )
4850{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004851 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004852 psa_algorithm_t alg = alg_arg;
4853 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004854 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004855 unsigned char output_buffer[16];
4856 size_t expected_capacity = requested_capacity;
4857 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004859
Gilles Peskine8817f612018-12-18 00:18:46 +01004860 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004861
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004862 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4863 psa_set_key_algorithm( &attributes, alg );
4864 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004865
Gilles Peskine049c7532019-05-15 20:22:09 +02004866 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4867 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004868
Janos Follathf2815ea2019-07-03 12:41:36 +01004869 if( !setup_key_derivation_wrap( &operation, handle, alg,
4870 input1->x, input1->len,
4871 input2->x, input2->len,
4872 requested_capacity ) )
4873 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004874
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004876 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004877 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004878
4879 /* Expansion phase. */
4880 while( current_capacity > 0 )
4881 {
4882 size_t read_size = sizeof( output_buffer );
4883 if( read_size > current_capacity )
4884 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004885 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004886 output_buffer,
4887 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004888 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004889 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004890 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004891 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004892 }
4893
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 /* Check that the operation refuses to go over capacity. */
4895 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004896 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004897
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004899
4900exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004902 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004903 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004904}
4905/* END_CASE */
4906
Janos Follathe60c9052019-07-03 13:51:30 +01004907/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004908void derive_key_exercise( int alg_arg,
4909 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004910 data_t *input1,
4911 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004912 int derived_type_arg,
4913 int derived_bits_arg,
4914 int derived_usage_arg,
4915 int derived_alg_arg )
4916{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004917 psa_key_handle_t base_handle = 0;
4918 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004919 psa_algorithm_t alg = alg_arg;
4920 psa_key_type_t derived_type = derived_type_arg;
4921 size_t derived_bits = derived_bits_arg;
4922 psa_key_usage_t derived_usage = derived_usage_arg;
4923 psa_algorithm_t derived_alg = derived_alg_arg;
4924 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004927 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004928
Gilles Peskine8817f612018-12-18 00:18:46 +01004929 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004930
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004931 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4932 psa_set_key_algorithm( &attributes, alg );
4933 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004934 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4935 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004936
4937 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004938 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4939 input1->x, input1->len,
4940 input2->x, input2->len, capacity ) )
4941 goto exit;
4942
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004943 psa_set_key_usage_flags( &attributes, derived_usage );
4944 psa_set_key_algorithm( &attributes, derived_alg );
4945 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004946 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004948 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004949
4950 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004951 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4952 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4953 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004954
4955 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004956 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004957 goto exit;
4958
4959exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004961 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004962 psa_destroy_key( base_handle );
4963 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004964 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004965}
4966/* END_CASE */
4967
Janos Follath42fd8882019-07-03 14:17:09 +01004968/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004969void derive_key_export( int alg_arg,
4970 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004971 data_t *input1,
4972 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004973 int bytes1_arg,
4974 int bytes2_arg )
4975{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004976 psa_key_handle_t base_handle = 0;
4977 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004978 psa_algorithm_t alg = alg_arg;
4979 size_t bytes1 = bytes1_arg;
4980 size_t bytes2 = bytes2_arg;
4981 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004983 uint8_t *output_buffer = NULL;
4984 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004985 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4986 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004987 size_t length;
4988
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004989 ASSERT_ALLOC( output_buffer, capacity );
4990 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004991 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004992
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004993 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4994 psa_set_key_algorithm( &base_attributes, alg );
4995 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004996 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4997 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004998
4999 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01005000 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5001 input1->x, input1->len,
5002 input2->x, input2->len, capacity ) )
5003 goto exit;
5004
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005006 output_buffer,
5007 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005009
5010 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01005011 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5012 input1->x, input1->len,
5013 input2->x, input2->len, capacity ) )
5014 goto exit;
5015
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005016 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5017 psa_set_key_algorithm( &derived_attributes, 0 );
5018 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005019 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005020 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005021 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005022 PSA_ASSERT( psa_export_key( derived_handle,
5023 export_buffer, bytes1,
5024 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005025 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01005026 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005027 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005028 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005029 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005030 PSA_ASSERT( psa_export_key( derived_handle,
5031 export_buffer + bytes1, bytes2,
5032 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005033 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005034
5035 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005036 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5037 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005038
5039exit:
5040 mbedtls_free( output_buffer );
5041 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005043 psa_destroy_key( base_handle );
5044 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005045 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005046}
5047/* END_CASE */
5048
5049/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005050void derive_key( int alg_arg,
5051 data_t *key_data, data_t *input1, data_t *input2,
5052 int type_arg, int bits_arg,
5053 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005054{
5055 psa_key_handle_t base_handle = 0;
5056 psa_key_handle_t derived_handle = 0;
5057 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005058 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005059 size_t bits = bits_arg;
5060 psa_status_t expected_status = expected_status_arg;
5061 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5062 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5063 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5064
5065 PSA_ASSERT( psa_crypto_init( ) );
5066
5067 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5068 psa_set_key_algorithm( &base_attributes, alg );
5069 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5070 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5071 &base_handle ) );
5072
5073 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5074 input1->x, input1->len,
5075 input2->x, input2->len, SIZE_MAX ) )
5076 goto exit;
5077
5078 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5079 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005080 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005081 psa_set_key_bits( &derived_attributes, bits );
5082 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
5083 &derived_handle ),
5084 expected_status );
5085
5086exit:
5087 psa_key_derivation_abort( &operation );
5088 psa_destroy_key( base_handle );
5089 psa_destroy_key( derived_handle );
5090 PSA_DONE( );
5091}
5092/* END_CASE */
5093
5094/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005095void key_agreement_setup( int alg_arg,
5096 int our_key_type_arg, data_t *our_key_data,
5097 data_t *peer_key_data,
5098 int expected_status_arg )
5099{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005100 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005101 psa_algorithm_t alg = alg_arg;
5102 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005103 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005105 psa_status_t expected_status = expected_status_arg;
5106 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005107
Gilles Peskine8817f612018-12-18 00:18:46 +01005108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005109
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005110 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5111 psa_set_key_algorithm( &attributes, alg );
5112 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005113 PSA_ASSERT( psa_import_key( &attributes,
5114 our_key_data->x, our_key_data->len,
5115 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005116
Gilles Peskine77f40d82019-04-11 21:27:06 +02005117 /* The tests currently include inputs that should fail at either step.
5118 * Test cases that fail at the setup step should be changed to call
5119 * key_derivation_setup instead, and this function should be renamed
5120 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005122 if( status == PSA_SUCCESS )
5123 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005124 TEST_EQUAL( psa_key_derivation_key_agreement(
5125 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5126 our_key,
5127 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005128 expected_status );
5129 }
5130 else
5131 {
5132 TEST_ASSERT( status == expected_status );
5133 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005134
5135exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005137 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005138 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005139}
5140/* END_CASE */
5141
5142/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005143void raw_key_agreement( int alg_arg,
5144 int our_key_type_arg, data_t *our_key_data,
5145 data_t *peer_key_data,
5146 data_t *expected_output )
5147{
5148 psa_key_handle_t our_key = 0;
5149 psa_algorithm_t alg = alg_arg;
5150 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005152 unsigned char *output = NULL;
5153 size_t output_length = ~0;
5154
5155 ASSERT_ALLOC( output, expected_output->len );
5156 PSA_ASSERT( psa_crypto_init( ) );
5157
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005158 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5159 psa_set_key_algorithm( &attributes, alg );
5160 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005161 PSA_ASSERT( psa_import_key( &attributes,
5162 our_key_data->x, our_key_data->len,
5163 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005164
Gilles Peskinebe697d82019-05-16 18:00:41 +02005165 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5166 peer_key_data->x, peer_key_data->len,
5167 output, expected_output->len,
5168 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005169 ASSERT_COMPARE( output, output_length,
5170 expected_output->x, expected_output->len );
5171
5172exit:
5173 mbedtls_free( output );
5174 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005175 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005176}
5177/* END_CASE */
5178
5179/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005180void key_agreement_capacity( int alg_arg,
5181 int our_key_type_arg, data_t *our_key_data,
5182 data_t *peer_key_data,
5183 int expected_capacity_arg )
5184{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005185 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005186 psa_algorithm_t alg = alg_arg;
5187 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005188 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005190 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005191 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005192
Gilles Peskine8817f612018-12-18 00:18:46 +01005193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005194
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005195 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5196 psa_set_key_algorithm( &attributes, alg );
5197 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005198 PSA_ASSERT( psa_import_key( &attributes,
5199 our_key_data->x, our_key_data->len,
5200 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005201
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005203 PSA_ASSERT( psa_key_derivation_key_agreement(
5204 &operation,
5205 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5206 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005207 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5208 {
5209 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005211 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005212 NULL, 0 ) );
5213 }
Gilles Peskine59685592018-09-18 12:11:34 +02005214
Gilles Peskinebf491972018-10-25 22:36:12 +02005215 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005216 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005218 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005219
Gilles Peskinebf491972018-10-25 22:36:12 +02005220 /* Test the actual capacity by reading the output. */
5221 while( actual_capacity > sizeof( output ) )
5222 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005224 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005225 actual_capacity -= sizeof( output );
5226 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005227 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005228 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005229 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005230 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005231
Gilles Peskine59685592018-09-18 12:11:34 +02005232exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005233 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005234 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005235 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005236}
5237/* END_CASE */
5238
5239/* BEGIN_CASE */
5240void key_agreement_output( int alg_arg,
5241 int our_key_type_arg, data_t *our_key_data,
5242 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005243 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005244{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005245 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005246 psa_algorithm_t alg = alg_arg;
5247 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005248 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005250 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005251
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005252 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5253 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005254
Gilles Peskine8817f612018-12-18 00:18:46 +01005255 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005256
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5258 psa_set_key_algorithm( &attributes, alg );
5259 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005260 PSA_ASSERT( psa_import_key( &attributes,
5261 our_key_data->x, our_key_data->len,
5262 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005263
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005265 PSA_ASSERT( psa_key_derivation_key_agreement(
5266 &operation,
5267 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5268 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005269 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5270 {
5271 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005273 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005274 NULL, 0 ) );
5275 }
Gilles Peskine59685592018-09-18 12:11:34 +02005276
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005278 actual_output,
5279 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005280 ASSERT_COMPARE( actual_output, expected_output1->len,
5281 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005282 if( expected_output2->len != 0 )
5283 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005285 actual_output,
5286 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005287 ASSERT_COMPARE( actual_output, expected_output2->len,
5288 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005289 }
Gilles Peskine59685592018-09-18 12:11:34 +02005290
5291exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005292 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005293 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005294 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005295 mbedtls_free( actual_output );
5296}
5297/* END_CASE */
5298
5299/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005300void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005301{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005302 size_t bytes = bytes_arg;
5303 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005304 unsigned char *output = NULL;
5305 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005306 size_t i;
5307 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005308
Simon Butcher49f8e312020-03-03 15:51:50 +00005309 TEST_ASSERT( bytes_arg >= 0 );
5310
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005311 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5312 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005313 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005314
Gilles Peskine8817f612018-12-18 00:18:46 +01005315 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005316
Gilles Peskinea50d7392018-06-21 10:22:13 +02005317 /* Run several times, to ensure that every output byte will be
5318 * nonzero at least once with overwhelming probability
5319 * (2^(-8*number_of_runs)). */
5320 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005321 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005322 if( bytes != 0 )
5323 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005324 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005325
5326 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005327 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5328 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005329
5330 for( i = 0; i < bytes; i++ )
5331 {
5332 if( output[i] != 0 )
5333 ++changed[i];
5334 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005335 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005336
5337 /* Check that every byte was changed to nonzero at least once. This
5338 * validates that psa_generate_random is overwriting every byte of
5339 * the output buffer. */
5340 for( i = 0; i < bytes; i++ )
5341 {
5342 TEST_ASSERT( changed[i] != 0 );
5343 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005344
5345exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005346 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005347 mbedtls_free( output );
5348 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005349}
5350/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005351
5352/* BEGIN_CASE */
5353void generate_key( int type_arg,
5354 int bits_arg,
5355 int usage_arg,
5356 int alg_arg,
5357 int expected_status_arg )
5358{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005359 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005360 psa_key_type_t type = type_arg;
5361 psa_key_usage_t usage = usage_arg;
5362 size_t bits = bits_arg;
5363 psa_algorithm_t alg = alg_arg;
5364 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005366 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005367
Gilles Peskine8817f612018-12-18 00:18:46 +01005368 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005369
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005370 psa_set_key_usage_flags( &attributes, usage );
5371 psa_set_key_algorithm( &attributes, alg );
5372 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005373 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005374
5375 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005376 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005377 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005378 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005379
5380 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005381 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5382 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5383 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005384
Gilles Peskine818ca122018-06-20 18:16:48 +02005385 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005386 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005387 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005388
5389exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005390 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005391 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005392 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005393}
5394/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005395
Gilles Peskinee56e8782019-04-26 17:34:02 +02005396/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5397void generate_key_rsa( int bits_arg,
5398 data_t *e_arg,
5399 int expected_status_arg )
5400{
5401 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005402 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005403 size_t bits = bits_arg;
5404 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5405 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5406 psa_status_t expected_status = expected_status_arg;
5407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5408 uint8_t *exported = NULL;
5409 size_t exported_size =
5410 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5411 size_t exported_length = SIZE_MAX;
5412 uint8_t *e_read_buffer = NULL;
5413 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005414 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005415 size_t e_read_length = SIZE_MAX;
5416
5417 if( e_arg->len == 0 ||
5418 ( e_arg->len == 3 &&
5419 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5420 {
5421 is_default_public_exponent = 1;
5422 e_read_size = 0;
5423 }
5424 ASSERT_ALLOC( e_read_buffer, e_read_size );
5425 ASSERT_ALLOC( exported, exported_size );
5426
5427 PSA_ASSERT( psa_crypto_init( ) );
5428
5429 psa_set_key_usage_flags( &attributes, usage );
5430 psa_set_key_algorithm( &attributes, alg );
5431 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5432 e_arg->x, e_arg->len ) );
5433 psa_set_key_bits( &attributes, bits );
5434
5435 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005436 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005437 if( expected_status != PSA_SUCCESS )
5438 goto exit;
5439
5440 /* Test the key information */
5441 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5442 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5443 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5444 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5445 e_read_buffer, e_read_size,
5446 &e_read_length ) );
5447 if( is_default_public_exponent )
5448 TEST_EQUAL( e_read_length, 0 );
5449 else
5450 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5451
5452 /* Do something with the key according to its type and permitted usage. */
5453 if( ! exercise_key( handle, usage, alg ) )
5454 goto exit;
5455
5456 /* Export the key and check the public exponent. */
5457 PSA_ASSERT( psa_export_public_key( handle,
5458 exported, exported_size,
5459 &exported_length ) );
5460 {
5461 uint8_t *p = exported;
5462 uint8_t *end = exported + exported_length;
5463 size_t len;
5464 /* RSAPublicKey ::= SEQUENCE {
5465 * modulus INTEGER, -- n
5466 * publicExponent INTEGER } -- e
5467 */
5468 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005469 MBEDTLS_ASN1_SEQUENCE |
5470 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005471 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5472 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5473 MBEDTLS_ASN1_INTEGER ) );
5474 if( len >= 1 && p[0] == 0 )
5475 {
5476 ++p;
5477 --len;
5478 }
5479 if( e_arg->len == 0 )
5480 {
5481 TEST_EQUAL( len, 3 );
5482 TEST_EQUAL( p[0], 1 );
5483 TEST_EQUAL( p[1], 0 );
5484 TEST_EQUAL( p[2], 1 );
5485 }
5486 else
5487 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5488 }
5489
5490exit:
5491 psa_reset_key_attributes( &attributes );
5492 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005493 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005494 mbedtls_free( e_read_buffer );
5495 mbedtls_free( exported );
5496}
5497/* END_CASE */
5498
Darryl Greend49a4992018-06-18 17:27:26 +01005499/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005500void persistent_key_load_key_from_storage( data_t *data,
5501 int type_arg, int bits_arg,
5502 int usage_flags_arg, int alg_arg,
5503 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005504{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005505 psa_key_id_t key_id = 1;
5506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005507 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005508 psa_key_handle_t base_key = 0;
5509 psa_key_type_t type = type_arg;
5510 size_t bits = bits_arg;
5511 psa_key_usage_t usage_flags = usage_flags_arg;
5512 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005513 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005514 unsigned char *first_export = NULL;
5515 unsigned char *second_export = NULL;
5516 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5517 size_t first_exported_length;
5518 size_t second_exported_length;
5519
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005520 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5521 {
5522 ASSERT_ALLOC( first_export, export_size );
5523 ASSERT_ALLOC( second_export, export_size );
5524 }
Darryl Greend49a4992018-06-18 17:27:26 +01005525
Gilles Peskine8817f612018-12-18 00:18:46 +01005526 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005527
Gilles Peskinec87af662019-05-15 16:12:22 +02005528 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005529 psa_set_key_usage_flags( &attributes, usage_flags );
5530 psa_set_key_algorithm( &attributes, alg );
5531 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005532 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005533
Darryl Green0c6575a2018-11-07 16:05:30 +00005534 switch( generation_method )
5535 {
5536 case IMPORT_KEY:
5537 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005538 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5539 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005540 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005541
Darryl Green0c6575a2018-11-07 16:05:30 +00005542 case GENERATE_KEY:
5543 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005544 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005545 break;
5546
5547 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005548 {
5549 /* Create base key */
5550 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5551 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5552 psa_set_key_usage_flags( &base_attributes,
5553 PSA_KEY_USAGE_DERIVE );
5554 psa_set_key_algorithm( &base_attributes, derive_alg );
5555 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005556 PSA_ASSERT( psa_import_key( &base_attributes,
5557 data->x, data->len,
5558 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005559 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005561 PSA_ASSERT( psa_key_derivation_input_key(
5562 &operation,
5563 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005564 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005566 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005567 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5568 &operation,
5569 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005570 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005571 PSA_ASSERT( psa_destroy_key( base_key ) );
5572 base_key = 0;
5573 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005574 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005575 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005576 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005577
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005578 /* Export the key if permitted by the key policy. */
5579 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5580 {
5581 PSA_ASSERT( psa_export_key( handle,
5582 first_export, export_size,
5583 &first_exported_length ) );
5584 if( generation_method == IMPORT_KEY )
5585 ASSERT_COMPARE( data->x, data->len,
5586 first_export, first_exported_length );
5587 }
Darryl Greend49a4992018-06-18 17:27:26 +01005588
5589 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005590 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005591 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005592 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005593
Darryl Greend49a4992018-06-18 17:27:26 +01005594 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005595 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005596 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5597 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5598 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5599 PSA_KEY_LIFETIME_PERSISTENT );
5600 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5601 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5602 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5603 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005604
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005605 /* Export the key again if permitted by the key policy. */
5606 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005607 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005608 PSA_ASSERT( psa_export_key( handle,
5609 second_export, export_size,
5610 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005611 ASSERT_COMPARE( first_export, first_exported_length,
5612 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005613 }
5614
5615 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005616 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005617 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005618
5619exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005620 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005621 mbedtls_free( first_export );
5622 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005623 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005624 psa_destroy_key( base_key );
5625 if( handle == 0 )
5626 {
5627 /* In case there was a test failure after creating the persistent key
5628 * but while it was not open, try to re-open the persistent key
5629 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005630 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005631 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005632 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005633 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005634}
5635/* END_CASE */