blob: 4576b8bb4c848293accf862ad242db24c16ea208 [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
Paul Elliott75e27032020-06-03 15:17:39 +01001302 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1303 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1304 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1305 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1306 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1307
1308 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1309 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001310#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001311}
1312/* END_CASE */
1313
1314/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001315void attributes_set_get( int id_arg, int lifetime_arg,
1316 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001317 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001318{
Gilles Peskine4747d192019-04-17 15:05:45 +02001319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001320 psa_key_id_t id = id_arg;
1321 psa_key_lifetime_t lifetime = lifetime_arg;
1322 psa_key_usage_t usage_flags = usage_flags_arg;
1323 psa_algorithm_t alg = alg_arg;
1324 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001325 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001326
1327 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1328 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1329 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1330 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1331 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001332 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001333
Gilles Peskinec87af662019-05-15 16:12:22 +02001334 psa_set_key_id( &attributes, id );
1335 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001336 psa_set_key_usage_flags( &attributes, usage_flags );
1337 psa_set_key_algorithm( &attributes, alg );
1338 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001339 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001340
1341 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1342 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1343 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1344 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1345 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001346 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001347
1348 psa_reset_key_attributes( &attributes );
1349
1350 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1351 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1352 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1353 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1354 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001355 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001356}
1357/* END_CASE */
1358
1359/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001360void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1361 int expected_id_arg, int expected_lifetime_arg )
1362{
1363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1364 psa_key_id_t id1 = id1_arg;
1365 psa_key_lifetime_t lifetime = lifetime_arg;
1366 psa_key_id_t id2 = id2_arg;
1367 psa_key_id_t expected_id = expected_id_arg;
1368 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1369
1370 if( id1_arg != -1 )
1371 psa_set_key_id( &attributes, id1 );
1372 if( lifetime_arg != -1 )
1373 psa_set_key_lifetime( &attributes, lifetime );
1374 if( id2_arg != -1 )
1375 psa_set_key_id( &attributes, id2 );
1376
1377 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1378 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1379}
1380/* END_CASE */
1381
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001382/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1383void slot_number_attribute( )
1384{
1385 psa_key_slot_number_t slot_number = 0xdeadbeef;
1386 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1387
1388 /* Initially, there is no slot number. */
1389 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1390 PSA_ERROR_INVALID_ARGUMENT );
1391
1392 /* Test setting a slot number. */
1393 psa_set_key_slot_number( &attributes, 0 );
1394 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1395 TEST_EQUAL( slot_number, 0 );
1396
1397 /* Test changing the slot number. */
1398 psa_set_key_slot_number( &attributes, 42 );
1399 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1400 TEST_EQUAL( slot_number, 42 );
1401
1402 /* Test clearing the slot number. */
1403 psa_clear_key_slot_number( &attributes );
1404 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1405 PSA_ERROR_INVALID_ARGUMENT );
1406
1407 /* Clearing again should have no effect. */
1408 psa_clear_key_slot_number( &attributes );
1409 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1410 PSA_ERROR_INVALID_ARGUMENT );
1411
1412 /* Test that reset clears the slot number. */
1413 psa_set_key_slot_number( &attributes, 42 );
1414 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1415 TEST_EQUAL( slot_number, 42 );
1416 psa_reset_key_attributes( &attributes );
1417 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1418 PSA_ERROR_INVALID_ARGUMENT );
1419}
1420/* END_CASE */
1421
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001422/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001423void import_with_policy( int type_arg,
1424 int usage_arg, int alg_arg,
1425 int expected_status_arg )
1426{
1427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1428 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1429 psa_key_handle_t handle = 0;
1430 psa_key_type_t type = type_arg;
1431 psa_key_usage_t usage = usage_arg;
1432 psa_algorithm_t alg = alg_arg;
1433 psa_status_t expected_status = expected_status_arg;
1434 const uint8_t key_material[16] = {0};
1435 psa_status_t status;
1436
1437 PSA_ASSERT( psa_crypto_init( ) );
1438
1439 psa_set_key_type( &attributes, type );
1440 psa_set_key_usage_flags( &attributes, usage );
1441 psa_set_key_algorithm( &attributes, alg );
1442
1443 status = psa_import_key( &attributes,
1444 key_material, sizeof( key_material ),
1445 &handle );
1446 TEST_EQUAL( status, expected_status );
1447 if( status != PSA_SUCCESS )
1448 goto exit;
1449
1450 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1451 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1452 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1453 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001454 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001455
1456 PSA_ASSERT( psa_destroy_key( handle ) );
1457 test_operations_on_invalid_handle( handle );
1458
1459exit:
1460 psa_destroy_key( handle );
1461 psa_reset_key_attributes( &got_attributes );
1462 PSA_DONE( );
1463}
1464/* END_CASE */
1465
1466/* BEGIN_CASE */
1467void import_with_data( data_t *data, int type_arg,
1468 int attr_bits_arg,
1469 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001470{
1471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1472 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001473 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001474 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001475 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001476 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001477 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001478
Gilles Peskine8817f612018-12-18 00:18:46 +01001479 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001480
Gilles Peskine4747d192019-04-17 15:05:45 +02001481 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001482 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001483
Gilles Peskine73676cb2019-05-15 20:15:10 +02001484 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001485 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001486 if( status != PSA_SUCCESS )
1487 goto exit;
1488
1489 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1490 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001491 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001492 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001493 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001494
1495 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001496 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001497
1498exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001499 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001500 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001501 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502}
1503/* END_CASE */
1504
1505/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001506void import_large_key( int type_arg, int byte_size_arg,
1507 int expected_status_arg )
1508{
1509 psa_key_type_t type = type_arg;
1510 size_t byte_size = byte_size_arg;
1511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1512 psa_status_t expected_status = expected_status_arg;
1513 psa_key_handle_t handle = 0;
1514 psa_status_t status;
1515 uint8_t *buffer = NULL;
1516 size_t buffer_size = byte_size + 1;
1517 size_t n;
1518
1519 /* It would be better to skip the test than fail it if the allocation
1520 * fails, but the test framework doesn't support this yet. */
1521 ASSERT_ALLOC( buffer, buffer_size );
1522 memset( buffer, 'K', byte_size );
1523
1524 PSA_ASSERT( psa_crypto_init( ) );
1525
1526 /* Try importing the key */
1527 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1528 psa_set_key_type( &attributes, type );
1529 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1530 TEST_EQUAL( status, expected_status );
1531
1532 if( status == PSA_SUCCESS )
1533 {
1534 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1535 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1536 TEST_EQUAL( psa_get_key_bits( &attributes ),
1537 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001538 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001539 memset( buffer, 0, byte_size + 1 );
1540 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1541 for( n = 0; n < byte_size; n++ )
1542 TEST_EQUAL( buffer[n], 'K' );
1543 for( n = byte_size; n < buffer_size; n++ )
1544 TEST_EQUAL( buffer[n], 0 );
1545 }
1546
1547exit:
1548 psa_destroy_key( handle );
1549 PSA_DONE( );
1550 mbedtls_free( buffer );
1551}
1552/* END_CASE */
1553
1554/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001555void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1556{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001557 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001558 size_t bits = bits_arg;
1559 psa_status_t expected_status = expected_status_arg;
1560 psa_status_t status;
1561 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001562 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001563 size_t buffer_size = /* Slight overapproximations */
1564 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001565 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001566 unsigned char *p;
1567 int ret;
1568 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001570
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001572 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001573
1574 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1575 bits, keypair ) ) >= 0 );
1576 length = ret;
1577
1578 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001579 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001580 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001581 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001582
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001583 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001584 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001585
1586exit:
1587 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001588 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001589}
1590/* END_CASE */
1591
1592/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001593void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001594 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001595 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001596 int expected_bits,
1597 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001598 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001599 int canonical_input )
1600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001602 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001603 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001604 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001605 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001606 unsigned char *exported = NULL;
1607 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001608 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001609 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001610 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001612 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001613
Moran Pekercb088e72018-07-17 17:36:59 +03001614 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001615 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001616 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001617 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001618 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001619
Gilles Peskine4747d192019-04-17 15:05:45 +02001620 psa_set_key_usage_flags( &attributes, usage_arg );
1621 psa_set_key_algorithm( &attributes, alg );
1622 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001623
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001624 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001625 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001626
1627 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001628 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1629 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1630 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001631 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001632
1633 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001634 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001635 exported, export_size,
1636 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001637 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001638
1639 /* The exported length must be set by psa_export_key() to a value between 0
1640 * and export_size. On errors, the exported length must be 0. */
1641 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1642 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1643 TEST_ASSERT( exported_length <= export_size );
1644
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001645 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001646 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001647 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001648 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001649 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001650 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001651 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001652
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001653 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001654 goto exit;
1655
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001656 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001657 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001658 else
1659 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001660 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001661 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1662 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001663 PSA_ASSERT( psa_export_key( handle2,
1664 reexported,
1665 export_size,
1666 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001667 ASSERT_COMPARE( exported, exported_length,
1668 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001669 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001670 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001671 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001672
1673destroy:
1674 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001675 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001676 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001677
1678exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001679 mbedtls_free( exported );
1680 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001681 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001682 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001683}
1684/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001685
Moran Pekerf709f4a2018-06-06 17:26:04 +03001686/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001687void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001688 int type_arg,
1689 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001690 int export_size_delta,
1691 int expected_export_status_arg,
1692 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001693{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001694 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001695 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001696 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001697 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001698 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001699 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001700 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001701 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001703
Gilles Peskine8817f612018-12-18 00:18:46 +01001704 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001705
Gilles Peskine4747d192019-04-17 15:05:45 +02001706 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1707 psa_set_key_algorithm( &attributes, alg );
1708 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001709
1710 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001711 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001712
Gilles Peskine49c25912018-10-29 15:15:31 +01001713 /* Export the public key */
1714 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001715 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001716 exported, export_size,
1717 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001718 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001719 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001720 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001721 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001722 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001723 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1724 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001725 TEST_ASSERT( expected_public_key->len <=
1726 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001727 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1728 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001729 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001730
1731exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001732 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001733 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001734 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001735 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001736}
1737/* END_CASE */
1738
Gilles Peskine20035e32018-02-03 22:44:14 +01001739/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001740void import_and_exercise_key( data_t *data,
1741 int type_arg,
1742 int bits_arg,
1743 int alg_arg )
1744{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001745 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001746 psa_key_type_t type = type_arg;
1747 size_t bits = bits_arg;
1748 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001749 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001750 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001751 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001752
Gilles Peskine8817f612018-12-18 00:18:46 +01001753 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001754
Gilles Peskine4747d192019-04-17 15:05:45 +02001755 psa_set_key_usage_flags( &attributes, usage );
1756 psa_set_key_algorithm( &attributes, alg );
1757 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001758
1759 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001760 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001761
1762 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001763 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1764 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1765 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001766
1767 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001768 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001769 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001770
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001771 PSA_ASSERT( psa_destroy_key( handle ) );
1772 test_operations_on_invalid_handle( handle );
1773
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001774exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001775 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001776 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001777 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001778}
1779/* END_CASE */
1780
1781/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001782void effective_key_attributes( int type_arg, int expected_type_arg,
1783 int bits_arg, int expected_bits_arg,
1784 int usage_arg, int expected_usage_arg,
1785 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001786{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001787 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001788 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001789 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001790 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001791 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001792 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001793 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001794 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001795 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001797
Gilles Peskine8817f612018-12-18 00:18:46 +01001798 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001799
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001800 psa_set_key_usage_flags( &attributes, usage );
1801 psa_set_key_algorithm( &attributes, alg );
1802 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001803 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001804
Gilles Peskine1a960492019-11-26 17:12:21 +01001805 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1806 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001807
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001808 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001809 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1810 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1811 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1812 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001813
1814exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001815 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001816 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001817 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001818}
1819/* END_CASE */
1820
1821/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001822void check_key_policy( int type_arg, int bits_arg,
1823 int usage_arg, int alg_arg )
1824{
1825 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1826 usage_arg, usage_arg, alg_arg, alg_arg );
1827 goto exit;
1828}
1829/* END_CASE */
1830
1831/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001832void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001833{
1834 /* Test each valid way of initializing the object, except for `= {0}`, as
1835 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1836 * though it's OK by the C standard. We could test for this, but we'd need
1837 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001838 psa_key_attributes_t func = psa_key_attributes_init( );
1839 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1840 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001841
1842 memset( &zero, 0, sizeof( zero ) );
1843
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001844 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1845 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1846 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001847
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001848 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1849 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1850 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1851
1852 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1853 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1854 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1855
1856 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1857 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1858 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1859
1860 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1861 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1862 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001863}
1864/* END_CASE */
1865
1866/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001867void mac_key_policy( int policy_usage,
1868 int policy_alg,
1869 int key_type,
1870 data_t *key_data,
1871 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001872{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001873 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001874 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001875 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001876 psa_status_t status;
1877 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001878
Gilles Peskine8817f612018-12-18 00:18:46 +01001879 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001880
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001881 psa_set_key_usage_flags( &attributes, policy_usage );
1882 psa_set_key_algorithm( &attributes, policy_alg );
1883 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001884
Gilles Peskine049c7532019-05-15 20:22:09 +02001885 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1886 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001887
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001888 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001890 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001891 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001892 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001894 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001895
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001896 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001897 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001898 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001899 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001900 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001901 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001902 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001903
1904exit:
1905 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001906 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001907 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001908}
1909/* END_CASE */
1910
1911/* BEGIN_CASE */
1912void cipher_key_policy( int policy_usage,
1913 int policy_alg,
1914 int key_type,
1915 data_t *key_data,
1916 int exercise_alg )
1917{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001918 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001920 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921 psa_status_t status;
1922
Gilles Peskine8817f612018-12-18 00:18:46 +01001923 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001925 psa_set_key_usage_flags( &attributes, policy_usage );
1926 psa_set_key_algorithm( &attributes, policy_alg );
1927 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001928
Gilles Peskine049c7532019-05-15 20:22:09 +02001929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1930 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001932 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001933 if( policy_alg == exercise_alg &&
1934 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001935 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001936 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001937 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001938 psa_cipher_abort( &operation );
1939
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001940 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941 if( policy_alg == exercise_alg &&
1942 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001943 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001944 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001945 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946
1947exit:
1948 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001949 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001950 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951}
1952/* END_CASE */
1953
1954/* BEGIN_CASE */
1955void aead_key_policy( int policy_usage,
1956 int policy_alg,
1957 int key_type,
1958 data_t *key_data,
1959 int nonce_length_arg,
1960 int tag_length_arg,
1961 int exercise_alg )
1962{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001963 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001965 psa_status_t status;
1966 unsigned char nonce[16] = {0};
1967 size_t nonce_length = nonce_length_arg;
1968 unsigned char tag[16];
1969 size_t tag_length = tag_length_arg;
1970 size_t output_length;
1971
1972 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1973 TEST_ASSERT( tag_length <= sizeof( tag ) );
1974
Gilles Peskine8817f612018-12-18 00:18:46 +01001975 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001976
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001977 psa_set_key_usage_flags( &attributes, policy_usage );
1978 psa_set_key_algorithm( &attributes, policy_alg );
1979 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001980
Gilles Peskine049c7532019-05-15 20:22:09 +02001981 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1982 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001984 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985 nonce, nonce_length,
1986 NULL, 0,
1987 NULL, 0,
1988 tag, tag_length,
1989 &output_length );
1990 if( policy_alg == exercise_alg &&
1991 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001992 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001993 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001994 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995
1996 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001997 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001998 nonce, nonce_length,
1999 NULL, 0,
2000 tag, tag_length,
2001 NULL, 0,
2002 &output_length );
2003 if( policy_alg == exercise_alg &&
2004 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002005 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002007 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002008
2009exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002010 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002011 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002012}
2013/* END_CASE */
2014
2015/* BEGIN_CASE */
2016void asymmetric_encryption_key_policy( int policy_usage,
2017 int policy_alg,
2018 int key_type,
2019 data_t *key_data,
2020 int exercise_alg )
2021{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002022 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 psa_status_t status;
2025 size_t key_bits;
2026 size_t buffer_length;
2027 unsigned char *buffer = NULL;
2028 size_t output_length;
2029
Gilles Peskine8817f612018-12-18 00:18:46 +01002030 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002031
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002032 psa_set_key_usage_flags( &attributes, policy_usage );
2033 psa_set_key_algorithm( &attributes, policy_alg );
2034 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035
Gilles Peskine049c7532019-05-15 20:22:09 +02002036 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2037 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002039 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
2040 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2042 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002043 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002045 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046 NULL, 0,
2047 NULL, 0,
2048 buffer, buffer_length,
2049 &output_length );
2050 if( policy_alg == exercise_alg &&
2051 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002052 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002053 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002054 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002055
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002056 if( buffer_length != 0 )
2057 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002058 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002059 buffer, buffer_length,
2060 NULL, 0,
2061 buffer, buffer_length,
2062 &output_length );
2063 if( policy_alg == exercise_alg &&
2064 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002065 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002067 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002068
2069exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002070 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002071 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002072 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073 mbedtls_free( buffer );
2074}
2075/* END_CASE */
2076
2077/* BEGIN_CASE */
2078void asymmetric_signature_key_policy( int policy_usage,
2079 int policy_alg,
2080 int key_type,
2081 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002082 int exercise_alg,
2083 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002085 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002086 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002088 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2089 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2090 * compatible with the policy and `payload_length_arg` is supposed to be
2091 * a valid input length to sign. If `payload_length_arg <= 0`,
2092 * `exercise_alg` is supposed to be forbidden by the policy. */
2093 int compatible_alg = payload_length_arg > 0;
2094 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002095 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096 size_t signature_length;
2097
Gilles Peskine8817f612018-12-18 00:18:46 +01002098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002100 psa_set_key_usage_flags( &attributes, policy_usage );
2101 psa_set_key_algorithm( &attributes, policy_alg );
2102 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103
Gilles Peskine049c7532019-05-15 20:22:09 +02002104 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2105 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002106
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002107 status = psa_sign_hash( handle, exercise_alg,
2108 payload, payload_length,
2109 signature, sizeof( signature ),
2110 &signature_length );
2111 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002112 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002114 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002115
2116 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002117 status = psa_verify_hash( handle, exercise_alg,
2118 payload, payload_length,
2119 signature, sizeof( signature ) );
2120 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002121 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002123 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002124
2125exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002126 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002127 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002128}
2129/* END_CASE */
2130
Janos Follathba3fab92019-06-11 14:50:16 +01002131/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002132void derive_key_policy( int policy_usage,
2133 int policy_alg,
2134 int key_type,
2135 data_t *key_data,
2136 int exercise_alg )
2137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002138 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002140 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002141 psa_status_t status;
2142
Gilles Peskine8817f612018-12-18 00:18:46 +01002143 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002144
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002145 psa_set_key_usage_flags( &attributes, policy_usage );
2146 psa_set_key_algorithm( &attributes, policy_alg );
2147 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002148
Gilles Peskine049c7532019-05-15 20:22:09 +02002149 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2150 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002151
Janos Follathba3fab92019-06-11 14:50:16 +01002152 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2153
2154 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2155 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002156 {
Janos Follathba3fab92019-06-11 14:50:16 +01002157 PSA_ASSERT( psa_key_derivation_input_bytes(
2158 &operation,
2159 PSA_KEY_DERIVATION_INPUT_SEED,
2160 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002161 }
Janos Follathba3fab92019-06-11 14:50:16 +01002162
2163 status = psa_key_derivation_input_key( &operation,
2164 PSA_KEY_DERIVATION_INPUT_SECRET,
2165 handle );
2166
Gilles Peskineea0fb492018-07-12 17:17:20 +02002167 if( policy_alg == exercise_alg &&
2168 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002169 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002170 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002171 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002172
2173exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002174 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002175 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002176 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002177}
2178/* END_CASE */
2179
2180/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002181void agreement_key_policy( int policy_usage,
2182 int policy_alg,
2183 int key_type_arg,
2184 data_t *key_data,
2185 int exercise_alg )
2186{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002187 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002189 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002190 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002191 psa_status_t status;
2192
Gilles Peskine8817f612018-12-18 00:18:46 +01002193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002195 psa_set_key_usage_flags( &attributes, policy_usage );
2196 psa_set_key_algorithm( &attributes, policy_alg );
2197 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002198
Gilles Peskine049c7532019-05-15 20:22:09 +02002199 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2200 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002201
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002202 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2203 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002204
Gilles Peskine01d718c2018-09-18 12:01:02 +02002205 if( policy_alg == exercise_alg &&
2206 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002207 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002208 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002209 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002210
2211exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002212 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002213 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002214 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002215}
2216/* END_CASE */
2217
2218/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002219void key_policy_alg2( int key_type_arg, data_t *key_data,
2220 int usage_arg, int alg_arg, int alg2_arg )
2221{
2222 psa_key_handle_t handle = 0;
2223 psa_key_type_t key_type = key_type_arg;
2224 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2225 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2226 psa_key_usage_t usage = usage_arg;
2227 psa_algorithm_t alg = alg_arg;
2228 psa_algorithm_t alg2 = alg2_arg;
2229
2230 PSA_ASSERT( psa_crypto_init( ) );
2231
2232 psa_set_key_usage_flags( &attributes, usage );
2233 psa_set_key_algorithm( &attributes, alg );
2234 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2235 psa_set_key_type( &attributes, key_type );
2236 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2237 &handle ) );
2238
2239 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2240 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2241 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2242 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2243
2244 if( ! exercise_key( handle, usage, alg ) )
2245 goto exit;
2246 if( ! exercise_key( handle, usage, alg2 ) )
2247 goto exit;
2248
2249exit:
2250 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002251 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002252}
2253/* END_CASE */
2254
2255/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002256void raw_agreement_key_policy( int policy_usage,
2257 int policy_alg,
2258 int key_type_arg,
2259 data_t *key_data,
2260 int exercise_alg )
2261{
2262 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002264 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002265 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002266 psa_status_t status;
2267
2268 PSA_ASSERT( psa_crypto_init( ) );
2269
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002270 psa_set_key_usage_flags( &attributes, policy_usage );
2271 psa_set_key_algorithm( &attributes, policy_alg );
2272 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002273
Gilles Peskine049c7532019-05-15 20:22:09 +02002274 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2275 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002276
2277 status = raw_key_agreement_with_self( exercise_alg, handle );
2278
2279 if( policy_alg == exercise_alg &&
2280 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2281 PSA_ASSERT( status );
2282 else
2283 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2284
2285exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002286 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002287 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002288 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002289}
2290/* END_CASE */
2291
2292/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002293void copy_success( int source_usage_arg,
2294 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002295 int type_arg, data_t *material,
2296 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002297 int target_usage_arg,
2298 int target_alg_arg, int target_alg2_arg,
2299 int expected_usage_arg,
2300 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002301{
Gilles Peskineca25db92019-04-19 11:43:08 +02002302 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2303 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002304 psa_key_usage_t expected_usage = expected_usage_arg;
2305 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002306 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002307 psa_key_handle_t source_handle = 0;
2308 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002309 uint8_t *export_buffer = NULL;
2310
Gilles Peskine57ab7212019-01-28 13:03:09 +01002311 PSA_ASSERT( psa_crypto_init( ) );
2312
Gilles Peskineca25db92019-04-19 11:43:08 +02002313 /* Prepare the source key. */
2314 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2315 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002316 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002317 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002318 PSA_ASSERT( psa_import_key( &source_attributes,
2319 material->x, material->len,
2320 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002321 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002322
Gilles Peskineca25db92019-04-19 11:43:08 +02002323 /* Prepare the target attributes. */
2324 if( copy_attributes )
2325 target_attributes = source_attributes;
2326 if( target_usage_arg != -1 )
2327 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2328 if( target_alg_arg != -1 )
2329 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002330 if( target_alg2_arg != -1 )
2331 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002332
2333 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002334 PSA_ASSERT( psa_copy_key( source_handle,
2335 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002336
2337 /* Destroy the source to ensure that this doesn't affect the target. */
2338 PSA_ASSERT( psa_destroy_key( source_handle ) );
2339
2340 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002341 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2342 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2343 psa_get_key_type( &target_attributes ) );
2344 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2345 psa_get_key_bits( &target_attributes ) );
2346 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2347 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002348 TEST_EQUAL( expected_alg2,
2349 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002350 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2351 {
2352 size_t length;
2353 ASSERT_ALLOC( export_buffer, material->len );
2354 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2355 material->len, &length ) );
2356 ASSERT_COMPARE( material->x, material->len,
2357 export_buffer, length );
2358 }
2359 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2360 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002361 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2362 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002363
2364 PSA_ASSERT( psa_close_key( target_handle ) );
2365
2366exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002367 psa_reset_key_attributes( &source_attributes );
2368 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002369 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002370 mbedtls_free( export_buffer );
2371}
2372/* END_CASE */
2373
2374/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002375void copy_fail( int source_usage_arg,
2376 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002377 int type_arg, data_t *material,
2378 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002379 int target_usage_arg,
2380 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002381 int expected_status_arg )
2382{
2383 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2384 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2385 psa_key_handle_t source_handle = 0;
2386 psa_key_handle_t target_handle = 0;
2387
2388 PSA_ASSERT( psa_crypto_init( ) );
2389
2390 /* Prepare the source key. */
2391 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2392 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002393 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002394 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002395 PSA_ASSERT( psa_import_key( &source_attributes,
2396 material->x, material->len,
2397 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002398
2399 /* Prepare the target attributes. */
2400 psa_set_key_type( &target_attributes, target_type_arg );
2401 psa_set_key_bits( &target_attributes, target_bits_arg );
2402 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2403 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002404 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002405
2406 /* Try to copy the key. */
2407 TEST_EQUAL( psa_copy_key( source_handle,
2408 &target_attributes, &target_handle ),
2409 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002410
2411 PSA_ASSERT( psa_destroy_key( source_handle ) );
2412
Gilles Peskine4a644642019-05-03 17:14:08 +02002413exit:
2414 psa_reset_key_attributes( &source_attributes );
2415 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002416 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002417}
2418/* END_CASE */
2419
2420/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002421void hash_operation_init( )
2422{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002423 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002424 /* Test each valid way of initializing the object, except for `= {0}`, as
2425 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2426 * though it's OK by the C standard. We could test for this, but we'd need
2427 * to supress the Clang warning for the test. */
2428 psa_hash_operation_t func = psa_hash_operation_init( );
2429 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2430 psa_hash_operation_t zero;
2431
2432 memset( &zero, 0, sizeof( zero ) );
2433
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002434 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002435 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2436 PSA_ERROR_BAD_STATE );
2437 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2438 PSA_ERROR_BAD_STATE );
2439 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2440 PSA_ERROR_BAD_STATE );
2441
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002442 /* A default hash operation should be abortable without error. */
2443 PSA_ASSERT( psa_hash_abort( &func ) );
2444 PSA_ASSERT( psa_hash_abort( &init ) );
2445 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002446}
2447/* END_CASE */
2448
2449/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002450void hash_setup( int alg_arg,
2451 int expected_status_arg )
2452{
2453 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002454 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002455 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002456 psa_status_t status;
2457
Gilles Peskine8817f612018-12-18 00:18:46 +01002458 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002460 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002461 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002462
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002463 /* Whether setup succeeded or failed, abort must succeed. */
2464 PSA_ASSERT( psa_hash_abort( &operation ) );
2465
2466 /* If setup failed, reproduce the failure, so as to
2467 * test the resulting state of the operation object. */
2468 if( status != PSA_SUCCESS )
2469 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2470
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002471 /* Now the operation object should be reusable. */
2472#if defined(KNOWN_SUPPORTED_HASH_ALG)
2473 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2474 PSA_ASSERT( psa_hash_abort( &operation ) );
2475#endif
2476
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002477exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002478 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002479}
2480/* END_CASE */
2481
2482/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002483void hash_compute_fail( int alg_arg, data_t *input,
2484 int output_size_arg, int expected_status_arg )
2485{
2486 psa_algorithm_t alg = alg_arg;
2487 uint8_t *output = NULL;
2488 size_t output_size = output_size_arg;
2489 size_t output_length = INVALID_EXPORT_LENGTH;
2490 psa_status_t expected_status = expected_status_arg;
2491 psa_status_t status;
2492
2493 ASSERT_ALLOC( output, output_size );
2494
2495 PSA_ASSERT( psa_crypto_init( ) );
2496
2497 status = psa_hash_compute( alg, input->x, input->len,
2498 output, output_size, &output_length );
2499 TEST_EQUAL( status, expected_status );
2500 TEST_ASSERT( output_length <= output_size );
2501
2502exit:
2503 mbedtls_free( output );
2504 PSA_DONE( );
2505}
2506/* END_CASE */
2507
2508/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002509void hash_compare_fail( int alg_arg, data_t *input,
2510 data_t *reference_hash,
2511 int expected_status_arg )
2512{
2513 psa_algorithm_t alg = alg_arg;
2514 psa_status_t expected_status = expected_status_arg;
2515 psa_status_t status;
2516
2517 PSA_ASSERT( psa_crypto_init( ) );
2518
2519 status = psa_hash_compare( alg, input->x, input->len,
2520 reference_hash->x, reference_hash->len );
2521 TEST_EQUAL( status, expected_status );
2522
2523exit:
2524 PSA_DONE( );
2525}
2526/* END_CASE */
2527
2528/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002529void hash_compute_compare( int alg_arg, data_t *input,
2530 data_t *expected_output )
2531{
2532 psa_algorithm_t alg = alg_arg;
2533 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2534 size_t output_length = INVALID_EXPORT_LENGTH;
2535 size_t i;
2536
2537 PSA_ASSERT( psa_crypto_init( ) );
2538
2539 /* Compute with tight buffer */
2540 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2541 output, PSA_HASH_SIZE( alg ),
2542 &output_length ) );
2543 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2544 ASSERT_COMPARE( output, output_length,
2545 expected_output->x, expected_output->len );
2546
2547 /* Compute with larger buffer */
2548 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2549 output, sizeof( output ),
2550 &output_length ) );
2551 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2552 ASSERT_COMPARE( output, output_length,
2553 expected_output->x, expected_output->len );
2554
2555 /* Compare with correct hash */
2556 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2557 output, output_length ) );
2558
2559 /* Compare with trailing garbage */
2560 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2561 output, output_length + 1 ),
2562 PSA_ERROR_INVALID_SIGNATURE );
2563
2564 /* Compare with truncated hash */
2565 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2566 output, output_length - 1 ),
2567 PSA_ERROR_INVALID_SIGNATURE );
2568
2569 /* Compare with corrupted value */
2570 for( i = 0; i < output_length; i++ )
2571 {
2572 test_set_step( i );
2573 output[i] ^= 1;
2574 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2575 output, output_length ),
2576 PSA_ERROR_INVALID_SIGNATURE );
2577 output[i] ^= 1;
2578 }
2579
2580exit:
2581 PSA_DONE( );
2582}
2583/* END_CASE */
2584
2585/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002586void hash_bad_order( )
2587{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002588 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002589 unsigned char input[] = "";
2590 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002591 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002592 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2593 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2594 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002595 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002596 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002597 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002598
Gilles Peskine8817f612018-12-18 00:18:46 +01002599 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002600
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002601 /* Call setup twice in a row. */
2602 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2603 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2604 PSA_ERROR_BAD_STATE );
2605 PSA_ASSERT( psa_hash_abort( &operation ) );
2606
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002607 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002608 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002609 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002610 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002611
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002612 /* Call update after finish. */
2613 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2614 PSA_ASSERT( psa_hash_finish( &operation,
2615 hash, sizeof( hash ), &hash_len ) );
2616 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002617 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002618 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002619
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002620 /* Call verify without calling setup beforehand. */
2621 TEST_EQUAL( psa_hash_verify( &operation,
2622 valid_hash, sizeof( valid_hash ) ),
2623 PSA_ERROR_BAD_STATE );
2624 PSA_ASSERT( psa_hash_abort( &operation ) );
2625
2626 /* Call verify after finish. */
2627 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2628 PSA_ASSERT( psa_hash_finish( &operation,
2629 hash, sizeof( hash ), &hash_len ) );
2630 TEST_EQUAL( psa_hash_verify( &operation,
2631 valid_hash, sizeof( valid_hash ) ),
2632 PSA_ERROR_BAD_STATE );
2633 PSA_ASSERT( psa_hash_abort( &operation ) );
2634
2635 /* Call verify twice in a row. */
2636 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2637 PSA_ASSERT( psa_hash_verify( &operation,
2638 valid_hash, sizeof( valid_hash ) ) );
2639 TEST_EQUAL( psa_hash_verify( &operation,
2640 valid_hash, sizeof( valid_hash ) ),
2641 PSA_ERROR_BAD_STATE );
2642 PSA_ASSERT( psa_hash_abort( &operation ) );
2643
2644 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002645 TEST_EQUAL( psa_hash_finish( &operation,
2646 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002647 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002648 PSA_ASSERT( psa_hash_abort( &operation ) );
2649
2650 /* Call finish twice in a row. */
2651 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2652 PSA_ASSERT( psa_hash_finish( &operation,
2653 hash, sizeof( hash ), &hash_len ) );
2654 TEST_EQUAL( psa_hash_finish( &operation,
2655 hash, sizeof( hash ), &hash_len ),
2656 PSA_ERROR_BAD_STATE );
2657 PSA_ASSERT( psa_hash_abort( &operation ) );
2658
2659 /* Call finish after calling verify. */
2660 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2661 PSA_ASSERT( psa_hash_verify( &operation,
2662 valid_hash, sizeof( valid_hash ) ) );
2663 TEST_EQUAL( psa_hash_finish( &operation,
2664 hash, sizeof( hash ), &hash_len ),
2665 PSA_ERROR_BAD_STATE );
2666 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002667
2668exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002669 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002670}
2671/* END_CASE */
2672
itayzafrir27e69452018-11-01 14:26:34 +02002673/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2674void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002675{
2676 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002677 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2678 * appended to it */
2679 unsigned char hash[] = {
2680 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2681 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2682 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002683 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002684 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002685
Gilles Peskine8817f612018-12-18 00:18:46 +01002686 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002687
itayzafrir27e69452018-11-01 14:26:34 +02002688 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002689 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002690 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002691 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002692
itayzafrir27e69452018-11-01 14:26:34 +02002693 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002694 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002695 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002696 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002697
itayzafrir27e69452018-11-01 14:26:34 +02002698 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002699 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002700 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002701 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002702
itayzafrirec93d302018-10-18 18:01:10 +03002703exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002704 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002705}
2706/* END_CASE */
2707
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002708/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2709void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002710{
2711 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002712 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002713 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002714 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002715 size_t hash_len;
2716
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002718
itayzafrir58028322018-10-25 10:22:01 +03002719 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002721 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002722 hash, expected_size - 1, &hash_len ),
2723 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002724
2725exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002726 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002727}
2728/* END_CASE */
2729
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002730/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2731void hash_clone_source_state( )
2732{
2733 psa_algorithm_t alg = PSA_ALG_SHA_256;
2734 unsigned char hash[PSA_HASH_MAX_SIZE];
2735 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2736 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2737 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2738 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2739 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2740 size_t hash_len;
2741
2742 PSA_ASSERT( psa_crypto_init( ) );
2743 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2744
2745 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2746 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2747 PSA_ASSERT( psa_hash_finish( &op_finished,
2748 hash, sizeof( hash ), &hash_len ) );
2749 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2750 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2751
2752 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2753 PSA_ERROR_BAD_STATE );
2754
2755 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2756 PSA_ASSERT( psa_hash_finish( &op_init,
2757 hash, sizeof( hash ), &hash_len ) );
2758 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2759 PSA_ASSERT( psa_hash_finish( &op_finished,
2760 hash, sizeof( hash ), &hash_len ) );
2761 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2762 PSA_ASSERT( psa_hash_finish( &op_aborted,
2763 hash, sizeof( hash ), &hash_len ) );
2764
2765exit:
2766 psa_hash_abort( &op_source );
2767 psa_hash_abort( &op_init );
2768 psa_hash_abort( &op_setup );
2769 psa_hash_abort( &op_finished );
2770 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002771 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002772}
2773/* END_CASE */
2774
2775/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2776void hash_clone_target_state( )
2777{
2778 psa_algorithm_t alg = PSA_ALG_SHA_256;
2779 unsigned char hash[PSA_HASH_MAX_SIZE];
2780 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2781 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2782 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2783 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2784 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2785 size_t hash_len;
2786
2787 PSA_ASSERT( psa_crypto_init( ) );
2788
2789 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2790 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2791 PSA_ASSERT( psa_hash_finish( &op_finished,
2792 hash, sizeof( hash ), &hash_len ) );
2793 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2794 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2795
2796 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2797 PSA_ASSERT( psa_hash_finish( &op_target,
2798 hash, sizeof( hash ), &hash_len ) );
2799
2800 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2801 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2802 PSA_ERROR_BAD_STATE );
2803 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2804 PSA_ERROR_BAD_STATE );
2805
2806exit:
2807 psa_hash_abort( &op_target );
2808 psa_hash_abort( &op_init );
2809 psa_hash_abort( &op_setup );
2810 psa_hash_abort( &op_finished );
2811 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002812 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002813}
2814/* END_CASE */
2815
itayzafrir58028322018-10-25 10:22:01 +03002816/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002817void mac_operation_init( )
2818{
Jaeden Amero252ef282019-02-15 14:05:35 +00002819 const uint8_t input[1] = { 0 };
2820
Jaeden Amero769ce272019-01-04 11:48:03 +00002821 /* Test each valid way of initializing the object, except for `= {0}`, as
2822 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2823 * though it's OK by the C standard. We could test for this, but we'd need
2824 * to supress the Clang warning for the test. */
2825 psa_mac_operation_t func = psa_mac_operation_init( );
2826 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2827 psa_mac_operation_t zero;
2828
2829 memset( &zero, 0, sizeof( zero ) );
2830
Jaeden Amero252ef282019-02-15 14:05:35 +00002831 /* A freshly-initialized MAC operation should not be usable. */
2832 TEST_EQUAL( psa_mac_update( &func,
2833 input, sizeof( input ) ),
2834 PSA_ERROR_BAD_STATE );
2835 TEST_EQUAL( psa_mac_update( &init,
2836 input, sizeof( input ) ),
2837 PSA_ERROR_BAD_STATE );
2838 TEST_EQUAL( psa_mac_update( &zero,
2839 input, sizeof( input ) ),
2840 PSA_ERROR_BAD_STATE );
2841
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002842 /* A default MAC operation should be abortable without error. */
2843 PSA_ASSERT( psa_mac_abort( &func ) );
2844 PSA_ASSERT( psa_mac_abort( &init ) );
2845 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002846}
2847/* END_CASE */
2848
2849/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002850void mac_setup( int key_type_arg,
2851 data_t *key,
2852 int alg_arg,
2853 int expected_status_arg )
2854{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002855 psa_key_type_t key_type = key_type_arg;
2856 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002857 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002858 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002859 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2860#if defined(KNOWN_SUPPORTED_MAC_ALG)
2861 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2862#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002863
Gilles Peskine8817f612018-12-18 00:18:46 +01002864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002865
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002866 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2867 &operation, &status ) )
2868 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002869 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002870
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002871 /* The operation object should be reusable. */
2872#if defined(KNOWN_SUPPORTED_MAC_ALG)
2873 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2874 smoke_test_key_data,
2875 sizeof( smoke_test_key_data ),
2876 KNOWN_SUPPORTED_MAC_ALG,
2877 &operation, &status ) )
2878 goto exit;
2879 TEST_EQUAL( status, PSA_SUCCESS );
2880#endif
2881
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002882exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002883 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002884}
2885/* END_CASE */
2886
2887/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002888void mac_bad_order( )
2889{
2890 psa_key_handle_t handle = 0;
2891 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2892 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2893 const uint8_t key[] = {
2894 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2895 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2896 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002898 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2899 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2900 size_t sign_mac_length = 0;
2901 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2902 const uint8_t verify_mac[] = {
2903 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2904 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2905 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2906
2907 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002909 psa_set_key_algorithm( &attributes, alg );
2910 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002911
Gilles Peskine73676cb2019-05-15 20:15:10 +02002912 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002913
Jaeden Amero252ef282019-02-15 14:05:35 +00002914 /* Call update without calling setup beforehand. */
2915 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2916 PSA_ERROR_BAD_STATE );
2917 PSA_ASSERT( psa_mac_abort( &operation ) );
2918
2919 /* Call sign finish without calling setup beforehand. */
2920 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2921 &sign_mac_length),
2922 PSA_ERROR_BAD_STATE );
2923 PSA_ASSERT( psa_mac_abort( &operation ) );
2924
2925 /* Call verify finish without calling setup beforehand. */
2926 TEST_EQUAL( psa_mac_verify_finish( &operation,
2927 verify_mac, sizeof( verify_mac ) ),
2928 PSA_ERROR_BAD_STATE );
2929 PSA_ASSERT( psa_mac_abort( &operation ) );
2930
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002931 /* Call setup twice in a row. */
2932 PSA_ASSERT( psa_mac_sign_setup( &operation,
2933 handle, alg ) );
2934 TEST_EQUAL( psa_mac_sign_setup( &operation,
2935 handle, alg ),
2936 PSA_ERROR_BAD_STATE );
2937 PSA_ASSERT( psa_mac_abort( &operation ) );
2938
Jaeden Amero252ef282019-02-15 14:05:35 +00002939 /* Call update after sign finish. */
2940 PSA_ASSERT( psa_mac_sign_setup( &operation,
2941 handle, alg ) );
2942 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2943 PSA_ASSERT( psa_mac_sign_finish( &operation,
2944 sign_mac, sizeof( sign_mac ),
2945 &sign_mac_length ) );
2946 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2947 PSA_ERROR_BAD_STATE );
2948 PSA_ASSERT( psa_mac_abort( &operation ) );
2949
2950 /* Call update after verify finish. */
2951 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002952 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002953 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2954 PSA_ASSERT( psa_mac_verify_finish( &operation,
2955 verify_mac, sizeof( verify_mac ) ) );
2956 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2957 PSA_ERROR_BAD_STATE );
2958 PSA_ASSERT( psa_mac_abort( &operation ) );
2959
2960 /* Call sign finish twice in a row. */
2961 PSA_ASSERT( psa_mac_sign_setup( &operation,
2962 handle, alg ) );
2963 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2964 PSA_ASSERT( psa_mac_sign_finish( &operation,
2965 sign_mac, sizeof( sign_mac ),
2966 &sign_mac_length ) );
2967 TEST_EQUAL( psa_mac_sign_finish( &operation,
2968 sign_mac, sizeof( sign_mac ),
2969 &sign_mac_length ),
2970 PSA_ERROR_BAD_STATE );
2971 PSA_ASSERT( psa_mac_abort( &operation ) );
2972
2973 /* Call verify finish twice in a row. */
2974 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002975 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002976 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2977 PSA_ASSERT( psa_mac_verify_finish( &operation,
2978 verify_mac, sizeof( verify_mac ) ) );
2979 TEST_EQUAL( psa_mac_verify_finish( &operation,
2980 verify_mac, sizeof( verify_mac ) ),
2981 PSA_ERROR_BAD_STATE );
2982 PSA_ASSERT( psa_mac_abort( &operation ) );
2983
2984 /* Setup sign but try verify. */
2985 PSA_ASSERT( psa_mac_sign_setup( &operation,
2986 handle, alg ) );
2987 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2988 TEST_EQUAL( psa_mac_verify_finish( &operation,
2989 verify_mac, sizeof( verify_mac ) ),
2990 PSA_ERROR_BAD_STATE );
2991 PSA_ASSERT( psa_mac_abort( &operation ) );
2992
2993 /* Setup verify but try sign. */
2994 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002995 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002996 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2997 TEST_EQUAL( psa_mac_sign_finish( &operation,
2998 sign_mac, sizeof( sign_mac ),
2999 &sign_mac_length ),
3000 PSA_ERROR_BAD_STATE );
3001 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003002
Gilles Peskine76b29a72019-05-28 14:08:50 +02003003 PSA_ASSERT( psa_destroy_key( handle ) );
3004
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003005exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003006 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003007}
3008/* END_CASE */
3009
3010/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003011void mac_sign( int key_type_arg,
3012 data_t *key,
3013 int alg_arg,
3014 data_t *input,
3015 data_t *expected_mac )
3016{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003017 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003018 psa_key_type_t key_type = key_type_arg;
3019 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003020 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003022 /* Leave a little extra room in the output buffer. At the end of the
3023 * test, we'll check that the implementation didn't overwrite onto
3024 * this extra room. */
3025 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
3026 size_t mac_buffer_size =
3027 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
3028 size_t mac_length = 0;
3029
3030 memset( actual_mac, '+', sizeof( actual_mac ) );
3031 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
3032 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
3033
Gilles Peskine8817f612018-12-18 00:18:46 +01003034 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003035
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003036 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003037 psa_set_key_algorithm( &attributes, alg );
3038 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003039
Gilles Peskine73676cb2019-05-15 20:15:10 +02003040 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003041
3042 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003043 PSA_ASSERT( psa_mac_sign_setup( &operation,
3044 handle, alg ) );
3045 PSA_ASSERT( psa_mac_update( &operation,
3046 input->x, input->len ) );
3047 PSA_ASSERT( psa_mac_sign_finish( &operation,
3048 actual_mac, mac_buffer_size,
3049 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003050
3051 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003052 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3053 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003054
3055 /* Verify that the end of the buffer is untouched. */
3056 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
3057 sizeof( actual_mac ) - mac_length ) );
3058
3059exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003060 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003061 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003062}
3063/* END_CASE */
3064
3065/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003066void mac_verify( int key_type_arg,
3067 data_t *key,
3068 int alg_arg,
3069 data_t *input,
3070 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003071{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003072 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003073 psa_key_type_t key_type = key_type_arg;
3074 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003075 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003076 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003077
Gilles Peskine69c12672018-06-28 00:07:19 +02003078 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3079
Gilles Peskine8817f612018-12-18 00:18:46 +01003080 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003081
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003082 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003083 psa_set_key_algorithm( &attributes, alg );
3084 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003085
Gilles Peskine73676cb2019-05-15 20:15:10 +02003086 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003087
Gilles Peskine8817f612018-12-18 00:18:46 +01003088 PSA_ASSERT( psa_mac_verify_setup( &operation,
3089 handle, alg ) );
3090 PSA_ASSERT( psa_destroy_key( handle ) );
3091 PSA_ASSERT( psa_mac_update( &operation,
3092 input->x, input->len ) );
3093 PSA_ASSERT( psa_mac_verify_finish( &operation,
3094 expected_mac->x,
3095 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003096
3097exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003098 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003099 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003100}
3101/* END_CASE */
3102
3103/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003104void cipher_operation_init( )
3105{
Jaeden Ameroab439972019-02-15 14:12:05 +00003106 const uint8_t input[1] = { 0 };
3107 unsigned char output[1] = { 0 };
3108 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003109 /* Test each valid way of initializing the object, except for `= {0}`, as
3110 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3111 * though it's OK by the C standard. We could test for this, but we'd need
3112 * to supress the Clang warning for the test. */
3113 psa_cipher_operation_t func = psa_cipher_operation_init( );
3114 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3115 psa_cipher_operation_t zero;
3116
3117 memset( &zero, 0, sizeof( zero ) );
3118
Jaeden Ameroab439972019-02-15 14:12:05 +00003119 /* A freshly-initialized cipher operation should not be usable. */
3120 TEST_EQUAL( psa_cipher_update( &func,
3121 input, sizeof( input ),
3122 output, sizeof( output ),
3123 &output_length ),
3124 PSA_ERROR_BAD_STATE );
3125 TEST_EQUAL( psa_cipher_update( &init,
3126 input, sizeof( input ),
3127 output, sizeof( output ),
3128 &output_length ),
3129 PSA_ERROR_BAD_STATE );
3130 TEST_EQUAL( psa_cipher_update( &zero,
3131 input, sizeof( input ),
3132 output, sizeof( output ),
3133 &output_length ),
3134 PSA_ERROR_BAD_STATE );
3135
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003136 /* A default cipher operation should be abortable without error. */
3137 PSA_ASSERT( psa_cipher_abort( &func ) );
3138 PSA_ASSERT( psa_cipher_abort( &init ) );
3139 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003140}
3141/* END_CASE */
3142
3143/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003144void cipher_setup( int key_type_arg,
3145 data_t *key,
3146 int alg_arg,
3147 int expected_status_arg )
3148{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003149 psa_key_type_t key_type = key_type_arg;
3150 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003151 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003152 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003153 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003154#if defined(KNOWN_SUPPORTED_MAC_ALG)
3155 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3156#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003160 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3161 &operation, &status ) )
3162 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003163 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003165 /* The operation object should be reusable. */
3166#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3167 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3168 smoke_test_key_data,
3169 sizeof( smoke_test_key_data ),
3170 KNOWN_SUPPORTED_CIPHER_ALG,
3171 &operation, &status ) )
3172 goto exit;
3173 TEST_EQUAL( status, PSA_SUCCESS );
3174#endif
3175
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003176exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003177 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003178}
3179/* END_CASE */
3180
3181/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003182void cipher_bad_order( )
3183{
3184 psa_key_handle_t handle = 0;
3185 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3186 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003188 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3189 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3190 const uint8_t key[] = {
3191 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3192 0xaa, 0xaa, 0xaa, 0xaa };
3193 const uint8_t text[] = {
3194 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3195 0xbb, 0xbb, 0xbb, 0xbb };
3196 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3197 size_t length = 0;
3198
3199 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003200 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3201 psa_set_key_algorithm( &attributes, alg );
3202 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003203 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003204
3205
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003206 /* Call encrypt setup twice in a row. */
3207 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3208 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3209 PSA_ERROR_BAD_STATE );
3210 PSA_ASSERT( psa_cipher_abort( &operation ) );
3211
3212 /* Call decrypt setup twice in a row. */
3213 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3214 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3215 PSA_ERROR_BAD_STATE );
3216 PSA_ASSERT( psa_cipher_abort( &operation ) );
3217
Jaeden Ameroab439972019-02-15 14:12:05 +00003218 /* Generate an IV without calling setup beforehand. */
3219 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3220 buffer, sizeof( buffer ),
3221 &length ),
3222 PSA_ERROR_BAD_STATE );
3223 PSA_ASSERT( psa_cipher_abort( &operation ) );
3224
3225 /* Generate an IV twice in a row. */
3226 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3227 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3228 buffer, sizeof( buffer ),
3229 &length ) );
3230 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3231 buffer, sizeof( buffer ),
3232 &length ),
3233 PSA_ERROR_BAD_STATE );
3234 PSA_ASSERT( psa_cipher_abort( &operation ) );
3235
3236 /* Generate an IV after it's already set. */
3237 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3238 PSA_ASSERT( psa_cipher_set_iv( &operation,
3239 iv, sizeof( iv ) ) );
3240 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3241 buffer, sizeof( buffer ),
3242 &length ),
3243 PSA_ERROR_BAD_STATE );
3244 PSA_ASSERT( psa_cipher_abort( &operation ) );
3245
3246 /* Set an IV without calling setup beforehand. */
3247 TEST_EQUAL( psa_cipher_set_iv( &operation,
3248 iv, sizeof( iv ) ),
3249 PSA_ERROR_BAD_STATE );
3250 PSA_ASSERT( psa_cipher_abort( &operation ) );
3251
3252 /* Set an IV after it's already set. */
3253 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3254 PSA_ASSERT( psa_cipher_set_iv( &operation,
3255 iv, sizeof( iv ) ) );
3256 TEST_EQUAL( psa_cipher_set_iv( &operation,
3257 iv, sizeof( iv ) ),
3258 PSA_ERROR_BAD_STATE );
3259 PSA_ASSERT( psa_cipher_abort( &operation ) );
3260
3261 /* Set an IV after it's already generated. */
3262 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3263 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3264 buffer, sizeof( buffer ),
3265 &length ) );
3266 TEST_EQUAL( psa_cipher_set_iv( &operation,
3267 iv, sizeof( iv ) ),
3268 PSA_ERROR_BAD_STATE );
3269 PSA_ASSERT( psa_cipher_abort( &operation ) );
3270
3271 /* Call update without calling setup beforehand. */
3272 TEST_EQUAL( psa_cipher_update( &operation,
3273 text, sizeof( text ),
3274 buffer, sizeof( buffer ),
3275 &length ),
3276 PSA_ERROR_BAD_STATE );
3277 PSA_ASSERT( psa_cipher_abort( &operation ) );
3278
3279 /* Call update without an IV where an IV is required. */
3280 TEST_EQUAL( psa_cipher_update( &operation,
3281 text, sizeof( text ),
3282 buffer, sizeof( buffer ),
3283 &length ),
3284 PSA_ERROR_BAD_STATE );
3285 PSA_ASSERT( psa_cipher_abort( &operation ) );
3286
3287 /* Call update after finish. */
3288 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3289 PSA_ASSERT( psa_cipher_set_iv( &operation,
3290 iv, sizeof( iv ) ) );
3291 PSA_ASSERT( psa_cipher_finish( &operation,
3292 buffer, sizeof( buffer ), &length ) );
3293 TEST_EQUAL( psa_cipher_update( &operation,
3294 text, sizeof( text ),
3295 buffer, sizeof( buffer ),
3296 &length ),
3297 PSA_ERROR_BAD_STATE );
3298 PSA_ASSERT( psa_cipher_abort( &operation ) );
3299
3300 /* Call finish without calling setup beforehand. */
3301 TEST_EQUAL( psa_cipher_finish( &operation,
3302 buffer, sizeof( buffer ), &length ),
3303 PSA_ERROR_BAD_STATE );
3304 PSA_ASSERT( psa_cipher_abort( &operation ) );
3305
3306 /* Call finish without an IV where an IV is required. */
3307 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3308 /* Not calling update means we are encrypting an empty buffer, which is OK
3309 * for cipher modes with padding. */
3310 TEST_EQUAL( psa_cipher_finish( &operation,
3311 buffer, sizeof( buffer ), &length ),
3312 PSA_ERROR_BAD_STATE );
3313 PSA_ASSERT( psa_cipher_abort( &operation ) );
3314
3315 /* Call finish twice in a row. */
3316 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3317 PSA_ASSERT( psa_cipher_set_iv( &operation,
3318 iv, sizeof( iv ) ) );
3319 PSA_ASSERT( psa_cipher_finish( &operation,
3320 buffer, sizeof( buffer ), &length ) );
3321 TEST_EQUAL( psa_cipher_finish( &operation,
3322 buffer, sizeof( buffer ), &length ),
3323 PSA_ERROR_BAD_STATE );
3324 PSA_ASSERT( psa_cipher_abort( &operation ) );
3325
Gilles Peskine76b29a72019-05-28 14:08:50 +02003326 PSA_ASSERT( psa_destroy_key( handle ) );
3327
Jaeden Ameroab439972019-02-15 14:12:05 +00003328exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003329 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003330}
3331/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003332
Gilles Peskine50e586b2018-06-08 14:28:46 +02003333/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003334void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003335 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003336 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003337 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003338{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003339 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003340 psa_status_t status;
3341 psa_key_type_t key_type = key_type_arg;
3342 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003343 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003344 unsigned char *output = NULL;
3345 size_t output_buffer_size = 0;
3346 size_t function_output_length = 0;
3347 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003348 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003350
Gilles Peskine8817f612018-12-18 00:18:46 +01003351 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003352
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003353 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3354 psa_set_key_algorithm( &attributes, alg );
3355 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003356
Gilles Peskine73676cb2019-05-15 20:15:10 +02003357 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3360 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003361
Gilles Peskine423005e2019-05-06 15:22:57 +02003362 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003363 output_buffer_size = ( (size_t) input->len +
3364 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003365 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003366
Gilles Peskine8817f612018-12-18 00:18:46 +01003367 PSA_ASSERT( psa_cipher_update( &operation,
3368 input->x, input->len,
3369 output, output_buffer_size,
3370 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003371 total_output_length += function_output_length;
3372 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003373 output + total_output_length,
3374 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003375 &function_output_length );
3376 total_output_length += function_output_length;
3377
Gilles Peskinefe11b722018-12-18 00:24:04 +01003378 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003379 if( expected_status == PSA_SUCCESS )
3380 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003381 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003382 ASSERT_COMPARE( expected_output->x, expected_output->len,
3383 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003384 }
3385
3386exit:
3387 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003388 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003389 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003390}
3391/* END_CASE */
3392
3393/* BEGIN_CASE */
3394void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003395 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003396 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003397 int first_part_size_arg,
3398 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003399 data_t *expected_output )
3400{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003401 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003402 psa_key_type_t key_type = key_type_arg;
3403 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003404 size_t first_part_size = first_part_size_arg;
3405 size_t output1_length = output1_length_arg;
3406 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003407 unsigned char *output = NULL;
3408 size_t output_buffer_size = 0;
3409 size_t function_output_length = 0;
3410 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003411 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003413
Gilles Peskine8817f612018-12-18 00:18:46 +01003414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003415
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003416 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3417 psa_set_key_algorithm( &attributes, alg );
3418 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003419
Gilles Peskine73676cb2019-05-15 20:15:10 +02003420 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003421
Gilles Peskine8817f612018-12-18 00:18:46 +01003422 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3423 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003424
Gilles Peskine423005e2019-05-06 15:22:57 +02003425 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003426 output_buffer_size = ( (size_t) input->len +
3427 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003428 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003429
Gilles Peskinee0866522019-02-19 19:44:00 +01003430 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003431 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3432 output, output_buffer_size,
3433 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003434 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003435 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003436 PSA_ASSERT( psa_cipher_update( &operation,
3437 input->x + first_part_size,
3438 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003439 output + total_output_length,
3440 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003441 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003442 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003443 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003444 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003445 output + total_output_length,
3446 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003447 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003448 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003449 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003450
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003451 ASSERT_COMPARE( expected_output->x, expected_output->len,
3452 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003453
3454exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003455 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003456 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003457 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003458}
3459/* END_CASE */
3460
3461/* BEGIN_CASE */
3462void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003463 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003464 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003465 int first_part_size_arg,
3466 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003467 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003468{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003469 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003470
3471 psa_key_type_t key_type = key_type_arg;
3472 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003473 size_t first_part_size = first_part_size_arg;
3474 size_t output1_length = output1_length_arg;
3475 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003476 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003477 size_t output_buffer_size = 0;
3478 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003479 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003480 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003482
Gilles Peskine8817f612018-12-18 00:18:46 +01003483 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003484
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003485 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3486 psa_set_key_algorithm( &attributes, alg );
3487 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003488
Gilles Peskine73676cb2019-05-15 20:15:10 +02003489 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003490
Gilles Peskine8817f612018-12-18 00:18:46 +01003491 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3492 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003493
Gilles Peskine423005e2019-05-06 15:22:57 +02003494 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003495
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003496 output_buffer_size = ( (size_t) input->len +
3497 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003498 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003499
Gilles Peskinee0866522019-02-19 19:44:00 +01003500 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003501 PSA_ASSERT( psa_cipher_update( &operation,
3502 input->x, first_part_size,
3503 output, output_buffer_size,
3504 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003505 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003506 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003507 PSA_ASSERT( psa_cipher_update( &operation,
3508 input->x + first_part_size,
3509 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003510 output + total_output_length,
3511 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003513 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003514 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003515 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003516 output + total_output_length,
3517 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003518 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003519 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003520 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003521
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003522 ASSERT_COMPARE( expected_output->x, expected_output->len,
3523 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003524
3525exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003526 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003527 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003528 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003529}
3530/* END_CASE */
3531
Gilles Peskine50e586b2018-06-08 14:28:46 +02003532/* BEGIN_CASE */
3533void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003534 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003535 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003536 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003537{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003538 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003539 psa_status_t status;
3540 psa_key_type_t key_type = key_type_arg;
3541 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003542 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003543 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544 size_t output_buffer_size = 0;
3545 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003546 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003547 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003548 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003549
Gilles Peskine8817f612018-12-18 00:18:46 +01003550 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003552 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3553 psa_set_key_algorithm( &attributes, alg );
3554 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003555
Gilles Peskine73676cb2019-05-15 20:15:10 +02003556 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003557
Gilles Peskine8817f612018-12-18 00:18:46 +01003558 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3559 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003560
Gilles Peskine423005e2019-05-06 15:22:57 +02003561 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003562
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003563 output_buffer_size = ( (size_t) input->len +
3564 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003565 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003566
Gilles Peskine8817f612018-12-18 00:18:46 +01003567 PSA_ASSERT( psa_cipher_update( &operation,
3568 input->x, input->len,
3569 output, output_buffer_size,
3570 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003571 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003572 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003573 output + total_output_length,
3574 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003575 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003576 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003577 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003578
3579 if( expected_status == PSA_SUCCESS )
3580 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003582 ASSERT_COMPARE( expected_output->x, expected_output->len,
3583 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003584 }
3585
Gilles Peskine50e586b2018-06-08 14:28:46 +02003586exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003587 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003588 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003589 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003590}
3591/* END_CASE */
3592
Gilles Peskine50e586b2018-06-08 14:28:46 +02003593/* BEGIN_CASE */
3594void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003595 data_t *key,
3596 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003597{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003598 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003599 psa_key_type_t key_type = key_type_arg;
3600 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003601 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003602 size_t iv_size = 16;
3603 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003604 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003605 size_t output1_size = 0;
3606 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003607 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003608 size_t output2_size = 0;
3609 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003610 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003611 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3612 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003614
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003616
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3618 psa_set_key_algorithm( &attributes, alg );
3619 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003620
Gilles Peskine73676cb2019-05-15 20:15:10 +02003621 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003622
Gilles Peskine8817f612018-12-18 00:18:46 +01003623 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3624 handle, alg ) );
3625 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3626 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003627
Gilles Peskine8817f612018-12-18 00:18:46 +01003628 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3629 iv, iv_size,
3630 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003631 output1_size = ( (size_t) input->len +
3632 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003633 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003634
Gilles Peskine8817f612018-12-18 00:18:46 +01003635 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3636 output1, output1_size,
3637 &output1_length ) );
3638 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003639 output1 + output1_length,
3640 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003641 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003642
Gilles Peskine048b7f02018-06-08 14:20:49 +02003643 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003644
Gilles Peskine8817f612018-12-18 00:18:46 +01003645 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003646
3647 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003648 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003649
Gilles Peskine8817f612018-12-18 00:18:46 +01003650 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3651 iv, iv_length ) );
3652 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3653 output2, output2_size,
3654 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003655 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003656 PSA_ASSERT( psa_cipher_finish( &operation2,
3657 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003658 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003659 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003660
Gilles Peskine048b7f02018-06-08 14:20:49 +02003661 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003662
Gilles Peskine8817f612018-12-18 00:18:46 +01003663 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003664
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003665 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003666
3667exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003668 mbedtls_free( output1 );
3669 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003670 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003671 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003672}
3673/* END_CASE */
3674
3675/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003676void cipher_verify_output_multipart( int alg_arg,
3677 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003678 data_t *key,
3679 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003680 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003681{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003682 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003683 psa_key_type_t key_type = key_type_arg;
3684 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003685 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003686 unsigned char iv[16] = {0};
3687 size_t iv_size = 16;
3688 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003689 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003690 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003691 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003692 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003693 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003694 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003695 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003696 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3697 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003699
Gilles Peskine8817f612018-12-18 00:18:46 +01003700 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003701
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003702 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3703 psa_set_key_algorithm( &attributes, alg );
3704 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003705
Gilles Peskine73676cb2019-05-15 20:15:10 +02003706 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003707
Gilles Peskine8817f612018-12-18 00:18:46 +01003708 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3709 handle, alg ) );
3710 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3711 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003712
Gilles Peskine8817f612018-12-18 00:18:46 +01003713 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3714 iv, iv_size,
3715 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003716 output1_buffer_size = ( (size_t) input->len +
3717 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003718 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003719
Gilles Peskinee0866522019-02-19 19:44:00 +01003720 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003721
Gilles Peskine8817f612018-12-18 00:18:46 +01003722 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3723 output1, output1_buffer_size,
3724 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003725 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003726
Gilles Peskine8817f612018-12-18 00:18:46 +01003727 PSA_ASSERT( psa_cipher_update( &operation1,
3728 input->x + first_part_size,
3729 input->len - first_part_size,
3730 output1, output1_buffer_size,
3731 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003732 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003733
Gilles Peskine8817f612018-12-18 00:18:46 +01003734 PSA_ASSERT( psa_cipher_finish( &operation1,
3735 output1 + output1_length,
3736 output1_buffer_size - output1_length,
3737 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003738 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003739
Gilles Peskine8817f612018-12-18 00:18:46 +01003740 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003741
Gilles Peskine048b7f02018-06-08 14:20:49 +02003742 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003743 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003744
Gilles Peskine8817f612018-12-18 00:18:46 +01003745 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3746 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003747
Gilles Peskine8817f612018-12-18 00:18:46 +01003748 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3749 output2, output2_buffer_size,
3750 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003751 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003752
Gilles Peskine8817f612018-12-18 00:18:46 +01003753 PSA_ASSERT( psa_cipher_update( &operation2,
3754 output1 + first_part_size,
3755 output1_length - first_part_size,
3756 output2, output2_buffer_size,
3757 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003758 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003759
Gilles Peskine8817f612018-12-18 00:18:46 +01003760 PSA_ASSERT( psa_cipher_finish( &operation2,
3761 output2 + output2_length,
3762 output2_buffer_size - output2_length,
3763 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003764 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003765
Gilles Peskine8817f612018-12-18 00:18:46 +01003766 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003767
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003768 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003769
3770exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003771 mbedtls_free( output1 );
3772 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003773 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003774 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003775}
3776/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003777
Gilles Peskine20035e32018-02-03 22:44:14 +01003778/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003779void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003780 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003781 data_t *nonce,
3782 data_t *additional_data,
3783 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003784 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003785{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003786 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003787 psa_key_type_t key_type = key_type_arg;
3788 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003789 unsigned char *output_data = NULL;
3790 size_t output_size = 0;
3791 size_t output_length = 0;
3792 unsigned char *output_data2 = NULL;
3793 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003794 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003795 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003797
Gilles Peskine4abf7412018-06-18 16:35:34 +02003798 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003799 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3800 * should be exact. */
3801 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3802 TEST_EQUAL( output_size,
3803 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003804 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003805
Gilles Peskine8817f612018-12-18 00:18:46 +01003806 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003807
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003808 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3809 psa_set_key_algorithm( &attributes, alg );
3810 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003811
Gilles Peskine049c7532019-05-15 20:22:09 +02003812 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3813 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003814
Gilles Peskinefe11b722018-12-18 00:24:04 +01003815 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3816 nonce->x, nonce->len,
3817 additional_data->x,
3818 additional_data->len,
3819 input_data->x, input_data->len,
3820 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003821 &output_length ),
3822 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003823
3824 if( PSA_SUCCESS == expected_result )
3825 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003826 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003827
Gilles Peskine003a4a92019-05-14 16:09:40 +02003828 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3829 * should be exact. */
3830 TEST_EQUAL( input_data->len,
3831 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3832
Gilles Peskinefe11b722018-12-18 00:24:04 +01003833 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3834 nonce->x, nonce->len,
3835 additional_data->x,
3836 additional_data->len,
3837 output_data, output_length,
3838 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003839 &output_length2 ),
3840 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003841
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003842 ASSERT_COMPARE( input_data->x, input_data->len,
3843 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003844 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003845
Gilles Peskinea1cac842018-06-11 19:33:02 +02003846exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003847 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003848 mbedtls_free( output_data );
3849 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003850 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003851}
3852/* END_CASE */
3853
3854/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003855void aead_encrypt( int key_type_arg, data_t *key_data,
3856 int alg_arg,
3857 data_t *nonce,
3858 data_t *additional_data,
3859 data_t *input_data,
3860 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003861{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003862 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003863 psa_key_type_t key_type = key_type_arg;
3864 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003865 unsigned char *output_data = NULL;
3866 size_t output_size = 0;
3867 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003868 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003869 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003870
Gilles Peskine4abf7412018-06-18 16:35:34 +02003871 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003872 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3873 * should be exact. */
3874 TEST_EQUAL( output_size,
3875 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003876 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003877
Gilles Peskine8817f612018-12-18 00:18:46 +01003878 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003879
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003880 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3881 psa_set_key_algorithm( &attributes, alg );
3882 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003883
Gilles Peskine049c7532019-05-15 20:22:09 +02003884 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3885 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003886
Gilles Peskine8817f612018-12-18 00:18:46 +01003887 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3888 nonce->x, nonce->len,
3889 additional_data->x, additional_data->len,
3890 input_data->x, input_data->len,
3891 output_data, output_size,
3892 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003893
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003894 ASSERT_COMPARE( expected_result->x, expected_result->len,
3895 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003896
Gilles Peskinea1cac842018-06-11 19:33:02 +02003897exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003898 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003899 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003900 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003901}
3902/* END_CASE */
3903
3904/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003905void aead_decrypt( int key_type_arg, data_t *key_data,
3906 int alg_arg,
3907 data_t *nonce,
3908 data_t *additional_data,
3909 data_t *input_data,
3910 data_t *expected_data,
3911 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003912{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003913 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003914 psa_key_type_t key_type = key_type_arg;
3915 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003916 unsigned char *output_data = NULL;
3917 size_t output_size = 0;
3918 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003919 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003920 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003921 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003922
Gilles Peskine003a4a92019-05-14 16:09:40 +02003923 output_size = input_data->len - tag_length;
3924 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3925 * should be exact. */
3926 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3927 TEST_EQUAL( output_size,
3928 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003929 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003930
Gilles Peskine8817f612018-12-18 00:18:46 +01003931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003932
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3934 psa_set_key_algorithm( &attributes, alg );
3935 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003936
Gilles Peskine049c7532019-05-15 20:22:09 +02003937 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3938 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003939
Gilles Peskinefe11b722018-12-18 00:24:04 +01003940 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3941 nonce->x, nonce->len,
3942 additional_data->x,
3943 additional_data->len,
3944 input_data->x, input_data->len,
3945 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003946 &output_length ),
3947 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003948
Gilles Peskine2d277862018-06-18 15:41:12 +02003949 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003950 ASSERT_COMPARE( expected_data->x, expected_data->len,
3951 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003952
Gilles Peskinea1cac842018-06-11 19:33:02 +02003953exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003954 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003955 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003956 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003957}
3958/* END_CASE */
3959
3960/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003961void signature_size( int type_arg,
3962 int bits,
3963 int alg_arg,
3964 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003965{
3966 psa_key_type_t type = type_arg;
3967 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003968 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003969
Gilles Peskinefe11b722018-12-18 00:24:04 +01003970 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003971#if defined(MBEDTLS_TEST_DEPRECATED)
3972 TEST_EQUAL( actual_size,
3973 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3974#endif /* MBEDTLS_TEST_DEPRECATED */
3975
Gilles Peskinee59236f2018-01-27 23:32:46 +01003976exit:
3977 ;
3978}
3979/* END_CASE */
3980
3981/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003982void sign_deterministic( int key_type_arg, data_t *key_data,
3983 int alg_arg, data_t *input_data,
3984 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003985{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003986 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003987 psa_key_type_t key_type = key_type_arg;
3988 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003989 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003990 unsigned char *signature = NULL;
3991 size_t signature_size;
3992 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003994
Gilles Peskine8817f612018-12-18 00:18:46 +01003995 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003996
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003997 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003998 psa_set_key_algorithm( &attributes, alg );
3999 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004000
Gilles Peskine049c7532019-05-15 20:22:09 +02004001 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4002 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004003 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4004 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004005
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004006 /* Allocate a buffer which has the size advertized by the
4007 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004008 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004009 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004010 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004011 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004012 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004013
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004014 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004015 PSA_ASSERT( psa_sign_hash( handle, alg,
4016 input_data->x, input_data->len,
4017 signature, signature_size,
4018 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004019 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004020 ASSERT_COMPARE( output_data->x, output_data->len,
4021 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004022
Gilles Peskine0627f982019-11-26 19:12:16 +01004023#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004024 memset( signature, 0, signature_size );
4025 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01004026 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
4027 input_data->x, input_data->len,
4028 signature, signature_size,
4029 &signature_length ) );
4030 ASSERT_COMPARE( output_data->x, output_data->len,
4031 signature, signature_length );
4032#endif /* MBEDTLS_TEST_DEPRECATED */
4033
Gilles Peskine20035e32018-02-03 22:44:14 +01004034exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004035 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004036 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01004037 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004038 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004039}
4040/* END_CASE */
4041
4042/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004043void sign_fail( int key_type_arg, data_t *key_data,
4044 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004045 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004046{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004047 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01004048 psa_key_type_t key_type = key_type_arg;
4049 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004050 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004051 psa_status_t actual_status;
4052 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004053 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004054 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004056
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004057 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004058
Gilles Peskine8817f612018-12-18 00:18:46 +01004059 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004060
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004061 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004062 psa_set_key_algorithm( &attributes, alg );
4063 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004064
Gilles Peskine049c7532019-05-15 20:22:09 +02004065 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4066 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004067
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004068 actual_status = psa_sign_hash( handle, alg,
4069 input_data->x, input_data->len,
4070 signature, signature_size,
4071 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004072 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004073 /* The value of *signature_length is unspecified on error, but
4074 * whatever it is, it should be less than signature_size, so that
4075 * if the caller tries to read *signature_length bytes without
4076 * checking the error code then they don't overflow a buffer. */
4077 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004078
Gilles Peskine895242b2019-11-29 12:15:40 +01004079#if defined(MBEDTLS_TEST_DEPRECATED)
4080 signature_length = INVALID_EXPORT_LENGTH;
4081 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
4082 input_data->x, input_data->len,
4083 signature, signature_size,
4084 &signature_length ),
4085 expected_status );
4086 TEST_ASSERT( signature_length <= signature_size );
4087#endif /* MBEDTLS_TEST_DEPRECATED */
4088
Gilles Peskine20035e32018-02-03 22:44:14 +01004089exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004090 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004091 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01004092 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004093 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004094}
4095/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004096
4097/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004098void sign_verify( int key_type_arg, data_t *key_data,
4099 int alg_arg, data_t *input_data )
4100{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004101 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02004102 psa_key_type_t key_type = key_type_arg;
4103 psa_algorithm_t alg = alg_arg;
4104 size_t key_bits;
4105 unsigned char *signature = NULL;
4106 size_t signature_size;
4107 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004109
Gilles Peskine8817f612018-12-18 00:18:46 +01004110 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004111
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004112 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004113 psa_set_key_algorithm( &attributes, alg );
4114 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004115
Gilles Peskine049c7532019-05-15 20:22:09 +02004116 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4117 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004118 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4119 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004120
4121 /* Allocate a buffer which has the size advertized by the
4122 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004123 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004124 key_bits, alg );
4125 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004126 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004127 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004128
4129 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004130 PSA_ASSERT( psa_sign_hash( handle, alg,
4131 input_data->x, input_data->len,
4132 signature, signature_size,
4133 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004134 /* Check that the signature length looks sensible. */
4135 TEST_ASSERT( signature_length <= signature_size );
4136 TEST_ASSERT( signature_length > 0 );
4137
4138 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004139 PSA_ASSERT( psa_verify_hash( handle, alg,
4140 input_data->x, input_data->len,
4141 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004142
4143 if( input_data->len != 0 )
4144 {
4145 /* Flip a bit in the input and verify that the signature is now
4146 * detected as invalid. Flip a bit at the beginning, not at the end,
4147 * because ECDSA may ignore the last few bits of the input. */
4148 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004149 TEST_EQUAL( psa_verify_hash( handle, alg,
4150 input_data->x, input_data->len,
4151 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004152 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004153 }
4154
4155exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004156 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004157 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004158 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004159 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004160}
4161/* END_CASE */
4162
4163/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004164void asymmetric_verify( int key_type_arg, data_t *key_data,
4165 int alg_arg, data_t *hash_data,
4166 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004167{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004168 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004169 psa_key_type_t key_type = key_type_arg;
4170 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004172
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004173 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004174
Gilles Peskine8817f612018-12-18 00:18:46 +01004175 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004176
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004177 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004178 psa_set_key_algorithm( &attributes, alg );
4179 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004180
Gilles Peskine049c7532019-05-15 20:22:09 +02004181 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4182 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004183
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004184 PSA_ASSERT( psa_verify_hash( handle, alg,
4185 hash_data->x, hash_data->len,
4186 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004187
4188#if defined(MBEDTLS_TEST_DEPRECATED)
4189 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4190 hash_data->x, hash_data->len,
4191 signature_data->x,
4192 signature_data->len ) );
4193
4194#endif /* MBEDTLS_TEST_DEPRECATED */
4195
itayzafrir5c753392018-05-08 11:18:38 +03004196exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004197 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004198 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004199 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004200}
4201/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004202
4203/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004204void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4205 int alg_arg, data_t *hash_data,
4206 data_t *signature_data,
4207 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004208{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004209 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004210 psa_key_type_t key_type = key_type_arg;
4211 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004212 psa_status_t actual_status;
4213 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004215
Gilles Peskine8817f612018-12-18 00:18:46 +01004216 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004217
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004218 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004219 psa_set_key_algorithm( &attributes, alg );
4220 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004221
Gilles Peskine049c7532019-05-15 20:22:09 +02004222 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4223 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004224
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004225 actual_status = psa_verify_hash( handle, alg,
4226 hash_data->x, hash_data->len,
4227 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004228 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004229
Gilles Peskine895242b2019-11-29 12:15:40 +01004230#if defined(MBEDTLS_TEST_DEPRECATED)
4231 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4232 hash_data->x, hash_data->len,
4233 signature_data->x, signature_data->len ),
4234 expected_status );
4235#endif /* MBEDTLS_TEST_DEPRECATED */
4236
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004237exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004238 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004239 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004240 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004241}
4242/* END_CASE */
4243
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004244/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004245void asymmetric_encrypt( int key_type_arg,
4246 data_t *key_data,
4247 int alg_arg,
4248 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004249 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004250 int expected_output_length_arg,
4251 int expected_status_arg )
4252{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004253 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004254 psa_key_type_t key_type = key_type_arg;
4255 psa_algorithm_t alg = alg_arg;
4256 size_t expected_output_length = expected_output_length_arg;
4257 size_t key_bits;
4258 unsigned char *output = NULL;
4259 size_t output_size;
4260 size_t output_length = ~0;
4261 psa_status_t actual_status;
4262 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004264
Gilles Peskine8817f612018-12-18 00:18:46 +01004265 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004266
Gilles Peskine656896e2018-06-29 19:12:28 +02004267 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004268 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4269 psa_set_key_algorithm( &attributes, alg );
4270 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004271 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4272 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004273
4274 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004275 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4276 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004277 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004278 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004279
4280 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004281 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004282 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004283 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004284 output, output_size,
4285 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004286 TEST_EQUAL( actual_status, expected_status );
4287 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004288
Gilles Peskine68428122018-06-30 18:42:41 +02004289 /* If the label is empty, the test framework puts a non-null pointer
4290 * in label->x. Test that a null pointer works as well. */
4291 if( label->len == 0 )
4292 {
4293 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004294 if( output_size != 0 )
4295 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004296 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004297 input_data->x, input_data->len,
4298 NULL, label->len,
4299 output, output_size,
4300 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004301 TEST_EQUAL( actual_status, expected_status );
4302 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004303 }
4304
Gilles Peskine656896e2018-06-29 19:12:28 +02004305exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004306 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004307 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004308 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004309 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004310}
4311/* END_CASE */
4312
4313/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004314void asymmetric_encrypt_decrypt( int key_type_arg,
4315 data_t *key_data,
4316 int alg_arg,
4317 data_t *input_data,
4318 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004319{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004320 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004321 psa_key_type_t key_type = key_type_arg;
4322 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004323 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004324 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004325 size_t output_size;
4326 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004327 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004328 size_t output2_size;
4329 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004330 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004331
Gilles Peskine8817f612018-12-18 00:18:46 +01004332 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004333
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004334 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4335 psa_set_key_algorithm( &attributes, alg );
4336 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004337
Gilles Peskine049c7532019-05-15 20:22:09 +02004338 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4339 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004340
4341 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004342 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4343 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004344 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004345 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004346 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004347 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004348
Gilles Peskineeebd7382018-06-08 18:11:54 +02004349 /* We test encryption by checking that encrypt-then-decrypt gives back
4350 * the original plaintext because of the non-optional random
4351 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004352 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4353 input_data->x, input_data->len,
4354 label->x, label->len,
4355 output, output_size,
4356 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004357 /* We don't know what ciphertext length to expect, but check that
4358 * it looks sensible. */
4359 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004360
Gilles Peskine8817f612018-12-18 00:18:46 +01004361 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4362 output, output_length,
4363 label->x, label->len,
4364 output2, output2_size,
4365 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004366 ASSERT_COMPARE( input_data->x, input_data->len,
4367 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004368
4369exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004370 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004371 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004372 mbedtls_free( output );
4373 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004374 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004375}
4376/* END_CASE */
4377
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004378/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004379void asymmetric_decrypt( int key_type_arg,
4380 data_t *key_data,
4381 int alg_arg,
4382 data_t *input_data,
4383 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004384 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004385{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004386 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004387 psa_key_type_t key_type = key_type_arg;
4388 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004389 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004390 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004391 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004393
Jaeden Amero412654a2019-02-06 12:57:46 +00004394 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004395 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004396
Gilles Peskine8817f612018-12-18 00:18:46 +01004397 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004398
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004399 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4400 psa_set_key_algorithm( &attributes, alg );
4401 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004402
Gilles Peskine049c7532019-05-15 20:22:09 +02004403 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4404 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004405
Gilles Peskine8817f612018-12-18 00:18:46 +01004406 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4407 input_data->x, input_data->len,
4408 label->x, label->len,
4409 output,
4410 output_size,
4411 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004412 ASSERT_COMPARE( expected_data->x, expected_data->len,
4413 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004414
Gilles Peskine68428122018-06-30 18:42:41 +02004415 /* If the label is empty, the test framework puts a non-null pointer
4416 * in label->x. Test that a null pointer works as well. */
4417 if( label->len == 0 )
4418 {
4419 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004420 if( output_size != 0 )
4421 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004422 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4423 input_data->x, input_data->len,
4424 NULL, label->len,
4425 output,
4426 output_size,
4427 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004428 ASSERT_COMPARE( expected_data->x, expected_data->len,
4429 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004430 }
4431
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004432exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004433 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004434 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004435 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004436 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004437}
4438/* END_CASE */
4439
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004440/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004441void asymmetric_decrypt_fail( int key_type_arg,
4442 data_t *key_data,
4443 int alg_arg,
4444 data_t *input_data,
4445 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004446 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004447 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004448{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004449 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004450 psa_key_type_t key_type = key_type_arg;
4451 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004452 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004453 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004454 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004455 psa_status_t actual_status;
4456 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004457 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004458
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004459 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004460
Gilles Peskine8817f612018-12-18 00:18:46 +01004461 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004462
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004463 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4464 psa_set_key_algorithm( &attributes, alg );
4465 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004466
Gilles Peskine049c7532019-05-15 20:22:09 +02004467 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4468 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004469
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004470 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004471 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004472 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004473 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004474 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004475 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004476 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004477
Gilles Peskine68428122018-06-30 18:42:41 +02004478 /* If the label is empty, the test framework puts a non-null pointer
4479 * in label->x. Test that a null pointer works as well. */
4480 if( label->len == 0 )
4481 {
4482 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004483 if( output_size != 0 )
4484 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004485 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004486 input_data->x, input_data->len,
4487 NULL, label->len,
4488 output, output_size,
4489 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004490 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004491 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004492 }
4493
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004494exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004495 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004496 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004497 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004498 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004499}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004500/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004501
4502/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004503void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004504{
4505 /* Test each valid way of initializing the object, except for `= {0}`, as
4506 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4507 * though it's OK by the C standard. We could test for this, but we'd need
4508 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004509 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004510 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4511 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4512 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004513
4514 memset( &zero, 0, sizeof( zero ) );
4515
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004516 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004517 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004518 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004519 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004520 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004521 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004522 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004523
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004524 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004525 PSA_ASSERT( psa_key_derivation_abort(&func) );
4526 PSA_ASSERT( psa_key_derivation_abort(&init) );
4527 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004528}
4529/* END_CASE */
4530
Janos Follath16de4a42019-06-13 16:32:24 +01004531/* BEGIN_CASE */
4532void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004533{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004534 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004535 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004536 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004537
Gilles Peskine8817f612018-12-18 00:18:46 +01004538 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004539
Janos Follath16de4a42019-06-13 16:32:24 +01004540 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004541 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004542
4543exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004545 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004546}
4547/* END_CASE */
4548
Janos Follathaf3c2a02019-06-12 12:34:34 +01004549/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004550void derive_set_capacity( int alg_arg, int capacity_arg,
4551 int expected_status_arg )
4552{
4553 psa_algorithm_t alg = alg_arg;
4554 size_t capacity = capacity_arg;
4555 psa_status_t expected_status = expected_status_arg;
4556 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4557
4558 PSA_ASSERT( psa_crypto_init( ) );
4559
4560 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4561
4562 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4563 expected_status );
4564
4565exit:
4566 psa_key_derivation_abort( &operation );
4567 PSA_DONE( );
4568}
4569/* END_CASE */
4570
4571/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004572void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004573 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004574 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004575 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004576 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004577 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004578 int expected_status_arg3,
4579 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004580{
4581 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004582 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4583 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004584 psa_status_t expected_statuses[] = {expected_status_arg1,
4585 expected_status_arg2,
4586 expected_status_arg3};
4587 data_t *inputs[] = {input1, input2, input3};
4588 psa_key_handle_t handles[] = {0, 0, 0};
4589 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4590 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4591 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004592 psa_key_type_t output_key_type = output_key_type_arg;
4593 psa_key_handle_t output_handle = 0;
4594 psa_status_t expected_output_status = expected_output_status_arg;
4595 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004596
4597 PSA_ASSERT( psa_crypto_init( ) );
4598
4599 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4600 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004601
4602 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4603
4604 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4605 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004606 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004607 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004608 psa_set_key_type( &attributes, key_types[i] );
4609 PSA_ASSERT( psa_import_key( &attributes,
4610 inputs[i]->x, inputs[i]->len,
4611 &handles[i] ) );
4612 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4613 handles[i] ),
4614 expected_statuses[i] );
4615 }
4616 else
4617 {
4618 TEST_EQUAL( psa_key_derivation_input_bytes(
4619 &operation, steps[i],
4620 inputs[i]->x, inputs[i]->len ),
4621 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004622 }
4623 }
4624
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004625 if( output_key_type != PSA_KEY_TYPE_NONE )
4626 {
4627 psa_reset_key_attributes( &attributes );
4628 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4629 psa_set_key_bits( &attributes, 8 );
4630 actual_output_status =
4631 psa_key_derivation_output_key( &attributes, &operation,
4632 &output_handle );
4633 }
4634 else
4635 {
4636 uint8_t buffer[1];
4637 actual_output_status =
4638 psa_key_derivation_output_bytes( &operation,
4639 buffer, sizeof( buffer ) );
4640 }
4641 TEST_EQUAL( actual_output_status, expected_output_status );
4642
Janos Follathaf3c2a02019-06-12 12:34:34 +01004643exit:
4644 psa_key_derivation_abort( &operation );
4645 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4646 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004647 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004648 PSA_DONE( );
4649}
4650/* END_CASE */
4651
Janos Follathd958bb72019-07-03 15:02:16 +01004652/* BEGIN_CASE */
4653void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004654{
Janos Follathd958bb72019-07-03 15:02:16 +01004655 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004656 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004657 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004658 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004659 unsigned char input1[] = "Input 1";
4660 size_t input1_length = sizeof( input1 );
4661 unsigned char input2[] = "Input 2";
4662 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004663 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004664 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004665 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4666 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4667 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004669
Gilles Peskine8817f612018-12-18 00:18:46 +01004670 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004671
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004672 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4673 psa_set_key_algorithm( &attributes, alg );
4674 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004675
Gilles Peskine73676cb2019-05-15 20:15:10 +02004676 PSA_ASSERT( psa_import_key( &attributes,
4677 key_data, sizeof( key_data ),
4678 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004679
4680 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004681 if( !setup_key_derivation_wrap( &operation, handle, alg,
4682 input1, input1_length,
4683 input2, input2_length,
4684 capacity ) )
4685 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004686
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004687 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004688 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004689 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004690
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004691 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004692
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004693 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004694 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004695
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004696exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004698 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004699 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004700}
4701/* END_CASE */
4702
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004703/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004704void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004705{
4706 uint8_t output_buffer[16];
4707 size_t buffer_size = 16;
4708 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004710
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004711 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4712 output_buffer, buffer_size )
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 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004716 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004717
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004718 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004719
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004720 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4721 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004722 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004723
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004725 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004726
4727exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004729}
4730/* END_CASE */
4731
4732/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004733void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004734 int step1_arg, data_t *input1,
4735 int step2_arg, data_t *input2,
4736 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004737 int requested_capacity_arg,
4738 data_t *expected_output1,
4739 data_t *expected_output2 )
4740{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004741 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004742 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4743 data_t *inputs[] = {input1, input2, input3};
4744 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004745 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004747 uint8_t *expected_outputs[2] =
4748 {expected_output1->x, expected_output2->x};
4749 size_t output_sizes[2] =
4750 {expected_output1->len, expected_output2->len};
4751 size_t output_buffer_size = 0;
4752 uint8_t *output_buffer = NULL;
4753 size_t expected_capacity;
4754 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004755 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004756 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004757 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004758
4759 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4760 {
4761 if( output_sizes[i] > output_buffer_size )
4762 output_buffer_size = output_sizes[i];
4763 if( output_sizes[i] == 0 )
4764 expected_outputs[i] = NULL;
4765 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004766 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004767 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004768
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004769 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4770 psa_set_key_algorithm( &attributes, alg );
4771 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004772
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004773 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004774 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4775 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4776 requested_capacity ) );
4777 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004778 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004779 switch( steps[i] )
4780 {
4781 case 0:
4782 break;
4783 case PSA_KEY_DERIVATION_INPUT_SECRET:
4784 PSA_ASSERT( psa_import_key( &attributes,
4785 inputs[i]->x, inputs[i]->len,
4786 &handles[i] ) );
4787 PSA_ASSERT( psa_key_derivation_input_key(
4788 &operation, steps[i],
4789 handles[i] ) );
4790 break;
4791 default:
4792 PSA_ASSERT( psa_key_derivation_input_bytes(
4793 &operation, steps[i],
4794 inputs[i]->x, inputs[i]->len ) );
4795 break;
4796 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004797 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004798
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004800 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004801 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004802 expected_capacity = requested_capacity;
4803
4804 /* Expansion phase. */
4805 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4806 {
4807 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004809 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004810 if( expected_capacity == 0 && output_sizes[i] == 0 )
4811 {
4812 /* Reading 0 bytes when 0 bytes are available can go either way. */
4813 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004814 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004815 continue;
4816 }
4817 else if( expected_capacity == 0 ||
4818 output_sizes[i] > expected_capacity )
4819 {
4820 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004821 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004822 expected_capacity = 0;
4823 continue;
4824 }
4825 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004826 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004827 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004828 ASSERT_COMPARE( output_buffer, output_sizes[i],
4829 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004831 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004832 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004833 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004834 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004835 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004837
4838exit:
4839 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004841 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4842 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004843 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004844}
4845/* END_CASE */
4846
4847/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004848void derive_full( int alg_arg,
4849 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004850 data_t *input1,
4851 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004852 int requested_capacity_arg )
4853{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004854 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004855 psa_algorithm_t alg = alg_arg;
4856 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004857 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004858 unsigned char output_buffer[16];
4859 size_t expected_capacity = requested_capacity;
4860 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004861 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004862
Gilles Peskine8817f612018-12-18 00:18:46 +01004863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004864
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004865 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4866 psa_set_key_algorithm( &attributes, alg );
4867 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004868
Gilles Peskine049c7532019-05-15 20:22:09 +02004869 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4870 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004871
Janos Follathf2815ea2019-07-03 12:41:36 +01004872 if( !setup_key_derivation_wrap( &operation, handle, alg,
4873 input1->x, input1->len,
4874 input2->x, input2->len,
4875 requested_capacity ) )
4876 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004877
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004878 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004879 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004880 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004881
4882 /* Expansion phase. */
4883 while( current_capacity > 0 )
4884 {
4885 size_t read_size = sizeof( output_buffer );
4886 if( read_size > current_capacity )
4887 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004889 output_buffer,
4890 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004891 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004892 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004893 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004894 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004895 }
4896
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897 /* Check that the operation refuses to go over capacity. */
4898 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004899 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004900
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004902
4903exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004905 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004906 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004907}
4908/* END_CASE */
4909
Janos Follathe60c9052019-07-03 13:51:30 +01004910/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004911void derive_key_exercise( int alg_arg,
4912 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004913 data_t *input1,
4914 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004915 int derived_type_arg,
4916 int derived_bits_arg,
4917 int derived_usage_arg,
4918 int derived_alg_arg )
4919{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004920 psa_key_handle_t base_handle = 0;
4921 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004922 psa_algorithm_t alg = alg_arg;
4923 psa_key_type_t derived_type = derived_type_arg;
4924 size_t derived_bits = derived_bits_arg;
4925 psa_key_usage_t derived_usage = derived_usage_arg;
4926 psa_algorithm_t derived_alg = derived_alg_arg;
4927 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004930 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004931
Gilles Peskine8817f612018-12-18 00:18:46 +01004932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004933
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4935 psa_set_key_algorithm( &attributes, alg );
4936 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004937 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4938 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004939
4940 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004941 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4942 input1->x, input1->len,
4943 input2->x, input2->len, capacity ) )
4944 goto exit;
4945
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004946 psa_set_key_usage_flags( &attributes, derived_usage );
4947 psa_set_key_algorithm( &attributes, derived_alg );
4948 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004949 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004951 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004952
4953 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004954 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4955 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4956 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004957
4958 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004959 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004960 goto exit;
4961
4962exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004964 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004965 psa_destroy_key( base_handle );
4966 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004967 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004968}
4969/* END_CASE */
4970
Janos Follath42fd8882019-07-03 14:17:09 +01004971/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004972void derive_key_export( int alg_arg,
4973 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004974 data_t *input1,
4975 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004976 int bytes1_arg,
4977 int bytes2_arg )
4978{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004979 psa_key_handle_t base_handle = 0;
4980 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004981 psa_algorithm_t alg = alg_arg;
4982 size_t bytes1 = bytes1_arg;
4983 size_t bytes2 = bytes2_arg;
4984 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004985 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004986 uint8_t *output_buffer = NULL;
4987 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004988 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4989 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004990 size_t length;
4991
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004992 ASSERT_ALLOC( output_buffer, capacity );
4993 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004994 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004995
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004996 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4997 psa_set_key_algorithm( &base_attributes, alg );
4998 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004999 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5000 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005001
5002 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01005003 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5004 input1->x, input1->len,
5005 input2->x, input2->len, capacity ) )
5006 goto exit;
5007
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005009 output_buffer,
5010 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005011 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005012
5013 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01005014 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5015 input1->x, input1->len,
5016 input2->x, input2->len, capacity ) )
5017 goto exit;
5018
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005019 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5020 psa_set_key_algorithm( &derived_attributes, 0 );
5021 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005022 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005024 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005025 PSA_ASSERT( psa_export_key( derived_handle,
5026 export_buffer, bytes1,
5027 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005028 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01005029 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005030 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005031 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005032 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005033 PSA_ASSERT( psa_export_key( derived_handle,
5034 export_buffer + bytes1, bytes2,
5035 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005036 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005037
5038 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005039 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5040 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005041
5042exit:
5043 mbedtls_free( output_buffer );
5044 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005046 psa_destroy_key( base_handle );
5047 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005048 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005049}
5050/* END_CASE */
5051
5052/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005053void derive_key( int alg_arg,
5054 data_t *key_data, data_t *input1, data_t *input2,
5055 int type_arg, int bits_arg,
5056 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005057{
5058 psa_key_handle_t base_handle = 0;
5059 psa_key_handle_t derived_handle = 0;
5060 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005061 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005062 size_t bits = bits_arg;
5063 psa_status_t expected_status = expected_status_arg;
5064 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5065 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5066 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5067
5068 PSA_ASSERT( psa_crypto_init( ) );
5069
5070 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5071 psa_set_key_algorithm( &base_attributes, alg );
5072 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5073 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5074 &base_handle ) );
5075
5076 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5077 input1->x, input1->len,
5078 input2->x, input2->len, SIZE_MAX ) )
5079 goto exit;
5080
5081 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5082 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005083 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005084 psa_set_key_bits( &derived_attributes, bits );
5085 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
5086 &derived_handle ),
5087 expected_status );
5088
5089exit:
5090 psa_key_derivation_abort( &operation );
5091 psa_destroy_key( base_handle );
5092 psa_destroy_key( derived_handle );
5093 PSA_DONE( );
5094}
5095/* END_CASE */
5096
5097/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005098void key_agreement_setup( int alg_arg,
5099 int our_key_type_arg, data_t *our_key_data,
5100 data_t *peer_key_data,
5101 int expected_status_arg )
5102{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005103 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005104 psa_algorithm_t alg = alg_arg;
5105 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005108 psa_status_t expected_status = expected_status_arg;
5109 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005110
Gilles Peskine8817f612018-12-18 00:18:46 +01005111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005112
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005113 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5114 psa_set_key_algorithm( &attributes, alg );
5115 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005116 PSA_ASSERT( psa_import_key( &attributes,
5117 our_key_data->x, our_key_data->len,
5118 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005119
Gilles Peskine77f40d82019-04-11 21:27:06 +02005120 /* The tests currently include inputs that should fail at either step.
5121 * Test cases that fail at the setup step should be changed to call
5122 * key_derivation_setup instead, and this function should be renamed
5123 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005124 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005125 if( status == PSA_SUCCESS )
5126 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005127 TEST_EQUAL( psa_key_derivation_key_agreement(
5128 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5129 our_key,
5130 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005131 expected_status );
5132 }
5133 else
5134 {
5135 TEST_ASSERT( status == expected_status );
5136 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005137
5138exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005140 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005141 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005142}
5143/* END_CASE */
5144
5145/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005146void raw_key_agreement( int alg_arg,
5147 int our_key_type_arg, data_t *our_key_data,
5148 data_t *peer_key_data,
5149 data_t *expected_output )
5150{
5151 psa_key_handle_t our_key = 0;
5152 psa_algorithm_t alg = alg_arg;
5153 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005154 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005155 unsigned char *output = NULL;
5156 size_t output_length = ~0;
5157
5158 ASSERT_ALLOC( output, expected_output->len );
5159 PSA_ASSERT( psa_crypto_init( ) );
5160
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005161 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5162 psa_set_key_algorithm( &attributes, alg );
5163 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005164 PSA_ASSERT( psa_import_key( &attributes,
5165 our_key_data->x, our_key_data->len,
5166 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005167
Gilles Peskinebe697d82019-05-16 18:00:41 +02005168 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5169 peer_key_data->x, peer_key_data->len,
5170 output, expected_output->len,
5171 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005172 ASSERT_COMPARE( output, output_length,
5173 expected_output->x, expected_output->len );
5174
5175exit:
5176 mbedtls_free( output );
5177 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005178 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005179}
5180/* END_CASE */
5181
5182/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005183void key_agreement_capacity( int alg_arg,
5184 int our_key_type_arg, data_t *our_key_data,
5185 data_t *peer_key_data,
5186 int expected_capacity_arg )
5187{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005188 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005189 psa_algorithm_t alg = alg_arg;
5190 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005191 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005193 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005194 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005195
Gilles Peskine8817f612018-12-18 00:18:46 +01005196 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005197
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005198 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5199 psa_set_key_algorithm( &attributes, alg );
5200 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005201 PSA_ASSERT( psa_import_key( &attributes,
5202 our_key_data->x, our_key_data->len,
5203 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005204
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005205 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005206 PSA_ASSERT( psa_key_derivation_key_agreement(
5207 &operation,
5208 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5209 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005210 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5211 {
5212 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005214 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005215 NULL, 0 ) );
5216 }
Gilles Peskine59685592018-09-18 12:11:34 +02005217
Gilles Peskinebf491972018-10-25 22:36:12 +02005218 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005219 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005220 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005221 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005222
Gilles Peskinebf491972018-10-25 22:36:12 +02005223 /* Test the actual capacity by reading the output. */
5224 while( actual_capacity > sizeof( output ) )
5225 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005227 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005228 actual_capacity -= sizeof( output );
5229 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005231 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005232 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005233 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005234
Gilles Peskine59685592018-09-18 12:11:34 +02005235exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005236 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005237 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005238 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005239}
5240/* END_CASE */
5241
5242/* BEGIN_CASE */
5243void key_agreement_output( int alg_arg,
5244 int our_key_type_arg, data_t *our_key_data,
5245 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005246 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005247{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005248 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005249 psa_algorithm_t alg = alg_arg;
5250 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005253 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005254
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005255 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5256 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005257
Gilles Peskine8817f612018-12-18 00:18:46 +01005258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005259
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5261 psa_set_key_algorithm( &attributes, alg );
5262 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005263 PSA_ASSERT( psa_import_key( &attributes,
5264 our_key_data->x, our_key_data->len,
5265 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005266
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005268 PSA_ASSERT( psa_key_derivation_key_agreement(
5269 &operation,
5270 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5271 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005272 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5273 {
5274 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005276 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005277 NULL, 0 ) );
5278 }
Gilles Peskine59685592018-09-18 12:11:34 +02005279
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005281 actual_output,
5282 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005283 ASSERT_COMPARE( actual_output, expected_output1->len,
5284 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005285 if( expected_output2->len != 0 )
5286 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005288 actual_output,
5289 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005290 ASSERT_COMPARE( actual_output, expected_output2->len,
5291 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005292 }
Gilles Peskine59685592018-09-18 12:11:34 +02005293
5294exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005295 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005296 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005297 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005298 mbedtls_free( actual_output );
5299}
5300/* END_CASE */
5301
5302/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005303void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005304{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005305 size_t bytes = bytes_arg;
5306 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005307 unsigned char *output = NULL;
5308 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005309 size_t i;
5310 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005311
Simon Butcher49f8e312020-03-03 15:51:50 +00005312 TEST_ASSERT( bytes_arg >= 0 );
5313
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005314 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5315 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005316 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005317
Gilles Peskine8817f612018-12-18 00:18:46 +01005318 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005319
Gilles Peskinea50d7392018-06-21 10:22:13 +02005320 /* Run several times, to ensure that every output byte will be
5321 * nonzero at least once with overwhelming probability
5322 * (2^(-8*number_of_runs)). */
5323 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005324 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005325 if( bytes != 0 )
5326 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005327 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005328
5329 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005330 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5331 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005332
5333 for( i = 0; i < bytes; i++ )
5334 {
5335 if( output[i] != 0 )
5336 ++changed[i];
5337 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005338 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005339
5340 /* Check that every byte was changed to nonzero at least once. This
5341 * validates that psa_generate_random is overwriting every byte of
5342 * the output buffer. */
5343 for( i = 0; i < bytes; i++ )
5344 {
5345 TEST_ASSERT( changed[i] != 0 );
5346 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005347
5348exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005349 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005350 mbedtls_free( output );
5351 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005352}
5353/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005354
5355/* BEGIN_CASE */
5356void generate_key( int type_arg,
5357 int bits_arg,
5358 int usage_arg,
5359 int alg_arg,
5360 int expected_status_arg )
5361{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005362 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005363 psa_key_type_t type = type_arg;
5364 psa_key_usage_t usage = usage_arg;
5365 size_t bits = bits_arg;
5366 psa_algorithm_t alg = alg_arg;
5367 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005369 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005370
Gilles Peskine8817f612018-12-18 00:18:46 +01005371 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005372
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005373 psa_set_key_usage_flags( &attributes, usage );
5374 psa_set_key_algorithm( &attributes, alg );
5375 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005376 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005377
5378 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005379 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005380 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005381 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005382
5383 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005384 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5385 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5386 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005387
Gilles Peskine818ca122018-06-20 18:16:48 +02005388 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005389 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005390 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005391
5392exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005393 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005394 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005395 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005396}
5397/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005398
Gilles Peskinee56e8782019-04-26 17:34:02 +02005399/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5400void generate_key_rsa( int bits_arg,
5401 data_t *e_arg,
5402 int expected_status_arg )
5403{
5404 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005405 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005406 size_t bits = bits_arg;
5407 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5408 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5409 psa_status_t expected_status = expected_status_arg;
5410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5411 uint8_t *exported = NULL;
5412 size_t exported_size =
5413 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5414 size_t exported_length = SIZE_MAX;
5415 uint8_t *e_read_buffer = NULL;
5416 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005417 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005418 size_t e_read_length = SIZE_MAX;
5419
5420 if( e_arg->len == 0 ||
5421 ( e_arg->len == 3 &&
5422 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5423 {
5424 is_default_public_exponent = 1;
5425 e_read_size = 0;
5426 }
5427 ASSERT_ALLOC( e_read_buffer, e_read_size );
5428 ASSERT_ALLOC( exported, exported_size );
5429
5430 PSA_ASSERT( psa_crypto_init( ) );
5431
5432 psa_set_key_usage_flags( &attributes, usage );
5433 psa_set_key_algorithm( &attributes, alg );
5434 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5435 e_arg->x, e_arg->len ) );
5436 psa_set_key_bits( &attributes, bits );
5437
5438 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005439 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005440 if( expected_status != PSA_SUCCESS )
5441 goto exit;
5442
5443 /* Test the key information */
5444 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5445 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5446 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5447 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5448 e_read_buffer, e_read_size,
5449 &e_read_length ) );
5450 if( is_default_public_exponent )
5451 TEST_EQUAL( e_read_length, 0 );
5452 else
5453 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5454
5455 /* Do something with the key according to its type and permitted usage. */
5456 if( ! exercise_key( handle, usage, alg ) )
5457 goto exit;
5458
5459 /* Export the key and check the public exponent. */
5460 PSA_ASSERT( psa_export_public_key( handle,
5461 exported, exported_size,
5462 &exported_length ) );
5463 {
5464 uint8_t *p = exported;
5465 uint8_t *end = exported + exported_length;
5466 size_t len;
5467 /* RSAPublicKey ::= SEQUENCE {
5468 * modulus INTEGER, -- n
5469 * publicExponent INTEGER } -- e
5470 */
5471 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005472 MBEDTLS_ASN1_SEQUENCE |
5473 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005474 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5475 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5476 MBEDTLS_ASN1_INTEGER ) );
5477 if( len >= 1 && p[0] == 0 )
5478 {
5479 ++p;
5480 --len;
5481 }
5482 if( e_arg->len == 0 )
5483 {
5484 TEST_EQUAL( len, 3 );
5485 TEST_EQUAL( p[0], 1 );
5486 TEST_EQUAL( p[1], 0 );
5487 TEST_EQUAL( p[2], 1 );
5488 }
5489 else
5490 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5491 }
5492
5493exit:
5494 psa_reset_key_attributes( &attributes );
5495 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005496 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005497 mbedtls_free( e_read_buffer );
5498 mbedtls_free( exported );
5499}
5500/* END_CASE */
5501
Darryl Greend49a4992018-06-18 17:27:26 +01005502/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005503void persistent_key_load_key_from_storage( data_t *data,
5504 int type_arg, int bits_arg,
5505 int usage_flags_arg, int alg_arg,
5506 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005507{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005508 psa_key_id_t key_id = 1;
5509 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005510 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005511 psa_key_handle_t base_key = 0;
5512 psa_key_type_t type = type_arg;
5513 size_t bits = bits_arg;
5514 psa_key_usage_t usage_flags = usage_flags_arg;
5515 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005516 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005517 unsigned char *first_export = NULL;
5518 unsigned char *second_export = NULL;
5519 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5520 size_t first_exported_length;
5521 size_t second_exported_length;
5522
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005523 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5524 {
5525 ASSERT_ALLOC( first_export, export_size );
5526 ASSERT_ALLOC( second_export, export_size );
5527 }
Darryl Greend49a4992018-06-18 17:27:26 +01005528
Gilles Peskine8817f612018-12-18 00:18:46 +01005529 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005530
Gilles Peskinec87af662019-05-15 16:12:22 +02005531 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005532 psa_set_key_usage_flags( &attributes, usage_flags );
5533 psa_set_key_algorithm( &attributes, alg );
5534 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005535 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005536
Darryl Green0c6575a2018-11-07 16:05:30 +00005537 switch( generation_method )
5538 {
5539 case IMPORT_KEY:
5540 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005541 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5542 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005543 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005544
Darryl Green0c6575a2018-11-07 16:05:30 +00005545 case GENERATE_KEY:
5546 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005547 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005548 break;
5549
5550 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005551 {
5552 /* Create base key */
5553 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5554 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5555 psa_set_key_usage_flags( &base_attributes,
5556 PSA_KEY_USAGE_DERIVE );
5557 psa_set_key_algorithm( &base_attributes, derive_alg );
5558 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005559 PSA_ASSERT( psa_import_key( &base_attributes,
5560 data->x, data->len,
5561 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005562 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005563 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005564 PSA_ASSERT( psa_key_derivation_input_key(
5565 &operation,
5566 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005567 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005568 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005569 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005570 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5571 &operation,
5572 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005573 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005574 PSA_ASSERT( psa_destroy_key( base_key ) );
5575 base_key = 0;
5576 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005577 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005578 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005579 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005580
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005581 /* Export the key if permitted by the key policy. */
5582 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5583 {
5584 PSA_ASSERT( psa_export_key( handle,
5585 first_export, export_size,
5586 &first_exported_length ) );
5587 if( generation_method == IMPORT_KEY )
5588 ASSERT_COMPARE( data->x, data->len,
5589 first_export, first_exported_length );
5590 }
Darryl Greend49a4992018-06-18 17:27:26 +01005591
5592 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005593 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005594 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005595 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005596
Darryl Greend49a4992018-06-18 17:27:26 +01005597 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005598 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005599 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5600 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5601 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5602 PSA_KEY_LIFETIME_PERSISTENT );
5603 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5604 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5605 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5606 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005607
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005608 /* Export the key again if permitted by the key policy. */
5609 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005610 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005611 PSA_ASSERT( psa_export_key( handle,
5612 second_export, export_size,
5613 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005614 ASSERT_COMPARE( first_export, first_exported_length,
5615 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005616 }
5617
5618 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005619 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005620 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005621
5622exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005623 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005624 mbedtls_free( first_export );
5625 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005626 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005627 psa_destroy_key( base_key );
5628 if( handle == 0 )
5629 {
5630 /* In case there was a test failure after creating the persistent key
5631 * but while it was not open, try to re-open the persistent key
5632 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005633 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005634 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005635 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005636 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005637}
5638/* END_CASE */